-
Notifications
You must be signed in to change notification settings - Fork 0
Geocoding the Unmatched Addresses
albertkun edited this page Mar 6, 2017
·
1 revision
We have 72,613 addresses from the LASD data that isn't gecoded. I exported the fields labeled booking number, home address, city, state and zip the later 4 are necessary to run the script while the booking number is key to joining the results to the addresses we've already geocoded. Dan gave me her python script which uses the ESRI api geocoder. Yoh helped modify the script to ensure that the output gives the geocoded address, original address, lat, long, and score. Here is python script:
import json
import arcpy, urllib, urllib2, json, os
import csv
#change
inputFile = r'W:\IOES_Data\PIER_Data\Utilities\Fall2015CPUC_Data\geocoding\sce\street_res\input\inputSCE.csv'
inputFile = r'C:\Users\jcineus\Downloads\LASD_Bookings_Unmatched.csv'
def callEsriApi(address,city,region,postal):
print ("Attempting geocoding for:"+address)
url = "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?f=pjson&address=" + address + "&city=" + city + "®ion="+region+"&postal="+postal
response = urllib.urlopen(url)
data = json.loads(response.read())
if(len(data["candidates"]) > 0):
print ("found "+ str(len(data["candidates"])))
# matched addressed
addressMatched = data["candidates"] [0] ["address"]
print addressMatched
#getting the lat/lngs of the matched addressed
addressLat = data["candidates"][0]["location"]["y"]
addressLng = data["candidates"][0]["location"]["x"]
print addressLat
print addressLng
#getting the Score of the matched addressed
matchedAddressScore = data["candidates"][0]["score"]
print matchedAddressScore
print (str(addressMatched)+" "+str(addressLat)+" "+str(addressLng)+" "+str(matchedAddressScore))
return addressMatched, addressLat, addressLng, matchedAddressScore
else:
addressMatched = 'none'
addressLat = 0
addressLng = 0
return addressMatched, addressLat, addressLng, matchedAddressScore
def getAddress():
rownum = 0
#with open(r'W:\IOES_Data\PIER_Data\Utilities\Fall2015CPUC_Data\geocoding\sce\street_res\input\resultFromInputSCE.csv', 'w') as csvfile:
with open(r'C:\Users\jcineus\Downloads\LASD_Bookings_Unmatched_ESRIOnline2.csv', 'w') as csvfile:
# headers
fieldnames = ['booking_nu','addressMatched', 'lat','lon','score','originalAddress']
writer = csv.DictWriter(csvfile, delimiter=',', lineterminator='\n', fieldnames=fieldnames)
writer.writerow({'booking_nu':'booking_nu','addressMatched':'addressMatched','lat': 'lat','lon': 'lon','score': 'score','originalAddress': 'originalAddress' })
with open (inputFile, 'rb') as csvFile:
reader = csv.DictReader(csvFile)
for row in reader:
try:
if rownum == 0:
header = row;
else:
address = row['home_addre'] + ',' + row['city_1'] + ',' + row['state'] + ',' + row['zipcode']
booking_nu = row['booking_nu']
result = callEsriApi(row['home_addre'],row['city_1'],row['state'],row['zipcode'])
addressMatched =result[0]
addressLat = result[1]
addressLng = result[2]
matchedAddressScore = result[3]
writer.writerow({'booking_nu':booking_nu, 'addressMatched':addressMatched,'lat': addressLat,'lon': addressLng,'score': matchedAddressScore,'originalAddress':address })
rownum +=1
print rownum
except Exception as e:
rownum +=1
print e
print rownum
continue
def main():
getAddress()
if __name__ == "__main__":
main()