Skip to content

Instantly share code, notes, and snippets.

@michalpelka
Created February 25, 2024 23:15
Show Gist options
  • Save michalpelka/1ed915555a3c813c3bd454a047bfe934 to your computer and use it in GitHub Desktop.
Save michalpelka/1ed915555a3c813c3bd454a047bfe934 to your computer and use it in GitHub Desktop.
from pathlib import Path
import gpxpy
import gpxpy.gpx
from rosbags.highlevel import AnyReader
import sys
bag = sys.argv[1]
gpx = sys.argv[2]
def create_gpx_from_datapoints(datapoints, output_file='output.gpx'):
gpx = gpxpy.gpx.GPX()
for data in datapoints:
lat, lon, ele = data
gpx_track = gpxpy.gpx.GPXTrack()
gpx_segment = gpxpy.gpx.GPXTrackSegment()
gpx_point = gpxpy.gpx.GPXTrackPoint(latitude=lat, longitude=lon, elevation=ele)
gpx_segment.points.append(gpx_point)
gpx_track.segments.append(gpx_segment)
gpx.tracks.append(gpx_track)
with open(output_file, 'w') as f:
f.write(gpx.to_xml())
# create reader instance and open for reading
with AnyReader([Path('/home/michal/wawa/1/rosbag2_2024_02_22-10_36_29')]) as reader:
connections = [x for x in reader.connections if x.topic == '/fix']
coordinates = []
for connection, timestamp, rawdata in reader.messages(connections=connections):
msg = reader.deserialize(rawdata, connection.msgtype)
coordinates.append([msg.latitude , msg.longitude, msg.altitude])
create_gpx_from_datapoints(coordinates, gpx)
print ("done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment