Wednesday, 8 January 2020

geojson - arcpy.geometry __geo_interface__ and AsShape() function: loss of precision and holes


I'm serializing my arcpy geometries as geojson so that I can 'hydrate' them back as geometries later and I'm having 2 problems in the cycle.:


PROBLEM 1: Precision


    R0 = arcpy.SearchCursor(self.shpTest, "FID=0").next().getValue("Shape")          
geojson = R0.__geo_interface__
R1 = arcpy.AsShape(geojson)
self.assertTrue(R0.equals(R1)) <<< THIS FAILS

If I check the string representation, the coordinates have slightly changed:


    geojson2 = R1.__geo_interface__

print geojson
print geojson2

{'type': 'Polygon', 'coordinates': [[(442343.5516410945, 4814166.6184399202), (442772.17749834526, 4811610.7383281607), (441565.67508534156, 4811499.6131059099), (440772.50052100699, 4814184.7808806188), (442343.5516410945, 4814166.6184399202)]]}
{'type': 'Polygon', 'coordinates': [[(442343.55169677734, 4814166.6185302734), (442772.17749023438, 4811610.73828125), (441565.67510986328, 4811499.6130981445), (440772.50048828125, 4814184.7808837891), (442343.55169677734, 4814166.6185302734)]]}

PROBLEM 2: Holes If the polygon has holes, geo_interface generates an error:


    R0_WithHoles = arcpy.SearchCursor(self.shpTest, "FID=0").next().getValue("Shape")          
geojson = R0.__geo_interface__ <<< generates this ERROR:


File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\geometries.py", line 68, in __geo_interface__
return {'type': 'Polygon', 'coordinates': [[(pt.X, pt.Y) for pt in part] for part in self]}
AttributeError: 'NoneType' object has no attribute 'X'

Any ideas on how to solve these problems?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...