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.
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.
C:ArcpyBookCh12ExportMapToImage.py
.requests
and json
modules:import requests import json
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"
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'}
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)
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)
C:ArcpyBookcodeCh12ExportMapToImage.py
solution file..gif
file that was generated into your browser address bar, and click on return on your keyboard to see the file: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.