Skip to content

Instantly share code, notes, and snippets.

@quandyfactory
Last active December 12, 2015 06:19
Show Gist options
  • Save quandyfactory/4728806 to your computer and use it in GitHub Desktop.
Save quandyfactory/4728806 to your computer and use it in GitHub Desktop.
Fetches geocode lat and lng for Hamilton building permits
import xlrd
import json
import urllib
path = 'permits.xls'
outfile = 'permits.csv'
template = 'http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address='
def get_data(path=path):
"""Gets the records from the excel file"""
wb = xlrd.open_workbook(path)
sh = wb.sheet_by_name(u'Section A')
data = []
for rownum in range(6,sh.nrows): # skip garbage at top
data.append(sh.row_values(rownum))
return data
def get_coordinates(addr, template=template, city='Hamilton, ON', debug=False):
"""Returns x/y coordinates and full address for an address"""
address = '%s %s' % (addr, city)
address = address.replace(' ', ' ')
url = '%s%s' % (template, urllib.quote(address))
if debug == True: print url
response = urllib.urlopen(url)
content = response.read()
try:
obj = json.loads(content)
except:
obj = None
print 'No json content for %s' % (url)
if obj == None:
return ['', '', '']
lat = obj['results'][0]['geometry']['location']['lat'] # get latitude
lng = obj['results'][0]['geometry']['location']['lng'] # get longitude
full_address = obj['results'][0]['formatted_address'] # get full address
return [lat, lng, full_address]
def process_data(data):
"""adds coordinates to data"""
with open('/home/ryan/Desktop/permits.csv', 'w') as file:
file.write('')
for row in data:
print row
print 'Processing %s...' % (row[0])
print
addr = ''
extra = []
try:
addr = row[6]
except:
print row
if addr != '':
extra = get_coordinates(row[6])
rowlist = row + extra
with open(outfile, 'a') as file:
file.write('%s\n' % (
', '.join(['%s' % ('"'+item+'"' if type(item).__name__ == 'unicode' else item) for item in rowlist])
)
)
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment