-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
opengeo.py
52 lines (37 loc) · 1.22 KB
/
opengeo.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
44
45
46
47
48
import urllib.request, urllib.parse
import json, ssl
# Heavily rate limited proxy of https://www.geoapify.com/ api
serviceurl = 'https://py4e-data.dr-chuck.net/opengeo?'
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
while True:
address = input('Enter location: ')
if len(address) < 1: break
address = address.strip()
parms = dict()
parms['q'] = address
url = serviceurl + urllib.parse.urlencode(parms)
print('Retrieving', url)
uh = urllib.request.urlopen(url, context=ctx)
data = uh.read().decode()
print('Retrieved', len(data), 'characters', data[:20].replace('\n', ' '))
try:
js = json.loads(data)
except:
js = None
if not js or 'features' not in js:
print('==== Download error ===')
print(data)
break
if len(js['features']) == 0:
print('==== Object not found ====')
print(data)
break
# print(json.dumps(js, indent=4))
lat = js['features'][0]['properties']['lat']
lon = js['features'][0]['properties']['lon']
print('lat', lat, 'lon', lon)
location = js['features'][0]['properties']['formatted']
print(location)