5

I'm using PyScripter and running ArcGIS 10.2.1.

I am having troubles with the Insert Cursor in the arcpy module.

I have an empty feature class in my geodatabase and have created the short script below to try and find the issue, but have not been able to. When I run the script, it executes fine, but when I look in the attribute table of the feature class afterwards it is still empty.

import arcpy

fc = r"C:\Users\djh\Desktop\topo_map\test.gdb\well_location"
cursor = arcpy.da.InsertCursor(fc, ["SHAPE@XY"])
xy = (206901.75, 5997594.47)

cursor.insertRow([xy])

I have copied this script straight from the "writing geometries" page on the ArcGIS help website, so I don't think it's a problem with syntax. Please let me know if there's a way to get around this issue.

4
  • If the table is open when you do this, you will see nothing since the table doesn't get updated until it is reopened
    – user681
    Commented Jul 18, 2014 at 21:22
  • @Dan Patterson I only have PyScripter open when I run the script. I then open ArcMap, add the feature class, and open the attribute table, and it is still empty.
    – David
    Commented Jul 18, 2014 at 21:25
  • Hmmmm resources.arcgis.com/en/help/main/10.2/index.html#//… try using the alternative of creating a Point object rather than using the SHAPE@XY method and see if that works
    – user681
    Commented Jul 18, 2014 at 21:42
  • I tried the alternative (using a combination of arcpy.Point, arcpy.PointGeometry, and arcpy.CopyFeatures_management) and it now appears in the table. So this solves the issue, although I wonder why the Insert Cursor wasn't working. In any case, thanks very much for you help.
    – David
    Commented Jul 18, 2014 at 22:09

2 Answers 2

7

PyScripter is somewhat lax with object lifetimes and will keep stuff around after it's run. Use the with statement to ensure you close the edit session.

import arcpy

fc = r"C:\Users\djh\Desktop\topo_map\test.gdb\well_location"
xy = (206901.75, 5997594.47)

with arcpy.da.InsertCursor(fc, ["SHAPE@XY"]) as cursor:
    cursor.insertRow([xy])
4

Using IDLE, I ran your test using ArcGIS 10.2.2 for Desktop, immediately after a reboot (by coincidence) so there should be nothing laying around from previous tests. The code is identical to the third example at Writing Geometries, and I agree with you that the point does not get written.

I believe that part of the documentation is in error and that instead you should use the following which I tested successfully.

import arcpy

fc = r"C:\Users\djh\Desktop\topo_map\test.gdb\well_location"
cursor = arcpy.da.InsertCursor(fc, ["SHAPE@XY"])
xy = arcpy.Point(206901.75, 5997594.47)

cursor.insertRow([xy])

As @JasonScheirer commented, this combination will also work:

cursor = arcpy.da.InsertCursor(fc, ["SHAPE@"])
xy = (206901.75, 5997594.47)

while this does not:

cursor = arcpy.da.InsertCursor(fc, ["SHAPE@"])
xy = arcpy.Point(206901.75, 5997594.47)

but this should:

cursor = arcpy.da.InsertCursor(fc, ["SHAPE@"])
xy = arcpy.PointGeometry(arcpy.Point(206901.75, 5997594.47))
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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