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

245 lines
5.7 KiB
C++
Raw Normal View History

2016-10-23 11:09:20 +02:00
#include "gpxparser.h"
2015-10-05 01:43:48 +02:00
qreal GPXParser::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(
_reader.name().toString()));
2015-10-05 01:43:48 +02:00
return ret;
}
QDateTime GPXParser::time()
2016-08-09 01:16:19 +02:00
{
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(
_reader.name().toString()));
return d;
2016-08-09 01:16:19 +02:00
}
Coordinates GPXParser::coordinates()
{
bool res;
qreal lon, lat;
const QXmlStreamAttributes &attr = _reader.attributes();
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
lon = attr.value("lon").toString().toDouble(&res);
#else // QT_VERSION < 5
lon = attr.value("lon").toDouble(&res);
#endif // QT_VERSION < 5
if (!res || (lon < -180.0 || lon > 180.0)) {
2016-10-29 12:22:28 +02:00
_reader.raiseError("Invalid longitude");
return Coordinates();
}
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
lat = attr.value("lat").toString().toDouble(&res);
#else // QT_VERSION < 5
lat = attr.value("lat").toDouble(&res);
#endif // QT_VERSION < 5
if (!res || (lat < -90.0 || lat > 90.0)) {
2016-10-29 12:22:28 +02:00
_reader.raiseError("Invalid latitude");
return Coordinates();
}
return Coordinates(lon, lat);
}
2016-10-27 08:09:12 +02:00
void GPXParser::handleTrackpointData(DataType type, Trackpoint &trackpoint)
2016-08-09 01:16:19 +02:00
{
switch (type) {
case Elevation:
2016-10-27 08:09:12 +02:00
trackpoint.setElevation(number());
break;
case Time:
2016-10-27 08:09:12 +02:00
trackpoint.setTimestamp(time());
break;
case Speed:
2016-10-27 08:09:12 +02:00
trackpoint.setSpeed(number());
break;
case HeartRate:
2016-10-27 08:09:12 +02:00
trackpoint.setHeartRate(number());
break;
case Temperature:
2016-10-27 08:09:12 +02:00
trackpoint.setTemperature(number());
break;
default:
break;
}
2016-08-09 01:16:19 +02:00
}
void GPXParser::handleWaypointData(DataType type, Waypoint &waypoint)
{
switch (type) {
case Name:
waypoint.setName(_reader.readElementText());
break;
case Description:
waypoint.setDescription(_reader.readElementText());
break;
case Time:
waypoint.setTimestamp(time());
break;
case Elevation:
waypoint.setElevation(number());
break;
default:
break;
}
}
2015-10-05 01:43:48 +02:00
2016-10-27 08:09:12 +02:00
void GPXParser::tpExtension(Trackpoint &trackpoint)
{
while (_reader.readNextStartElement()) {
2016-06-20 23:56:42 +02:00
if (_reader.name() == "hr")
2016-10-27 08:09:12 +02:00
handleTrackpointData(HeartRate, trackpoint);
2016-06-20 23:56:42 +02:00
else if (_reader.name() == "atemp")
2016-10-27 08:09:12 +02:00
handleTrackpointData(Temperature, trackpoint);
else
_reader.skipCurrentElement();
}
}
2016-10-27 08:09:12 +02:00
void GPXParser::extensions(Trackpoint &trackpoint)
{
while (_reader.readNextStartElement()) {
2016-06-20 23:56:42 +02:00
if (_reader.name() == "speed")
2016-10-27 08:09:12 +02:00
handleTrackpointData(Speed, trackpoint);
2016-06-20 23:56:42 +02:00
else if (_reader.name() == "hr" || _reader.name() == "heartrate")
2016-10-27 08:09:12 +02:00
handleTrackpointData(HeartRate, trackpoint);
2016-06-20 23:56:42 +02:00
else if (_reader.name() == "temp")
2016-10-27 08:09:12 +02:00
handleTrackpointData(Temperature, trackpoint);
else if (_reader.name() == "TrackPointExtension")
2016-10-27 08:09:12 +02:00
tpExtension(trackpoint);
else
_reader.skipCurrentElement();
}
}
2016-10-27 08:09:12 +02:00
void GPXParser::trackpointData(Trackpoint &trackpoint)
2015-10-05 01:43:48 +02:00
{
qreal gh = NAN;
2015-10-05 01:43:48 +02:00
while (_reader.readNextStartElement()) {
2016-06-20 23:56:42 +02:00
if (_reader.name() == "ele")
2016-10-27 08:09:12 +02:00
handleTrackpointData(Elevation, trackpoint);
2016-06-20 23:56:42 +02:00
else if (_reader.name() == "time")
2016-10-27 08:09:12 +02:00
handleTrackpointData(Time, trackpoint);
2016-06-20 23:56:42 +02:00
else if (_reader.name() == "geoidheight")
gh = number();
else if (_reader.name() == "extensions")
2016-10-27 08:09:12 +02:00
extensions(trackpoint);
2015-10-05 01:43:48 +02:00
else
_reader.skipCurrentElement();
}
2016-10-27 08:09:12 +02:00
if (!std::isnan(gh) && !std::isnan(trackpoint.elevation()))
trackpoint.setElevation(trackpoint.elevation() - gh);
2016-08-09 01:16:19 +02:00
}
void GPXParser::waypointData(Waypoint &waypoint)
2016-10-23 11:09:20 +02:00
{
qreal gh = NAN;
2016-10-23 11:09:20 +02:00
while (_reader.readNextStartElement()) {
if (_reader.name() == "name")
handleWaypointData(Name, waypoint);
2016-10-23 11:09:20 +02:00
else if (_reader.name() == "desc")
handleWaypointData(Description, waypoint);
2016-10-23 11:09:20 +02:00
else if (_reader.name() == "ele")
handleWaypointData(Elevation, waypoint);
2016-10-23 11:09:20 +02:00
else if (_reader.name() == "geoidheight")
gh = number();
2016-10-23 11:09:20 +02:00
else if (_reader.name() == "time")
handleWaypointData(Time, waypoint);
2016-10-23 11:09:20 +02:00
else
_reader.skipCurrentElement();
}
if (!std::isnan(gh) && !std::isnan(waypoint.elevation()))
waypoint.setElevation(waypoint.elevation() - gh);
2016-10-23 11:09:20 +02:00
}
void GPXParser::trackpoints(TrackData &track)
2015-10-05 01:43:48 +02:00
{
while (_reader.readNextStartElement()) {
if (_reader.name() == "trkpt") {
2016-10-27 08:27:25 +02:00
track.append(Trackpoint(coordinates()));
trackpointData(track.last());
2015-10-05 01:43:48 +02:00
} else
_reader.skipCurrentElement();
}
}
void GPXParser::routepoints(RouteData &route)
2016-08-09 01:16:19 +02:00
{
while (_reader.readNextStartElement()) {
if (_reader.name() == "rtept") {
2016-10-27 08:27:25 +02:00
route.append(Waypoint(coordinates()));
waypointData(route.last());
} else if (_reader.name() == "name")
route.setName(_reader.readElementText());
else if (_reader.name() == "desc")
route.setDescription(_reader.readElementText());
else
2016-08-09 01:16:19 +02:00
_reader.skipCurrentElement();
}
}
void GPXParser::track(TrackData &track)
2015-10-05 01:43:48 +02:00
{
while (_reader.readNextStartElement()) {
if (_reader.name() == "trkseg")
2016-10-27 08:27:25 +02:00
trackpoints(track);
else if (_reader.name() == "name")
track.setName(_reader.readElementText());
else if (_reader.name() == "desc")
track.setDescription(_reader.readElementText());
else
2015-10-05 01:43:48 +02:00
_reader.skipCurrentElement();
}
}
2016-10-23 11:09:20 +02:00
void GPXParser::gpx()
2015-10-05 01:43:48 +02:00
{
while (_reader.readNextStartElement()) {
if (_reader.name() == "trk") {
_tracks.append(TrackData());
2016-10-27 08:27:25 +02:00
track(_tracks.back());
2016-08-09 01:16:19 +02:00
} else if (_reader.name() == "rte") {
_routes.append(RouteData());
2016-10-27 08:27:25 +02:00
routepoints(_routes.back());
} else if (_reader.name() == "wpt") {
_waypoints.append(Waypoint(coordinates()));
waypointData(_waypoints.last());
} else
2015-10-05 01:43:48 +02:00
_reader.skipCurrentElement();
}
}
2016-10-23 11:09:20 +02:00
bool GPXParser::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
2016-10-29 12:22:28 +02:00
_reader.raiseError("Not a GPX file");
2015-10-05 01:43:48 +02:00
}
return !_reader.error();
}
2016-10-29 10:40:30 +02:00
bool GPXParser::loadFile(QFile *file)
2015-10-05 01:43:48 +02:00
{
_reader.clear();
2016-10-29 10:40:30 +02:00
_reader.setDevice(file);
2015-10-05 01:43:48 +02:00
return parse();
2015-10-05 01:43:48 +02:00
}