Ok, I'm stumped! I have a list "xys" that if I do this:
xys.sort()
for i in xys:
print i
I get this: (id, xFrom, yFrom, xTo, yTo)
[225351, 1858897, 889223, 1883031, 882436]
[225396, 1908393, 871758, 1908090, 871791]
[225405, 1899127, 884235, 1885213, 861191]
[225423, 1891186, 878961, 1885811, 881262]
[225435, 1887101, 888042, 1894165, 884325]
[225438, 1888139, 885803, 1888050, 885319]
[225438, 1888139, 885803, 1888081, 884837]
[225441, 1885023, 888542, 1884356, 888364]
[225351, 1858897, 889223, 1883031, 882436]
and then i do this:
for j in xys:
print j
myList = []
myList.append(arcpy.Point(j[1], j[2]))
myList.append(arcpy.Point(j[3], j[4]))
array = arcpy.Array(myList)
polyline = arcpy.Polyline(array)
cursor = arcpy.da.InsertCursor(output, ("SHAPE@"))
cursor.insertRow((polyline,))
print 'inserted row'
I get an empty table... I had this working at one point. Any obvious blunders here?
Solved! Go to Solution.
If I run this code in ArcGIS 10.2.2, it runs just fine and creates the polylines.
import arcpy fc = r"C:\Forum\DistLinePol\test.gdb\Polyline2" xys = [[225351, 1858897, 889223, 1883031, 882436], [225396, 1908393, 871758, 1908090, 871791], [225405, 1899127, 884235, 1885213, 861191], [225423, 1891186, 878961, 1885811, 881262], [225435, 1887101, 888042, 1894165, 884325], [225438, 1888139, 885803, 1888050, 885319], [225438, 1888139, 885803, 1888081, 884837], [225441, 1885023, 888542, 1884356, 888364], [225351, 1858897, 889223, 1883031, 882436]] with arcpy.da.InsertCursor(fc, ("SHAPE@")) as cursor: for j in xys: array = arcpy.Array() array.add(arcpy.Point(j[1], j[2])) array.add(arcpy.Point(j[3], j[4])) polyline = arcpy.Polyline(array) cursor.insertRow((polyline, ))
Richard Fairhurst; there are many examples where the list of fields is handled as tuple and the comma in "(polyline, )" is to create the tuple.
myList = [] is the list initialization supposed to be inside or outside your 'for' loop?
myList is supposed to be inside the for loop (I think!)
myList contains 2 point objects that are passed to the polyline object, which is supposed to get inserted.
...but it doesn't do what I want!
If I remove the myList stuff and do this instead:
array = arcpy.Array([arcpy.Point(j[1], j[2]), arcpy.Point(j[3], j[4])])
I get the same results... empty.
Why is there a comma in the line that reads: cursor.insertRow((polyline,))? Also, shouldn't the field list and polygon array be lists? So try:
for j in xys: print j myList = [] myList.append(arcpy.Point(j[1], j[2])) myList.append(arcpy.Point(j[3], j[4])) array = arcpy.Array(myList) polyline = arcpy.Polyline(array) cursor = arcpy.da.InsertCursor(output, ["SHAPE@"]) cursor.insertRow([polyline]) print 'inserted row'
Thanks, I tried that, but still no cigar. The code should work. I'm stumped.
Then go back and make the code more like the InsertCursor help. Also, never create an InsertCursor inside a loop without also deleting it inside the loop. For this code and most code an InsertCursor should only be created once outside the loop and only the insertRow operations should be inside the loop. Then be sure to delete the InsertCursor, since there is no other correct way to stop it and release its locks. Failure to delete the cursor in your previous code may be the reason why the cursor never completes and refuses to show the records it created, since it still thinks it is waiting to create more records and you have opened many cursors on the same source.
cursor = arcpy.da.InsertCursor(output, ["SHAPE@"]) for j in xys: print j array = arcpy.Array([arcpy.Point(j[1], j[2]), arcpy.Point(j[3], j[4])]) polyline = arcpy.Polyline(array) cursor.insertRow([polyline]) del array del polyline print 'inserted row' del cursor
Richard, that is definitely much cleaner, but it's still not working.
In the loop I've even printed the type(j[1]) etc, and they all are 'int', so I've verified that I'm not feeding it bad data.
The missing part of your code that most likely is part of the problem is that you have not shown what the output variable is assigned. Maybe it is writing somewhere else than where you think it is writing, depending on the workspace settings, fc name, etc. Since it is not throwing an error, it probably is a valid output, but I have wasted plenty of time checking what I thought I told my code to do when I actually told my code to do something different.
If I run this code in ArcGIS 10.2.2, it runs just fine and creates the polylines.
import arcpy fc = r"C:\Forum\DistLinePol\test.gdb\Polyline2" xys = [[225351, 1858897, 889223, 1883031, 882436], [225396, 1908393, 871758, 1908090, 871791], [225405, 1899127, 884235, 1885213, 861191], [225423, 1891186, 878961, 1885811, 881262], [225435, 1887101, 888042, 1894165, 884325], [225438, 1888139, 885803, 1888050, 885319], [225438, 1888139, 885803, 1888081, 884837], [225441, 1885023, 888542, 1884356, 888364], [225351, 1858897, 889223, 1883031, 882436]] with arcpy.da.InsertCursor(fc, ("SHAPE@")) as cursor: for j in xys: array = arcpy.Array() array.add(arcpy.Point(j[1], j[2])) array.add(arcpy.Point(j[3], j[4])) polyline = arcpy.Polyline(array) cursor.insertRow((polyline, ))
Richard Fairhurst; there are many examples where the list of fields is handled as tuple and the comma in "(polyline, )" is to create the tuple.
The exact same code doesn't insert at 10.3!