2

I want to insert new data into a empty shapefile. I have an array with the fields values of each row and I use InsertCursor to add these data to my shapefile.

When I run the code if the shapefile is in edit form I get the result, but if the shapefile is not in editing form I get nothing.

I want to achive the result in the second case, but I suppose that I have to create a newRow first and then use insertCursor.

The code is:

for i in arcpy.da.SearchCursor(a, ["SHAPE@XY"]):
cor = i[0]

p1=float(cor[0])+1.25, float(cor[1])-0.75
p2=float(cor[0])+1.25, float(cor[1])+0.75
p3=float(cor[0])-1.25, float(cor[1])+0.75
p4=float(cor[0])-1.25, float(cor[1])-0.75 

row_values=[((p1[0],p1[1]),1),((p2[0],p2[1]),1),((p3[0],p3[1]),1),((p4[0],p4[1]),1)]  #esto hay que mejorarlo para meter un FOR

cursor=arcpy.da.InsertCursor('points',['SHAPE@XY','Id'])

for row in row_values:
    cursor.insertRow(row)
  • 1
    You've got indentation flaws in your code sample. Plus, row appears to be used in both levels of nesting. – Vince Jul 11 at 13:08
  • Yes @Vince, when I nest the code I use i instead of row in one of them. – MikV89 yesterday
5

From the InsertCursor documentation:

Opening simultaneous insert or update operations on the same workspace using different cursors requires the start of an edit session.

Since you're not closing the cursor, it remains open during the next run. Try using the following pattern instead:

with arcpy.da.InsertCuror('points', ['SHAPE@XY', 'Id']) as cursor:
    for row in row_values:
        cursor.insertRow(row)

This will automatically close the cursor at the end of the script.

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.