More

Arcpy handling of floating points

Arcpy handling of floating points


This arises from my own question How to handle coordinates accuracy in ArcGIS where I have tried to use the documentation entitled Using geometry objects with geoprocessing tools as a reference.

I have a table with coordinates in degrees:

I created event table and added it to the view with coordinates system 'GCS_NZGD_2000 WKID: 4167 Authority: EPSG'. I converted this single point to shapefile, defined it projection and computed coordinates of the point using 'Add Geometry Attributes' tool. This is resulting table with numbers as expected:

To replicate this in arcpy I've used this code:

corners =[[174.73,-36.76]] p=[arcpy.PointGeometry(arcpy.Point(*coords)) for coords in corners] arcpy.CopyFeatures_management(p, "d:/rubbish/points.shp")

I added output 'points.shp' to the view, defined projection and computed coordinates of the point using 'Add Geometry Attributes' tool. This is resulting table:

As one can see from the picture below the distance between 2 supposedly identical points is close to 10 meters:

However when I updated existing dataset with defined projection using

infc =r'd:scratchfrom_xy.shp' outRows = arcpy.da.InsertCursor(infc,("[email protected]","X")) feat = (arcpy.Point(174.73,-36.76),0) outRows.insertRow(feat)

It worked. Lessons:

  1. Don't use examples similar to the documentation entitled Using geometry objects with geoprocessing tools
  2. Define projection of dataset prior to any games with geometries.

Does the documentation entitled Using geometry objects with geoprocessing tools need to be revised?


As requested… posting my comment here

Use a spatial reference object, which I think pointgeometry supports, check the help. If a defined coordinate system is not used single precision is used in the calculations. References to this phenomenon are documented on this site and on geonet and a comment isn't the place to put them. No SR… =… inaccurate results - Dan Patterson 33 mins ago