Created
February 11, 2015 15:00
-
-
Save evz/3a3b18707e5c16a8abe7 to your computer and use it in GitHub Desktop.
GeoCoder script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import usaddress | |
import csv | |
def getRecords(): | |
with open('addresses.csv', 'rb') as f: | |
reader = csv.reader(f) | |
for row in reader: | |
yield row | |
def geocode(address): | |
url = 'https://geomap.ffiec.gov/FFIECGeocMap/GeocodeMap1.aspx/GetGeocodeData' | |
headers = {'content-type': 'application/json; charset=utf-8'} | |
params = {'sSingleLine': address, 'iCensusYear': "2014"} | |
r = requests.post(url, headers=headers, data=json.dumps(params)) | |
return r.json() | |
if __name__ == "__main__": | |
import time | |
f = open('geocoded.csv', 'wb') | |
writer = csv.writer(f) | |
iterator = iter(getRecords()) | |
header = iterator.next() | |
writer.writerow(header) | |
while True: | |
try: | |
row = iterator.next() | |
add = row[0] | |
response = geocode(add) | |
latitude = response['d']['sLatitude'] | |
longitude = response['d']['sLongitude'] | |
if not latitude: | |
print 'trying again' | |
parsed_address = usaddress.parse(row[0]) | |
add = ' '.join([component for component, label in parsed_address \ | |
if label in ['AddressNumber', 'StreetNamePreDirectional', 'StreetName']]) | |
response = geocode('{0} Chicago, IL'.format(add)) | |
latitude = response['d']['sLatitude'] | |
longitude = response['d']['sLongitude'] | |
row.extend([latitude, longitude]) | |
print (add, row[0], latitude, longitude) | |
time.sleep(0.4) | |
writer.writerow(row) | |
except StopIteration: | |
break | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment