forked from DL6AKU/CallmapGermany
-
Notifications
You must be signed in to change notification settings - Fork 2
/
makegeo.py
executable file
·43 lines (35 loc) · 1.23 KB
/
makegeo.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#By Ulrich Thiel, VK2UTL/DK1UT
#Adds coordinates to calls.db
import sqlite3
import geocoder
import requests
import sys
import time
from tqdm import tqdm
dbconn = sqlite3.connect('calls.db')
dbcursor = dbconn.cursor()
#For initial geocoding
dbcursor.execute("SELECT Id, Street, Zip, City FROM Callsigns WHERE Geocode IS NULL AND Street IS NOT NULL")
#For update geocoding
#dbcursor.execute("SELECT * FROM Callsigns WHERE Geocode = 0")
res = dbcursor.fetchall()
print str(len(res)) + " addresses selected for geocoding"
#keep session alive
with requests.Session() as session:
for i in tqdm(range(len(res))):
row = res[i]
address = row[1] + ", " + row[2] + " " + row[3] + ", Germany"
#print address
g = geocoder.google(address, session=session)
#print g.status
if g.status == 'OVER_QUERY_LIMIT':
print "Query limit reached"
sys.exit(0)
if g.status == 'ZERO_RESULTS' or g.status == 'ERROR - No results found' or g.lng == None:
dbcursor.execute("UPDATE Callsigns SET Geocode = 0 WHERE Id = " + str(row[0]))
else:
dbcursor.execute("UPDATE Callsigns SET Lng = " + str(g.lng) + ", Lat = " + str(g.lat) + ", Geocode = 1 WHERE Id = " + str(row[0]))
dbconn.commit()
dbconn.close()