-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathgeocode.py
64 lines (52 loc) · 2.07 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
import urllib2
import json
import os
import csv
import time
nameOverrides = {
"Congo, Dem. Rep." : "Democratic Republic of Congo",
"Congo, Rep." : "Republic of Congo",
"West Bank and Gaza" : "West Bank",
"Yemen, Rep." : "Yemen",
"Macedonia, FYR" : "Macedonia",
"Korea, Dem. Rep." : "North Korea",
"Korea, Rep." : "South Korea",
"Micronesia, Fed. Sts." : "Micronesia"
}
indexOverrides = {
"Mayotte" : 1,
"Reunion" : 1,
"French Guiana" : 1
}
if __name__ == "__main__":
fin = open("nations.json", "r")
countriesJson = json.load(fin)
fin.close()
for country in countriesJson:
#print country["name"]
# some of the country names in the raw dataset are hard to geocode
# provide an opportunity to manually override these country names for easier geocoding
if nameOverrides.has_key(country["name"]):
url = 'http://nominatim.openstreetmap.org/search?format=json&q=%s' % (nameOverrides[country["name"]])
else:
url = 'http://nominatim.openstreetmap.org/search?format=json&country=%s' % (country["name"])
url = url.replace(" ", "%20")
response = json.loads(urllib2.urlopen(url).read())
if len(response) == 0:
url = 'http://nominatim.openstreetmap.org/search?format=json&q=%s' % (country["name"])
url = url.replace(" ", "%20")
response = json.loads(urllib2.urlopen(url).read())
if len(response) == 0:
print country["name"]
continue
# sometimes the first result from nominatim is incorrect
# provide an opportunity to manually override the nominatim importance rating
# by default take the first result (index = 0)
resultIdx = 0
if indexOverrides.has_key(country["name"]):
resultIdx = indexOverrides[country["name"]]
country["lat"] = float(response[resultIdx]["lat"])
country["lon"] = float(response[resultIdx]["lon"])
fout = open('nations_geo.json', 'w')
json.dump(countriesJson, fout)
fout.close()