2018-08-08 20:18:08 +02:00
|
|
|
#include "locparser.h"
|
|
|
|
|
|
|
|
Coordinates LOCParser::coordinates()
|
|
|
|
{
|
|
|
|
bool res;
|
|
|
|
const QXmlStreamAttributes &attr = _reader.attributes();
|
|
|
|
|
2020-12-22 22:09:09 +01:00
|
|
|
qreal lon = attr.value("lon").toDouble(&res);
|
2018-08-08 20:18:08 +02:00
|
|
|
if (!res || (lon < -180.0 || lon > 180.0)) {
|
|
|
|
_reader.raiseError("Invalid longitude");
|
|
|
|
return Coordinates();
|
|
|
|
}
|
2020-12-22 22:09:09 +01:00
|
|
|
qreal lat = attr.value("lat").toDouble(&res);
|
2018-08-08 20:18:08 +02:00
|
|
|
if (!res || (lat < -90.0 || lat > 90.0)) {
|
|
|
|
_reader.raiseError("Invalid latitude");
|
|
|
|
return Coordinates();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Coordinates(lon, lat);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LOCParser::waypoint(Waypoint &waypoint)
|
|
|
|
{
|
|
|
|
while (_reader.readNextStartElement()) {
|
2018-08-27 22:25:55 +02:00
|
|
|
if (_reader.name() == QLatin1String("name")) {
|
2018-08-08 20:18:08 +02:00
|
|
|
const QXmlStreamAttributes &attr = _reader.attributes();
|
|
|
|
waypoint.setName(attr.value("id").toString());
|
|
|
|
waypoint.setDescription(_reader.readElementText());
|
2018-08-27 22:25:55 +02:00
|
|
|
} else if (_reader.name() == QLatin1String("coord")) {
|
2018-08-08 20:18:08 +02:00
|
|
|
waypoint.setCoordinates(coordinates());
|
|
|
|
_reader.skipCurrentElement();
|
2019-10-14 01:16:38 +02:00
|
|
|
} else if (_reader.name() == QLatin1String("link")) {
|
|
|
|
const QXmlStreamAttributes &attr = _reader.attributes();
|
2019-10-20 20:30:10 +02:00
|
|
|
QString URL(_reader.readElementText());
|
|
|
|
if (!URL.isEmpty())
|
|
|
|
waypoint.addLink(Link(URL, attr.value("text").toString()));
|
2018-08-08 20:18:08 +02:00
|
|
|
} else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (waypoint.coordinates().isNull())
|
|
|
|
_reader.raiseError("Missing waypoint coordinates");
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:17:28 +01:00
|
|
|
void LOCParser::loc(QVector<Waypoint> &waypoints)
|
2018-08-08 20:18:08 +02:00
|
|
|
{
|
|
|
|
while (_reader.readNextStartElement()) {
|
2018-08-27 22:25:55 +02:00
|
|
|
if (_reader.name() == QLatin1String("waypoint")) {
|
2018-08-08 20:18:08 +02:00
|
|
|
waypoints.append(Waypoint());
|
|
|
|
waypoint(waypoints.last());
|
|
|
|
} else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LOCParser::parse(QFile *file, QList<TrackData> &tracks,
|
2019-01-31 01:46:53 +01:00
|
|
|
QList<RouteData> &routes, QList<Area> &polygons,
|
|
|
|
QVector<Waypoint> &waypoints)
|
2018-08-08 20:18:08 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(tracks);
|
|
|
|
Q_UNUSED(routes);
|
2019-01-31 01:46:53 +01:00
|
|
|
Q_UNUSED(polygons);
|
2018-08-08 20:18:08 +02:00
|
|
|
|
|
|
|
_reader.clear();
|
|
|
|
_reader.setDevice(file);
|
|
|
|
|
|
|
|
if (_reader.readNextStartElement()) {
|
2018-08-27 22:25:55 +02:00
|
|
|
if (_reader.name() == QLatin1String("loc"))
|
2018-08-08 20:18:08 +02:00
|
|
|
loc(waypoints);
|
|
|
|
else
|
|
|
|
_reader.raiseError("Not a LOC file");
|
|
|
|
}
|
|
|
|
|
|
|
|
return !_reader.error();
|
|
|
|
}
|