本文整理汇总了Python中gpxpy.parse方法的典型用法代码示例。如果您正苦于以下问题:Python gpxpy.parse方法的具体用法?Python gpxpy.parse怎么用?Python gpxpy.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gpxpy
的用法示例。
在下文中一共展示了gpxpy.parse方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_gpx
# 需要导入模块: import gpxpy [as 别名]
# 或者: from gpxpy import parse [as 别名]
def load_gpx(self):
gpx_file = open(self.path_file, 'r')
gpx = gpxpy.parse(gpx_file)
if len(gpx.tracks) == 0:
raise RuntimeError('GPX file does not contain a track')
points = []
track = gpx.tracks[0]
for segment in track.segments:
for point in segment.points:
points.append({"lat": point.latitude, "lng": point.longitude,
"alt": point.elevation, "location": point.name})
return points
示例2: read_gpx_file
# 需要导入模块: import gpxpy [as 别名]
# 或者: from gpxpy import parse [as 别名]
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,
point.time, speed, time_difference, distance, gpx.name])
prev_point = point
except Exception as e:
raise TrackException('GPX file "' + filename + '" malformed', e)
except FileNotFoundError as e:
raise TrackException('GPX file "' + filename + '" not found', e)
示例3: load_gpx
# 需要导入模块: import gpxpy [as 别名]
# 或者: from gpxpy import parse [as 别名]
def load_gpx(self, file_name: str):
"""Load the GPX file into self.
Args:
file_name: GPX file to be loaded .
Raises:
TrackLoadError: An error occurred while parsing the GPX file (empty or bad format).
PermissionError: An error occurred while opening the GPX file.
"""
try:
self.file_names = [os.path.basename(file_name)]
# Handle empty gpx files
# (for example, treadmill runs pulled via garmin-connect-export)
if os.path.getsize(file_name) == 0:
raise TrackLoadError("Empty GPX file")
with open(file_name, "r") as file:
self._load_gpx_data(mod_gpxpy.parse(file))
except TrackLoadError as e:
raise e
except mod_gpxpy.gpx.GPXXMLSyntaxException as e:
raise TrackLoadError("Failed to parse GPX.") from e
except PermissionError as e:
raise TrackLoadError("Cannot load GPX (bad permissions)") from e
except Exception as e:
raise TrackLoadError("Something went wrong when loading GPX.") from e
示例4: gps_distance_elevation
# 需要导入模块: import gpxpy [as 别名]
# 或者: from gpxpy import parse [as 别名]
def gps_distance_elevation(fname):
segment = gpxpy.parse(open(fname + '.gpx', 'r')).tracks[0].segments[0]
elevation = []
loc = []
for p in segment.points:
elevation.append(p.elevation)
lat, lon = p.latitude, p.longitude
loc.append((lat, lon))
distance = np.array([0] + [vincenty(loc[i], loc[i-1]).meters for i in range(len(loc)-1)]).cumsum()
plt.plot(distance, elevation, label=fname)
plt.savefig(fname + '.png')
plt.clf()
return distance, elevation
示例5: get_lat_lon_time_from_gpx
# 需要导入模块: import gpxpy [as 别名]
# 或者: from gpxpy import parse [as 别名]
def get_lat_lon_time_from_gpx(gpx_file, local_time=True):
'''
Read location and time stamps from a track in a GPX file.
Returns a list of tuples (time, lat, lon).
GPX stores time in UTC, by default we assume your camera used the local time
and convert accordingly.
'''
with open(gpx_file, 'r') as f:
gpx = gpxpy.parse(f)
points = []
if len(gpx.tracks) > 0:
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
t = utc_to_localtime(point.time) if local_time else point.time
points.append((t, point.latitude, point.longitude, point.elevation))
if len(gpx.waypoints) > 0:
for point in gpx.waypoints:
t = utc_to_localtime(point.time) if local_time else point.time
points.append((t, point.latitude, point.longitude, point.elevation))
# sort by time just in case
points.sort()
return points
示例6: get_lat_lon_time_from_nmea
# 需要导入模块: import gpxpy [as 别名]
# 或者: from gpxpy import parse [as 别名]
def get_lat_lon_time_from_nmea(nmea_file, local_time=True):
'''
Read location and time stamps from a track in a NMEA file.
Returns a list of tuples (time, lat, lon).
GPX stores time in UTC, by default we assume your camera used the local time
and convert accordingly.
'''
with open(nmea_file, "r") as f:
lines = f.readlines()
lines = [l.rstrip("\n\r") for l in lines]
# Get initial date
for l in lines:
if "GPRMC" in l:
data = pynmea2.parse(l)
date = data.datetime.date()
break
# Parse GPS trace
points = []
for l in lines:
if "GPRMC" in l:
data = pynmea2.parse(l)
date = data.datetime.date()
if "$GPGGA" in l:
data = pynmea2.parse(l)
timestamp = datetime.datetime.combine(date, data.timestamp)
lat, lon, alt = data.latitude, data.longitude, data.altitude
points.append((timestamp, lat, lon, alt))
points.sort()
return points
示例7: save_gpx
# 需要导入模块: import gpxpy [as 别名]
# 或者: from gpxpy import parse [as 别名]
def save_gpx(self, path_object, filename="saved_trips/temp.gpx"):
"""
Paths is the values from .get_results() function
for the solved LP problem
"""
# Need some way to order the results together
if not self.results:
self.get_results()
results = self.results
self.make_new_gpx(filename)
gpx_file = open(filename, 'r')
gpx = gpxpy.parse(gpx_file)
gpx = gpxpy.gpx.GPX()
gpx_segment = {}
for path_name in results:
gpx_track = gpxpy.gpx.GPXTrack()
gpx.tracks.append(gpx_track)
gpx_segment[path_name] = gpxpy.gpx.GPXTrackSegment()
gpx_track.segments.append(gpx_segment[path_name])
path = path_object.get(path_name).points
if path.type == 'LineString':
points = path.coords
else:
points = path[0].coords
for coord in points:
gpx_segment[path_name].points.append(gpxpy.gpx.GPXTrackPoint(coord[1], coord[0]))
f = open(filename, 'w+')
f.write(gpx.to_xml())
f.close()
示例8: matchGPXTrace
# 需要导入模块: import gpxpy [as 别名]
# 或者: from gpxpy import parse [as 别名]
def matchGPXTrace(self, fn):
"""Equivalent to matchPostgresTrace for a GPX file
If there are multiple segments, only matches the first one
(for now...this is easy to modify)"""
self.clearCurrentRoutes()
import gpxpy
with open(fn) as f:
tracks = gpxpy.parse(f).tracks
if len(tracks) == 0:
raise Exception('GPX file %s contains no tracks' % fn)
elif len(tracks) > 1:
raise Warning('GPX file %s contains multiple tracks. Only parsing the first one.' % fn)
elif len(tracks[0].segments) > 1:
raise Warning('GPX file %s contains multiple segments on track 1. Only parsing the first one.' % fn)
traceSegment = tracks[0].segments[0]
if not(traceSegment.has_times()) and any([pt.time is None for pt in traceSegment.points]): # second is because of bug in gpxpy that doesn't correctly return has_times for short traces
raise Exception('GPX file %s track 1 has no readable timestamps' % fn)
self.traceLineStr = 'ST_Transform(ST_SetSRID(ST_MakeLine(ARRAY['+', '.join([
'ST_MakePointM('+str(pt.longitude)+','+str(pt.latitude)+',' +
str(int((pt.time.replace(tzinfo=None) - datetime.datetime(1970, 1, 1)).total_seconds()))+')'
for pt in traceSegment.points])+']),4326),%s)' % self.streets_srid
self.startEndPts = ['ST_Transform(ST_SetSRID(ST_MakePoint('+str(np.round(pt.longitude,5))+','
+ str(np.round(pt.latitude,5))+'),4326),'+self.streets_srid+')' for pt in [traceSegment.points[0], traceSegment.points[-1]]]
self.matchTrace()