-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPSlookup.py
More file actions
63 lines (55 loc) · 2.3 KB
/
Copy pathGPSlookup.py
File metadata and controls
63 lines (55 loc) · 2.3 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
""" GPS info transformation + reverse Geo lookup
"""
import requests
import logging
def reverse_lookup(lat, lon):
url = 'https://nominatim.openstreetmap.org/reverse'
payload = { 'format': 'jsonv2', 'lat': lat, 'lon': lon, 'zoom': 10, 'addressdetails': 1 }
headers = { 'User-Agent': 'pi3d RaspiPicFrame' }
ret='ERROR'
try:
response = requests.get( url, payload, headers=headers, timeout=3 )
except requests.exceptions.RequestException as err:
logging.error( "Couldn't request openstreetmap API: Exception {:s}".format(err) )
else:
if response.status_code == 200:
ret = response.json()
else:
logging.error( "Error while requesting openstreetmap API: {:s} -> {:d} {:s}".format( str(payload), response.status_code, response.reason) )
return ret
def latlon2dec(direction, degrees, minutes, seconds):
# GPS info can be decimal or tuples
if isinstance(degrees, tuple):
degrees = degrees[0] / degrees[1]
if isinstance(minutes, tuple):
minutes = minutes[0] / minutes[1]
if isinstance(seconds, tuple):
seconds = seconds[0] / seconds[1]
dec = float(degrees) + float(minutes)/60 + float(seconds)/(60*60)
if direction == 'S' or direction == 'W':
dec *= -1
return dec
def lookup( gps_info ):
try:
lat = latlon2dec( gps_info[1], gps_info[2][0], gps_info[2][1], gps_info[2][2] )
lon = latlon2dec( gps_info[3], gps_info[4][0], gps_info[4][1], gps_info[4][2] )
json = reverse_lookup(lat, lon)
if json != 'ERROR':
addr = json.get('address')
if addr:
ret = addr.get('country_code','??').upper() + '-'
if addr.get('postcode'):
ret += addr.get('postcode') + ' '
if json.get('name'):
ret += json.get('name')
except:
ret = ''
return ret
#############################################################################
if __name__ == "__main__":
logging.basicConfig( level=logging.INFO, format="%(asctime)s : %(levelname)s : %(message)s" )
gps_info = { 1: 'N', 2: ((48, 1), (7, 1), (3162, 100)), 3: 'E', 4: ((11, 1), (21, 1), (5236, 100)),
5: b'\x00', 6: (691071, 1286), 12: 'K', 13: (0, 1), 16: 'T', 17: (171448, 1293), 23: 'T', 24: (171448, 1293), 31: (65, 1)}
print( lookup(gps_info) )
gps_info = { 1: 'N', 2: (48, 7, 1, 3.162), 3: 'E', 4: (11, 21, 5.236 ) }
print( lookup(gps_info) )