Address, buffer, select

1289
11
Jump to solution
03-23-2020 09:04 AM
jaykapalczynski
Frequent Contributor

I am looking for the best way to eventually publish a GP tool that will accept an address, geocode the address, buffer the location X amount of distance and then select all the features it intersects.  Resulting in a list of intersected features.

Any examples?  

0 Kudos
11 Replies
jaykapalczynski
Frequent Contributor

I believe it was the projection on my Counties....passing DD and data was in UTM....

think I got it...

will be posting all code shortly

0 Kudos
jaykapalczynski
Frequent Contributor

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"

# CONVERT MILE TO METERS FOR SPATIAL BUFFER
distanceMeters = searchDistance * 1609.34

FINAL CODE

import arcpy
import requests

# PARAMETERS PASSED FROM CALL TO THE SERVICE
searchAddress = "86 Rockland Street West Davenport, NY 13860"
searchDistance = 20
searchid = "123"

# GEOCODERS
# esri geocoding service
geoCodeUrl = "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates"

# CONVERT MILE TO METERS FOR SPATIAL BUFFER
distanceMeters = searchDistance * 1609.34

# SEARCH PARAMETERS
print " - SEARCH PARAMETERS -"
print "search address: " + searchAddress
print "passed unique ID : " + searchid
print "search miles: " + str(searchDistance)
print "search meters: " + str(distanceMeters)
print ""
print "-------------------"

# LOCAL VARIABLES
coordinates = ""
countList = []   
countyField = "FIPS2"  
countList2 = []
finalList = []
txt_list = ""
txt_list2 = ""

def singleAdressGeocode(address, geoCodeUrl, outSR = "26917"):
  ##clean up the address for url encoding
  address = address.replace(" ", "+")
  address = address.replace(",", "%3B")

  #send address to geocode service
  lookup = requests.get(geoCodeUrl + "?SingleLine=" + address + "&outSR=" + outSR + "&maxLocations=1&f=pjson")

  data = lookup.json()
  
  if data["candidates"]:
    ##coords = data["candidates"][0]["location"]
    ##return coords
    return data
  else:
    ##no results
    return "Address not geocoded: " + address


# DEFINE VARIABLES FOR THE ADDRESS AND X AND Y VALUES
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 "-------------------"

# CREATE THE POINT FROM THE X AND Y
point = arcpy.Point(coordinateX, coordinateY)
ptGeometry = arcpy.PointGeometry(point)

# FEED THE BUFFER ANALYSIS THE POINT GEOMETRY THE DISTANCE PARAMETERS
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')

# GET THE FIPS CODES
rows = arcpy.SearchCursor(results,"","","FIPS2")
for row in rows:  
 neighborVal = str(row.FIPS2)  
 #print(neighborVal)  
 countList.append(neighborVal)  
 txt_list = ','.join(countList)
print "FIPS codes: " + str(txt_list)
print "-------------------"

# GET THE JURIS NAME
rows2 = arcpy.SearchCursor(results,"","","FIRST_JURI")
for row in rows2:  
 neighborVal2 = str(row.FIRST_JURI)  
 #print(neighborVal)  
 countList2.append(neighborVal2)  
 txt_list2 = ','.join(countList2)
print "County Name: " + str(txt_list2)
print "-------------------"

# GET BOTH THE FIPS AND JURIS NAME
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('{0}, {1}'.format(row[0], row[1]))
print "[" + "[" + strcoordinateX + "]," + "[" + strcoordinateY + "]," + "[" + str(searchDistance) + "]," + "[" + searchid + "]," + "[" + txt_list + "]," + "[" + txt_list2 + "], [" + str(finalList) + "] ]"
print "-------------------"


# GET THE COUNT OF THE RETURNED VALUES    
# If features matched criteria write them to a new feature class
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 "-------------------"
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos