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