3

I have some working code using arcpy.InsertCursor which I want to convert to arcpy.da.InsertCursor. This I want to do primarily as ESRI claims that da methods are more optimized. The code I have is follows:

array = arcpy.Array()
point = arcpy.Point()
tie_shp_file = arcpy.env.workspace + "/" + out_name
insert_cursor = arcpy.InsertCursor(tie_shp_file)
feat = insert_cursor.newRow()

for line in all_lines:   #all_lines is of the form array([[0, 3],[0, 7],[1, 3]) indicating line [from, to]
    #from
    point.X=coords_pts[str(pt[line[0]])][0]
    point.Y=coords_pts[str(pt[line[0]])][1]
    array.add(point)
    #to
    point.X=coords_pts[str(pt[line[1]])][0]
    point.Y=coords_pts[str(pt[line[1]])][1]
    array.add(point)
    polyline=arcpy.Polyline(array)# Create a Polyline object based on the array of points
    array.removeAll() #Clear the array
    feat.shape = polyline
    insert_cursor.insertRow(feat)

The code inserts lines to a line shapefile. The lines are created by specifying its end points.

How do I convert this? I am specifically stuck on how to pass the to and from points in the new version.

10

The key difference between "classic" cursors like arcpy.InsertCursor and the new cursors like arcpy.da.InsertCursor is that the list of field names to search, update, or insert is now a required parameter, where it was only optional before. This parameter should be passed as a list of string objects. In order to search, update, or insert geometry, you need to use one of the SHAPE tokens: SHAPE@ will allow for full read/write access to the geometry of the feature.

For arcpy.da.InsertCursor specifically, there is no longer a newRow() method. Instead, you just pass a list of objects corresponding to the field names you specified in the arcpy.da.InsertCursor.

Here's how you would change your code:

array = arcpy.Array()
point = arcpy.Point()
tie_shp_file = arcpy.env.workspace + "/" + out_name
insert_cursor = arcpy.da.InsertCursor(tie_shp_file,["SHAPE@"]) #Editing only geometry field

for line in all_lines:   #all_lines is of the form array([[0, 3],[0, 7],[1, 3]) indicating line [from, to]
    #from
    point.X=coords_pts[str(pt[line[0]])][0]
    point.Y=coords_pts[str(pt[line[0]])][1]
    array.add(point)
    #to
    point.X=coords_pts[str(pt[line[1]])][0]
    point.Y=coords_pts[str(pt[line[1]])][1]
    array.add(point)
    polyline=arcpy.Polyline(array)# Create a Polyline object based on the array of points
    array.removeAll() #Clear the array
    insert_cursor.insertRow([polyline]) #Pass list with polyline to insert cursor. 

You can see how the [polyline] list used in the insertRow method corresponds to the ["SHAPE@"] list in the cursor's constructor. If you needed to insert additional fields, they would have to be passed in corresponding order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.