-
Notifications
You must be signed in to change notification settings - Fork 226
Description
Problem
When a GPX contains only routes (<rte>/<rtept>) and no tracks, gpx.length_3d() and gpx.get_uphill_downhill() return 0, despite elevation and points being present. This is because the built-in computations operate on tracks/segments, not routes.
Expected
Non-zero distance and elevation gain are computed for route-only files when elevation is present.
Proposed solution (non-breaking)
Provide a helper/utility (or mention in docs) to convert routes to tracks so built-in computations work as expected.
def convert_routes_to_tracks(gpx: gpxpy.gpx.GPX):
"""Convert all routes to tracks (1 segment per route, 1:1 points)."""
for route in list(gpx.routes):
track = gpxpy.gpx.GPXTrack(name=route.name or "Unnamed Route")
segment = gpxpy.gpx.GPXTrackSegment()
for rp in route.points:
segment.points.append(
gpxpy.gpx.GPXTrackPoint(
rp.latitude,
rp.longitude,
elevation=rp.elevation,
)
)
track.segments.append(segment)
gpx.tracks.append(track)
gpx.routes = []I currently employ this solution in my code and I suspect @tkrajina does too in his own tool. Upon parsing, I convert all routes to tracks.
Why this helps
- After conversion, gpx.length_3d() and gpx.get_uphill_downhill() produce correct values without changing downstream code.
- Keeps behavior backward-compatible and leverages existing track-based computations.
Request
Consider adding a built-in converter (e.g., gpx.convert_routes_to_tracks()), or document this pattern clearly for users working with route-only GPX files.
I’m happy to submit a PR with this helper and a note in the docs.