3

I have an ArcGIS Feature Server and what I have accomplished so far is this:

import json, requests

url = 'http://hebid.hessen.de/hlbk/hlbk_grundlagen/MapServer/1/query?f=json&returnGeometry=true&spatialRel=esriSpatialRelIntersects&geometry={%22xmin%22:531435,%22ymin%22:5700831,%22xmax%22:543116,%22ymax%22:5716844,%22spatialReference%22:{%22wkid%22:25832}}&geometryType=esriGeometryEnvelope&inSR=25832&outFields=*&outSR=25832'
resp = requests.get(url)
data = resp.json()
file_path = r'D:\testlayer.geojson'
with open(file_path, 'w') as f:
    json.dump(data, f)

This produces a perfect valid geojson (works with ArcMap and QGIS) using a given boundary-box, but I want to query this layer by attribute. According to this link https://resources.arcgis.com/en/help/rest/apiref/fsservicequery.html this should look something like this:

http://hebid.hessen.de/hlbk/hlbk_grundlagen/MapServer/query?layerDefs={"1":"OBJECTID=5256"}&returnGeometry=true&f=json

This does not give me my polygon as a geojson, but just the general information about the service itself. Any ideas what I am doing wrong?

  • how do you install restapi... by pip or by conda – A.A Aug 24 '18 at 14:09
3

You will probably have better luck doing the query against the layer itself. This worked for me:

http://hebid.hessen.de/hlbk/hlbk_grundlagen/MapServer/1/query?where=OBJECTID=5256&returnGeometry=true&f=pjson&outFields=*

When using the query from a layer, you must include an outFields argument. Another thing to note, you will not be able to get the value as geojson from the service you referenced, as that is currently at version 10.2.2 and geojson as an output format was not added until 10.3. So unfortunately, you'll only be able to get esri json. This is fairly easy to convert to geojson though.

Also, no need to reinvent the wheel. There are existing packages available to work with ArcGIS REST Services such as ArcREST or restapi. I maintain the (horribly named) restapi package, but it is easy to use and here is how you can accomplish getting geojson:

import restapi

# sample to get geojson
url = 'https://services2.arcgis.com/ZkOsbg84o8DsPPaP/arcgis/rest/services/MN_Breweries/FeatureServer/0'
lyr = restapi.FeatureLayer(url) # can provide username, password, token, or proxy if secured

# get feature set as GeoJson, very important - must set outSR to 4326 for geojson
features = lyr.query(where="Name = 'Mankato Brewery'", outSR=4326, f='geojson')

# save geojson file
features.dump(r'C:\TEMP\Test.geojson', indent=2) # indent allows for pretty view

# OR, you can save it directly to a shapefile (does not require arcpy)
lyr.export_layer(r'C:\TEMP\test.shp', where="Name = 'Mankato Brewery'")

In the above example, I used a restapi.FeatureLayer, but there is also support for Map Service Layers via the restapi.MapServiceLayer. Using your example:

lyr = restapi.MapServiceLayer('http://hebid.hessen.de/hlbk/hlbk_grundlagen/MapServer/1')

features = lyr.query(where='OBJECTID=5256')

features.dump(r'C:\TEMP\feature.json')
  • Wow, thx for this answer, works like a charm! My feature-server questions solved, now I have to start to deal with the map-server questions (with ogc wms/wfs everything seemed to be a little easier). – AndreasK Nov 27 '17 at 22:32
  • 1
    If you have conda and Python 3, you could Python API for ArcGIS (developers.arcgis.com/python) that provides fairly Pythonic API into the ArcGIS REST API. restapi is awesome, but Python API for ArcGIS provides more options as it covers almost all capabilities of the ArcGIS REST API. You don't need ArcGIS software to be able to use this conda package. – Alex Tereshenkov Nov 28 '17 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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