Friday, June 3, 2011

Geometry to GeoJSON

Working off of yesterday's post, the goal is converting the Geometry Objects back to GeoJSON. The geometry object has a method called __geo_interface__. It will work with Geometry, Multipoint, PointGeometry, Polygon, or Polyline classes.  It is not implemented on the Point object.

Example: Convert all features in a feature class to GeoJSON


import arcpy
from arcpy import env
import os
 

wrksp = r"c:\temp"
rows = None
row = None


env.workspace = wrksp


for ds in arcpy.ListFeatureClasses():
   print os.path.basename(ds)
   rows = arcpy.SearchCursor(ds)
   for row in rows:
      print row.getValue("Shape").__geo_interface__
   del rows, row


It's that simple to convert geometry to GeoJSON.

3 comments:

Victor said...

Andrew, I have a problem with this topic --> shape to geojson an back...

http://gis.stackexchange.com/questions/10201/arcpy-geometry-geo-interface-and-asshape-function-loss-of-precision-and-ho

what do you think about this problem?

Andrew said...

I believe part of the problem stems from python itself with significant digits and the way it is represented
See:
http://docs.python.org/tutorial/floatingpoint.html#representation-error
and
http://docs.python.org/tutorial/floatingpoint.html#

Victor said...

Thank you very much Andrew for your answer.

I can see the common floating point problem, but I'm perplexed about the holes one... It seems that esri can't process its own polygons with holes