Getting XY from locator service in Python Script

212
2
03-20-2019 01:59 PM
AllenScully
Occasional Contributor II

Working on a GP tool that will be published as a GP service for use in a GP Widget/WebApp.  User will need to enter an address OR click on a map to kick of the tool, then a bunch of other things happen.

Basically, I want the input parameters for the tool to be X/Y - so I'm calling one of our locator services to handle the scenario where user inputs an address.  

All is working fairly well - can hit the locator, and get results.  However, there a few steps I'm fuzzy on as far as working with the results that come back from the locator.

1 - how to specify that we only need 1 result from the locator (the first one) - typically we are getting at least 2 results.

2 - how to specify that really all I want is the X and Y back

I think once I'm just getting a single result back, and then only the X/Y values, I can go forward.

Code is below, for just the call and return to/from the Locator service(locator should be publicly accessible) - the 'address' variable will be converted to a GetParameterAsText variable to allow user input, just hard-coded currently for development: 

import urllib
import json
import arcpy,os
arcpy.env.overwriteOutput = True

address = '201 N Stone Av'


data = {'SingleLine': address,
'f': 'pjson'}


URL = 'https://gis.tucsonaz.gov/public/rest/services/Geocoders/CENTERLINE_LOCATOR_WEB/GeocodeServer/findAddressCandidates'


result = urllib.urlopen(URL, urllib.urlencode(data)).read()
print json.loads(result)

Thanks - 

Allen Scully 

0 Kudos
2 Replies
AllenScully
Occasional Contributor II

Ok - sometimes it takes putting it out to there to see what's right in front of you -

Got it to return a single record (maxLocations):


data = {'SingleLine': address,
'maxLocations': 1,
'f': 'pjson'}

What I'm stuck on now is getting the 'outFields' portion to  be X and Y - simple formatting issue I think, as far as how to properly write out multiple fields to the 'outFields parameter - it works as below, returning the X attribute - now just need to add the Y attribute - have tried a bracketed lists: ('x','y'),  ['x','y'], {'x','y'},  - no luck, and simply putting a comma between:  'x','y', reads as invalid in the IDE

for example:

data = {'SingleLine': address,
'maxLocations': 1,

'outFields': 'x',
'f': 'pjson'}

0 Kudos
AllenScully
Occasional Contributor II

Looks like in order to specify multiple attributes using the format here, you just need your attributes to be in a comma-separated list, in DOUBLE quotes

So, full code being used to call a locator service and get the X,Y attributes returned - may vary depending on the configuration of the locator service you are hitting - but essentially the 'data' variable here is what gets put in the URL query to the locator so you can try submitting a request directly from the REST endpoint of the locator and see how that looks in the URL to determine what you want in the 'data' parameters.  

import arcpy,os
import urllib
import json
arcpy.env.overwriteOutput = True


address = '201 N Stone Av'


data = {'SingleLine': address,
'maxLocations': 1,
'outFields': "x,y",
'f': 'pjson'}

URL = 'https://gis.tucsonaz.gov/public/rest/services/Geocoders/CENTERLINE_LOCATOR_WEB/GeocodeServer/findAddressCandidates'

result = urllib.urlopen(URL, urllib.urlencode(data)).read()

print json.loads(result)

0 Kudos