Well here is the final result.....and I will try and explain what is going on.....
Right now its set up to grab a few parameters that are going to be passed to it once it is published as a Rest Endpoint service....I have those 3 parameters hardcoded for testing purposes (address, distance, and unique ID)
# PARAMETERS PASSED FROM CALL TO THE SERVICE
searchAddress = "86 Rockland Street West Davenport, NY 13860"
searchDistance = 20
searchid = "123"
The script takes the address and geocodes it to get XY
The XY are then used to create a temp Buffer at the user specified distance
this buffer is then used to select counties that INTERSECT it
this returned data is then packaged up to be send back to the requester...
Any questions please feel free to ask.....
THANK YOU ALL FOR YOUR HELP
OH one more thing....I was tripping up on the spatial reference....my Counties were in UTM and the XY being returned from the Geocode was Decimal Degrees....it could never find anything in the spatial select. Knowing that my data was in UTM I made sure that I set the output of the Geocode result to UTM as well
def singleAdressGeocode(address, geoCodeUrl, outSR = "26917"):
Last thing my data was also in meters so when the Miles were sent as parameters from the requester I had to convert those to meters to feed the buffer parameters
searchDistance = 20
searchid = "123"
distanceMeters = searchDistance * 1609.34
FINAL CODE
import arcpy
import requests
searchAddress = "86 Rockland Street West Davenport, NY 13860"
searchDistance = 20
searchid = "123"
geoCodeUrl = "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates"
distanceMeters = searchDistance * 1609.34
print " - SEARCH PARAMETERS -"
print "search address: " + searchAddress
print "passed unique ID : " + searchid
print "search miles: " + str(searchDistance)
print "search meters: " + str(distanceMeters)
print ""
print "-------------------"
coordinates = ""
countList = []
countyField = "FIPS2"
countList2 = []
finalList = []
txt_list = ""
txt_list2 = ""
def singleAdressGeocode(address, geoCodeUrl, outSR = "26917"):
address = address.replace(" ", "+")
address = address.replace(",", "%3B")
lookup = requests.get(geoCodeUrl + "?SingleLine=" + address + "&outSR=" + outSR + "&maxLocations=1&f=pjson")
data = lookup.json()
if data["candidates"]:
return data
else:
return "Address not geocoded: " + address
geocodeResult = singleAdressGeocode(searchAddress, geoCodeUrl)
addressResult = geocodeResult["candidates"][0]["address"]
coordinateX = geocodeResult["candidates"][0]["location"]["x"]
coordinateY = geocodeResult["candidates"][0]["location"]["y"]
strcoordinateX = str(coordinateX)
strcoordinateY = str(coordinateY)
print "gecode result: " + str(geocodeResult)
print "-------------------"
print "geocoded address: " + addressResult
print "-------------------"
print "X coordinate: " + str(coordinateX)
print "-------------------"
print "Y coordinate: " + str(coordinateY)
print "-------------------"
point = arcpy.Point(coordinateX, coordinateY)
ptGeometry = arcpy.PointGeometry(point)
arcpy.Buffer_analysis(ptGeometry, 'IN_MEMORY/BufferGeom', distanceMeters)
arcpy.env.workspace = "C:\Work\CountySelection\Data\SelectCounties.gdb"
arcpy.MakeFeatureLayer_management('CountiesDissolved', 'counties_lyr')
results = arcpy.SelectLayerByLocation_management('counties_lyr', 'INTERSECT', 'IN_MEMORY/BufferGeom')
rows = arcpy.SearchCursor(results,"","","FIPS2")
for row in rows:
neighborVal = str(row.FIPS2)
countList.append(neighborVal)
txt_list = ','.join(countList)
print "FIPS codes: " + str(txt_list)
print "-------------------"
rows2 = arcpy.SearchCursor(results,"","","FIRST_JURI")
for row in rows2:
neighborVal2 = str(row.FIRST_JURI)
countList2.append(neighborVal2)
txt_list2 = ','.join(countList2)
print "County Name: " + str(txt_list2)
print "-------------------"
fields = ['FIPS2', 'FIRST_JURI']
with arcpy.da.SearchCursor(results, fields) as cursor:
for row in cursor:
finalList.append('[{0}-{1}]'.format(row[0], row[1]))
print "[" + "[" + strcoordinateX + "]," + "[" + strcoordinateY + "]," + "[" + str(searchDistance) + "]," + "[" + searchid + "]," + "[" + txt_list + "]," + "[" + txt_list2 + "], [" + str(finalList) + "] ]"
print "-------------------"
matchcount = int(arcpy.GetCount_management('counties_lyr')[0])
if matchcount == 0:
print('no features matched spatial and attribute criteria')
else:
print('{0} counties were located within {1} miles of your location'.format(
matchcount, searchDistance))
print "-------------------"