4

I am trying to use the arcpy.da.InsertCursor to add simple points to a feature class, but it doesn't work for me as every single point I add ends up somewhere near Paris. Here's the simplest code block that doesn't work for me:

arcpy.CreateFeatureclass_management(' (my path here) ...\Map.gdb','Hotspots2', 'POINT')
a = arcpy.Point(7, 7)
cursor = arcpy.da.InsertCursor("Hotspots2",["SHAPE@XY"])
cursor.insertRow([a])
del cursor

It ends up here:

Paris, ugh

at coordinate ~ (3.3, 48). Whatever coordinates I enter into a don't matter for the outcome. If I change a to a point geometry it doesn't work. If I use SHAPE@ and a point geometry it also doesn't work. Every darn point ends up there. What am I doing wrong and can someone test to see if they get the same result??

4 Answers 4

5

I don't really work in ArcGIS, but from a general GIS perspective it looks as if you may be entering coordinates in a different coordinate system to that used by the map window. Have you checked that your input coordinates use the same system as that of the map window?

Those coordinates (3.3, 48) are mapped correctly using WGS84, where are you expecting them to map to?

3
  • In general, arcpy workspace settings should take care of that and they are set to WGS84 by default IIRC. If it were a coordinate system mismatch, I would expect all coordinates that I put in to be shifted in some way. Instead, every single coordinate that I put in are ignored in favor of this random (3,48) coordinate. I'll do a doublecheck though.
    – veen
    Commented Dec 15, 2016 at 13:14
  • OH MY GOD, it turned out that the empty feature class I had created indeed did not have any GCS associated with it. I parsed arcpy.SpatialReference(4326)) to the CreateFeatureclass_management function and finally it works! Thanks a ton!
    – veen
    Commented Dec 15, 2016 at 13:26
  • 1
    Always a first port of call when things don't map to the locations you expect them to. It is almost always an issue with the way they are being projected. Glad you got it sorted!
    – T_Bacon
    Commented Dec 15, 2016 at 16:05
4

I think that when you create a point through arcpy.Point({x},{y}) one should use SHAPE@, not SHAPE@XY in your cursor:

InsertCursor - help:

SHAPE@XY —A tuple of the feature's centroid x,y coordinates.

SHAPE@ —A geometry object for the feature.

Try adjusting that field in your cursor, that's how I'm inserting points in my script.

If that doesn't work, try setting your X and Y as a string, that's how I'm using them inside arcpy.Point({x},{y}).

3
  • Like this? arcpy.CreateFeatureclass_management('[...] \\Map.gdb','Hotspots2', 'POINT') a = arcpy.Point('7','6') cursor = arcpy.da.InsertCursor("Hotspots2",["SHAPE@"]) cursor.insertRow([a]) del cursor Because this doesn't work either...
    – veen
    Commented Dec 15, 2016 at 12:19
  • In that case, I don't think that the inserting of the cursor isn't the problem. I just used both your approach, mine and BERA's, and they all work for me. A point gets added to the (7,7) unit. @T_Bacon's answer is probably hitting closer to the underlying issue here, maybe the system of the newly created feature class is undefined or takes a default one, which differs from the one you desire? Commented Dec 15, 2016 at 13:14
  • You need to use PointGeometry(), not just Point().
    – ianbroad
    Commented Dec 15, 2016 at 16:49
4

When using SHAPE@ token the cursor is expecting a geometry. You are creating this with arcpy.Point:

import arcpy
pointfc = r'C:\TEST.gdb\point'

a = [arcpy.Point(5,7)]

cursor = arcpy.da.InsertCursor(pointfc, ['SHAPE@'])
cursor.insertRow(a)
del cursor

If you want to use the SHAPE@XY token the cursor is expecting a tuple, for example (5,7) :

import arcpy
pointfc = r'C:\TEST.gdb\point'

a = (5,7)

cursor = arcpy.da.InsertCursor(pointfc, ['SHAPE@XY'])
cursor.insertRow((a,))
del cursor
3

Try using arcpy.PointGeometry object instead of point.
Suggestion 2: you didn't define the spatial reference of your feature class. This means it is inheriting it from whatever your dataframe is. Most likely it's going in as web mercator, but I am guessing you want WGS-1984.

From the help: CreateFeatureclass_management (out_path, out_name, {geometry_type}, {template}, {has_m}, {has_z}, {spatial_reference}, {config_keyword}, {spatial_grid_1}, {spatial_grid_2}, {spatial_grid_3})

To create the spatial reference object and perform the insert do the following:

 sr = arcpy.SpatialReference(4326)
 coords = [(3.3, 48) ]
 fc = arcpy.CreateFeatureclass_management(r'<path>', '<name>', 'POINT', spatial_reference=sr)[0] 
 cursor = arcpy.da.InsertCursor(fc,['SHAPE@XY'])
 for coord in coords:
     cursor.insertRow([coord])
     del coord
 del cursor

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.