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.
-
If the table is open when you do this, you will see nothing since the table doesn't get updated until it is reopeneduser681– user6812014年07月18日 21:22:05 +00:00Commented 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– David2014年07月18日 21:25:27 +00:00Commented 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 worksuser681– user6812014年07月18日 21:42:18 +00:00Commented 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– David2014年07月18日 22:09:11 +00:00Commented Jul 18, 2014 at 22:09
2 Answers 2
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])
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))
-
SHAPE@
would work too then.Jason Scheirer– Jason Scheirer2014年07月18日 22:45:11 +00:00Commented Jul 18, 2014 at 22:45 -
3Now
arcpy.Point(206901.75, 5997594.47)
should bearcpy.PointGeometry(arcpy.Point(206901.75, 5997594.47))
Jason Scheirer– Jason Scheirer2014年07月20日 04:20:21 +00:00Commented 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/…Marc_Alx– Marc_Alx2017年02月24日 14:37:17 +00:00Commented Feb 24, 2017 at 14:37
Explore related questions
See similar questions with these tags.