Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
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())
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:
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())
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:
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 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
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]
# e.g.: 2018-04-28T14:30:50.000Z
point_time = gpxpy.gpxfield.parse_time(point_time)
# print('latitude', latitude, 'longitude', longitude, "elevation", elevation)
point = gpxpy.gpx.GPXTrackPoint(latitude=latitude, longitude=longitude, elevation=elevation, time=point_time)
gpx.tracks[0].segments[0].points.append(point)
return gpx