-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsv_geocoder.py
More file actions
executable file
·74 lines (64 loc) · 2.58 KB
/
csv_geocoder.py
File metadata and controls
executable file
·74 lines (64 loc) · 2.58 KB
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
#!/usr/bin/env python
# encoding: utf-8
import csv
import rigeocoder
import sys
def main(path_to_csv, path_to_outcsv):
try:
outfile = open(path_to_outcsv, 'wb')
outcsv = csv.writer(outfile)
with open(path_to_csv, 'rb') as file:
reader = csv.reader(file)
header = reader.next()
outcsv.writerow(['street','city','state','linkid2', 'lat','lng'])
# count = 0
for row in reader:
#count +=1
#if count > 5:
# break
street1 = row[0]
street2 = row[1]
city2 = row[3]
state = row[4]
linkid2=row[5]
street_used = ''
# try the first combination then try with street1
print 'Tryin first geocoder with address: ' + street1
geo = rigeocoder.geocode_address(street1, city2)
if geo != None:
lat, lng = geo
street_used = street1
else:
# try the second combo
print 'Tryin first geocoder with address: ' + street2
geo = rigeocoder.geocode_address(street2, city2)
if geo != None:
lat, lng = geo
street_used=street2
else:
# try google!
print 'Tryin google geocoder with address: ' + street1
geo = rigeocoder.geocode_address_google(street1, city2)
if geo != None:
lat, lng = geo
street_used=street1
else:
# try the second address
print 'Tryin google geocoder with address: ' + street2
geo = rigeocoder.geocode_address_google(street2, city2)
if geo != None:
lat, lng = geo
street_used=street2
else:
street_used = street1
print street_used, city2 , linkid2, " Failed to geocode"
lat = 0
lng = 0
#output row
out = [street_used, city2, state, linkid2, lat, lng]
outcsv.writerow(out)
finally:
outfile.close()
if __name__ == '__main__':
#print sys.argv[1]
main(sys.argv[1], sys.argv[2])