Creating polyline from text file

6807
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
1 Solution

Accepted Solutions
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.

View solution in original post

17 Replies
DarrenWiens2
MVP Honored Contributor

Please post your entire script with code formatting.

Posting Code blocks in the new GeoNet

edit: you reference ln[0] before creating the ln variable.

edit 2: not sure what this line means: Pointillist = occupies. Array () # initial Array

Should probably be: pointList = arcpy.Array()

edit 3: your variable names need to match exactly. Pointllist <> pointList

KennethKirkeby
New Contributor III

Corrected code and formatting, I hope.

0 Kudos
DanPatterson_Retired
MVP Emeritus

your polyline line 09 needs to be completely dedented

0 Kudos
KennethKirkeby
New Contributor III
for ln 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)  
0 Kudos
DanPatterson_Retired
MVP Emeritus

Kenneth...

nice dedent...

Now work with us...

What did it print out?

I bet you thought it would print out the coordinates...assuming it worked at all?

Did you get an error?

Anything else to add?

0 Kudos
KennethKirkeby
New Contributor III

The print was just a way to know when it ended. I did get it to update my shapefile but with only the last line of the loop.

0 Kudos
DanPatterson_Retired
MVP Emeritus

logic with indentation (since this is still homework I presume)

make an empty point list (array)

read the file line by line:

    split the line, and get the point oordinates

    make the point

    add the point to the point array

    read next line

when done, add the points to the polyline

Now if this has to be done for multiple files, then the above logic has to be included indented accordingly.

Darren, offers a solution to reach your end goal, but it uses csv reader to read text files and does essentially the task for file inputs

DarrenWiens2
MVP Honored Contributor

Check out the example on the Polyline help page, paying particular attention to fact that multiple lines require multiple lists, or rather, a list of lists.

KennethKirkeby
New Contributor III

@Aaron - That's what I'm having trouble doing if I understand your point.  Create a set of points for Line_1, then Line_2, etc. That's what I do not know how to do.

0 Kudos