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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jul 18, 2014 at 21:12
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 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. 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 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. 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])
answered Jul 18, 2014 at 22:38
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))
answered Jul 18, 2014 at 22:40
3
  • SHAPE@ would work too then. Commented Jul 18, 2014 at 22:45
  • 3
    Now arcpy.Point(206901.75, 5997594.47) should be arcpy.PointGeometry(arcpy.Point(206901.75, 5997594.47)) Commented Jul 20, 2014 at 4:20
  • For those who are wondering about the meaning of SHAPE@, SHAPE@XY : desktop.arcgis.com/fr/arcmap/latest/analyze/arcpy-data-access/… Commented Feb 24, 2017 at 14:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.