Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# -*- coding: utf-8 -*-
"""
Example on how to add elevations to existing GPX file
"""
import gpxpy as mod_gpxpy
import srtm as mod_srtm
gpx_contents = """test2714.02652.02414.02679.02710.02145.02004.01782.01592.02714.02522.02421.02099.01689.02080.02279.02343.01973.0"""
gpx = mod_gpxpy.parse(gpx_contents)
geo_elevation_data = mod_srtm.get_data()
for segment_no, segment in enumerate(gpx.tracks[0].segments):
for point in segment.points:
calculated = geo_elevation_data.get_elevation(point.latitude, point.longitude)
print 'segment #%s (%13s, %13s) -> gpx:%10s calculated:%10s' % (segment_no, point.latitude, point.longitude, point.elevation, calculated)
def parse(self, file, encoding=None, version = None):
f = custom_open('test_files/%s' % file, encoding=encoding)
parser = mod_parser.GPXParser(f)
gpx = parser.parse(version)
f.close()
if not gpx:
print('Parser error: %s' % parser.get_error())
return gpx
def reparse(self, gpx):
xml = gpx.to_xml()
parser = mod_parser.GPXParser(xml)
gpx = parser.parse()
if not gpx:
print('Parser error while reparsing: %s' % parser.get_error())
return gpx
def test_checklxml(self):
self.assertEqual('LXML', mod_parser.GPXParser._GPXParser__library())
def read_gpx_file(self, filename):
try:
with open(filename, "r") as f:
prev_point = None
head, tail = os.path.split(filename)
code_route = tail.replace(".gpx", "")
try:
gpx = gpxpy.parse(f)
for point in gpx.walk(only_points=True):
speed = point.speed_between(prev_point)
if speed is None:
speed = 0
time_difference = point.time_difference(prev_point)
if time_difference is None:
time_difference = 0
distance = point.distance_3d(prev_point)
if not distance:
distance = point.distance_2d(prev_point)
if distance is None:
distance = 0
self.points_list.append([code_route, point.latitude, point.longitude, point.elevation,
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)
def __init__(self, xml_or_file=None):
"""
Initialize new GPXParser instance.
Arguments:
xml_or_file: string or file object containing the gpx
formatted xml
"""
self.init(xml_or_file)
self.gpx = mod_gpx.GPX()
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())
"""
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())
def init(self, xml_or_file):
"""
Store the XML and remove utf-8 Byte Order Mark if present.
Args:
xml_or_file: string or file object containing the gpx
formatted xml
"""
text = xml_or_file.read() if hasattr(xml_or_file, 'read') else xml_or_file
self.xml = mod_utils.make_str(text)