3

I have a Python program that is not using arcpy. I need to get an image from a mapservice centered on a single feature using the REST api.

I can call the export method to get a map image:

http://<server>/arcgis/rest/services/<name>/MapServer/export?bbox=<bounds>&f=image

I can also query the mapservice layer using a where clause and get the feature geometry (in my case, it's going to be a polygon feature).

Without using arcpy, is there an easy way to take the polygon's geometry, get a rectangular extent to generate the bounding box necessary to call the export?

What would be ideal is if I could just say "Give me a map image centered on a feature from a layer defined by this where clause" but, unless I'm missing something, you can't do this in one step. Neither can I figure out how to do a Query on a layer and get the extent back instead of just the feature geometry.

1 Answer 1

5

You can use my restapi module. It is very simple to do these kinds of things:

import restapi
import os

url = 'http://gis.mda.state.mn.us/arcgis/rest/services/MN_Agriculture_EAB_Detection/MapServer'

# reference map service
mapService = restapi.MapService(url)

# reference quarantine county layer so we can get a single feature
quarantines = mapService.layer('emerald ash borer quarantines') # could also just use the service id of 3

# query this to get a single feature
fs = quarantines.query(where="COUNTYNAME = 'Hennepin'")

featureGeom = restapi.Geometry(fs) # will grab the first polygon

# now export map png
png = r'C:\TEMP\test_export.png'
r = mapService.export(png, bbox=featureGeom.envelope(), bboxSR=mapService.spatialReference)
os.startfile(png)

This module does NOT require arcpy, but will use it if available. Otherwise it uses open source alternatives.

1
  • Awesome. I'll take a look and approve the answer if it works for me. Meanwhile, I adapted some code from here: gis.stackexchange.com/questions/90553/… and got it working in a brute force sorta way (after adapting this code to use ESRI json instead of GeoJSON).
    – CMPalmer
    Commented Apr 1, 2016 at 17:14

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.