Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def path_to_gpx(path, filename=None):
gpx = gpxpy.gpx.GPX()
# Create first track in our GPX:
gpx_track = gpxpy.gpx.GPXTrack()
gpx.tracks.append(gpx_track)
# Create first segment in our GPX track:
gpx_segment = gpxpy.gpx.GPXTrackSegment()
gpx_track.segments.append(gpx_segment)
gpx_segment.points = [(gpxpy.gpx.GPXTrackPoint(lat, lon, time=time)) for (lat, lon, time) in path]
if filename:
with open(filename, 'w') as gpx_fh:
gpx_fh.write(gpx.to_xml())
return gpx
def write_gpx(path, data):
gpx = gpxpy.gpx.GPX()
gpx_track = gpxpy.gpx.GPXTrack()
gpx.tracks.append(gpx_track)
gpx_segment = gpxpy.gpx.GPXTrackSegment()
gpx_track.segments.append(gpx_segment)
for point in data:
gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(
point[1], point[2], elevation=point[3], time=point[0]))
with open(path, "w") as f:
f.write(gpx.to_xml())
Point -> add as a Way Point
LineString -> add all Points in a Route
Polygon -> add all Points of the external linering in a Route
Collection (of LineString or Point) -> add as a route, concatening all points
"""
if isinstance(geom, GeometryCollection):
for i, g in enumerate(geom):
self.geomToGPX(g, u"%s (%s)" % (name, i), description)
elif isinstance(geom, Point):
wp = self._point_to_GPX(geom)
wp.name = name
wp.description = description
self.gpx.waypoints.append(wp)
elif isinstance(geom, LineString):
gpx_track = gpxpy.gpx.GPXTrack(name=name, description=description)
gpx_segment = gpxpy.gpx.GPXTrackSegment()
gpx_segment.points = [self._point_to_GPX(point, klass=gpxpy.gpx.GPXTrackPoint) for point in geom]
gpx_track.segments.append(gpx_segment)
self.gpx.tracks.append(gpx_track)
elif isinstance(geom, Polygon):
self.geomToGPX(geom[0], name, description)
else:
raise ValueError("Unsupported geometry %s" % geom)
def write_gpx(path, data):
gpx = gpxpy.gpx.GPX()
gpx_track = gpxpy.gpx.GPXTrack()
gpx.tracks.append(gpx_track)
gpx_segment = gpxpy.gpx.GPXTrackSegment()
gpx_track.segments.append(gpx_segment)
for point in data:
if len(point) > 3:
gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(
latitude=point[1], longitude=point[2], elevation=point[3], time=point[0]))
else:
gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(
latitude=point[1], longitude=point[2], time=point[0]))
with open(path, "w") as f:
f.write(gpx.to_xml()) # loosing milliseconds here
def extract_segment_from_gpx_file(content, segment_id):
gpx_content = gpxpy.parse(content)
if len(gpx_content.tracks) == 0:
return None
track_segment = get_gpx_segments(
gpx_content.tracks[0].segments, segment_id
)
gpx = gpxpy.gpx.GPX()
gpx_track = gpxpy.gpx.GPXTrack()
gpx.tracks.append(gpx_track)
gpx_segment = gpxpy.gpx.GPXTrackSegment()
gpx_track.segments.append(gpx_segment)
for point_idx, point in enumerate(track_segment[0].points):
gpx_segment.points.append(
gpxpy.gpx.GPXTrackPoint(
point.latitude, point.longitude, elevation=point.elevation
)
)
return gpx.to_xml()