Data served via ArcGIS Servers support a REST API for accessing these services. This notebook provides some examples on how we can use Python to access these services using the requests
package.
→ Full documentation on the ArcGIS REST API: https://developers.arcgis.com/rest/services-reference/get-started-with-the-services-directory.htm
We'll explore NCOneMap's web services, accessed here: https://services.nconemap.gov/secure/rest/services.
ImageServer
, FeatureServer
, MapServer
. These services provide access to different types of data. Other services provide access to data processing capabilities.NC1Map_Census (MapServer)
link to open the metadata for that map service.Description
, a list of Layers
included in that service, Spatial Reference
, and at the very bottom, a list of the Supported operations
.Export Map
operation to extract data from this service.-79.3, 35, -77.9, 36.5
4269
show:0
in the Layers: box to show only the first layer contained in this map service (1970 Census Boundary/Population). Upate the map by hitting Export Map (GET) again.HTML
to Image
and hit Export Map (GET) again.from IPython.display import Image
Image(url='https://services.nconemap.gov/secure/rest/services/NC1Map_Census/MapServer/export?bbox=-79.3%2C+35%2C+-77.9%2C+36.5&bboxSR=4269&layers=show%3A0&layerDefs=&size=&imageSR=&format=png&transparent=false&dpi=&time=&layerTimeOptions=&dynamicLayers=&gdbVersion=&mapScale=&rotation=&datumTransformations=&layerParameterValues=&mapRangeValues=&layerRangeValues=&f=image')
Now let's dissect that URL into a service endpoint (its address) and a list of parmaters.
Below is the full url:
https://services.nconemap.gov/secure/rest/services/NC1Map_Census/MapServer/export?bbox=-79.3%2C+35%2C+-77.9%2C+36.5&bboxSR=4269&layers=show%3A0&layerDefs=&size=&imageSR=&format=png&transparent=false&dpi=&time=&layerTimeOptions=&dynamicLayers=&gdbVersion=&mapScale=&rotation=&datumTransformations=&layerParameterValues=&mapRangeValues=&layerRangeValues=&f=image
Everything up to the ?
is the service end point (the server's internet address and the service location). Everything after that are parameters separated by &
.
requests
package, i.e. a URL variable and a dictionary of parmeter name:value pairs.theURL = 'https://services.nconemap.gov/secure/rest/services/NC1Map_Census/MapServer/export'
params = {"bbox":"-79.3, 35,-77.9, 36.5",
"bboxSR":"4269",
"layers":"show:0",
"layerDefs":"",
"size":"",
"imageSR":"",
"format":"png",
"transparent":"false",
"dpi":"",
"time":"",
"layerTimeOptions":"",
"dynamicLayers":"",
"gdbVersion":"",
"mapScale":"",
"rotation":"",
"datumTransformation":"",
"layerParameterValue":"",
"mapRangeValues":"",
"layerRangeValues":"",
"f":"image"
}
requests
package and execute our REST request.#import the modules
import requests
response = requests.get(theURL, params)
from IPython.display import Image
Image(response.content)
Ok, it's just the same as above, but we can easily change values in our params
dict and modify the output.
#Zoom more into Durham and show a different layer
params['bbox'] = '-79.0, 35.85, -78.8, 36.25'
params['layers'] = 'show:8'
response = requests.get(theURL, params)
Image(response.content)
#Add a layer definition to just show durham county
params['layerDefs']="{8:GEOID10 LIKE '37063%'}"
response = requests.get(theURL, params)
Image(response.content)