Python post request to Map Service with spatial filter

10-05-2017 09:52 AM
New Contributor III


i have used this script to call a Map Service an retrivethe geometry:

URL = "http://server/arcgis/rest/services/services/MapServer/0/query"
where = "1=1" 
fields = "*"
query = "?where={}&outFields={}&returnGeometry=true&f=json".format(where, fields)
fsURL = URL + query
fs = arcpy.FeatureSet()

Now i need to request this service with POST method by REST folder and pass it InpputGeometry with Spatial Relationship, so i can load only a subset of data. How i can do this?

There is some example?


0 Kudos
2 Replies
MVP Frequent Contributor

Edit: Sorry, I misread/misunderstood what you were asking for.  You will want to implement urlib2.

approw[2] is the 'SHAPE@JSON' key of a SearchCursor of the feature class we are intersecting with the map service features.

import urllib, urllib2

#process waterbody features
#FDEP is the authoritative source and steward for this data and is accessed via thier published map service
queryURL = ""
params = urllib.urlencode({'f': 'json', 'geometryType': 'esriGeometryPolygon',
       'geometry': approw[2], 'spatialRel': 'esriSpatialRelIntersects',
       'where': '1=1', 'outFields': 'WBID,WATER_TYPE,WATERBODY_NAME,CLASS', 'returnGeometry': 'false'})

req = urllib2.Request(queryURL, params)
response = urllib2.urlopen(req)
print '*** req start ***'
print str(approw[2])
print '*** req end ***'

jsonResult = json.load(response)

for feature in jsonResult['features']:
    wbdy = feature['attributes']['WATERBODY_NAME']
    wbdytype = feature['attributes']['WATER_TYPE']
    wbid = feature['attributes']['WBID']
    wbdyclass = feature['attributes']['CLASS']
    #print wbdy, wbdytype, wbid

    values = {'WATERBODYID': wbid,
              'WATERBODYNAME': wbdy,
              'WATERBODYTYPE': wbdytype,
              'WATERBODYCLASS': wbdyclass


data.update({'WATERBODIES': waterbodieslst})

New Contributor III

hi, James,

thanks for your replay. Your response if that I needed and i have correct my script in this way :

desc = arcpy.Describe(m)
xmin = desc.extent.XMin
xmax = desc.extent.XMax
ymin = desc.extent.YMin
ymax = desc.extent.YMax

envelope = '{"xmin":%s,"ymin":%s,"xmax":%s,"ymax":%s}' % (xmin, xmax, ymin, ymax)
line = arcpy.da.SearchCursor(m, ("SHAPE@JSON",)).next()[0]

queryURL = "http://server/arcgis/rest/services/services/MapServer/1/query"
params = urllib.urlencode({'f': 'json', 'geometryType': 'esriGeometryEnvelope', 'inSR':'4326',
       'spatialRel': 'esriSpatialRelIntersects','relationParam': 'FFFTTT***', 'outFields': '*','outSR':'4326', 'returnGeometry': 'true'})

req = urllib2.Request(queryURL, params)
response = urllib2.urlopen(req)
jsonResult = json.load(response)

but now i would like to create a in_memory feature of this result but 

fc = arcpy.CopyFeatures_management(jsonResult, r"in_memory\feature")

copy feature give me errorinstead with arcpy.Feature Set i could.

I can create feature by the result of query Map Service?


0 Kudos