Tuesday, March 31, 2015

Python (ArcPy) - Processing Single Breakline file


I get a breakline file from a CAD person in below mentioned format. So I am calling this as a “Input” file. The input file can be downloaded from the below mentioned link.






Inorder to accept breakline into ArcGIS, We need to change it to below mentioned format.




Here is the Python (ArcPy) Code....

import os, sys

inFile = r'J:\Thumb drive\Technical\GIS Exercise\Chapters\Chapter 14 - R by example in Statistics & Geospatial Analysis\R\reneedyourhelpmann\Incomplete\Breakline\BreaklineInput.txt'
outFile = r'J:\Thumb drive\Technical\GIS Exercise\Chapters\Chapter 14 - R by example in Statistics & Geospatial Analysis\R\reneedyourhelpmann\Incomplete\Breakline\BreaklineOutput.txt'
firstFeature = True

#Open the input file for reading
with open(inFile, 'r') as inF:
    #Open the output file for writing
    with open(outFile, 'w') as outF:
        #Read in the first line
        row = inF.readline()
        #Loop while there are still lines in the input file
        while(row is not None and len(row) > 0):
            #Ignore the headers, specified with ;
            if (row[0] != ";"):
                #remove the leading and trailing spaces
                row = row.strip()
                #replace the inconsistent space-delimiters with commas
                row = row.replace("           ",",").replace("    ","").replace(" ",",")
                #separate the components
                tokens = row.split(",")
                #If the last parameter is 1, start writing out a new feature
                if(tokens[3] == '1'):
                    if not (firstFeature):
                        outF.write("END\n")
                    else:
                        firstFeature = False
                    outF.write('3\n')
                    #Write out the coordinates
                    outF.write(tokens[0] + "  " + tokens[1] + "  " + tokens[2] + "\n")
                #otherwise just write out the coordinates
                else:
                    outF.write(tokens[0] + "  " + tokens[1] + "  " + tokens[2] + "\n")
                #Read in the next line
                row = inF.readline()
            #Read the next line
            else:
                row = inF.readline()
        #Close off the final feature
        outF.write("END\n")
print("Finished")

No comments:

Post a Comment