aboutsummaryrefslogtreecommitdiffstats
path: root/python/experiments/decode_gps.py
blob: da7904a9757dec5d44eb5afaaed1b779602a29d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python
import struct
from math import pow

EXAMPLE_MSG = b"<\xff\x01HB9EGM-7>APLRT1,WIDE1-1:!/7%=6P\')8[I#Q"

def decodeEncodedGPS(packet):
    """Returns a string with decoded latitude and longitude, or raises ValueError"""
    ix = packet.index(b":!/")
    gps_packet = packet[ix+3:]
    encoded_latitude = gps_packet[:4]
    encoded_longitude = gps_packet[4:8]

    (y1, y2, y3, y4) = struct.unpack("<bbbb", encoded_latitude)

    decodedLatitude = 90.0 - ((((y1-33) * pow(91,3)) + ((y2-33) * pow(91,2)) + ((y3-33) * 91) + y4-33) / 380926.0);

    (x1, x2, x3, x4) = struct.unpack("<bbbb", encoded_longitude)

    decodedLongitude = -180.0 + ((((x1-33) * pow(91,3)) + ((x2-33) * pow(91,2)) + ((x3-33) * 91) + x4-33) / 190463.0);
    return f"{decodedLatitude}N / {decodedLongitude}E"

print(decodeEncodedGPS(EXAMPLE_MSG))