Tuesday, March 31, 2015

Python (ArcPy) - Processing Multiple Breakline files in a folder


Let's say I get multiple breakline files from a CAD person in below mentioned format. So I am calling this as a “BreaklineInput1.txt, BreaklineInput2.txt, BreaklineInput3.txt, BreaklineInput4.txt” files. The sample input file can be downloaded from the below mentioned link.
The file format looks like this one



All the files needs to get converted into the below mentioned format



The output file will going to look like this one in a folder.



Here is the Python (ArcPy) code..

import os, sys

#directory holding the input files
inDir = r'J:\Thumb drive\Technical\GIS Exercise\Chapters\Chapter 14 - R by example in Statistics & Geospatial Analysis\R\reneedyourhelpmann\Incomplete\Breakline\Multiple Breakline'

#Find all text files in the specified directory
for inFile in os.listdir(inDir):
    if inFile.endswith(".txt"):

        #Name the output file the same as the input, but with _output appended to the name
        outFile = inFile.replace(".txt","_output.txt")

        #Open the input file for reading
        with open(os.path.join(inDir, inFile), 'r') as inF:

            #Open the output file for writing
            with open(os.path.join(inDir, outFile), 'w') as outF:
                #Read in the first line
                firstFeature = True
                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