-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_geocodes.py
145 lines (127 loc) · 4.27 KB
/
get_geocodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from models import *
from time import sleep
from config import api_key
from requests import Session
import json
# import googlemaps
# gmaps = googlemaps.Client(key=api_key)
# get already geocoded voters
geocoded = []
print ' Checking current geocodes...'
# for i in Voter_Geocode.select():
# geocoded.append(i.voter_id)
# set up a requests session
with Session() as r_sesh:
# loop over all the Columbia city voters who are not yet geocoded
for voter in Voter.select(
).where(
~(Voter.vr_id << geocoded)
& (Voter.res_cty _st == 'COLUMBIA')
).order_by(Voter.vr_id):
# set up an address string
address_str = '{vr_housno} {vr_dir} {vr_street} {vr_kind} {vr_suffix} {vr_apt} {vr_add2} {res_cty_st} MO {vr_zipa}'.format(**voter.__dict__['_data']).replace('None ', '')
print ' Getting geocodes for {0} (vr_id: {1})'.format(address_str, voter.vr_id)
# and the request arguments
url = 'https://maps.googleapis.com/maps/api/geocode/json'
params = {
'address': address_str
# exclude results that aren't in Boone County
, 'components': 'administrative_area_level_2:Boone County'
# bias results to area surrounding CoMO
, 'bounds': '38.7738743197085,-92.5692039802915|39.2191059,-92.1350410197085'
, 'key': api_key
}
# pause for a tenth of a second
sleep(.13)
# make the get request
try:
response = r_sesh.get(url, params = params)
except Exception as e:
print type(e)
print e
else:
# if we get a response, turn the json into a dict
dict_response = json.loads(response.content)
print ' Response Status: {}'.format(dict_response['status'])
geocode = Voter_Geocode(
voter = voter.vr_id
, service = 'GoogleV3'
, status = dict_response['status']
, json_results = response.content
)
# check to see if results include a geocode
try:
geometry = dict_response['results'][0]['geometry']
except (IndexError, KeyError):
pass
else:
# set the lat attribute, if included in the results
geocode.lat = geometry['location']['lat']
# set the lng attribute, if included in the results
geocode.lng = geometry['location']['lng']
# set the location_type attribute, if included in the results
geocode.location_type = geometry['location_type']
# check to see if results include a formatted address
try:
geocode.formated_address = dict_response['results'][0]['formatted_address']
except (IndexError, KeyError):
pass
# check to see if results include address components
try:
address_components = dict_response['results'][0]['address_components']
except (IndexError, KeyError):
pass
else:
# set up a dictionary for the address components
addr_dict = {}
# add all the components to the dict
for i in address_components:
addr_dict[i['types'][0]] = i['long_name']
# now set each geocode attribute
try:
geocode.street_number = addr_dict['street_number']
except KeyError:
geocode.street_number = None
try:
geocode.route = addr_dict['route']
except KeyError:
geocode.route = None
try:
geocode.locality = addr_dict['locality']
except KeyError:
geocode.locality = None
try:
geocode.administrative_area_level_3 = addr_dict['administrative_area_level_3']
except KeyError:
geocode.administrative_area_level_3 = None
try:
geocode.administrative_area_level_2 = addr_dict['administrative_area_level_2']
except KeyError:
geocode.administrative_area_level_2 = None
try:
geocode.administrative_area_level_1 = addr_dict['administrative_area_level_1']
except KeyError:
geocode.administrative_area_level_1 = None
try:
geocode.postal_code = addr_dict['postal_code']
except KeyError:
geocode.postal_code = None
try:
geocode.postal_code_suffix = addr_dict['postal_code_suffix']
except KeyError:
geocode.postal_code_suffix = None
try:
geocode.country = addr_dict['country']
except KeyError:
geocode.country = None
# try saving the geocode
try:
with db.atomic():
geocode.save()
except Exception as e:
if 'duplicate' in e.message:
print 'Duplicate'
else:
print 'Error on line #{0}: {1}'.format(inspect.currentframe().f_lineno, e)
print '--------------'
print 'fin.'