1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-30 22:51:16 +01:00

TCX parser code cleanup

This commit is contained in:
Martin Tůma 2016-10-27 09:15:05 +02:00
parent a773921da0
commit 7f12c0ca95
3 changed files with 57 additions and 48 deletions

View File

@ -2,7 +2,6 @@
#define GPXPARSER_H #define GPXPARSER_H
#include <QXmlStreamReader> #include <QXmlStreamReader>
#include <QVector>
#include "parser.h" #include "parser.h"

View File

@ -1,6 +1,28 @@
#include "tcxparser.h" #include "tcxparser.h"
qreal TCXParser::number()
{
bool res;
qreal ret = _reader.readElementText().toDouble(&res);
if (!res)
_reader.raiseError(QString("Invalid %1.").arg(
_reader.name().toString()));
return ret;
}
QDateTime TCXParser::time()
{
QDateTime d = QDateTime::fromString(_reader.readElementText(),
Qt::ISODate);
if (!d.isValid())
_reader.raiseError(QString("Invalid %1.").arg(
_reader.name().toString()));
return d;
}
Coordinates TCXParser::position() Coordinates TCXParser::position()
{ {
Coordinates pos; Coordinates pos;
@ -11,13 +33,13 @@ Coordinates TCXParser::position()
if (_reader.name() == "LatitudeDegrees") { if (_reader.name() == "LatitudeDegrees") {
val = _reader.readElementText().toDouble(&res); val = _reader.readElementText().toDouble(&res);
if (!res || (val < -90.0 || val > 90.0)) if (!res || (val < -90.0 || val > 90.0))
_reader.raiseError("Invalid latitude."); _reader.raiseError("Invalid LatitudeDegrees.");
else else
pos.setLat(val); pos.setLat(val);
} else if (_reader.name() == "LongitudeDegrees") { } else if (_reader.name() == "LongitudeDegrees") {
val = _reader.readElementText().toDouble(&res); val = _reader.readElementText().toDouble(&res);
if (!res || (val < -180.0 || val > 180.0)) if (!res || (val < -180.0 || val > 180.0))
_reader.raiseError("Invalid longitude."); _reader.raiseError("Invalid LongitudeDegrees.");
else else
pos.setLon(val); pos.setLon(val);
} else } else
@ -27,78 +49,71 @@ Coordinates TCXParser::position()
return pos; return pos;
} }
void TCXParser::trackpointData(Trackpoint &t) void TCXParser::trackpointData(Trackpoint &trackpoint)
{ {
while (_reader.readNextStartElement()) { while (_reader.readNextStartElement()) {
if (_reader.name() == "Position") if (_reader.name() == "Position")
t.setCoordinates(position()); trackpoint.setCoordinates(position());
else if (_reader.name() == "AltitudeMeters") else if (_reader.name() == "AltitudeMeters")
t.setElevation(_reader.readElementText().toDouble()); trackpoint.setElevation(number());
else if (_reader.name() == "Time") else if (_reader.name() == "Time")
t.setTimestamp(QDateTime::fromString(_reader.readElementText(), trackpoint.setTimestamp(time());
Qt::ISODate));
else if (_reader.name() == "HeartRateBpm") else if (_reader.name() == "HeartRateBpm")
t.setHeartRate(_reader.readElementText().toDouble()); trackpoint.setHeartRate(number());
else else
_reader.skipCurrentElement(); _reader.skipCurrentElement();
} }
} }
void TCXParser::routepointData(Waypoint &w) void TCXParser::routepointData(Waypoint &waypoint)
{ {
while (_reader.readNextStartElement()) { while (_reader.readNextStartElement()) {
if (_reader.name() == "Position") if (_reader.name() == "Position")
w.setCoordinates(position()); waypoint.setCoordinates(position());
else if (_reader.name() == "AltitudeMeters") else if (_reader.name() == "AltitudeMeters")
w.setElevation(_reader.readElementText().toDouble()); waypoint.setElevation(number());
else if (_reader.name() == "Time") else if (_reader.name() == "Time")
w.setTimestamp(QDateTime::fromString(_reader.readElementText(), waypoint.setTimestamp(time());
Qt::ISODate));
else else
_reader.skipCurrentElement(); _reader.skipCurrentElement();
} }
} }
void TCXParser::waypointData(Waypoint &w) void TCXParser::waypointData(Waypoint &waypoint)
{ {
while (_reader.readNextStartElement()) { while (_reader.readNextStartElement()) {
if (_reader.name() == "Position") if (_reader.name() == "Position")
w.setCoordinates(position()); waypoint.setCoordinates(position());
else if (_reader.name() == "Name") else if (_reader.name() == "Name")
w.setName(_reader.readElementText()); waypoint.setName(_reader.readElementText());
else if (_reader.name() == "Notes") else if (_reader.name() == "Notes")
w.setDescription(_reader.readElementText()); waypoint.setDescription(_reader.readElementText());
else if (_reader.name() == "AltitudeMeters") else if (_reader.name() == "AltitudeMeters")
w.setElevation(_reader.readElementText().toDouble()); waypoint.setElevation(number());
else if (_reader.name() == "Time") else if (_reader.name() == "Time")
w.setTimestamp(QDateTime::fromString(_reader.readElementText(), waypoint.setTimestamp(time());
Qt::ISODate));
else else
_reader.skipCurrentElement(); _reader.skipCurrentElement();
} }
} }
void TCXParser::trackpoints() void TCXParser::trackpoints(QVector<Trackpoint> &track)
{ {
while (_reader.readNextStartElement()) { while (_reader.readNextStartElement()) {
if (_reader.name() == "Trackpoint") { if (_reader.name() == "Trackpoint") {
Trackpoint t; track.append(Trackpoint());
trackpointData(t); trackpointData(track.back());
if (t.coordinates().isValid())
_track->append(t);
} else } else
_reader.skipCurrentElement(); _reader.skipCurrentElement();
} }
} }
void TCXParser::routepoints() void TCXParser::routepoints(QVector<Waypoint> &route)
{ {
while (_reader.readNextStartElement()) { while (_reader.readNextStartElement()) {
if (_reader.name() == "Trackpoint") { if (_reader.name() == "Trackpoint") {
Waypoint w; route.append(Waypoint());
routepointData(w); routepointData(route.back());
if (w.coordinates().isValid())
_route->append(w);
} else } else
_reader.skipCurrentElement(); _reader.skipCurrentElement();
} }
@ -109,8 +124,7 @@ void TCXParser::lap()
while (_reader.readNextStartElement()) { while (_reader.readNextStartElement()) {
if (_reader.name() == "Track") { if (_reader.name() == "Track") {
_tracks.append(QVector<Trackpoint>()); _tracks.append(QVector<Trackpoint>());
_track = &_tracks.back(); trackpoints(_tracks.back());
trackpoints();
} else } else
_reader.skipCurrentElement(); _reader.skipCurrentElement();
} }
@ -121,13 +135,10 @@ void TCXParser::course()
while (_reader.readNextStartElement()) { while (_reader.readNextStartElement()) {
if (_reader.name() == "Track") { if (_reader.name() == "Track") {
_routes.append(QVector<Waypoint>()); _routes.append(QVector<Waypoint>());
_route = &_routes.back(); routepoints(_routes.back());
routepoints();
} else if (_reader.name() == "CoursePoint") { } else if (_reader.name() == "CoursePoint") {
Waypoint w; _waypoints.append(Waypoint());
waypointData(w); waypointData(_waypoints.back());
if (w.coordinates().isValid())
_waypoints.append(w);
} else } else
_reader.skipCurrentElement(); _reader.skipCurrentElement();
} }

View File

@ -2,7 +2,6 @@
#define TCXPARSER_H #define TCXPARSER_H
#include <QXmlStreamReader> #include <QXmlStreamReader>
#include <QVector>
#include "parser.h" #include "parser.h"
@ -11,7 +10,7 @@ class TCXParser : public Parser
public: public:
TCXParser(QList<QVector<Trackpoint> > &tracks, TCXParser(QList<QVector<Trackpoint> > &tracks,
QList<QVector<Waypoint> > &routes, QList<Waypoint> &waypoints) QList<QVector<Waypoint> > &routes, QList<Waypoint> &waypoints)
: Parser(tracks, routes, waypoints) {_track = 0; _route = 0;} : Parser(tracks, routes, waypoints) {}
~TCXParser() {} ~TCXParser() {}
bool loadFile(QIODevice *device); bool loadFile(QIODevice *device);
@ -26,16 +25,16 @@ private:
void course(); void course();
void activity(); void activity();
void lap(); void lap();
void trackpoints(); void trackpoints(QVector<Trackpoint> &track);
void routepoints(); void routepoints(QVector<Waypoint> &route);
void trackpointData(Trackpoint &t); void trackpointData(Trackpoint &trackpoint);
void routepointData(Waypoint &w); void routepointData(Waypoint &waypoint);
void waypointData(Waypoint &w); void waypointData(Waypoint &waypoint);
Coordinates position(); Coordinates position();
qreal number();
QDateTime time();
QXmlStreamReader _reader; QXmlStreamReader _reader;
QVector<Trackpoint> *_track;
QVector<Waypoint> *_route;
}; };
#endif // TCXPARSER_H #endif // TCXPARSER_H