0

Are there any open source command-line or python tools available to convert a shapefile (or other esri datasource) to ArcGIS Server JSON?

I'd like something that emulates the ArcGIS Features to JSON tool.

Alternatively, a tool to convert standard GeoJSON to ArcGIS Server JSON would also work, but I haven't been able to find one of those either.

My ultimate goal is to convert a shapefile to ArcGIS Server geojson, then POST that to an ArcGIS Service endpoint.

2
  • ogr2ogr tool blog.adamw523.com/shapefile-to-geojson
    – Mapperz
    Jul 14, 2016 at 15:00
  • Thanks for your answer. That command ogr2ogr -t_srs EPSG:4269 -f geoJSON Neighbourhoods.json Neighbourhoods.shp converts the shapefile to vanilla GeoJSON, correct? I'm looking for something to convert to ArcGIS Server GeoJSON. Jul 14, 2016 at 15:14

2 Answers 2

2

Esri has some JavaScript libraries that will get you from GeoJSON to ArcGIS Server JSON.

1
  • Thanks @Jeff! I imagine this is the preferred option from a technical standpoint, but given my unfamiliarity with JavaScript/Node it presents a bit of a challenge. I'll try it out and see what I can do. Thanks! Jul 18, 2016 at 13:04
0

The GeoJSON format and the ArcGIS Server JSON format (ArcGIS Server REST API:geometry) are simple Python dictionaries

There are many Python spatial modules which have the Geo_interface protocol (GeoJSON like)(list of modules with examples).

For example with Fiona

import fiona
lines = fiona.open("a_shapefile.shp")
# first feature of the shapefile
line = lines.next()
print line 
{'geometry': {'type': 'LineString', 'coordinates': [(-97.06138, 32.837), (-97.06133, 32.836), (-97.06124, 32.834), (-97.06127, 32.832)]}, 'type': 'Feature', 'id': '0', 'properties': OrderedDict([(u'FID', 0), (u'long', 103.73)])}
geom = line['geometry']
print geom
{'type': 'LineString', 'coordinates': [(-97.06138, 32.837), (-97.06133, 32.836), (-97.06124, 32.834), (-97.06127, 32.832)]}

Transform to ArcGIS Server JSON format (dictionary to other dictionary)

arc = {"paths" : [ geom['coordinates']],"spatialReference" : {"wkid" : 4326}}
print arc
{'paths': [[(-97.06138, 32.837), (-97.06133, 32.836), (-97.06124, 32.834), (-97.06127, 32.832)]], 'spatialReference': {'wkid': 4326}}

Compare with the the 2D polyline in ArcGIS Server REST API:geometry

Transform to JSON

import json
json.dumps(res)
'{"paths": [[[-97.06138, 32.837], [-97.06133, 32.836], [-97.06124, 32.834], [-97.06127, 32.832]]], "spatialReference": {"wkid": 4326}}'

You can do the same things with other geometries.

The only problem is the "wkid" value (I dont'know ArcGIS). With Fiona, for example

lines.crs
{'init': u'epsg:4326'}

New

esri JSON handles inner rings differently, and requires the first and last point of the polygon to be the same.

It is not specific to ESRI but for all polygons:

Using Shapely

 from shapely.geometry.polygon import LinearRing
 # linearRing of a Polygon with 3 points 
 ring = LinearRing([(0, 0), (1, 1), (1, 0)])
 poly = Polygon(list(ring.coords))
 print poly
 POLYGON ((0 0, 1 1, 1 0, 0 0))
 # Polygon with 4 points and first and last point equal
 from shapely.geometry import mapping # geo_interface
 mapping(Polygon(list(ring.coords)))
 {'type': 'Polygon', 'coordinates': (((0.0, 0.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)),)}

Therefore, If I use the first polygon in ArcGIS Server REST API:geometry

ring = {'rings': [[[-97.06138, 32.837], [-97.06133, 32.836], [-97.06124, 32.834], [-97.06127, 32.832], [-97.06138, 32.837]], [[-97.06326, 32.759], [-97.06298, 32.755], [-97.06153, 32.749], [-97.06326, 32.759]]], 'spatialReference': {'wkid': 4326}}
shapely_ring =  LinearRing(ring['rings'][0])
mapping(shapely_ring)
{'type': 'LinearRing', 'coordinates': ((-97.06138, 32.837), (-97.06133, 32.836), (-97.06124, 32.834), (-97.06127, 32.832), (-97.06138, 32.837))}
mapping(Polygon(list(shapely_ring.coords)))
{'type': 'Polygon', 'coordinates': (((-97.06138, 32.837), (-97.06133, 32.836), (-97.06124, 32.834), (-97.06127, 32.832), (-97.06138, 32.837)),)}

4
  • Thanks for your response! Fiona is a great tool to programmatically access the coordinates of each polygon, but unfortunately I don't think esri JSON is that similar to the way these coordinates are listed. Per this strangely formatted slide deck, esri JSON handles inner rings differently, and requires the first and last point of the polygon to be the same. Jul 14, 2016 at 20:24
  • Given all these uncertainties, I'm looking for a tested script/utility, rather than hack together my own. Jul 14, 2016 at 20:25
  • look above in new
    – gene
    Jul 15, 2016 at 15:22
  • Thanks for adding this @gene. I imagine I'll run into some additional unforeseen gotchas with the differences in ArcGIS Server json and GeoJSON, but this gives me a good start. Thanks! Jul 18, 2016 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.