forked from dscassel/neweatskw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocation.py
More file actions
23 lines (18 loc) · 833 Bytes
/
location.py
File metadata and controls
23 lines (18 loc) · 833 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import urllib
import json
def getLocation( cursor, address, city ):
cursor.execute("SELECT latitude, longitude FROM locations WHERE city = ? AND address = ?", (city, address) )
row = cursor.fetchone()
if row is None:
# We don't have a location for this address; look one up via Google
fulladdress = "{address}, {city}, Ontario, Canada".format( address=address, city=city)
rawdata = urllib.urlopen(
"http://maps.googleapis.com/maps/api/geocode/json?address={}&sensor=false".format(
urllib.quote( fulladdress ) ) ).read()
data = json.loads( rawdata )
location = data['results'][0]['geometry']['location']
lat, lng = location['lat'], location['lng']
cursor.execute("INSERT INTO locations (city, address, latitude, longitude) VALUES (?,?,?,?);",
(city, address, lat, lng))
return lat, lng
return row