forked from h4ck3rm1k3/codefortrenton-geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeocode.py
81 lines (62 loc) · 1.82 KB
/
geocode.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
#!/usr/bin/env python
import sys
import pprint
# https://github.com/opencagedata/python-opencage-geocoder
sys.path.append('./python-opencage-geocoder/')
import opencage
import opencage.geocoder
import csv
#from opencage.geocoder import OpenCageGeocoder
from secrets import opencage_key
geocoder = opencage.geocoder.OpenCageGeocode(opencage_key)
fn = 'trenton-data-set-2015.csv'
#of = open(fn, 'wb'):
import pymongo
client = pymongo.MongoClient('mongodb://admin:[email protected]')
db = client.code_for_trenton
data = db.addresses
alld = {}
for c in data.find({}):
name = c['Name']
alld[name]=c
print "name", name
with open(fn, 'rb') as csvfile:
reader = csv.DictReader(csvfile,delimiter=',', quotechar='"' ) # fieldnames=fieldnames
for c in reader:
name = c['Name']
# unicode
name = unicode(name, 'utf-8')
if name not in alld:
print "Adding",name
alld[name]=c
data.insert(c)
for n in alld:
x = alld[n]
if (x['Street']):
if len(x['Zip Exten']) > 0:
addr= x['Street'] +"\n" + x['City'] + " " + x['State'] + " "+ x['Zip'] + "-" + x['Zip Exten'] + ", USA"
else:
addr= x['Street'] +"\n" + x['City'] + " " + x['State'] + " "+ x['Zip'] + ", USA"
u = False
if 'GeoCode' not in x:
gc = geocoder.geocode(addr)
x['GeoCode']=gc
u = True
#print addr
if x['Full Address']!=addr:
x['Full Address']=addr
u = True
if u:
print "Updating" + x['Name']
data.update(
{
'Name' : x['Name']
},
x,
upsert=True
)
else:
print "Already done" + x['Name']
#pprint.pprint(x)
#exit(0)
#exit(0)