· 5 years ago · May 27, 2020, 02:02 PM
1import urllib
2import urllib.request
3import sqlite3
4import json
5import time
6import ssl
7
8
9#key=AIzaSyAZOjISpzh67V1fzsn1YGDC7GjkJYy3gNY
10#serviceurl = "http://maps.googleapis.com/maps/api/geocode/json?"
11# If you are in China this URL might work (with key):
12# serviceurl = "http://maps.google.cn/maps/api/geocode/json?"
13
14serviceurl = "http://python-data.dr-chuck.net/geojson?"
15
16
17# Deal with SSL certificate anomalies Python > 2.7
18# scontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
19scontext = None
20
21conn = sqlite3.connect('geodata.sqlite')
22cur = conn.cursor()
23
24cur.execute('''
25CREATE TABLE IF NOT EXISTS Locations (address TEXT, geodata TEXT)''')
26
27fh = open("where.data", encoding="utf-8")
28count = 0
29for line in fh:
30 if count > 200 :
31 print ('Retrieved 200 locations, restart to retrieve more')
32 break
33 address = line.strip()
34 print ('')
35 cur.execute("SELECT geodata FROM Locations WHERE address= ?", ((address), ))
36 try:
37 data = cur.fetchone()[0]
38 print ("Found in database ",address)
39 continue
40 except:
41 pass
42
43 print ('Resolving', address)
44 url = serviceurl + urllib.parse.urlencode({"sensor":"false", "address": address})
45 print ('Retrieving', url)
46 uh = urllib.request.urlopen(url, context=scontext)
47 data = uh.read()
48 print ('Retrieved',len(data),'characters',data[:20].decode().replace('\n',' '))
49 count = count + 1
50 try:
51 js = json.loads(str(data))
52 # print js # We print in case unicode causes an error
53 except:
54 continue
55
56 if 'status' not in js or (js['status'] != 'OK' and js['status'] != 'ZERO_RESULTS') :
57 print ('==== Failure To Retrieve ====')
58 print(data)
59 continue
60
61 cur.execute('''INSERT INTO Locations (address, geodata)
62 VALUES ( ?, ? )''', bytes(( memoryview(address)),bytes(memoryview(data) )) )
63 conn.commit()
64 if count % 10 == 0 :
65 print('Pausing for a bit...')
66 time.sleep(5)
67
68print ("Run geodump.py to read the data from the database so you can visualize it on a map.")