From 0cd6a82a0feb42d1ea8034e410430a7a9d6f6dfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Wed, 16 May 2018 21:53:06 +0200 Subject: [PATCH] Added support for Garmin route extension Resolves #112 --- src/data/gpxparser.cpp | 43 ++++++++++++++++++++++++++++++++++++------ src/data/gpxparser.h | 8 +++++--- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/data/gpxparser.cpp b/src/data/gpxparser.cpp index 8648e10c..70e2fb99 100644 --- a/src/data/gpxparser.cpp +++ b/src/data/gpxparser.cpp @@ -51,6 +51,17 @@ Coordinates GPXParser::coordinates() return Coordinates(lon, lat); } +void GPXParser::rpExtension(TrackData *autoRoute) +{ + while (_reader.readNextStartElement()) { + if (_reader.name() == "rpt") { + autoRoute->append(Trackpoint(coordinates())); + trackpointData(autoRoute->last()); + } else + _reader.skipCurrentElement(); + } +} + void GPXParser::tpExtension(Trackpoint &trackpoint) { while (_reader.readNextStartElement()) { @@ -63,7 +74,17 @@ void GPXParser::tpExtension(Trackpoint &trackpoint) } } -void GPXParser::extensions(Trackpoint &trackpoint) +void GPXParser::rteptExtensions(TrackData *autoRoute) +{ + while (_reader.readNextStartElement()) { + if (_reader.name() == "RoutePointExtension") + rpExtension(autoRoute); + else + _reader.skipCurrentElement(); + } +} + +void GPXParser::trkptExtensions(Trackpoint &trackpoint) { while (_reader.readNextStartElement()) { if (_reader.name() == "speed") @@ -95,7 +116,7 @@ void GPXParser::trackpointData(Trackpoint &trackpoint) else if (_reader.name() == "geoidheight") gh = number(); else if (_reader.name() == "extensions") - extensions(trackpoint); + trkptExtensions(trackpoint); else _reader.skipCurrentElement(); } @@ -104,7 +125,7 @@ void GPXParser::trackpointData(Trackpoint &trackpoint) trackpoint.setElevation(trackpoint.elevation() - gh); } -void GPXParser::waypointData(Waypoint &waypoint) +void GPXParser::waypointData(Waypoint &waypoint, TrackData *autoRoute) { qreal gh = NAN; @@ -119,6 +140,8 @@ void GPXParser::waypointData(Waypoint &waypoint) gh = number(); else if (_reader.name() == "time") waypoint.setTimestamp(time()); + else if (autoRoute && _reader.name() == "extensions") + rteptExtensions(autoRoute); else _reader.skipCurrentElement(); } @@ -138,12 +161,14 @@ void GPXParser::trackpoints(TrackData &track) } } -void GPXParser::routepoints(RouteData &route) +void GPXParser::routepoints(RouteData &route, QList &tracks) { + TrackData autoRoute; + while (_reader.readNextStartElement()) { if (_reader.name() == "rtept") { route.append(Waypoint(coordinates())); - waypointData(route.last()); + waypointData(route.last(), &autoRoute); } else if (_reader.name() == "name") route.setName(_reader.readElementText()); else if (_reader.name() == "desc") @@ -151,6 +176,12 @@ void GPXParser::routepoints(RouteData &route) else _reader.skipCurrentElement(); } + + if (!autoRoute.isEmpty()) { + autoRoute.setName(route.name()); + autoRoute.setDescription(route.description()); + tracks.append(autoRoute); + } } void GPXParser::track(TrackData &track) @@ -176,7 +207,7 @@ void GPXParser::gpx(QList &tracks, QList &routes, track(tracks.back()); } else if (_reader.name() == "rte") { routes.append(RouteData()); - routepoints(routes.back()); + routepoints(routes.back(), tracks); } else if (_reader.name() == "wpt") { waypoints.append(Waypoint(coordinates())); waypointData(waypoints.last()); diff --git a/src/data/gpxparser.h b/src/data/gpxparser.h index 1abd55c0..4a4f57ed 100644 --- a/src/data/gpxparser.h +++ b/src/data/gpxparser.h @@ -18,11 +18,13 @@ private: QList &waypoints); void track(TrackData &track); void trackpoints(TrackData &track); - void routepoints(RouteData &route); + void routepoints(RouteData &route, QList &tracks); + void rpExtension(TrackData *autoRoute); void tpExtension(Trackpoint &trackpoint); - void extensions(Trackpoint &trackpoint); + void trkptExtensions(Trackpoint &trackpoint); + void rteptExtensions(TrackData *autoRoute); void trackpointData(Trackpoint &trackpoint); - void waypointData(Waypoint &waypoint); + void waypointData(Waypoint &waypoint, TrackData *autoRoute = 0); qreal number(); QDateTime time(); Coordinates coordinates();