The query
operation in the ArcGIS REST API performs a query against a map service and returns a feature set. The feature set includes values for fields requested by the user and can also return geometry, if requested.
In this recipe, you will build on the first recipe, in which you generated a URL using the ArcGIS Services page dialog box to generate results. In this recipe, you will use the ArcGIS Server Services page dialog box to generate a URL request that queried a map service layer and returned results. You may recall that the URL was http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/1/query?text=&geometry=&geometryType=esriGeometryPolygon&inSR=&spatialRel=esriSpatialRelIntersects&relationParam=&objectIds=&where=STATE_FIPS+%3D+%2748%27+and+CNTY_FIPS+%3D+%27021%27&time=&returnCountOnly=false&returnIdsOnly=false&returnGeometry=false&maxAllowableOffset=&outSR=&outFields=POP2000%2CPOP2007%2CBLKGRP&f=pjson
.
Now, let's learn how to submit this request using Python.
QueryMapService.py
and save it to the C:ArcpyBookCh12
folder.query
operation against a layer in a map service. As you scroll down the help
page, you should see the same parameters as were generated using the dialog box, such as geometry
, geometryType
, inSR
, spatialRel
, where
, and others.requests
and json
modules:import requests import json
agisurl
variable. This will serve as the base URL that references the query
operation on the census block group
layer (identified by an identifier of 1
in the URL) in the ESRI_Census_USA
map service:import requests
import json
agisurl = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/1/query"
import requests import json agisurl = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/1/query" payload = { 'where':'STATE_FIPS = '48' and CNTY_FIPS = '021'','returnCountOnly':'false', 'returnIdsOnly': 'false', 'returnGeometry':'false', 'outFields':'POP2000,POP2007,BLKGRP', 'f': 'pjson'}
requests.get()
method can accept a Python dictionary object as the second parameter. This dictionary defines the set of key/value pairs that help define the query string. Add the requests.get()
method:import requests
import json
agisurl = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/1/query"
payload = { 'where':'STATE_FIPS = '48' and CNTY_FIPS = '021'','returnCountOnly':'false',
'returnIdsOnly': 'false', 'returnGeometry':'false',
'outFields':'POP2000,POP2007,BLKGRP',
'f': 'pjson'}
r = requests.get(agisurl, params=payload)
print
statement in order to print the response that is returned.import requests, json
agisurl = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/1/query"
payload = { 'where':'STATE_FIPS = '48' and CNTY_FIPS = '021'','returnCountOnly':'false',
'returnIdsOnly': 'false', 'returnGeometry':'false',
'outFields':'POP2000,POP2007,BLKGRP',
'f': 'pjson'}
r = requests.get(agisurl, params=payload)
print(r.text)
print
statement that you added in the last step:r = requests.get(agisurl, params=payload) #print(r.text) decoded = json.loads(r.text)
json.loads()
method will contain the contents of the JSON object. You can then pull the individual data elements out of the dictionary. In this case, we want to pull out the attributes of each of the feature returned (BLKGRP
, POP2007
, and POP2000
). We can do so by using the following code, which you'll need to add to your script:r = requests.get(agisurl, params=payload) #print(r.text) decoded = json.loads(r.text) for rslt in decoded['features']: print("Block Group: " + str(rslt['attributes']['BLKGRP'])) print("Population 2000: " + str(rslt['attributes']['POP2000'])) print("Population 2007: " + str(rslt['attributes']['POP2007']))
C:ArcpyBookcodeCh12QueryMapService.py
solution file.The query
operation in the ArcGIS REST API can be used to perform spatial and attribute queries against a layer in an ArcGIS Server map service. We used the requests.get()
method to perform an attribute query against the census block groups
layer. We included various parameters, including a where clause that will only return records where the ST_FIPS
code is 48
and the CNTY_FIPS
code is 021
(Bexar County, Texas). The response
object was then converted to a Python dictionary and we included a for
loop to iterate through each of the returned records and print out the block group name, and the population for the years 2000 and 2007.