2

I've read a few questions\answers about converting a GeoJSON Shape into an ESRI Object.

Unfortunately, most of the answers pointed to decoding the dictionary's manually.

The problem:

The bug is that arcpy.asShape truncates all values to 3 decimal points and has been described at this site in arcpy.geometry __geo_interface__ and AsShape() function: loss of precision and holes. Naturally for most this is undesirable.

This is the solution i came up with:

from shapely.geometry import asShape
import arcpy

sr = arcpy.SpatialReference()
sr.factoryCode = 4326
sr.create()
arcpy.env.outputCoordinateSystem = sr

JSON = {'coordinates': [[[0.6049790000000712, 17.048294000000055],
                  [0.6068335000000502, 17.049157000000037],
                  [0.6067875000000527, 17.049668000000054]]],
 'type': 'Polygon'}

jsonobj = asShape(JSON)
arcshape = arcpy.FromWKT(jsonobj.to_wkt(),sr)


print (arcshape.WKT)
>>>u'MULTIPOLYGON (((0.60497900000007121 17.048294000000055, 0.60683350000005021 17.049157000000037, 0.60678750000005266 17.049668000000054, 0.60497900000007121 17.048294000000055)))'

Environment:

  • ArcGIS 10.1 SP1 for Desktop Quality Improvement Patch
  • ArcGIS 10.1 SP1 for Desktop Background Geoprocessing (64-bit) Quality Improvement Patch
jon kish
  • 21
  • 2
  • Have you thought of using the OGR library and their GeoJSON translator, taking that output and dumping it into the Arcpy featureclass? – Get Spatial Oct 16 '14 at 19:47
  • It could probably be done, Though I'm not sure of the benefit. In my case all my shapes are delivered as single JSON dictionaries with the expectation of being converted and stored 'in_memory' with little lag. Cheers. – jon kish Oct 16 '14 at 20:26
  • 1
    I'm entirely unaware of this particular bug and I authored the Python-facing part of that function. Are you passing in decoded JSON as dicts or JSON as a string? – Jason Scheirer Oct 16 '14 at 20:28
  • I pass JSON identical to the example above. My highlevel process is a server sends me x number of JSON Dictionaries as strings. These are passed into eval() then passed into arcpy.asShape per the previous process. – jon kish Oct 16 '14 at 20:32
  • Would you be able to edit your question to include the version of ArcGIS for Desktop that you are using ArcPy from, please? The reason I ask is that the Q&A you cite as describing the bug says in one of its comments that it was resolved at 10.1. Although another comment suggests that it was not. In either event, knowing what you are using would be useful. – PolyGeo Oct 23 '14 at 09:49
  • I updated the Q to reflect the version i'm running. I'd like to know if anyone on 10.2 has this proble. – jon kish Oct 31 '14 at 13:32
  • Oh man, the things that can go wrong when you eval() strings coming from elsewhere! Use json.loads() always. – sgillies Oct 31 '14 at 14:19
  • I think this needs an Esri bug number to support the assertion that there is one necessitating this workaround. If not then a reproducible procedure to show that ArcPy has a bug in the question followed by the workaround as an answer would work too. – PolyGeo Nov 23 '16 at 02:29

0 Answers0