Problem with adding points into a feature class with arcpy

1453
2
Jump to solution
02-17-2017 12:43 PM
JasonLevine
Occasional Contributor II

Hello,

I'm trying to add a list of coordinates to a feature class in arcpy.  When I manually add each xy pair as graphics into arc map using the go to xy tool, the points show up where I expect them to:

However, when I use arcpy, they are slightly offset:

Here is the code I'm using:

<code>

import arcpy, json, ConversionUtils

arcpy.Delete_management("in_memory")

coordinates = [
    [-118.3436607214932, 34.18465195557297],
    [-118.3437040116469, 34.18108463923645],
    [-118.338436218283, 34.18097397459129],
    [-118.3383716114292, 34.18454551218532],
    [-118.3421286432001, 34.18221218702963],
    [-118.3398871274149, 34.18236566520285]
]

feature_class = arcpy.CreateFeatureclass_management("in_memory", "temp_feature_class", "POINT")

cursor = arcpy.da.InsertCursor(feature_class, ['SHAPE@'])

points = []
for (x, y) in coordinates:
    points.append(arcpy.Point(x, y))

for point in points:
    cursor.insertRow([point])
    
sr = arcpy.SpatialReference(4326)
arcpy.DefineProjection_management(feature_class, sr)
    
arcpy.FeatureClassToShapefile_conversion(feature_class, "C:/Temp")

</code>

Is anyone able to reproduce this problem, and if so, what is causing it and what can I do to fix it.

Thanks,

Jason

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

not sure, but the PointGeometry class enables you to specify the spatial reference during the construction of the Point object... this rules out any possible misinterpretation of the full nature of the point object.  Worth a look to see if makes a difference especially if the dataframe is in a different coordinate system

View solution in original post

2 Replies
DanPatterson_Retired
MVP Emeritus

not sure, but the PointGeometry class enables you to specify the spatial reference during the construction of the Point object... this rules out any possible misinterpretation of the full nature of the point object.  Worth a look to see if makes a difference especially if the dataframe is in a different coordinate system

JoshuaBixby
MVP Esteemed Contributor

I agree with Dan that specifying spatial references is important with the ArcPy Geometry classes.  I can't remember where exactly it is documented, but the ArcPy Geometry classes will assume a simplified spatial reference, or spatial reference properties, when one is not specified.

Looking over your code, arcpy.Point doesn't take a spatial reference, and you weren't using arcpy.PointGeometry.  In this case, it was/is the lack of spatial reference with arcpy.CreateFeatureclass_management that caused the issue.  Many tools, similar to the ArcPy Geometry classes, assume simplified spatial reference properties when one isn't explicitly specified.

Using the first set of coordinates, you can see the rounding that is occurring before you use arcpy.DefineProjection_management.

>>> feature_class = arcpy.CreateFeatureclass_management("in_memory", "temp_feature_class", "POINT")
>>> cursor = arcpy.da.InsertCursor(feature_class, ['SHAPE@'])
>>> cursor.insertRow([arcpy.Point(-118.3436607214932, 34.18465195557297)])
1L
>>> pt = arcpy.CopyFeatures_management(feature_class, arcpy.Geometry())[0]
>>> pt.getPart(0)
<Point (-118.343688965, 34.1846923828, #, #)>
>>> 
0 Kudos