Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
auth_user_id=params['auth_user_id'],
activity_date=gpx_data['start'],
old_filename=filename,
sport=params['sport_label']
)
os.rename(params['file_path'], new_filepath)
gpx_data['filename'] = new_filepath
map_filepath = get_new_file_path(
auth_user_id=params['auth_user_id'],
activity_date=gpx_data['start'],
extension='.png',
sport=params['sport_label']
)
generate_map(map_filepath, map_data)
except (gpxpy.gpx.GPXXMLSyntaxException, TypeError) as e:
raise ActivityException('error', 'Error during gpx file parsing.', e)
except Exception as e:
raise ActivityException('error', 'Error during activity file save.', e)
try:
new_activity = create_activity(
params['auth_user_id'], params['activity_data'], gpx_data)
new_activity.map = map_filepath
new_activity.map_id = get_map_hash(map_filepath)
db.session.add(new_activity)
db.session.flush()
for segment_data in gpx_data['segments']:
new_segment = create_segment(new_activity.id, segment_data)
db.session.add(new_segment)
db.session.commit()
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
mod_etree.XMLParser(remove_comments=True))
else:
root = mod_etree.XML(self.xml)
except Exception as e:
# The exception here can be a lxml or ElementTree exception.
log.debug('Error in:\n%s\n-----------\n' % self.xml, exc_info=True)
# The library should work in the same way regardless of the
# underlying XML parser that's why the exception thrown
# here is GPXXMLSyntaxException (instead of simply throwing the
# original ElementTree or lxml exception e).
#
# But, if the user needs the original exception (lxml or ElementTree)
# it is available with GPXXMLSyntaxException.original_exception:
raise mod_gpx.GPXXMLSyntaxException('Error parsing XML: %s' % str(e), e)
if root is None:
raise mod_gpx.GPXException('Document must have a `gpx` root node.')
if version is None:
version = root.get('version')
mod_gpxfield.gpx_fields_from_xml(self.gpx, root, version)
return self.gpx
old_filename=filename,
sport=params['sport_label'],
)
absolute_gpx_filepath = get_absolute_file_path(new_filepath)
os.rename(params['file_path'], absolute_gpx_filepath)
gpx_data['filename'] = new_filepath
map_filepath = get_new_file_path(
auth_user_id=auth_user_id,
activity_date=gpx_data['start'],
extension='.png',
sport=params['sport_label'],
)
absolute_map_filepath = get_absolute_file_path(map_filepath)
generate_map(absolute_map_filepath, map_data)
except (gpxpy.gpx.GPXXMLSyntaxException, TypeError) as e:
raise ActivityException('error', 'Error during gpx file parsing.', e)
except Exception as e:
raise ActivityException('error', 'Error during gpx processing.', e)
try:
new_activity = create_activity(
params['user'], params['activity_data'], gpx_data
)
new_activity.map = map_filepath
new_activity.map_id = get_map_hash(map_filepath)
new_activity.weather_start = weather_data[0]
new_activity.weather_end = weather_data[1]
db.session.add(new_activity)
db.session.flush()
for segment_data in gpx_data['segments']:
def load(self,path):
if os.path.splitext(path)[1] != '.gpx':
print('Only GPX file is supported.')
return False
with open(path,'r') as f:
try:
gpx = gpxpy.parse(f)
for track in gpx.tracks:
for segment in track.segments:
start_time = segment.points[0].time
for point in segment.points:
self.points.append((point.longitude,point.latitude))
end = point.time
self.duration = end.timestamp()-start_time.timestamp()
except gpxpy.gpx.GPXXMLSyntaxException:
print("GPX file is not valid")
return False
return True
class Track:
def render(self, resp: requests.models.Response) -> ipyleaflet.Map:
"Return an ipyleaflet map with the GPX object rendered on it, or None."
import gpxpy
from gpxpy.gpx import GPXXMLSyntaxException
obj = resp.content.decode('utf-8')
try:
trace = gpxpy.parse(obj)
except GPXXMLSyntaxException:
return None
pts = [p.point for p in trace.get_points_data()]
bbox = trace.get_bounds()
mins = (bbox.min_latitude, bbox.min_longitude)
maxs = (bbox.max_latitude, bbox.max_longitude)
bbox = mins, maxs
center = list(bbox_center(*bbox))
z = zoom_for_bbox(*(mins + maxs))
m = ipyleaflet.Map(center=center, zoom=z + 1)
# FIXME: make path styling configurable
poly = ipyleaflet.Polyline(locations=[(p.latitude, p.longitude)
for p in pts], fill=False)
m.add_layer(layer=poly)
for p in pts:
cm = ipyleaflet.CircleMarker(location=(p.latitude, p.longitude), radius=5)
m.add_layer(layer=cm)