-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_locations_ebola.py
53 lines (45 loc) · 1.77 KB
/
build_locations_ebola.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
import sys
import urllib
import simplejson
import csv
googleGeocodeUrl = 'http://maps.googleapis.com/maps/api/geocode/json?'
csv_file = 'ebola_data_db_format2.csv'
def get_coordinates(query, from_sensor=False):
query = query.encode('utf-8')
params = {
'address': query,
'sensor': "true" if from_sensor else "false"
}
url = googleGeocodeUrl + urllib.urlencode(params)
json_response = urllib.urlopen(url)
response = simplejson.loads(json_response.read())
if response['results']:
print response
location = response['results'][0]['geometry']['location']
form_address = response['results'][0]['formatted_address']
latitude, longitude = location['lat'], location['lng']
print "Found", len(response['results']), "for:\n", query, "\nFormatted Address::\n", form_address, "\nHighest Accuracy:\n", latitude, longitude
else:
#latitude, longitude = None, None
print query, "<no results>"
return None
return [latitude, longitude]
def build_table():
countries_seen = {}
# open csv file
with open(csv_file, 'rb') as csvfile:
with open('OUT_' + csv_file, 'w+') as csvfileW:
reader = csv.DictReader(csvfile)
writer = csv.DictWriter(csvfileW, ['Latitude', 'Longitude'])
for row in reader:
country = row['Country']
print(country)
if not country in countries_seen:
coords = get_coordinates(country)
countries_seen[country] = coords
else:
coords = countries_seen[country]
print(coords)
writer.writerow({'Latitude': coords[0], 'Longitude': coords[1]})
if __name__=='__main__':
build_table()