A map service resource contains datasets that can include tables or layers. It contains basic information about a service, including feature layers, tables, and service descriptions. In this recipe, you will learn how to return layer information from a map service, using Python and the ArcGIS REST API.
To get information about a specific layer in a map service, you will need to reference the index number that is associated with the layer. When you examine the Services Directory page for a service, you will find a list of layers that are part of the map service along with index numbers for each layer. The index numbers are used instead of the layer name when requesting information about a layer. As we've done in the past few recipes, we'll use the Python requests
module to make the request and process the response.
Follow these steps to learn how to get information about a layer from a map service:
GetLayerInformation.py
and save it to the C:ArcpyBookCh12
folder.requests
and json
modules:import requests import json
agisurl
variable. This will serve as the base URL that references a specific layer in the ESRI_CENSUS_USA
map service. Here, we are referring to a layer with an index number of 1
. Also, include an output format of pjson
:import requests
import json
agisurl = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/1?f=pjson"
payload
variable. This variable will hold a Python dictionary object containing the parameters that will be passed as part of the request. We'll include a where
clause and set a few other properties:import requests import json agisurl = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/1" payload = { 'where': 'STATE_FIPS = '48' and CNTY_FIPS = '021'','returnCountyOnly': 'false', 'returnIdsOnly': 'false', 'returnGeometry': 'false', 'f': 'pjson'}
requests.get()
method and pass the agisurl
variable. The response will be stored in a variable called r
: import requests
import json
agisurl = http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/1?f=pjson
payload = { 'where': 'STATE_FIPS = '48' and CNTY_FIPS = '021'','returnCountyOnly': 'false',
'returnIdsOnly': 'false', 'returnGeometry': 'false',
'f': 'pjson'}
r = requests.get(agisurl, params=payload)
r = requests.get(agisurl, params=payload) decoded = json.loads(r.text)
r = requests.get(agisurl, params=payload) decoded = json.loads(r.text) print("The layer name is: " + decoded['name']) print("The xmin: " + str(decoded['extent']['xmin'])) print("The xmax: " + str(decoded['extent']['xmax'])) print("The ymin: " + str(decoded['extent']['ymin'])) print("The ymax: " + str(decoded['extent']['ymax'])) print("The fields in this layer: ") for rslt in decoded['fields']: print(rslt['name'])
C:ArcpyBookcodeCh12GetLayerInformation.py
solution file.In this recipe, we passed a reference to a URL that contains the path to a specific layer within a map service. The layer was specified through the use of an index number (1
, in this case). This URL was passed to the Python requests.get()
method. The response was returned in json
format and we then converted this to a Python dictionary. The dictionary included key/value pairs for the name, extent, and fields of the layer. That information was printed to the console.