2015-10-05 01:43:48 +02:00
|
|
|
#include "parser.h"
|
|
|
|
|
2015-10-15 01:30:21 +02:00
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
void Parser::handleTrackpointData(DataType type, const QString &value)
|
2015-10-15 01:30:21 +02:00
|
|
|
{
|
2016-07-28 00:23:22 +02:00
|
|
|
switch (type) {
|
2016-06-25 11:02:09 +02:00
|
|
|
case Elevation:
|
2016-07-28 00:23:22 +02:00
|
|
|
_track->last().setElevation(value.toLatin1().toDouble());
|
2016-06-25 11:02:09 +02:00
|
|
|
break;
|
|
|
|
case Time:
|
2016-07-28 00:23:22 +02:00
|
|
|
_track->last().setTimestamp(QDateTime::fromString(value.toLatin1(),
|
|
|
|
Qt::ISODate));
|
2016-06-25 11:02:09 +02:00
|
|
|
break;
|
|
|
|
case Geoidheight:
|
2016-07-28 00:23:22 +02:00
|
|
|
_track->last().setGeoidHeight(value.toLatin1().toDouble());
|
2016-06-25 11:02:09 +02:00
|
|
|
break;
|
|
|
|
case Speed:
|
2016-07-28 00:23:22 +02:00
|
|
|
_track->last().setSpeed(value.toDouble());
|
2016-06-25 11:02:09 +02:00
|
|
|
break;
|
|
|
|
case HeartRate:
|
2016-07-28 00:23:22 +02:00
|
|
|
_track->last().setHeartRate(value.toDouble());
|
2016-06-25 11:02:09 +02:00
|
|
|
break;
|
|
|
|
case Temperature:
|
2016-07-28 00:23:22 +02:00
|
|
|
_track->last().setTemperature(value.toDouble());
|
2016-06-25 11:02:09 +02:00
|
|
|
break;
|
2016-08-02 00:28:56 +02:00
|
|
|
default:
|
|
|
|
break;
|
2016-06-20 23:56:42 +02:00
|
|
|
}
|
2015-10-05 01:43:48 +02:00
|
|
|
}
|
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
void Parser::handleWaypointData(DataType type, const QString &value)
|
2016-02-11 20:58:52 +01:00
|
|
|
{
|
2016-07-28 00:23:22 +02:00
|
|
|
switch (type) {
|
2016-07-25 19:32:36 +02:00
|
|
|
case Name:
|
|
|
|
_waypoints.last().setName(value);
|
|
|
|
break;
|
|
|
|
case Description:
|
|
|
|
_waypoints.last().setDescription(value);
|
|
|
|
break;
|
2016-07-28 00:23:22 +02:00
|
|
|
case Time:
|
|
|
|
_waypoints.last().setTimestamp(QDateTime::fromString(
|
|
|
|
value.toLatin1(), Qt::ISODate));
|
|
|
|
break;
|
|
|
|
case Elevation:
|
|
|
|
_waypoints.last().setElevation(value.toLatin1().toDouble());
|
|
|
|
break;
|
|
|
|
case Geoidheight:
|
|
|
|
_waypoints.last().setGeoidHeight(value.toLatin1().toDouble());
|
|
|
|
break;
|
2016-08-02 00:28:56 +02:00
|
|
|
default:
|
|
|
|
break;
|
2016-07-25 19:32:36 +02:00
|
|
|
}
|
2016-02-11 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
2016-08-09 01:16:19 +02:00
|
|
|
void Parser::handleRoutepointData(DataType type, const QString &value)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case Name:
|
|
|
|
_route->last().setName(value);
|
|
|
|
break;
|
|
|
|
case Description:
|
|
|
|
_route->last().setDescription(value);
|
|
|
|
break;
|
|
|
|
case Time:
|
|
|
|
_route->last().setTimestamp(QDateTime::fromString(
|
|
|
|
value.toLatin1(), Qt::ISODate));
|
|
|
|
break;
|
|
|
|
case Elevation:
|
|
|
|
_route->last().setElevation(value.toLatin1().toDouble());
|
|
|
|
break;
|
|
|
|
case Geoidheight:
|
|
|
|
_route->last().setGeoidHeight(value.toLatin1().toDouble());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 00:12:34 +02:00
|
|
|
void Parser::handleTrackpointAttributes(const QXmlStreamAttributes &attr)
|
2015-10-05 01:43:48 +02:00
|
|
|
{
|
2016-07-28 00:23:22 +02:00
|
|
|
_track->last().setCoordinates(QPointF(
|
|
|
|
attr.value("lon").toLatin1().toDouble(),
|
|
|
|
attr.value("lat").toLatin1().toDouble()));
|
2015-10-05 01:43:48 +02:00
|
|
|
}
|
|
|
|
|
2016-08-09 01:16:19 +02:00
|
|
|
void Parser::handleRoutepointAttributes(const QXmlStreamAttributes &attr)
|
|
|
|
{
|
|
|
|
_route->last().setCoordinates(QPointF(
|
|
|
|
attr.value("lon").toLatin1().toDouble(),
|
|
|
|
attr.value("lat").toLatin1().toDouble()));
|
|
|
|
}
|
|
|
|
|
2016-06-21 00:12:34 +02:00
|
|
|
void Parser::handleWaypointAttributes(const QXmlStreamAttributes &attr)
|
2016-02-11 20:58:52 +01:00
|
|
|
{
|
|
|
|
_waypoints.last().setCoordinates(QPointF(
|
|
|
|
attr.value("lon").toLatin1().toDouble(),
|
|
|
|
attr.value("lat").toLatin1().toDouble()));
|
|
|
|
}
|
2015-10-05 01:43:48 +02:00
|
|
|
|
2016-03-21 23:13:46 +01:00
|
|
|
void Parser::tpExtension()
|
|
|
|
{
|
|
|
|
while (_reader.readNextStartElement()) {
|
2016-06-20 23:56:42 +02:00
|
|
|
if (_reader.name() == "hr")
|
2016-06-21 00:12:34 +02:00
|
|
|
handleTrackpointData(HeartRate, _reader.readElementText());
|
2016-06-20 23:56:42 +02:00
|
|
|
else if (_reader.name() == "atemp")
|
2016-06-21 00:12:34 +02:00
|
|
|
handleTrackpointData(Temperature, _reader.readElementText());
|
2016-03-21 23:13:46 +01:00
|
|
|
else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 17:53:09 +01:00
|
|
|
void Parser::extensions()
|
2015-10-15 01:30:21 +02:00
|
|
|
{
|
|
|
|
while (_reader.readNextStartElement()) {
|
2016-06-20 23:56:42 +02:00
|
|
|
if (_reader.name() == "speed")
|
2016-06-21 00:12:34 +02:00
|
|
|
handleTrackpointData(Speed, _reader.readElementText());
|
2016-06-20 23:56:42 +02:00
|
|
|
else if (_reader.name() == "hr" || _reader.name() == "heartrate")
|
2016-06-21 00:12:34 +02:00
|
|
|
handleTrackpointData(HeartRate, _reader.readElementText());
|
2016-06-20 23:56:42 +02:00
|
|
|
else if (_reader.name() == "temp")
|
2016-06-21 00:12:34 +02:00
|
|
|
handleTrackpointData(Temperature, _reader.readElementText());
|
2016-03-21 23:13:46 +01:00
|
|
|
else if (_reader.name() == "TrackPointExtension")
|
|
|
|
tpExtension();
|
2015-10-15 01:30:21 +02:00
|
|
|
else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 00:12:34 +02:00
|
|
|
void Parser::trackpointData()
|
2015-10-05 01:43:48 +02:00
|
|
|
{
|
|
|
|
while (_reader.readNextStartElement()) {
|
2016-06-20 23:56:42 +02:00
|
|
|
if (_reader.name() == "ele")
|
2016-06-21 00:12:34 +02:00
|
|
|
handleTrackpointData(Elevation, _reader.readElementText());
|
2016-06-20 23:56:42 +02:00
|
|
|
else if (_reader.name() == "time")
|
2016-06-21 00:12:34 +02:00
|
|
|
handleTrackpointData(Time, _reader.readElementText());
|
2016-06-20 23:56:42 +02:00
|
|
|
else if (_reader.name() == "geoidheight")
|
2016-06-21 00:12:34 +02:00
|
|
|
handleTrackpointData(Geoidheight, _reader.readElementText());
|
2015-10-15 01:30:21 +02:00
|
|
|
else if (_reader.name() == "extensions")
|
2016-02-08 17:53:09 +01:00
|
|
|
extensions();
|
2015-10-05 01:43:48 +02:00
|
|
|
else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 01:16:19 +02:00
|
|
|
void Parser::routepointData()
|
|
|
|
{
|
|
|
|
while (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == "name")
|
|
|
|
handleRoutepointData(Name, _reader.readElementText());
|
2016-08-30 21:26:28 +02:00
|
|
|
else if (_reader.name() == "desc")
|
|
|
|
handleRoutepointData(Description, _reader.readElementText());
|
2016-08-09 01:16:19 +02:00
|
|
|
else if (_reader.name() == "ele")
|
|
|
|
handleRoutepointData(Elevation, _reader.readElementText());
|
|
|
|
else if (_reader.name() == "geoidheight")
|
|
|
|
handleRoutepointData(Geoidheight, _reader.readElementText());
|
2016-08-30 21:26:28 +02:00
|
|
|
else if (_reader.name() == "time")
|
|
|
|
handleRoutepointData(Time, _reader.readElementText());
|
2016-08-09 01:16:19 +02:00
|
|
|
else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 00:12:34 +02:00
|
|
|
void Parser::trackpoints()
|
2015-10-05 01:43:48 +02:00
|
|
|
{
|
|
|
|
while (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == "trkpt") {
|
2016-02-19 21:42:54 +01:00
|
|
|
_track->append(Trackpoint());
|
2016-06-21 00:12:34 +02:00
|
|
|
handleTrackpointAttributes(_reader.attributes());
|
|
|
|
trackpointData();
|
2015-10-05 01:43:48 +02:00
|
|
|
} else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 01:16:19 +02:00
|
|
|
void Parser::routepoints()
|
|
|
|
{
|
|
|
|
while (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == "rtept") {
|
|
|
|
_route->append(Waypoint());
|
|
|
|
handleRoutepointAttributes(_reader.attributes());
|
|
|
|
routepointData();
|
|
|
|
} else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 17:53:09 +01:00
|
|
|
void Parser::track()
|
2015-10-05 01:43:48 +02:00
|
|
|
{
|
|
|
|
while (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == "trkseg") {
|
2016-06-21 00:12:34 +02:00
|
|
|
trackpoints();
|
2015-10-05 01:43:48 +02:00
|
|
|
} else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 00:12:34 +02:00
|
|
|
void Parser::waypointData()
|
2016-02-11 20:58:52 +01:00
|
|
|
{
|
|
|
|
while (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == "name")
|
2016-06-21 00:12:34 +02:00
|
|
|
handleWaypointData(Name, _reader.readElementText());
|
2016-07-25 19:32:36 +02:00
|
|
|
else if (_reader.name() == "desc")
|
|
|
|
handleWaypointData(Description, _reader.readElementText());
|
2016-07-28 00:23:22 +02:00
|
|
|
else if (_reader.name() == "ele")
|
|
|
|
handleWaypointData(Elevation, _reader.readElementText());
|
|
|
|
else if (_reader.name() == "geoidheight")
|
|
|
|
handleWaypointData(Geoidheight, _reader.readElementText());
|
|
|
|
else if (_reader.name() == "time")
|
|
|
|
handleWaypointData(Time, _reader.readElementText());
|
2016-02-11 20:58:52 +01:00
|
|
|
else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 17:53:09 +01:00
|
|
|
void Parser::gpx()
|
2015-10-05 01:43:48 +02:00
|
|
|
{
|
|
|
|
while (_reader.readNextStartElement()) {
|
2016-02-08 17:53:09 +01:00
|
|
|
if (_reader.name() == "trk") {
|
2016-02-19 21:42:54 +01:00
|
|
|
_tracks.append(QVector<Trackpoint>());
|
2016-02-11 20:58:52 +01:00
|
|
|
_track = &_tracks.back();
|
2016-02-08 17:53:09 +01:00
|
|
|
track();
|
2016-08-09 01:16:19 +02:00
|
|
|
} else if (_reader.name() == "rte") {
|
|
|
|
_routes.append(QVector<Waypoint>());
|
|
|
|
_route = &_routes.back();
|
|
|
|
routepoints();
|
2016-02-11 20:58:52 +01:00
|
|
|
} else if (_reader.name() == "wpt") {
|
2016-02-19 21:42:54 +01:00
|
|
|
_waypoints.append(Waypoint());
|
2016-06-21 00:12:34 +02:00
|
|
|
handleWaypointAttributes(_reader.attributes());
|
|
|
|
waypointData();
|
2016-02-08 17:53:09 +01:00
|
|
|
} else
|
2015-10-05 01:43:48 +02:00
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 17:53:09 +01:00
|
|
|
bool Parser::parse()
|
2015-10-05 01:43:48 +02:00
|
|
|
{
|
|
|
|
if (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == "gpx")
|
2016-02-08 17:53:09 +01:00
|
|
|
gpx();
|
2015-10-05 01:43:48 +02:00
|
|
|
else
|
2015-11-26 19:13:59 +01:00
|
|
|
_reader.raiseError("Not a GPX file.");
|
2015-10-05 01:43:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return !_reader.error();
|
|
|
|
}
|
|
|
|
|
2016-02-11 20:58:52 +01:00
|
|
|
bool Parser::loadFile(QIODevice *device)
|
2015-10-05 01:43:48 +02:00
|
|
|
{
|
|
|
|
_reader.clear();
|
|
|
|
_reader.setDevice(device);
|
|
|
|
|
2016-02-08 17:53:09 +01:00
|
|
|
return parse();
|
2015-10-05 01:43:48 +02:00
|
|
|
}
|