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.


Victor said...

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

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

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