Creating polyline from text file

6898
17
Jump to solution
02-12-2016 11:44 AM
KennethKirkeby
New Contributor III

So I need to create a line based on a series of points. In the example below, there are 4 lines to be created, Line_1. Line_2, etc. Struggling with the loop that creates each pointList.

cursor = arcpy.da.InsertCursor(fc, ["Name","SHAPE@"])
   
for ln in inputFile:
  name = ln[0]
  pointList = Point. Array() # initial Array
    ln = line.split(",") # Split the lines in text file
    point = arcpy.Point(ln[1], ln[2]) # Create points 
    pointList.add(point) # Add points to array
        polyline = arcpy.Polyline(pointList)
cursor.insertRow(name, polyline)

Line_1, 529018.125025, 4108038.05548
Line_1, 529005.718792, 4108028.20659
Line_1, 528993.340503, 4108018.73931
Line_1, 528980.990158, 4108009.65364
Line_1, 528968.667757, 4108000.94958
Line_2, 528956.373301, 4107992.62713
Line_2, 528944.106788, 4107984.68629
Line_2, 528931.86822, 4107977.12705
Line_3, 528919.657595, 4107969.94943
Line_3, 528907.474915, 4107963.15341
Line_3, 528895.320179, 4107956.739
Line_3, 528883.193387, 4107950.7062
Line_4, 528871.09454, 4107945.055
Line_4, 528859.023636, 4107939.78542
Line_4, 528846.980676, 4107934.89744
Line_4, 528834.965661, 4107930.39107

Tags (3)
0 Kudos
17 Replies
KennethKirkeby
New Contributor III
forln in inputFile: 
    pointList = arcpy.Array()
    ln = ln.split(",")
    name = ln[0]
    point = arcpy.Point(ln[1], ln[2])
    pointList.add(point) 
polyline = arcpy.Polyline(pointList)
cursor.insertRow((name, polyline))

Attribute table:

Shape    Name

polyline  Line_4

0 Kudos
DarrenWiens2
MVP Honored Contributor

pointList is getting wiped out for each line in your input file. Within each iteration, you need to add the contents of pointList to an overarching list to persist the coordinates until you are ready to create the lines.

DarrenWiens2
MVP Honored Contributor

This will probably take some digesting:

>>> import csv
... fc = "myLines"
... with open('C:/junk/points.csv', 'rb') as csvFile:
...     reader = csv.reader(csvFile)
...     dict = {}
...     for row in reader:
...         dict.setdefault(row[0],arcpy.Array()).add(arcpy.Point(row[1],row[2]))
... print dict
... cursor = arcpy.da.InsertCursor(fc, ["Name","SHAPE@"])
... for k,v in dict.iteritems():
...     cursor.insertRow([k,arcpy.Polyline(v)])
...     
{'Line_1': <Array [<Point (529018.125025, 4108038.05548, #, #)>, <Point (529005.718792, 4108028.20659, #, #)>, <Point (528993.340503, 4108018.73931, #, #)>, <Point (528980.990158, 4108009.65364, #, #)>, <Point (528968.667757, 4108000.94958, #, #)>]>, 'Line_3': <Array [<Point (528919.657595, 4107969.94943, #, #)>, <Point (528907.474915, 4107963.15341, #, #)>, <Point (528895.320179, 4107956.739, #, #)>, <Point (528883.193387, 4107950.7062, #, #)>]>, 'Line_2': <Array [<Point (528956.373301, 4107992.62713, #, #)>, <Point (528944.106788, 4107984.68629, #, #)>, <Point (528931.86822, 4107977.12705, #, #)>]>, 'Line_4': <Array [<Point (528871.09454, 4107945.055, #, #)>, <Point (528859.023636, 4107939.78542, #, #)>, <Point (528846.980676, 4107934.89744, #, #)>, <Point (528834.965661, 4107930.39107, #, #)>]>}

I saved your sample data to a csv file. Then, for each row in the csv, add the coordinates (point objects within an array object) to a dictionary, according to the line ID. Once the dictionary is created, for each line ID in the dictionary, insert a feature into the feature class, passing name and shape.

XanderBakker
Esri Esteemed Contributor

Darren Wiens shows a very clean piece of code to do what you are after. I would definitely go for the solution provided by Darren.

If you would have continued along the lines of what you where doing, you would probably end up with something like the code below. Just for you to understand what was missing in your initial code.

import arcpy

fc = r'D:\Xander\GeoNet\TXT2Polyline\test.gdb\myFeatureClass'
txt_file = r'D:\Xander\GeoNet\TXT2Polyline\myTextFiles.txt'

with arcpy.da.InsertCursor(fc, ["Name","SHAPE@"]) as cursor:
    with open(txt_file, 'r') as inputFile:
        prev_name = None
        pointList = arcpy.Array()
        for line in inputFile:
            ln = line.split(",")
            name = ln[0]
            point = arcpy.Point(float(ln[1]), float(ln[2]))
            if name != prev_name:
                # start of a new line
                if pointList.count > 1:
                    # if previous list has more than 2 points, write line
                    polyline = arcpy.Polyline(pointList)
                    cursor.insertRow((prev_name, polyline, ))
                # create a new array and add current point
                pointList = arcpy.Array()
                pointList.add(point)
            else:
                # same name as previous name, add point to current list
                pointList.add(point)
            prev_name = name

        # add last line
        polyline = arcpy.Polyline(pointList)
        cursor.insertRow((name, polyline, ))
KennethKirkeby
New Contributor III

This is what I was trying to do for my script. For whatever reason, I could not get the looping straight.Thanks so much for the assistance.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Could you mark the answer provided by Darren as the "Correct Answer"? I think it provides the best solution to your question.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Kenneth Kirkeby , I would really like to ask you to mark the post by Darren as the correct answer. Apart from being the first to provide a solution, the solution really shows a cleaner, better and shorter way to solve this problem. By marking his better solution as the correct answer you will enable other users to find his answer more easily.

Thanks, Xander

DanPatterson_Retired
MVP Emeritus

Fixed...