/
GeoLookup.py
63 lines (51 loc) · 1.86 KB
/
GeoLookup.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
import json
import urllib
import urllib2
class GeoLookup:
"""
GeoCode a list of geometries with the bing API and make GeoJSON.
"""
API_URL = "http://dev.virtualearth.net/REST/v1/Locations"
def __init__(self, key, srcfile):
"""
Open the sourcefile and load the resources into a variable.
"""
self.API_KEY = key
f = open(srcfile)
self.ogdata = json.loads(f.read())
self.geodata = []
def pullGeoCodes(self):
"""
Pull the geocodes for each point.
"""
for entity in self.ogdata:
params = entity
params['key'] = self.API_KEY
query_params = urllib.urlencode(params)
data = urllib2.urlopen("%s?%s" % (self.API_URL,
query_params)).read()
temp_json = json.loads(data)
lat = temp_json['resourceSets'][0]['resources'][0]['point']['coordinates'][1]
lon = temp_json['resourceSets'][0]['resources'][0]['point']['coordinates'][0]
temp_dict = {
'locality': params['locality'],
'adminDistrict': params['adminDistrict'],
'lat': lat,
'lon': lon
}
self.geodata.append(temp_dict)
def getGeoJSON(self):
geojson = {"type": "FeatureCollection",
"features": []}
for geo in self.geodata:
temp = {"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [geo['lat'], geo['lon']]
},
"properties": {
"locality": geo['locality'],
"adminDistrict": geo['adminDistrict']
}}
geojson['features'].append(temp)
return json.dumps(geojson)