1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 19:55:53 +01:00
GPXSee/src/data/tcxparser.cpp

269 lines
6.8 KiB
C++
Raw Normal View History

2016-10-23 11:09:20 +02:00
#include "tcxparser.h"
2016-11-02 17:33:06 +01:00
void TCXParser::warning(const char *text) const
2016-10-29 10:40:30 +02:00
{
2016-11-02 17:33:06 +01:00
const QFile *file = static_cast<QFile *>(_reader.device());
2018-09-24 23:07:11 +02:00
qWarning("%s:%lld: %s", qPrintable(file->fileName()),
2016-10-29 10:40:30 +02:00
_reader.lineNumber(), text);
}
2016-10-27 09:15:05 +02:00
qreal TCXParser::number()
{
bool res;
qreal ret = _reader.readElementText().toDouble(&res);
if (!res)
2016-10-29 12:22:28 +02:00
_reader.raiseError(QString("Invalid %1").arg(
2016-10-27 09:15:05 +02:00
_reader.name().toString()));
return ret;
}
QDateTime TCXParser::time()
{
QDateTime d = QDateTime::fromString(_reader.readElementText(),
Qt::ISODate);
if (!d.isValid())
2016-10-29 12:22:28 +02:00
_reader.raiseError(QString("Invalid %1").arg(
2016-10-27 09:15:05 +02:00
_reader.name().toString()));
return d;
}
Coordinates TCXParser::position()
2016-10-23 11:09:20 +02:00
{
Coordinates pos;
qreal val;
bool res;
2016-10-23 11:09:20 +02:00
while (_reader.readNextStartElement()) {
2018-08-27 22:25:55 +02:00
if (_reader.name() == QLatin1String("LatitudeDegrees")) {
val = _reader.readElementText().toDouble(&res);
if (!res || (val < -90.0 || val > 90.0))
2016-10-29 12:22:28 +02:00
_reader.raiseError("Invalid LatitudeDegrees");
else
2016-10-24 00:51:19 +02:00
pos.setLat(val);
2018-08-27 22:25:55 +02:00
} else if (_reader.name() == QLatin1String("LongitudeDegrees")) {
val = _reader.readElementText().toDouble(&res);
if (!res || (val < -180.0 || val > 180.0))
2016-10-29 12:22:28 +02:00
_reader.raiseError("Invalid LongitudeDegrees");
else
2016-10-24 00:51:19 +02:00
pos.setLon(val);
} else
2016-10-23 11:09:20 +02:00
_reader.skipCurrentElement();
}
return pos;
}
void TCXParser::heartRateBpm(Trackpoint &trackpoint)
{
while (_reader.readNextStartElement()) {
2018-08-27 22:25:55 +02:00
if (_reader.name() == QLatin1String("Value"))
trackpoint.setHeartRate(number());
else
_reader.skipCurrentElement();
}
}
void TCXParser::TPX(Trackpoint &trackpoint)
2016-11-06 03:28:08 +01:00
{
while (_reader.readNextStartElement()) {
2018-08-27 22:25:55 +02:00
if (_reader.name() == QLatin1String("RunCadence"))
2016-11-06 03:28:08 +01:00
trackpoint.setCadence(number());
2018-08-27 22:25:55 +02:00
else if (_reader.name() == QLatin1String("Watts"))
2016-11-06 03:28:08 +01:00
trackpoint.setPower(number());
else if (_reader.name() == QLatin1String("Speed"))
trackpoint.setSpeed(number());
else
_reader.skipCurrentElement();
}
}
void TCXParser::extensions(Trackpoint &trackpoint)
{
while (_reader.readNextStartElement()) {
if (_reader.name() == QLatin1String("TPX"))
TPX(trackpoint);
2016-11-06 03:28:08 +01:00
else
_reader.skipCurrentElement();
}
}
2016-10-27 09:15:05 +02:00
void TCXParser::trackpointData(Trackpoint &trackpoint)
2016-10-23 11:09:20 +02:00
{
while (_reader.readNextStartElement()) {
2018-08-27 22:25:55 +02:00
if (_reader.name() == QLatin1String("Position"))
2016-10-27 09:15:05 +02:00
trackpoint.setCoordinates(position());
2018-08-27 22:25:55 +02:00
else if (_reader.name() == QLatin1String("AltitudeMeters"))
2016-10-27 09:15:05 +02:00
trackpoint.setElevation(number());
2018-08-27 22:25:55 +02:00
else if (_reader.name() == QLatin1String("Time"))
2016-10-27 09:15:05 +02:00
trackpoint.setTimestamp(time());
2018-08-27 22:25:55 +02:00
else if (_reader.name() == QLatin1String("HeartRateBpm"))
heartRateBpm(trackpoint);
2018-08-27 22:25:55 +02:00
else if (_reader.name() == QLatin1String("Cadence"))
2016-11-06 03:28:08 +01:00
trackpoint.setCadence(number());
2018-08-27 22:25:55 +02:00
else if (_reader.name() == QLatin1String("Extensions"))
2016-11-06 03:28:08 +01:00
extensions(trackpoint);
2016-10-23 11:09:20 +02:00
else
_reader.skipCurrentElement();
}
}
2016-10-27 09:15:05 +02:00
void TCXParser::waypointData(Waypoint &waypoint)
2016-10-23 11:09:20 +02:00
{
while (_reader.readNextStartElement()) {
2018-08-27 22:25:55 +02:00
if (_reader.name() == QLatin1String("Position"))
2016-10-27 09:15:05 +02:00
waypoint.setCoordinates(position());
2018-08-27 22:25:55 +02:00
else if (_reader.name() == QLatin1String("Name"))
2016-10-27 09:15:05 +02:00
waypoint.setName(_reader.readElementText());
2018-08-27 22:25:55 +02:00
else if (_reader.name() == QLatin1String("Notes"))
2016-10-27 09:15:05 +02:00
waypoint.setDescription(_reader.readElementText());
2018-08-27 22:25:55 +02:00
else if (_reader.name() == QLatin1String("AltitudeMeters"))
2016-10-27 09:15:05 +02:00
waypoint.setElevation(number());
2018-08-27 22:25:55 +02:00
else if (_reader.name() == QLatin1String("Time"))
2016-10-27 09:15:05 +02:00
waypoint.setTimestamp(time());
2016-10-23 11:09:20 +02:00
else
_reader.skipCurrentElement();
}
}
2019-02-11 23:28:08 +01:00
void TCXParser::trackpoints(SegmentData &segment)
2016-10-23 11:09:20 +02:00
{
while (_reader.readNextStartElement()) {
2018-08-27 22:25:55 +02:00
if (_reader.name() == QLatin1String("Trackpoint")) {
2016-10-29 10:40:30 +02:00
Trackpoint t;
trackpointData(t);
if (t.coordinates().isValid())
2019-02-11 23:28:08 +01:00
segment.append(t);
2016-10-29 10:40:30 +02:00
else
warning("Missing Trackpoint coordinates");
2016-10-23 11:09:20 +02:00
} else
_reader.skipCurrentElement();
}
}
2019-02-11 23:28:08 +01:00
void TCXParser::lap(SegmentData &segment)
2016-10-23 11:09:20 +02:00
{
while (_reader.readNextStartElement()) {
2018-08-27 22:25:55 +02:00
if (_reader.name() == QLatin1String("Track"))
2019-02-11 23:28:08 +01:00
trackpoints(segment);
else
2016-10-23 11:09:20 +02:00
_reader.skipCurrentElement();
}
}
2019-01-18 00:17:28 +01:00
void TCXParser::course(QVector<Waypoint> &waypoints, TrackData &track)
2016-10-23 11:09:20 +02:00
{
while (_reader.readNextStartElement()) {
2019-02-11 23:28:08 +01:00
if (_reader.name() == QLatin1String("Track")) {
track.append(SegmentData());
trackpoints(track.last());
} else if (_reader.name() == QLatin1String("Name"))
track.setName(_reader.readElementText());
2018-08-27 22:25:55 +02:00
else if (_reader.name() == QLatin1String("Notes"))
track.setDescription(_reader.readElementText());
2018-08-27 22:25:55 +02:00
else if (_reader.name() == QLatin1String("CoursePoint")) {
2016-10-29 10:40:30 +02:00
Waypoint w;
waypointData(w);
if (w.coordinates().isValid())
2017-07-27 19:47:46 +02:00
waypoints.append(w);
2016-10-29 10:40:30 +02:00
else
warning("Missing Trackpoint coordinates");
2016-10-23 11:09:20 +02:00
} else
_reader.skipCurrentElement();
}
}
void TCXParser::activity(TrackData &track)
2016-10-23 11:09:20 +02:00
{
2019-02-11 23:28:08 +01:00
track.append(SegmentData());
2016-10-23 11:09:20 +02:00
while (_reader.readNextStartElement()) {
2018-08-27 22:25:55 +02:00
if (_reader.name() == QLatin1String("Lap"))
2019-02-11 23:28:08 +01:00
lap(track.last());
else if (_reader.name() == QLatin1String("Notes"))
track.setDescription(_reader.readElementText());
2016-10-23 11:09:20 +02:00
else
_reader.skipCurrentElement();
}
}
2019-01-18 00:17:28 +01:00
void TCXParser::courses(QList<TrackData> &tracks, QVector<Waypoint> &waypoints)
2016-10-23 11:09:20 +02:00
{
while (_reader.readNextStartElement()) {
2018-08-27 22:25:55 +02:00
if (_reader.name() == QLatin1String("Course")) {
2017-07-27 19:47:46 +02:00
tracks.append(TrackData());
course(waypoints, tracks.back());
} else
2016-10-23 11:09:20 +02:00
_reader.skipCurrentElement();
}
}
void TCXParser::sport(QList<TrackData> &tracks)
2016-10-23 11:09:20 +02:00
{
while (_reader.readNextStartElement()) {
2018-08-27 22:25:55 +02:00
if (_reader.name() == QLatin1String("Activity")) {
2017-07-27 19:47:46 +02:00
tracks.append(TrackData());
2019-02-11 23:28:08 +01:00
activity(tracks.last());
} else
2016-10-23 11:09:20 +02:00
_reader.skipCurrentElement();
}
}
void TCXParser::multiSportSession(QList<TrackData> &tracks)
{
while (_reader.readNextStartElement()) {
2018-08-27 22:25:55 +02:00
if (_reader.name() == QLatin1String("FirstSport")
|| _reader.name() == QLatin1String("NextSport"))
sport(tracks);
else
_reader.skipCurrentElement();
}
}
void TCXParser::activities(QList<TrackData> &tracks)
{
while (_reader.readNextStartElement()) {
2018-08-27 22:25:55 +02:00
if (_reader.name() == QLatin1String("Activity")) {
tracks.append(TrackData());
2019-02-11 23:28:08 +01:00
activity(tracks.last());
2018-08-27 22:25:55 +02:00
} else if (_reader.name() == QLatin1String("MultiSportSession"))
multiSportSession(tracks);
else
_reader.skipCurrentElement();
}
}
2019-01-18 00:17:28 +01:00
void TCXParser::tcx(QList<TrackData> &tracks, QVector<Waypoint> &waypoints)
2016-10-23 11:09:20 +02:00
{
while (_reader.readNextStartElement()) {
2018-08-27 22:25:55 +02:00
if (_reader.name() == QLatin1String("Courses"))
2017-07-27 19:47:46 +02:00
courses(tracks, waypoints);
2018-08-27 22:25:55 +02:00
else if (_reader.name() == QLatin1String("Activities"))
2017-07-27 19:47:46 +02:00
activities(tracks);
2016-10-23 11:09:20 +02:00
else
_reader.skipCurrentElement();
}
}
2017-07-27 19:47:46 +02:00
bool TCXParser::parse(QFile *file, QList<TrackData> &tracks,
2019-01-31 01:46:53 +01:00
QList<RouteData> &routes, QList<Area> &polygons,
QVector<Waypoint> &waypoints)
2016-10-23 11:09:20 +02:00
{
2017-07-27 19:47:46 +02:00
Q_UNUSED(routes);
2019-01-31 01:46:53 +01:00
Q_UNUSED(polygons);
2017-07-27 19:47:46 +02:00
_reader.clear();
_reader.setDevice(file);
2016-10-23 11:09:20 +02:00
if (_reader.readNextStartElement()) {
2018-08-27 22:25:55 +02:00
if (_reader.name() == QLatin1String("TrainingCenterDatabase"))
2017-07-27 19:47:46 +02:00
tcx(tracks, waypoints);
2016-10-23 11:09:20 +02:00
else
2016-10-29 12:22:28 +02:00
_reader.raiseError("Not a TCX file");
2016-10-23 11:09:20 +02:00
}
return !_reader.error();
}