Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def save_segments(segments, origina_filepath, folder):
""" Save segments to gpx
"""
name = origina_filepath.split('/')[-1]
for i, segment in enumerate(segments):
label, points = segment
filename = label + '.' + str(i) + '.' + str(len(points)) + '.' + name.split('.')[0] + '.gpx'
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 points:
gpx_point = gpxpy.gpx.GPXTrackPoint(
latitude=point['lat'],
longitude=point['lon'],
elevation=point['alt'],
time=point['time']
)
gpx_segment.points.append(gpx_point)
save_file(folder, filename, 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()
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())
def garmin2gpxpy(content):
gpx = gpxpy.gpx.GPX()
gpx.tracks.append(gpxpy.gpx.GPXTrack())
gpx.tracks[0].segments.append(gpxpy.gpx.GPXTrackSegment())
NS = {"ns": "http://www.topografix.com/GPX/1/1"}
if isinstance(content, str):
content = content.encode("utf-8")
tree = etree.fromstring(content)
for track_point in tree.xpath("//ns:trk/ns:trkseg/ns:trkpt", namespaces=NS):
latitude = float(track_point.get("lat"))
longitude = float(track_point.get("lon"))
elevation = track_point.xpath("ns:ele/text()", namespaces=NS)
elevation = float(elevation[0])
point_time = track_point.xpath("ns:time/text()", namespaces=NS)[0]