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