Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from collections import namedtuple
import struct
import sys
from enum import Enum
from datetime import datetime, date, time
class PointSource(Enum):
EAppleNative=0
EWindowsNative=1
EAndroidNative=2
EGoogle=3
ETizen=4
EPredictor=5
Point = namedtuple("Point", "timestamp latitude longitude altitude speed bearing horizontalAccuracy verticalAccuracy source")
PointPattern = '<ddddddddB'
PointPackSize = struct.calcsize(PointPattern)
HeaderSize = 4
print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
print("<gpx version=\"1.0\">")
print("<name>gpx</name>")
print("<trk><name>gpx</name><number>1</number><trkseg>")
with open(sys.argv[1], 'rb') as f:
data_input = f.read()
raw_points = data_input[HeaderSize:HeaderSize+PointPackSize*((len(data_input)-4)//PointPackSize)]
for rawPoint in struct.iter_unpack(PointPattern, raw_points):
point = Point(*rawPoint[:-1], PointSource(rawPoint[-1]))
print('<trkpt lat="'+str(point.latitude)+'" lon="'+str(point.longitude)+'"><fix>3d</fix><ele>'+str(point.altitude)+'</ele><time>'+datetime.utcfromtimestamp(point.timestamp).strftime('%Y-%m-%dT%H:%M:%SZ')+'</time></trkpt>')
print("</trkseg></trk>")
print("</gpx>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.