Exporting a map with the ArcGIS REST API and Python

The ArcGIS REST API has a large set of operations that you can use when requesting information from an ArcGIS Server instance. For example, you can export maps, query layers, geocode addresses, and much more. In this recipe, you will learn how to export a map image from a map service.

Getting ready

The export operation can be used to create a map image from a map service. The response to this request includes the URL of the image, width, height, extent, and scale. In this recipe, you'll use the export operation to export a map as an image file.

How to do it…

  1. In your Python development environment, create a new script, save it as C:ArcpyBookCh12ExportMapToImage.py.
  2. Import the requests and json modules:
    import requests
    import json
  3. Create a new variable called agisurl, assign the URL, and export the operation, as seen here:
    import requests
    import json
    agisurl = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/export"
    
  4. Create a new dictionary object that will hold the key/value pairs that help define the query string. These are the parameters that will be passed to the export operation:
    import requests
    import json
    agisurl = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/export"
    payload = { 'bbox':'-115.8,30.4, 85.5,50.5', 
                'size':'800,600', 
                'imageSR': '102004', 
                'format':'gif', 
                'transparent':'false', 
                'f': 'pjson'}
    
  5. Call the requests.get() method, passing the URL and the Python dictionary of parameters. The response will be stored in a variable called r:
    import requests
    import json
    agisurl = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/export"
    payload = { 'bbox':'-115.8,30.4, 85.5,50.5', 'size':'800,600', 'imageSR': '102004', 'format':'gif', 'transparent':'false', 'f': 'pjson'}
    r = requests.get(agisurl, params=payload)
    
  6. Print out the contents of the response object:
    import requests
    import json
    agisurl = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/export"
    payload = { 'bbox':'-115.8,30.4, 85.5,50.5', 
                'size':'800,600', 
                'imageSR': '102004', 
                'format':'gif', 
                'transparent':'false', 
                      'f': 'pjson'}
    r = requests.get(agisurl, params=payload)
    print(r.text)
    
  7. You can check your work by examining the C:ArcpyBookcodeCh12ExportMapToImage.py solution file.
  8. Save and run the script to see an output similar to this:
    How to do it…
  9. Copy and paste the URL for the .gif file that was generated into your browser address bar, and click on return on your keyboard to see the file:
    How to do it…

How it works…

The export operation in the ArcGIS REST API can be used to export an image file from a map service. If you examine http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/export, which we used to generate the map image in this recipe, you'll see the term export at the end of the URL. This is what triggers the execution of the export operation. In addition to this, we also appended a bounding box (map extent), size, spatial reference for the image, and format through the payload variable. The request is sent to the server through the requests.get() method which accepts both the URL and payload variable.

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

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