Getting layer information with the ArcGIS REST API and Python

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.

Getting ready

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.

How to do it…

Follow these steps to learn how to get information about a layer from a map service:

  1. In IDLE or another Python development environment, create a new Python script called GetLayerInformation.py and save it to the C:ArcpyBookCh12 folder.
  2. Import the requests and json modules:
    import requests
    import json
  3. Create the following 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"
    
  4. Create a 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'}
    
  5. Call the 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)
    
  6. Convert the JSON to a Python dictionary:
    r = requests.get(agisurl, params=payload)
    decoded = json.loads(r.text)
  7. Print out the name, geographic extent, and fields of the layer:
    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'])
    
  8. You can check your work by examining the C:ArcpyBookcodeCh12GetLayerInformation.py solution file.
  9. Save the script and run it to see this output:
    How to do it…

How it works…

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset