-
Notifications
You must be signed in to change notification settings - Fork 0
/
coordinate.py
51 lines (31 loc) · 942 Bytes
/
coordinate.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
from geopy.geocoders import GoogleV3
import sqlite3
import key
geolocator = GoogleV3(api_key=key.api_key)
def coor():
conn = sqlite3.connect('subway.db')
cursor = conn.cursor()
adds = [
'''
ALTER TABLE store
ADD COLUMN latitude REAL;
''',
'''
ALTER TABLE store
ADD COLUMN longitude REAL;
'''
]
for add in adds:
cursor.execute(add)
cursor.execute("SELECT name, address FROM store")
rows = cursor.fetchall()
for row in rows:
address = row[1] if row[1] else row[0]
location = geolocator.geocode(address)
if location:
latitude = location.latitude
longitude = location.longitude
cursor.execute("UPDATE store SET latitude = ?, longitude = ? WHERE address = ?", (latitude, longitude, row[1]))
conn.commit()
conn.close()
print("Coordinate added")