1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-28 05:34:47 +01:00
GPXSee/src/parser.cpp

253 lines
6.3 KiB
C++
Raw Normal View History

2015-10-05 01:43:48 +02:00
#include "parser.h"
void Parser::handleTrackpointData(DataType type, const QString &value)
{
switch (type) {
2016-06-25 11:02:09 +02:00
case Elevation:
_track->last().setElevation(value.toLatin1().toDouble());
2016-06-25 11:02:09 +02:00
break;
case Time:
_track->last().setTimestamp(QDateTime::fromString(value.toLatin1(),
Qt::ISODate));
2016-06-25 11:02:09 +02:00
break;
case Geoidheight:
_track->last().setGeoidHeight(value.toLatin1().toDouble());
2016-06-25 11:02:09 +02:00
break;
case Speed:
_track->last().setSpeed(value.toDouble());
2016-06-25 11:02:09 +02:00
break;
case HeartRate:
_track->last().setHeartRate(value.toDouble());
2016-06-25 11:02:09 +02:00
break;
case Temperature:
_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
}
void Parser::handleWaypointData(DataType type, const QString &value)
{
switch (type) {
2016-07-25 19:32:36 +02:00
case Name:
_waypoints.last().setName(value);
break;
case Description:
_waypoints.last().setDescription(value);
break;
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-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
{
_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)
{
_waypoints.last().setCoordinates(QPointF(
attr.value("lon").toLatin1().toDouble(),
attr.value("lat").toLatin1().toDouble()));
}
2015-10-05 01:43:48 +02: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());
else
_reader.skipCurrentElement();
}
}
void Parser::extensions()
{
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());
else if (_reader.name() == "TrackPointExtension")
tpExtension();
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());
else if (_reader.name() == "extensions")
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());
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());
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();
}
}
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()
{
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());
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());
else
_reader.skipCurrentElement();
}
}
void Parser::gpx()
2015-10-05 01:43:48 +02:00
{
while (_reader.readNextStartElement()) {
if (_reader.name() == "trk") {
2016-02-19 21:42:54 +01:00
_tracks.append(QVector<Trackpoint>());
_track = &_tracks.back();
track();
2016-08-09 01:16:19 +02:00
} else if (_reader.name() == "rte") {
_routes.append(QVector<Waypoint>());
_route = &_routes.back();
routepoints();
} 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();
} else
2015-10-05 01:43:48 +02:00
_reader.skipCurrentElement();
}
}
bool Parser::parse()
2015-10-05 01:43:48 +02:00
{
if (_reader.readNextStartElement()) {
if (_reader.name() == "gpx")
gpx();
2015-10-05 01:43:48 +02:00
else
_reader.raiseError("Not a GPX file.");
2015-10-05 01:43:48 +02:00
}
return !_reader.error();
}
bool Parser::loadFile(QIODevice *device)
2015-10-05 01:43:48 +02:00
{
_reader.clear();
_reader.setDevice(device);
return parse();
2015-10-05 01:43:48 +02:00
}