Thursday, 12 November 2015

arcpy - Update Cursor to Polyline Creates Records with Missing Geometry


I am working on an arcpy script to create a polyline feature class with a single polyline feature between each 2 points in a sequence of points. I use a search cursor to first create a table with all the necessary data, including the start and end point coordinates for each record. Then I use an update cursor to load the data into a polyline feature class. The output feature class has a row for every record, but is missing the geometry for most, but not all of the polylines I want to create. I've tried rewriting it several ways, always with similar results. Any ideas on how to fix this?


rowsTbl = arcpy.da.SearchCursor(attributeTable, ['SURSEGUID', 'TRACKSEGMENT', 'START_TIME', 'END_TIME', 'START_X', 'START_Y', 'END_X', 'END_Y'])

#New polyline featureclass fields = ['SHAPE@', 'SURSEGUID', 'TRACKSEGMENT', 'START_TIME', 'END_TIME']


with arcpy.da.InsertCursor(tempOutFC, fields) as cursorTbl:
for row in rowsTbl:
array = arcpy.Array()
point = arcpy.Point()
point.X = (row[4])
point.Y = (row[5])
array.add(point)
del point
point = arcpy.Point()
point.X = (row[6])

point.Y = (row[7])
array.add(point)
del point

#Create polyline from the array
polyline = arcpy.Polyline(array)

#Clear array for future use
array.removeAll()
#Append to the list of polyline objects


cursorTbl.insertRow([polyline,row[0], row[1], row[2], row[3]])
del polyline

del cursorTbl

Here's a screenshot of the attribute table I am trying to load: enter image description here As you can see, most output polylines are missing, although their records show up in the attribute table (with a shape length of zero). This is my output polyline feature class.



Answer



Something like this:


cursorTbl=arcpy.da.InsertCursor(tempOutFC, fields)

point = arcpy.Point()
with arcpy.da.SearchCursor(attributeTable, [...]]) as rowsTbl:
for row in rowsTbl:
array = arcpy.Array()
point.X = row[4]
point.Y = row[5]
array.add(point)
point.X = row[6]
point.Y = row[7]
array.add(point)

polyline = arcpy.Polyline(array)
array.removeAll()
cursorTbl.insertRow([polyline,row[0], row[1], row[2], row[3]])
del cursorTbl

Update on original answer:


Ignoring decimals when dealing with unknown projection is well known issue with ArcGIS How to handle coordinates accuracy in ArcGIS


this is why it creates 0 length polylines in your example.


So, add pairs of points manually (I have no clue how to do it using arcpy only, because add XY event tool is nowhere to be found. Perhaps creating PointGeometry, where one can define geographic projection can help), project them and use points to line tool


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...