Points added with da.InsertCursor have different coordinates than those used to create the point

2260
7
Jump to solution
07-21-2017 11:42 AM
AnneHagerman
New Contributor II

I have over 2000 kmz files each with one kml for one point - tree locations.  From each file, I am extracting the x & y coordinates and using an insert cursor to add the point with the WGS 84 x & y.  Many of the trees are only meters apart and what seems to be happening many tree points are being placed on top of each other at a "rounded" long / lat location.  Although it keeps the 6 decimal places, they all default to the same incorrect long / lat.  I've taken a subset of my code with just the insert cursor code to show here and it gives the same incorrect results.

import arcpy


fc = r"D:\ProjectData\FPI_ARCTIC\02_Working\Interiror\Kamloops\StemMaps_1.gdb\Trees"

fields = ["SHAPE@XY", "TREE_NUM"]
# GCS = WGS 84
sr = arcpy.Describe(fc).spatialReference
arcpy.env.outputCoordinateSystem = sr

iCursor = arcpy.da.InsertCursor(fc, fields)


xCoord = -119.822445
yCoord = 50.449748
##point = arcpy.Point(xCoord, yCoord)
xy = (xCoord, yCoord)

iCursor.insertRow((xy, 90))

del iCursor

I have 6 points that are close to these (only the last 3 decimal places change) that are all defaulting to this long / lat:

As you can see, although the xCoord and yCoord are the degrees passed in, the point is created at -199.822388 and 50.44989.  I've also tried using SHAPE@ and a point geometry (commented out) and passing in the point instead of the xy with the same results.  I can't find any documentation talking about the insert cursor's precision with x / y in degrees.  Does anyone know why this might be happening?  .

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

Anne...tTo ensure geometries use the spatial reference needed, you can set it at the cursor level

http://desktop.arcgis.com/en/arcmap/latest/analyze/python/setting-a-cursor-s-spatial-reference.htm

this ensures that it is used over any global settings.

View solution in original post

7 Replies
AlexanderBrown5
Occasional Contributor II

Anne,

You are adding the point through the cursor, then calculating the values of the POINT_X, and POINT_Y fields correct?  I would look at your precision and scale values for these fields.

You need to make sure these fields are:
Type: DOUBLE

Precision: 9

Scale: 6

I used your coordinates and input them through your cursor in a test feature class, then I calculated my field values in these two columns and they returned the coordinates exactly.

The coordinates are being written correctly as the geometry.  However, if you scale and precision are off, when you calculate these other fields directly the coordinates will round based on your precision/scale.

~Alex

0 Kudos
AnneHagerman
New Contributor II

Hi Alex.  Thanks for the response.  My issue is not really with calculating the X and Y coordinates, it's with the creation of the actual points themselves.  So, when I use that script and create the point from the xy variable, it actually gets created at (-199.822388, 50.44989) instead of my xy input of (-119.822445, 50.449748).  Any thoughts?

0 Kudos
AlexanderBrown5
Occasional Contributor II

Anne,

If you run just the snippet you posted, the values are getting created at (-199.822388, 50.44989)?  What is the output coordinate system of the target feature class?

~Alex

AnneHagerman
New Contributor II

Hi Alex,

Turns out the issue was not with the insert cursor, it was with the feature class creation itself.  It was created with the "entire" script and the coordinate system was set after the creation of the feature class with DefineProjection.  When the spatial reference was set (WGS84) at the time of feature class creation, the insert cursor created the points accurately.  We believe that when the projection was set after the feature class was created, the feature class was not updated before the points were added and it most likely did something with the precision / scale.  Thanks for your feedback.

DanPatterson_Retired
MVP Emeritus

Anne...tTo ensure geometries use the spatial reference needed, you can set it at the cursor level

http://desktop.arcgis.com/en/arcmap/latest/analyze/python/setting-a-cursor-s-spatial-reference.htm

this ensures that it is used over any global settings.

AnneHagerman
New Contributor II

Thanks Dan for the response.  I did come across the help page you referenced above.  Search Cursors and Update Cursors do allow you to set a spatial reference, however, Insert Cursors only have 2 inputs:

InsertCursor (in_table, field_names)

I could not figure out a way to set the spatial reference for my Insert Cursor.

0 Kudos
LauraTateosian_augori
New Contributor II

Hi Anne,

I'm guessing you figured this out by now, seeing how you posted your question in 2017 🙂  but for others, 

You can specify the spatial reference of a point before inserting it in the data:

 

pt = arcpy.Point(-12683890.6, 5811151.5)
pt_geometry = arcpy.PointGeometry(pt, spatial_reference=3857)

 

0 Kudos