-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgps.py
85 lines (63 loc) · 1.74 KB
/
gps.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import pprint
import serial
import os
import pynmea2
import requests
import json
mapquestkey=os.environ.get('MAPQUEST_KEY')
max=10
def annotate(bag):
print(mapquestkey)
url = f"http://www.mapquestapi.com/geocoding/v1/reverse?key={mapquestkey}&location={bag['latitude']},{bag['longitude']}&includeRoadMetadata=true&includeNearestIntersection=true"
print(f"about to call url {url}")
try:
r = requests.get(url)
if(r.status_code == 200):
rdict = r.json()
bag['mapquest'] = rdict
else:
print(r.status_code)
except Exception as mapex:
print(mapex)
return bag
def parseNMEA(lines):
print(f"about to parse {len(lines)}")
latitude = None
longitude = None
altitude = None
altitude_units = None
for l in lines:
try:
msg = pynmea2.parse(l)
longitude = msg.longitude
latitude = msg.latitude
altitude = msg.altitude
altitude_units = msg.altitude_units
except pynmea2.nmea.ChecksumError as csex:
continue
if(latitude is not None and longitude is not None and altitude is not None):
return {'latitude':latitude, 'longitude':longitude , 'altitude':altitude, 'altitude_units':altitude_units}
elif(latitude is not None and longitude is not None):
return {'latitude':latitude, 'longitude':longitude}
else:
raise Exception("bah humbug no data found!")
ser = serial.Serial( port='/dev/ttyACM0', baudrate=9600)
print("connected to: " + ser.portstr)
#this will store some lines
lines = []
counter=0
while True:
s = str(ser.readline())
ln = (s[2:][:-5])
if(ln[0:6]=='$GPGGA'):
lines.append(ln)
counter = counter + 1
if(counter == max):
break
print("about to close serial read")
ser.close()
pp = pprint.PrettyPrinter(indent=4)
ret = parseNMEA(lines)
print(ret)
annotated = annotate(ret)
pp.pprint(annotated)