-
Notifications
You must be signed in to change notification settings - Fork 3
/
geocode
executable file
·325 lines (277 loc) · 13.3 KB
/
geocode
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import csvkit
import os
import errno
import glob
import sys
import time
import math
import sqlite3
import urllib
import urllib2
import json
import hashlib
import argparse
import time
parser = argparse.ArgumentParser(
description="A geocoder for for Virginia State Corporation Commission records",
epilog="https://github.com/openva/crump/")
parser.add_argument('-i', '--input', help="the file to parse", metavar="addresses.csv", required=True)
parser.add_argument('-c', '--columns', help="the range of columns containing addresses", metavar="9-14", required=True)
parser.add_argument('-v', '--verbose', help="verbose mode", action='store_true')
parser.add_argument('-s', '--since', help="only geocode records updated since this date (inclusive)", metavar="YYYY-MM-DD")
args = parser.parse_args()
input_file = args.input
verbose = args.verbose
columns = args.columns
date_since = args.since
if date_since:
date_since = time.strptime(date_since, "%Y-%m-%d")
# Specify the file and the columnar start and end. Realistically, we should be pulling this data out
# of the YAML table maps, since we specify which collections of fields are address fields. That
# would obviate the need to specify filenames or column ranges.
range = columns.split('-')
file_column_start = range[0]
file_column_end = range[1]
#input_file = "output/2_corporate.csv" #9-13
#input_file = "output/3_lp.csv" #10-14
#input_file = "output/9_llc.csv" #9-13
def main():
# We keep track of how many consecutive errors the API throws, to stop if there are too many.
api_error_count = 0
# Make sure that we can connect to the database.
try:
db = sqlite3.connect('addresses.db')
except sqlite3.error, e:
print "Count not connect to SQLite, with error %s:" % e.args[0]
sys.exit(1)
# Create a SQLite cursor.
cursor = db.cursor()
# See if the addresses table already exists.
cursor.execute("SELECT 1 FROM sqlite_master WHERE type='table' AND name='addresses'")
exists = cursor.fetchone()
# If the addresses table does not exist, try to retrieve it from a remote cache.
if exists is None:
from urllib2 import Request, urlopen, HTTPError
url = 'https://s3.amazonaws.com/virginia-business/addresses.db'
req = Request(url)
try:
f = urlopen(req)
# If the addresses table cannot be retrieved from a remote cache, create it.
except HTTPError as e:
cursor.execute("CREATE TABLE addresses(address_hash TEXT PRIMARY KEY NOT NULL, "
+ "address_cleaned TEXT, latitude INTEGER, longitude INTEGER, date INTEGER, source TEXT)")
db.commit()
print "Downloading data from " + url
local_file = open('addresses.db', 'w')
local_file.write(f.read())
local_file.close()
# Start reading from our input file.
with open(input_file, 'rb') as csvfile:
# Iterate through the lines in our input file.
csvreader = csvkit.DictReader(csvfile)
for record in csvreader:
if verbose:
print "\n" + record['street_1'] + ", " + record['street_2'] + ", " + record['city'] + ", " \
+ record['state'] + ", " + record['zip']
# Skip records without addresses.
if record['street_1'] == '' and record['street_2'] == '':
if verbose:
print "Skipping record without address"
else:
sys.stdout.write('A')
sys.stdout.flush()
continue
# Optionally skip any old records that don't need to be re-geocoded.
if date_since:
if record['address_date'] != '':
if time.strptime(record['address_date'], "%Y-%m-%d") < date_since:
if verbose:
print "Skipping old record"
else:
sys.stdout.write('S')
sys.stdout.flush()
continue
if time.strptime(record['status_date'], "%Y-%m-%d") < date_since:
if verbose:
print "Skipping old record"
else:
sys.stdout.write('S')
sys.stdout.flush()
continue
# Note the time at which we started reading this record, to make sure that we don't hit
# the API more than per second.
start_time = time.time()
# Collapse our address columns and save that as a hash, to save in the database. We
# convert to a byte str to avoid fatal Unicode-related erors for the odd problematic
# character.
try:
address_hash = hashlib.md5(str(record['street_1']).encode('utf-8') \
+ "," + str(record['street_2']).encode('utf-8') + "," \
+ str(record['city']).encode('utf-8') + "," \
+ str(record['state']).encode('utf-8') + "," \
+ str(record['zip']).encode('utf-8')).hexdigest()
# In the very rare instance of a character encoding error, just skip this record.
except UnicodeEncodeError as exception:
continue
# If the hashed, combined string is already stored in the database, skip to the next
# address.
hash_param = (address_hash,)
cursor.execute("SELECT 1 FROM addresses WHERE address_hash = ?", hash_param)
exists = cursor.fetchone()
if exists is not None:
if verbose:
print "Address found in local cache"
else:
sys.stdout.write('C')
sys.stdout.flush()
continue
# Remove quotation marks from street addresses.
record['street_1'] = record['street_1'].replace('"', '')
record['street_2'] = record['street_2'].replace('"', '')
# If either address field starts with "C/O" or "ATTN:" drop it.
if record['street_1'][:4].lower() == 'c/o ':
del record['street_1']
elif record['street_1'][:6].lower() == 'attn: ':
del record['street_1']
if record['street_2'][:4].lower() == 'c/o ':
del record['street_2']
elif record['street_2'][:6].lower() == 'attn: ':
del record['street_2']
# If either address 1 or address 2 contains "P[\.?][\s*])O[\.?] BOX" (or its variants),
# then drop it.
if 'street_1' in record:
if record['street_1'][:6].lower() == 'po box':
del record['street_1']
elif record['street_1'][:8].lower() == 'p.o. box':
del record['street_1']
if 'street_2' in record:
if record['street_2'][:6].lower() == 'po box':
del record['street_2']
elif record['street_2'][:8].lower() == 'p.o. box':
del record['street_2']
# If either address 1 or address 2 is blank, drop it.
if 'street_1' in 'record' and record['street_1'] == '':
del record['street_1']
if 'street_2' in 'record' and record['street_2'] == '':
del record['street_2']
# Encode each field as a key/value pair, for our query, Key=[Value], combining address 1
# and 2 (if both fields exist) into a single field.
url_parameters = {}
try:
url_parameters['Street'] = record['street_1'] + ", " + record['street_2']
except:
try:
url_parameters['Street'] = record['street_1']
except:
try:
url_parameters['Street'] = record['street_2']
except:
pass
url_parameters['City'] = record['city']
url_parameters['State'] = record['state']
url_parameters['ZIP'] = record['zip']
url_parameters['maxLocations'] = '1'
url_parameters['f'] = 'json'
url_parameters = urllib.urlencode(url_parameters)
# Assemble the URL.
if record['state'] == 'VA':
request_url = 'http://gismaps.vita.virginia.gov/arcgis/rest/services/Geocoding/VGIN_Composite_Locator/GeocodeServer/findAddressCandidates?' \
+ url_parameters
else:
request_url = 'http://geocoding.geo.census.gov/geocoder/locations/address?benchmark=Public_AR_Current&format=json&' \
+ url_parameters
# Fetch that URL.
try:
response = urllib2.urlopen(request_url)
except urllib2.URLError as e:
sys.stdout.write('‽')
sys.stdout.flush()
api_error_count += 1
if api_error_count == 10:
print "Too many API errors: halting."
sys.exit()
continue
# Load the returned JSON as an object.
address_data = json.load(response)
# Since this worked, reset the API error count to zero.
api_error_count = 0
# If this is a Virginia record.
if record['state'] == 'VA':
# If no address candidates were found, then record this as a failure and skip to the
# next line.
if len(address_data['candidates']) == 0:
if verbose:
print "API returned no address candidates"
else:
sys.stdout.write('✗')
sys.stdout.flush()
continue
# If the top result score is below 80, then record this as a failure and skip to the
# next line.
if address_data['candidates'][0]['score'] < 80:
### Realistically, we need to store in the database that no solid match could be
### found. Otherwise, we're going to look up this address over and over again.
if verbose:
print "API has insufficient confidence in its result"
else:
sys.stdout.write('✗')
sys.stdout.flush()
continue
# If the address is just a ZIP (indicating that we're getting the ZIP centroid), then
# record this as a failure and skip to the next line.
if len(address_data['candidates'][0]['address']) < 6:
if verbose:
print "API only returned a ZIP centroid"
else:
sys.stdout.write('✗')
sys.stdout.flush()
continue
final = {}
final['address'] = address_data['candidates'][0]['address']
final['longitude'] = address_data['candidates'][0]['location']['x']
final['latitude'] = address_data['candidates'][0]['location']['y']
final['time'] = round(time.time())
# Note the name of the API that we're using to geocode this data.
final['source_api'] = 'VITA'
# If this is an extra-Virginia record.
if record['state'] != 'VA':
# Bring the useful contents up a level.
address_data = address_data['result']['addressMatches']
# If no address candidates were found, then record this as a failure and skip to the
# next line.
if len(address_data) == 0:
if verbose:
print "Census API returned no address candidates"
else:
sys.stdout.write('✗')
sys.stdout.flush()
continue
final = {}
final['address'] = address_data[0]['matchedAddress']
final['longitude'] = address_data[0]['coordinates']['x']
final['latitude'] = address_data[0]['coordinates']['y']
final['time'] = round(time.time())
# Note the name of the API that we're using to geocode this data.
final['source_api'] = 'VITA'
# Insert the hashed address, cleaned address, latitude, longitude, timestamp, and source
# into the database.
cursor.execute("INSERT INTO addresses VALUES(?, ?, ?, ?, ?, ?)", \
(address_hash, final['address'], final['latitude'], final['longitude'], \
final['time'], final['source_api']))
db.commit()
# Report success to the terminal.
if verbose:
print "Address geocoded"
else:
sys.stdout.write('✓')
sys.stdout.flush()
# Pause long enough that 1 second has elapsed for this loop, to avoid hammering the API.
if record['state'] != 'VA':
if time.time() - start_time < 1:
time.sleep(1 - (time.time() - start_time))
# Close our database connection.
db.close()
if __name__ == "__main__":
main()