2022-09-01 00:28:00 +02:00
|
|
|
/*
|
|
|
|
WARNING: This code uses internal Qt API - the QZipReader class for reading
|
|
|
|
ZIP files - and things may break if Qt changes the API. For Qt5 this is not
|
|
|
|
a problem as we can "see the future" now and there are no changes in all
|
|
|
|
the supported Qt5 versions up to the last one (5.15). In Qt6 the class
|
|
|
|
might change or even disappear in the future, but this is very unlikely
|
|
|
|
as there were no changes for several years and The Qt Company's policy
|
|
|
|
is: "do not invest any resources into any desktop related stuff unless
|
|
|
|
absolutely necessary". There is an issue (QTBUG-3897) since the year 2009 to
|
|
|
|
include the ZIP reader into the public API, which aptly illustrates the
|
|
|
|
effort The Qt Company is willing to make about anything desktop related...
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QTemporaryDir>
|
2022-09-02 08:36:58 +02:00
|
|
|
#include <QCryptographicHash>
|
|
|
|
#include <QtEndian>
|
2022-09-02 09:55:11 +02:00
|
|
|
#include <QUrl>
|
|
|
|
#include <QRegularExpression>
|
2022-09-01 00:28:00 +02:00
|
|
|
#include <private/qzipreader_p.h>
|
2022-09-02 22:53:38 +02:00
|
|
|
#include "common/util.h"
|
2016-10-24 20:00:29 +02:00
|
|
|
#include "kmlparser.h"
|
|
|
|
|
2022-09-02 08:36:58 +02:00
|
|
|
static bool isZIP(QFile *file)
|
2022-09-01 00:28:00 +02:00
|
|
|
{
|
2022-09-02 08:36:58 +02:00
|
|
|
quint32 magic;
|
|
|
|
|
|
|
|
return (file->read((char *)&magic, sizeof(magic)) == (qint64)sizeof(magic)
|
|
|
|
&& qFromLittleEndian(magic) == 0x04034b50);
|
2022-09-01 00:28:00 +02:00
|
|
|
}
|
2016-10-24 20:00:29 +02:00
|
|
|
|
2016-10-27 22:33:35 +02:00
|
|
|
qreal KMLParser::number()
|
|
|
|
{
|
|
|
|
bool res;
|
2022-09-26 22:50:40 +02:00
|
|
|
QString str(_reader.readElementText());
|
|
|
|
|
|
|
|
if (str.isEmpty())
|
|
|
|
return NAN;
|
|
|
|
|
|
|
|
qreal ret = str.toDouble(&res);
|
2016-10-27 22:33:35 +02:00
|
|
|
if (!res)
|
2016-10-29 12:22:28 +02:00
|
|
|
_reader.raiseError(QString("Invalid %1").arg(
|
2016-10-27 22:33:35 +02:00
|
|
|
_reader.name().toString()));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDateTime KMLParser::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 22:33:35 +02:00
|
|
|
_reader.name().toString()));
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2016-10-28 19:12:40 +02:00
|
|
|
bool KMLParser::coord(Trackpoint &trackpoint)
|
|
|
|
{
|
|
|
|
QString data = _reader.readElementText();
|
|
|
|
const QChar *sp, *ep, *cp, *vp;
|
|
|
|
int c = 0;
|
|
|
|
qreal val[3];
|
|
|
|
bool res;
|
|
|
|
|
2022-09-26 22:50:40 +02:00
|
|
|
if (data.isEmpty())
|
|
|
|
return true;
|
2016-10-28 19:12:40 +02:00
|
|
|
|
|
|
|
sp = data.constData();
|
|
|
|
ep = sp + data.size();
|
|
|
|
|
|
|
|
for (cp = sp; cp < ep; cp++)
|
|
|
|
if (!cp->isSpace())
|
|
|
|
break;
|
|
|
|
|
|
|
|
for (vp = cp; cp <= ep; cp++) {
|
|
|
|
if (cp->isSpace() || cp->isNull()) {
|
|
|
|
if (c > 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
val[c] = QString(vp, cp - vp).toDouble(&res);
|
|
|
|
if (!res)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (c == 1) {
|
|
|
|
trackpoint.setCoordinates(Coordinates(val[0], val[1]));
|
|
|
|
if (!trackpoint.coordinates().isValid())
|
|
|
|
return false;
|
|
|
|
} else if (c == 2)
|
|
|
|
trackpoint.setElevation(val[2]);
|
|
|
|
|
|
|
|
while (cp->isSpace())
|
|
|
|
cp++;
|
|
|
|
vp = cp;
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-27 22:33:35 +02:00
|
|
|
bool KMLParser::pointCoordinates(Waypoint &waypoint)
|
2016-10-24 20:00:29 +02:00
|
|
|
{
|
|
|
|
QString data = _reader.readElementText();
|
|
|
|
const QChar *sp, *ep, *cp, *vp;
|
|
|
|
int c = 0;
|
|
|
|
qreal val[3];
|
|
|
|
bool res;
|
|
|
|
|
|
|
|
|
|
|
|
sp = data.constData();
|
|
|
|
ep = sp + data.size();
|
|
|
|
|
2016-10-25 22:50:11 +02:00
|
|
|
for (cp = sp; cp < ep; cp++)
|
|
|
|
if (!cp->isSpace())
|
|
|
|
break;
|
|
|
|
|
|
|
|
for (vp = cp; cp <= ep; cp++) {
|
|
|
|
if (*cp == ',') {
|
2016-11-11 22:04:26 +01:00
|
|
|
if (c > 1)
|
2016-10-24 20:00:29 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
val[c] = QString(vp, cp - vp).toDouble(&res);
|
|
|
|
if (!res)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
c++;
|
|
|
|
vp = cp + 1;
|
2016-10-25 22:50:11 +02:00
|
|
|
} else if (cp->isSpace() || cp->isNull()) {
|
|
|
|
if (c < 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
val[c] = QString(vp, cp - vp).toDouble(&res);
|
|
|
|
if (!res)
|
|
|
|
return false;
|
|
|
|
|
2016-10-27 22:33:35 +02:00
|
|
|
waypoint.setCoordinates(Coordinates(val[0], val[1]));
|
2016-10-28 19:12:40 +02:00
|
|
|
if (!waypoint.coordinates().isValid())
|
|
|
|
return false;
|
2016-10-25 22:50:11 +02:00
|
|
|
if (c == 2)
|
2016-10-27 22:33:35 +02:00
|
|
|
waypoint.setElevation(val[2]);
|
2016-10-25 22:50:11 +02:00
|
|
|
|
|
|
|
while (cp->isSpace())
|
|
|
|
cp++;
|
|
|
|
c = 3;
|
2016-10-24 20:00:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-11 23:28:08 +01:00
|
|
|
bool KMLParser::lineCoordinates(SegmentData &segment)
|
2016-10-24 20:00:29 +02:00
|
|
|
{
|
|
|
|
QString data = _reader.readElementText();
|
|
|
|
const QChar *sp, *ep, *cp, *vp;
|
|
|
|
int c = 0;
|
|
|
|
qreal val[3];
|
|
|
|
bool res;
|
|
|
|
|
|
|
|
|
|
|
|
sp = data.constData();
|
|
|
|
ep = sp + data.size();
|
|
|
|
|
2016-10-25 22:50:11 +02:00
|
|
|
for (cp = sp; cp < ep; cp++)
|
|
|
|
if (!cp->isSpace())
|
|
|
|
break;
|
|
|
|
|
|
|
|
for (vp = cp; cp <= ep; cp++) {
|
|
|
|
if (*cp == ',') {
|
2016-11-11 22:04:26 +01:00
|
|
|
if (c > 1)
|
2016-10-24 20:00:29 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
val[c] = QString(vp, cp - vp).toDouble(&res);
|
|
|
|
if (!res)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
c++;
|
|
|
|
vp = cp + 1;
|
2016-10-25 22:50:11 +02:00
|
|
|
} else if (cp->isSpace() || cp->isNull()) {
|
|
|
|
if (c < 1 || c > 2)
|
2016-10-24 20:00:29 +02:00
|
|
|
return false;
|
2016-10-25 22:50:11 +02:00
|
|
|
|
|
|
|
val[c] = QString(vp, cp - vp).toDouble(&res);
|
|
|
|
if (!res)
|
|
|
|
return false;
|
|
|
|
|
2019-02-11 23:28:08 +01:00
|
|
|
segment.append(Trackpoint(Coordinates(val[0], val[1])));
|
|
|
|
if (!segment.last().coordinates().isValid())
|
2016-10-28 19:12:40 +02:00
|
|
|
return false;
|
2016-10-25 22:50:11 +02:00
|
|
|
if (c == 2)
|
2019-02-11 23:28:08 +01:00
|
|
|
segment.last().setElevation(val[2]);
|
2016-10-25 22:50:11 +02:00
|
|
|
|
2016-10-24 20:00:29 +02:00
|
|
|
while (cp->isSpace())
|
|
|
|
cp++;
|
|
|
|
c = 0;
|
2016-10-25 22:50:11 +02:00
|
|
|
vp = cp;
|
2016-10-24 20:00:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 22:50:11 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-31 01:46:53 +01:00
|
|
|
bool KMLParser::polygonCoordinates(QVector<Coordinates> &points)
|
|
|
|
{
|
|
|
|
QString data = _reader.readElementText();
|
|
|
|
const QChar *sp, *ep, *cp, *vp;
|
|
|
|
int c = 0;
|
|
|
|
qreal val[3];
|
|
|
|
bool res;
|
|
|
|
|
|
|
|
|
|
|
|
sp = data.constData();
|
|
|
|
ep = sp + data.size();
|
|
|
|
|
|
|
|
for (cp = sp; cp < ep; cp++)
|
|
|
|
if (!cp->isSpace())
|
|
|
|
break;
|
|
|
|
|
|
|
|
for (vp = cp; cp <= ep; cp++) {
|
|
|
|
if (*cp == ',') {
|
|
|
|
if (c > 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
val[c] = QString(vp, cp - vp).toDouble(&res);
|
|
|
|
if (!res)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
c++;
|
|
|
|
vp = cp + 1;
|
|
|
|
} else if (cp->isSpace() || cp->isNull()) {
|
|
|
|
if (c < 1 || c > 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
val[c] = QString(vp, cp - vp).toDouble(&res);
|
|
|
|
if (!res)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
points.append(Coordinates(val[0], val[1]));
|
|
|
|
if (!points.last().isValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (cp->isSpace())
|
|
|
|
cp++;
|
|
|
|
c = 0;
|
|
|
|
vp = cp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-28 14:33:36 +02:00
|
|
|
QDateTime KMLParser::timeStamp()
|
2016-10-25 22:50:11 +02:00
|
|
|
{
|
2016-10-28 14:33:36 +02:00
|
|
|
QDateTime ts;
|
|
|
|
|
2016-10-25 22:50:11 +02:00
|
|
|
while (_reader.readNextStartElement()) {
|
2018-08-27 22:25:55 +02:00
|
|
|
if (_reader.name() == QLatin1String("when"))
|
2016-10-28 14:33:36 +02:00
|
|
|
ts = time();
|
2016-10-27 22:33:35 +02:00
|
|
|
else
|
2016-10-25 22:50:11 +02:00
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
2016-10-28 14:33:36 +02:00
|
|
|
|
|
|
|
return ts;
|
2016-10-24 20:00:29 +02:00
|
|
|
}
|
|
|
|
|
2019-02-11 23:28:08 +01:00
|
|
|
void KMLParser::lineString(SegmentData &segment)
|
2016-10-24 20:00:29 +02:00
|
|
|
{
|
|
|
|
while (_reader.readNextStartElement()) {
|
2018-08-27 22:25:55 +02:00
|
|
|
if (_reader.name() == QLatin1String("coordinates")) {
|
2019-02-11 23:28:08 +01:00
|
|
|
if (!lineCoordinates(segment))
|
2016-10-29 12:22:28 +02:00
|
|
|
_reader.raiseError("Invalid coordinates");
|
2016-10-24 20:00:29 +02:00
|
|
|
} else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 01:46:53 +01:00
|
|
|
void KMLParser::linearRing(QVector<Coordinates> &coordinates)
|
|
|
|
{
|
|
|
|
while (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == QLatin1String("coordinates")) {
|
|
|
|
if (!polygonCoordinates(coordinates))
|
|
|
|
_reader.raiseError("Invalid coordinates");
|
|
|
|
} else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KMLParser::boundary(QVector<Coordinates> &coordinates)
|
|
|
|
{
|
|
|
|
while (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == QLatin1String("LinearRing"))
|
|
|
|
linearRing(coordinates);
|
|
|
|
else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KMLParser::polygon(Area &area)
|
|
|
|
{
|
2021-03-07 11:58:21 +01:00
|
|
|
Polygon polygon;
|
2019-01-31 01:46:53 +01:00
|
|
|
|
|
|
|
while (_reader.readNextStartElement()) {
|
2021-04-10 15:27:40 +02:00
|
|
|
QVector<Coordinates> path;
|
|
|
|
|
2019-01-31 01:46:53 +01:00
|
|
|
if (_reader.name() == QLatin1String("outerBoundaryIs")) {
|
2019-02-01 00:25:41 +01:00
|
|
|
if (!polygon.isEmpty()) {
|
2019-01-31 01:46:53 +01:00
|
|
|
_reader.raiseError("Multiple polygon outerBoundaryIss");
|
|
|
|
return;
|
|
|
|
}
|
2021-04-10 15:27:40 +02:00
|
|
|
boundary(path);
|
|
|
|
polygon.append(path);
|
2019-01-31 01:46:53 +01:00
|
|
|
} else if (_reader.name() == QLatin1String("innerBoundaryIs")) {
|
2019-02-01 00:25:41 +01:00
|
|
|
if (polygon.isEmpty()) {
|
2019-01-31 01:46:53 +01:00
|
|
|
_reader.raiseError("Missing polygon outerBoundaryIs");
|
|
|
|
return;
|
|
|
|
}
|
2021-04-10 15:27:40 +02:00
|
|
|
boundary(path);
|
|
|
|
polygon.append(path);
|
2019-01-31 01:46:53 +01:00
|
|
|
} else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
2021-03-07 11:58:21 +01:00
|
|
|
|
|
|
|
area.append(polygon);
|
2019-01-31 01:46:53 +01:00
|
|
|
}
|
|
|
|
|
2016-10-27 22:33:35 +02:00
|
|
|
void KMLParser::point(Waypoint &waypoint)
|
2016-10-24 20:00:29 +02:00
|
|
|
{
|
|
|
|
while (_reader.readNextStartElement()) {
|
2018-08-27 22:25:55 +02:00
|
|
|
if (_reader.name() == QLatin1String("coordinates")) {
|
2016-10-27 22:33:35 +02:00
|
|
|
if (!pointCoordinates(waypoint))
|
2016-10-29 12:22:28 +02:00
|
|
|
_reader.raiseError("Invalid coordinates");
|
2016-10-24 20:00:29 +02:00
|
|
|
} else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
2016-10-29 10:40:30 +02:00
|
|
|
|
|
|
|
if (waypoint.coordinates().isNull())
|
2016-10-29 12:22:28 +02:00
|
|
|
_reader.raiseError("Missing Point coordinates");
|
2016-10-24 20:00:29 +02:00
|
|
|
}
|
|
|
|
|
2022-09-26 22:50:40 +02:00
|
|
|
void KMLParser::heartRate(SegmentData &segment)
|
2017-10-13 08:15:54 +02:00
|
|
|
{
|
2022-09-26 22:50:40 +02:00
|
|
|
int i = 0;
|
2017-10-13 08:15:54 +02:00
|
|
|
const char error[] = "Heartrate data count mismatch";
|
|
|
|
|
|
|
|
while (_reader.readNextStartElement()) {
|
2018-08-27 22:25:55 +02:00
|
|
|
if (_reader.name() == QLatin1String("value")) {
|
2019-02-11 23:28:08 +01:00
|
|
|
if (i < segment.size())
|
|
|
|
segment[i++].setHeartRate(number());
|
2017-10-13 08:15:54 +02:00
|
|
|
else {
|
|
|
|
_reader.raiseError(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
|
2022-09-26 22:50:40 +02:00
|
|
|
if (!_reader.error() && i != segment.size())
|
2017-10-13 08:15:54 +02:00
|
|
|
_reader.raiseError(error);
|
|
|
|
}
|
|
|
|
|
2022-09-26 22:50:40 +02:00
|
|
|
void KMLParser::cadence(SegmentData &segment)
|
2017-10-13 08:15:54 +02:00
|
|
|
{
|
2022-09-26 22:50:40 +02:00
|
|
|
int i = 0;
|
2017-10-13 08:15:54 +02:00
|
|
|
const char error[] = "Cadence data count mismatch";
|
|
|
|
|
|
|
|
while (_reader.readNextStartElement()) {
|
2018-08-27 22:25:55 +02:00
|
|
|
if (_reader.name() == QLatin1String("value")) {
|
2019-02-11 23:28:08 +01:00
|
|
|
if (i < segment.size())
|
|
|
|
segment[i++].setCadence(number());
|
2017-10-13 08:15:54 +02:00
|
|
|
else {
|
|
|
|
_reader.raiseError(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
|
2022-09-26 22:50:40 +02:00
|
|
|
if (!_reader.error() && i != segment.size())
|
2017-10-13 08:15:54 +02:00
|
|
|
_reader.raiseError(error);
|
|
|
|
}
|
|
|
|
|
2022-09-26 22:50:40 +02:00
|
|
|
void KMLParser::speed(SegmentData &segment)
|
2017-10-13 08:15:54 +02:00
|
|
|
{
|
2022-09-26 22:50:40 +02:00
|
|
|
int i = 0;
|
2017-10-13 08:15:54 +02:00
|
|
|
const char error[] = "Speed data count mismatch";
|
|
|
|
|
|
|
|
while (_reader.readNextStartElement()) {
|
2018-08-27 22:25:55 +02:00
|
|
|
if (_reader.name() == QLatin1String("value")) {
|
2019-02-11 23:28:08 +01:00
|
|
|
if (i < segment.size())
|
|
|
|
segment[i++].setSpeed(number());
|
2017-10-13 08:15:54 +02:00
|
|
|
else {
|
|
|
|
_reader.raiseError(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
|
2022-09-26 22:50:40 +02:00
|
|
|
if (!_reader.error() && i != segment.size())
|
2017-10-13 08:15:54 +02:00
|
|
|
_reader.raiseError(error);
|
|
|
|
}
|
|
|
|
|
2022-09-26 22:50:40 +02:00
|
|
|
void KMLParser::temperature(SegmentData &segment)
|
2017-10-13 08:15:54 +02:00
|
|
|
{
|
2022-09-26 22:50:40 +02:00
|
|
|
int i = 0;
|
2017-10-13 08:15:54 +02:00
|
|
|
const char error[] = "Temperature data count mismatch";
|
|
|
|
|
|
|
|
while (_reader.readNextStartElement()) {
|
2018-08-27 22:25:55 +02:00
|
|
|
if (_reader.name() == QLatin1String("value")) {
|
2019-02-11 23:28:08 +01:00
|
|
|
if (i < segment.size())
|
|
|
|
segment[i++].setTemperature(number());
|
2017-10-13 08:15:54 +02:00
|
|
|
else {
|
|
|
|
_reader.raiseError(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
|
2022-09-26 22:50:40 +02:00
|
|
|
if (!_reader.error() && i != segment.size())
|
|
|
|
_reader.raiseError(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KMLParser::power(SegmentData &segment)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
const char error[] = "Power data count mismatch";
|
|
|
|
|
|
|
|
while (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == QLatin1String("value")) {
|
|
|
|
if (i < segment.size())
|
|
|
|
segment[i++].setPower(number());
|
|
|
|
else {
|
|
|
|
_reader.raiseError(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_reader.error() && i != segment.size())
|
2017-10-13 08:15:54 +02:00
|
|
|
_reader.raiseError(error);
|
|
|
|
}
|
|
|
|
|
2022-09-26 22:50:40 +02:00
|
|
|
void KMLParser::schemaData(SegmentData &segment)
|
2017-10-13 08:15:54 +02:00
|
|
|
{
|
|
|
|
while (_reader.readNextStartElement()) {
|
2018-08-27 22:25:55 +02:00
|
|
|
if (_reader.name() == QLatin1String("SimpleArrayData")) {
|
2017-10-13 08:15:54 +02:00
|
|
|
QXmlStreamAttributes attr = _reader.attributes();
|
2022-09-26 22:50:40 +02:00
|
|
|
// There are files using capitalized names in the wild!
|
|
|
|
QString name(attr.value("name").toString().toLower());
|
|
|
|
|
|
|
|
if (name == QLatin1String("heartrate"))
|
|
|
|
heartRate(segment);
|
|
|
|
else if (name == QLatin1String("cadence"))
|
|
|
|
cadence(segment);
|
|
|
|
else if (name == QLatin1String("speed"))
|
|
|
|
speed(segment);
|
|
|
|
else if (name == QLatin1String("temperature"))
|
|
|
|
temperature(segment);
|
|
|
|
else if (name == QLatin1String("power"))
|
|
|
|
power(segment);
|
2017-10-13 08:15:54 +02:00
|
|
|
else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
} else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-26 22:50:40 +02:00
|
|
|
void KMLParser::extendedData(SegmentData &segment)
|
2017-10-13 08:15:54 +02:00
|
|
|
{
|
|
|
|
while (_reader.readNextStartElement()) {
|
2018-08-27 22:25:55 +02:00
|
|
|
if (_reader.name() == QLatin1String("SchemaData"))
|
2022-09-26 22:50:40 +02:00
|
|
|
schemaData(segment);
|
2017-10-13 08:15:54 +02:00
|
|
|
else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-11 23:28:08 +01:00
|
|
|
void KMLParser::track(SegmentData &segment)
|
2016-10-28 19:12:40 +02:00
|
|
|
{
|
2017-10-13 08:15:54 +02:00
|
|
|
const char error[] = "gx:coord/when element count mismatch";
|
2022-09-26 22:50:40 +02:00
|
|
|
int i = 0;
|
2023-01-25 09:01:20 +01:00
|
|
|
bool empty = false;
|
2016-10-28 19:12:40 +02:00
|
|
|
|
|
|
|
while (_reader.readNextStartElement()) {
|
2018-08-27 22:25:55 +02:00
|
|
|
if (_reader.name() == QLatin1String("when")) {
|
2019-02-11 23:28:08 +01:00
|
|
|
segment.append(Trackpoint());
|
|
|
|
segment.last().setTimestamp(time());
|
2023-02-15 01:39:33 +01:00
|
|
|
if (_reader.error())
|
|
|
|
return;
|
2018-08-27 22:25:55 +02:00
|
|
|
} else if (_reader.name() == QLatin1String("coord")) {
|
2019-02-11 23:28:08 +01:00
|
|
|
if (i == segment.size()) {
|
2017-10-13 08:15:54 +02:00
|
|
|
_reader.raiseError(error);
|
2016-10-28 19:12:40 +02:00
|
|
|
return;
|
2019-02-11 23:28:08 +01:00
|
|
|
} else if (!coord(segment[i])) {
|
2016-10-29 12:22:28 +02:00
|
|
|
_reader.raiseError("Invalid coordinates");
|
2016-10-28 19:12:40 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-01-25 09:01:20 +01:00
|
|
|
if (segment.at(i).coordinates().isNull())
|
|
|
|
empty = true;
|
2016-10-28 19:12:40 +02:00
|
|
|
i++;
|
2018-08-27 22:25:55 +02:00
|
|
|
} else if (_reader.name() == QLatin1String("ExtendedData"))
|
2022-09-26 22:50:40 +02:00
|
|
|
extendedData(segment);
|
2017-10-13 08:15:54 +02:00
|
|
|
else
|
2016-10-28 19:12:40 +02:00
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
|
2022-09-26 22:50:40 +02:00
|
|
|
if (i != segment.size()) {
|
2017-10-13 08:15:54 +02:00
|
|
|
_reader.raiseError(error);
|
2022-09-26 22:50:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-25 09:01:20 +01:00
|
|
|
/* empty (invalid) coordinates are allowed per KML specification, but
|
|
|
|
invalid in our data representation so get rid of the segment entries */
|
|
|
|
if (empty) {
|
|
|
|
SegmentData filtered;
|
|
|
|
for (int i = 0; i < segment.size(); i++)
|
|
|
|
if (!segment.at(i).coordinates().isNull())
|
|
|
|
filtered.append(segment.at(i));
|
|
|
|
segment = filtered;
|
|
|
|
}
|
2016-10-28 19:12:40 +02:00
|
|
|
}
|
|
|
|
|
2017-10-11 22:41:01 +02:00
|
|
|
void KMLParser::multiTrack(TrackData &t)
|
|
|
|
{
|
|
|
|
while (_reader.readNextStartElement()) {
|
2019-02-11 23:28:08 +01:00
|
|
|
if (_reader.name() == QLatin1String("Track")) {
|
|
|
|
t.append(SegmentData());
|
|
|
|
track(t.last());
|
|
|
|
} else
|
2017-10-11 22:41:01 +02:00
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 08:36:58 +02:00
|
|
|
void KMLParser::photoOverlay(const Ctx &ctx, QVector<Waypoint> &waypoints,
|
2022-10-03 00:36:38 +02:00
|
|
|
PointStyleMap &pointStyles, QMap<QString, QString> &map)
|
2022-09-02 08:36:58 +02:00
|
|
|
{
|
2022-09-02 22:53:38 +02:00
|
|
|
QString img, id;
|
|
|
|
Waypoint w;
|
2022-09-23 02:34:18 +02:00
|
|
|
QMap<QString, PolygonStyle> unused;
|
|
|
|
QMap<QString, LineStyle> unused2;
|
2022-09-02 22:53:38 +02:00
|
|
|
static QRegularExpression re("\\$\\[[^\\]]+\\]");
|
2022-09-02 08:36:58 +02:00
|
|
|
|
|
|
|
while (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == QLatin1String("name"))
|
2022-09-02 22:53:38 +02:00
|
|
|
w.setName(_reader.readElementText());
|
2022-09-02 08:36:58 +02:00
|
|
|
else if (_reader.name() == QLatin1String("description"))
|
2022-09-02 22:53:38 +02:00
|
|
|
w.setDescription(_reader.readElementText());
|
2022-09-02 08:36:58 +02:00
|
|
|
else if (_reader.name() == QLatin1String("phoneNumber"))
|
2022-09-02 22:53:38 +02:00
|
|
|
w.setPhone(_reader.readElementText());
|
2022-09-02 08:36:58 +02:00
|
|
|
else if (_reader.name() == QLatin1String("address"))
|
2022-09-02 22:53:38 +02:00
|
|
|
w.setAddress(_reader.readElementText());
|
2022-09-02 08:36:58 +02:00
|
|
|
else if (_reader.name() == QLatin1String("TimeStamp"))
|
2022-09-02 22:53:38 +02:00
|
|
|
w.setTimestamp(timeStamp());
|
|
|
|
else if (_reader.name() == QLatin1String("Style")) {
|
2022-09-23 02:34:18 +02:00
|
|
|
style(ctx.dir, pointStyles, unused, unused2);
|
2022-09-02 22:53:38 +02:00
|
|
|
id = QString();
|
|
|
|
} else if (_reader.name() == QLatin1String("StyleMap"))
|
2022-10-03 00:36:38 +02:00
|
|
|
styleMap(map);
|
2022-09-02 22:53:38 +02:00
|
|
|
else if (_reader.name() == QLatin1String("Icon"))
|
|
|
|
img = icon();
|
|
|
|
else if (_reader.name() == QLatin1String("Point"))
|
2022-09-02 08:36:58 +02:00
|
|
|
point(w);
|
2022-09-02 22:53:38 +02:00
|
|
|
else if (_reader.name() == QLatin1String("styleUrl"))
|
|
|
|
id = styleUrl();
|
|
|
|
else
|
2022-09-02 08:36:58 +02:00
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
2022-09-02 22:53:38 +02:00
|
|
|
|
|
|
|
if (!w.coordinates().isNull()) {
|
2022-10-03 00:36:38 +02:00
|
|
|
PointStyleMap::iterator pit = pointStyles.find(id);
|
|
|
|
if (pit == pointStyles.end())
|
|
|
|
pit = pointStyles.find(map.value(id));
|
|
|
|
w.setStyle(pit == pointStyles.end()
|
|
|
|
? PointStyle(QColor(0x55, 0x55, 0x55)) : *pit);
|
2022-09-02 22:53:38 +02:00
|
|
|
|
|
|
|
img.replace(re, "0");
|
|
|
|
if (!QUrl(img).scheme().isEmpty())
|
|
|
|
w.addLink(Link(img, "Photo Overlay"));
|
|
|
|
else if (ctx.zip && Util::tempDir().isValid()) {
|
|
|
|
QFileInfo fi(img);
|
|
|
|
QByteArray id(ctx.path.toUtf8() + img.toUtf8());
|
|
|
|
QString path(Util::tempDir().path() + "/" + QString("%0.%1")
|
|
|
|
.arg(QCryptographicHash::hash(id, QCryptographicHash::Sha1)
|
|
|
|
.toHex(), QString(fi.suffix())));
|
|
|
|
QFile::rename(ctx.dir.absoluteFilePath(img), path);
|
|
|
|
w.addImage(path);
|
|
|
|
} else if (!ctx.zip)
|
|
|
|
w.addImage(ctx.dir.absoluteFilePath(img));
|
|
|
|
|
|
|
|
waypoints.append(w);
|
|
|
|
}
|
2022-09-02 08:36:58 +02:00
|
|
|
}
|
|
|
|
|
2022-09-24 13:20:58 +02:00
|
|
|
void KMLParser::multiGeometry(QList<TrackData> &tracks, QList<Area> &areas,
|
|
|
|
QVector<Waypoint> &waypoints)
|
|
|
|
{
|
|
|
|
TrackData *tp = 0;
|
|
|
|
Area *ap = 0;
|
|
|
|
|
|
|
|
while (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == QLatin1String("Point")) {
|
|
|
|
waypoints.append(Waypoint());
|
|
|
|
Waypoint &w = waypoints.last();
|
|
|
|
point(w);
|
|
|
|
} else if (_reader.name() == QLatin1String("LineString")) {
|
|
|
|
if (!tp) {
|
|
|
|
tracks.append(TrackData());
|
|
|
|
tp = &tracks.last();
|
|
|
|
}
|
|
|
|
tp->append(SegmentData());
|
|
|
|
lineString(tp->last());
|
|
|
|
} else if (_reader.name() == QLatin1String("Polygon")) {
|
|
|
|
if (!ap) {
|
|
|
|
areas.append(Area());
|
|
|
|
ap = &areas.last();
|
|
|
|
}
|
|
|
|
polygon(*ap);
|
|
|
|
} else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 08:36:58 +02:00
|
|
|
void KMLParser::placemark(const Ctx &ctx, QList<TrackData> &tracks,
|
2022-09-23 02:34:18 +02:00
|
|
|
QList<Area> &areas, QVector<Waypoint> &waypoints, PointStyleMap &pointStyles,
|
2022-10-03 00:36:38 +02:00
|
|
|
PolygonStyleMap &polyStyles, LineStyleMap &lineStyles,
|
|
|
|
QMap<QString, QString> &map)
|
2016-10-24 20:00:29 +02:00
|
|
|
{
|
2022-09-01 00:28:00 +02:00
|
|
|
QString name, desc, phone, address, id;
|
2016-10-28 14:33:36 +02:00
|
|
|
QDateTime timestamp;
|
2022-09-24 13:20:58 +02:00
|
|
|
int trkIdx = tracks.size();
|
|
|
|
int wptIdx = waypoints.size();
|
|
|
|
int areaIdx = areas.size();
|
2016-10-25 22:50:11 +02:00
|
|
|
|
2016-10-24 20:00:29 +02:00
|
|
|
while (_reader.readNextStartElement()) {
|
2018-08-27 22:25:55 +02:00
|
|
|
if (_reader.name() == QLatin1String("name"))
|
2016-10-28 14:33:36 +02:00
|
|
|
name = _reader.readElementText();
|
2018-08-27 22:25:55 +02:00
|
|
|
else if (_reader.name() == QLatin1String("description"))
|
2016-10-28 14:33:36 +02:00
|
|
|
desc = _reader.readElementText();
|
2021-05-12 21:58:46 +02:00
|
|
|
else if (_reader.name() == QLatin1String("phoneNumber"))
|
|
|
|
phone = _reader.readElementText();
|
|
|
|
else if (_reader.name() == QLatin1String("address"))
|
|
|
|
address = _reader.readElementText();
|
2018-08-27 22:25:55 +02:00
|
|
|
else if (_reader.name() == QLatin1String("TimeStamp"))
|
2016-10-28 14:33:36 +02:00
|
|
|
timestamp = timeStamp();
|
2022-09-02 22:53:38 +02:00
|
|
|
else if (_reader.name() == QLatin1String("Style")) {
|
2022-09-23 02:34:18 +02:00
|
|
|
style(ctx.dir, pointStyles, polyStyles, lineStyles);
|
2022-09-02 22:53:38 +02:00
|
|
|
id = QString();
|
|
|
|
} else if (_reader.name() == QLatin1String("StyleMap"))
|
2022-10-03 00:36:38 +02:00
|
|
|
styleMap(map);
|
2018-08-27 22:25:55 +02:00
|
|
|
else if (_reader.name() == QLatin1String("MultiGeometry"))
|
2022-09-24 13:20:58 +02:00
|
|
|
multiGeometry(tracks, areas, waypoints);
|
2018-08-27 22:25:55 +02:00
|
|
|
else if (_reader.name() == QLatin1String("Point")) {
|
2017-07-27 19:47:46 +02:00
|
|
|
waypoints.append(Waypoint());
|
2022-09-24 13:20:58 +02:00
|
|
|
point(waypoints.last());
|
2018-08-27 22:25:55 +02:00
|
|
|
} else if (_reader.name() == QLatin1String("LineString")
|
|
|
|
|| _reader.name() == QLatin1String("LinearRing")) {
|
2017-07-27 19:47:46 +02:00
|
|
|
tracks.append(TrackData());
|
2022-09-24 13:20:58 +02:00
|
|
|
tracks.last().append(SegmentData());
|
|
|
|
lineString(tracks.last().last());
|
2018-08-27 22:25:55 +02:00
|
|
|
} else if (_reader.name() == QLatin1String("Track")) {
|
2017-07-27 19:47:46 +02:00
|
|
|
tracks.append(TrackData());
|
2022-09-24 13:20:58 +02:00
|
|
|
tracks.last().append(SegmentData());
|
|
|
|
track(tracks.last().last());
|
2018-08-27 22:25:55 +02:00
|
|
|
} else if (_reader.name() == QLatin1String("MultiTrack")) {
|
2017-10-11 22:41:01 +02:00
|
|
|
tracks.append(TrackData());
|
2022-09-24 13:20:58 +02:00
|
|
|
multiTrack(tracks.last());
|
2019-01-31 01:46:53 +01:00
|
|
|
} else if (_reader.name() == QLatin1String("Polygon")) {
|
|
|
|
areas.append(Area());
|
2022-09-24 13:20:58 +02:00
|
|
|
polygon(areas.last());
|
2022-09-02 22:53:38 +02:00
|
|
|
} else if (_reader.name() == QLatin1String("styleUrl"))
|
|
|
|
id = styleUrl();
|
|
|
|
else
|
2016-10-25 22:50:11 +02:00
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
2022-09-02 22:53:38 +02:00
|
|
|
|
2022-09-24 13:20:58 +02:00
|
|
|
PointStyleMap::iterator pit = pointStyles.find(id);
|
2022-10-03 00:36:38 +02:00
|
|
|
if (pit == pointStyles.end())
|
|
|
|
pit = pointStyles.find(map.value(id));
|
2022-09-24 13:20:58 +02:00
|
|
|
LineStyleMap::iterator lit = lineStyles.find(id);
|
2022-10-03 00:36:38 +02:00
|
|
|
if (lit == lineStyles.end())
|
|
|
|
lit = lineStyles.find(map.value(id));
|
2022-09-24 13:20:58 +02:00
|
|
|
PolygonStyleMap::iterator ait = polyStyles.find(id);
|
2022-10-03 00:36:38 +02:00
|
|
|
if (ait == polyStyles.end())
|
|
|
|
ait = polyStyles.find(map.value(id));
|
2022-09-24 13:20:58 +02:00
|
|
|
|
|
|
|
for (int i = wptIdx; i < waypoints.size(); i++) {
|
|
|
|
Waypoint &w = waypoints[i];
|
|
|
|
w.setName(name);
|
|
|
|
w.setDescription(desc);
|
|
|
|
w.setTimestamp(timestamp);
|
|
|
|
w.setAddress(address);
|
|
|
|
w.setPhone(phone);
|
|
|
|
w.setStyle(pit == pointStyles.end()
|
|
|
|
? PointStyle(QColor(0x55, 0x55, 0x55)) : *pit);
|
|
|
|
}
|
|
|
|
for (int i = trkIdx; i < tracks.size(); i++) {
|
|
|
|
TrackData &t = tracks[i];
|
|
|
|
t.setName(name);
|
|
|
|
t.setDescription(desc);
|
|
|
|
t.setStyle(lit == lineStyles.end()
|
2022-10-07 21:54:39 +02:00
|
|
|
? LineStyle(QColor(0x55, 0x55, 0x55), 2, Qt::SolidLine) : *lit);
|
2022-09-24 13:20:58 +02:00
|
|
|
}
|
|
|
|
for (int i = areaIdx; i < areas.size(); i++) {
|
|
|
|
Area &a = areas[i];
|
|
|
|
a.setName(name);
|
|
|
|
a.setDescription(desc);
|
|
|
|
a.setStyle(ait == polyStyles.end()
|
2022-09-23 21:36:02 +02:00
|
|
|
? PolygonStyle(QColor(0x55, 0x55, 0x55, 0x99),
|
2022-09-24 13:20:58 +02:00
|
|
|
QColor(0x55, 0x55, 0x55, 0x99), 2) : *ait);
|
2022-09-02 22:53:38 +02:00
|
|
|
}
|
2016-10-25 22:50:11 +02:00
|
|
|
}
|
|
|
|
|
2022-09-02 08:36:58 +02:00
|
|
|
QString KMLParser::icon()
|
2016-10-25 22:50:11 +02:00
|
|
|
{
|
2022-09-01 00:28:00 +02:00
|
|
|
QString path;
|
|
|
|
|
2016-10-25 22:50:11 +02:00
|
|
|
while (_reader.readNextStartElement()) {
|
2022-09-01 00:28:00 +02:00
|
|
|
if (_reader.name() == QLatin1String("href"))
|
|
|
|
path = _reader.readElementText();
|
|
|
|
else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
|
2022-09-02 08:36:58 +02:00
|
|
|
return path;
|
2022-09-01 00:28:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-23 21:36:02 +02:00
|
|
|
QColor KMLParser::color()
|
|
|
|
{
|
|
|
|
QString str(_reader.readElementText());
|
|
|
|
if (str.size() != 8)
|
|
|
|
return QColor();
|
|
|
|
|
|
|
|
bool aok, bok, gok, rok;
|
2022-09-23 23:57:58 +02:00
|
|
|
int a = str.mid(0, 2).toInt(&aok, 16);
|
|
|
|
int b = str.mid(2, 2).toInt(&bok, 16);
|
|
|
|
int g = str.mid(4, 2).toInt(&gok, 16);
|
|
|
|
int r = str.mid(6, 2).toInt(&rok, 16);
|
2022-09-23 21:36:02 +02:00
|
|
|
|
|
|
|
return (aok && bok && gok && rok) ? QColor(r, g, b, a) : QColor();
|
|
|
|
}
|
|
|
|
|
2022-09-02 22:53:38 +02:00
|
|
|
QString KMLParser::styleUrl()
|
|
|
|
{
|
|
|
|
QString id(_reader.readElementText());
|
|
|
|
return (id.at(0) == '#') ? id.right(id.size() - 1) : QString();
|
|
|
|
}
|
|
|
|
|
2022-09-01 00:28:00 +02:00
|
|
|
void KMLParser::iconStyle(const QDir &dir, const QString &id,
|
2022-09-23 02:34:18 +02:00
|
|
|
PointStyleMap &styles)
|
2022-09-01 00:28:00 +02:00
|
|
|
{
|
2022-09-23 02:34:18 +02:00
|
|
|
QPixmap img;
|
2022-09-23 21:36:02 +02:00
|
|
|
QColor c(0x55, 0x55, 0x55);
|
2022-09-23 02:34:18 +02:00
|
|
|
|
2022-09-01 00:28:00 +02:00
|
|
|
while (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == QLatin1String("Icon"))
|
2022-09-23 02:34:18 +02:00
|
|
|
img = QPixmap(dir.absoluteFilePath(icon()));
|
|
|
|
else if (_reader.name() == QLatin1String("color"))
|
2022-09-23 21:36:02 +02:00
|
|
|
c = color();
|
2022-09-01 00:28:00 +02:00
|
|
|
else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
2022-09-23 02:34:18 +02:00
|
|
|
|
2022-09-23 21:36:02 +02:00
|
|
|
styles.insert(id, PointStyle(img, c));
|
2022-09-01 00:28:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-23 02:34:18 +02:00
|
|
|
void KMLParser::polyStyle(const QString &id, PolygonStyleMap &styles)
|
|
|
|
{
|
2022-09-23 21:36:02 +02:00
|
|
|
QColor c(0x55, 0x55, 0x55, 0x99);
|
2022-09-23 02:34:18 +02:00
|
|
|
uint fill = 1, outline = 1;
|
|
|
|
|
|
|
|
while (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == QLatin1String("color"))
|
2022-09-23 21:36:02 +02:00
|
|
|
c = color();
|
2022-09-23 02:34:18 +02:00
|
|
|
else if (_reader.name() == QLatin1String("fill"))
|
|
|
|
fill = _reader.readElementText().toUInt();
|
|
|
|
else if (_reader.name() == QLatin1String("outline"))
|
|
|
|
outline = _reader.readElementText().toUInt();
|
|
|
|
else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
|
2022-09-23 21:36:02 +02:00
|
|
|
styles.insert(id, PolygonStyle(fill ? c : QColor(),
|
|
|
|
outline ? c : QColor(), 2));
|
2022-09-23 02:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void KMLParser::lineStyle(const QString &id, LineStyleMap &styles)
|
|
|
|
{
|
2022-09-23 21:36:02 +02:00
|
|
|
QColor c(0x55, 0x55, 0x55);
|
|
|
|
double width = 2;
|
2022-09-23 02:34:18 +02:00
|
|
|
|
|
|
|
while (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == QLatin1String("color"))
|
2022-09-23 21:36:02 +02:00
|
|
|
c = color();
|
2022-09-23 02:34:18 +02:00
|
|
|
else if (_reader.name() == QLatin1String("width"))
|
2022-09-23 21:36:02 +02:00
|
|
|
width = _reader.readElementText().toDouble();
|
2022-09-23 02:34:18 +02:00
|
|
|
else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
|
2022-10-07 21:54:39 +02:00
|
|
|
styles.insert(id, LineStyle(c, width, Qt::SolidLine));
|
2022-09-23 02:34:18 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 00:36:38 +02:00
|
|
|
void KMLParser::styleMapPair(const QString &id, QMap<QString, QString> &map)
|
2022-09-02 22:53:38 +02:00
|
|
|
{
|
|
|
|
QString key, url;
|
|
|
|
|
|
|
|
while (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == QLatin1String("key"))
|
|
|
|
key = _reader.readElementText();
|
|
|
|
else if (_reader.name() == QLatin1String("styleUrl"))
|
|
|
|
url = styleUrl();
|
|
|
|
else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
|
2022-10-03 00:36:38 +02:00
|
|
|
if (key == "normal")
|
|
|
|
map.insert(id, url);
|
2022-09-02 22:53:38 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 00:36:38 +02:00
|
|
|
void KMLParser::styleMap(QMap<QString, QString> &map)
|
2022-09-02 22:53:38 +02:00
|
|
|
{
|
|
|
|
QString id = _reader.attributes().value("id").toString();
|
|
|
|
|
|
|
|
while (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == QLatin1String("Pair"))
|
2022-10-03 00:36:38 +02:00
|
|
|
styleMapPair(id, map);
|
2022-09-02 22:53:38 +02:00
|
|
|
else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 02:34:18 +02:00
|
|
|
void KMLParser::style(const QDir &dir, PointStyleMap &pointStyles,
|
|
|
|
PolygonStyleMap &polyStyles, LineStyleMap &lineStyles)
|
2022-09-01 00:28:00 +02:00
|
|
|
{
|
|
|
|
QString id = _reader.attributes().value("id").toString();
|
|
|
|
|
|
|
|
while (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == QLatin1String("IconStyle"))
|
2022-09-23 02:34:18 +02:00
|
|
|
iconStyle(dir, id, pointStyles);
|
|
|
|
else if (_reader.name() == QLatin1String("PolyStyle"))
|
|
|
|
polyStyle(id, polyStyles);
|
|
|
|
else if (_reader.name() == QLatin1String("LineStyle"))
|
|
|
|
lineStyle(id, lineStyles);
|
2022-09-01 00:28:00 +02:00
|
|
|
else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 08:36:58 +02:00
|
|
|
void KMLParser::folder(const Ctx &ctx, QList<TrackData> &tracks,
|
2022-09-01 00:28:00 +02:00
|
|
|
QList<Area> &areas, QVector<Waypoint> &waypoints,
|
2022-09-23 02:34:18 +02:00
|
|
|
PointStyleMap &pointStyles, PolygonStyleMap &polyStyles,
|
2022-10-03 00:36:38 +02:00
|
|
|
LineStyleMap &lineStyles, QMap<QString, QString> &map)
|
2022-09-01 00:28:00 +02:00
|
|
|
{
|
|
|
|
while (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == QLatin1String("Document"))
|
2022-09-02 08:36:58 +02:00
|
|
|
document(ctx, tracks, areas, waypoints);
|
2018-08-27 22:25:55 +02:00
|
|
|
else if (_reader.name() == QLatin1String("Folder"))
|
2022-09-23 02:34:18 +02:00
|
|
|
folder(ctx, tracks, areas, waypoints, pointStyles, polyStyles,
|
2022-10-03 00:36:38 +02:00
|
|
|
lineStyles, map);
|
2022-09-02 08:36:58 +02:00
|
|
|
else if (_reader.name() == QLatin1String("Placemark"))
|
2022-09-23 02:34:18 +02:00
|
|
|
placemark(ctx, tracks, areas, waypoints, pointStyles, polyStyles,
|
2022-10-03 00:36:38 +02:00
|
|
|
lineStyles, map);
|
2022-09-02 08:36:58 +02:00
|
|
|
else if (_reader.name() == QLatin1String("PhotoOverlay"))
|
2022-10-03 00:36:38 +02:00
|
|
|
photoOverlay(ctx, waypoints, pointStyles, map);
|
2016-10-24 20:00:29 +02:00
|
|
|
else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 08:36:58 +02:00
|
|
|
void KMLParser::document(const Ctx &ctx, QList<TrackData> &tracks,
|
2022-09-01 00:49:21 +02:00
|
|
|
QList<Area> &areas, QVector<Waypoint> &waypoints)
|
2016-10-24 20:00:29 +02:00
|
|
|
{
|
2022-09-23 02:34:18 +02:00
|
|
|
PointStyleMap pointStyles;
|
|
|
|
PolygonStyleMap polyStyles;
|
|
|
|
LineStyleMap lineStyles;
|
2022-10-03 00:36:38 +02:00
|
|
|
QMap<QString, QString> map;
|
2022-09-01 00:49:21 +02:00
|
|
|
|
2016-10-24 20:00:29 +02:00
|
|
|
while (_reader.readNextStartElement()) {
|
2020-05-30 13:59:34 +02:00
|
|
|
if (_reader.name() == QLatin1String("Document"))
|
2022-09-02 08:36:58 +02:00
|
|
|
document(ctx, tracks, areas, waypoints);
|
2018-08-27 22:25:55 +02:00
|
|
|
else if (_reader.name() == QLatin1String("Folder"))
|
2022-09-23 02:34:18 +02:00
|
|
|
folder(ctx, tracks, areas, waypoints, pointStyles, polyStyles,
|
2022-10-03 00:36:38 +02:00
|
|
|
lineStyles, map);
|
2022-09-02 08:36:58 +02:00
|
|
|
else if (_reader.name() == QLatin1String("Placemark"))
|
2022-09-23 02:34:18 +02:00
|
|
|
placemark(ctx, tracks, areas, waypoints, pointStyles, polyStyles,
|
2022-10-03 00:36:38 +02:00
|
|
|
lineStyles, map);
|
2022-09-02 08:36:58 +02:00
|
|
|
else if (_reader.name() == QLatin1String("PhotoOverlay"))
|
2022-10-03 00:36:38 +02:00
|
|
|
photoOverlay(ctx, waypoints, pointStyles, map);
|
2022-09-01 00:28:00 +02:00
|
|
|
else if (_reader.name() == QLatin1String("Style"))
|
2022-09-23 02:34:18 +02:00
|
|
|
style(ctx.dir, pointStyles, polyStyles, lineStyles);
|
2022-09-02 22:53:38 +02:00
|
|
|
else if (_reader.name() == QLatin1String("StyleMap"))
|
2022-10-03 00:36:38 +02:00
|
|
|
styleMap(map);
|
2016-10-24 20:00:29 +02:00
|
|
|
else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 08:36:58 +02:00
|
|
|
void KMLParser::kml(const Ctx &ctx, QList<TrackData> &tracks,
|
2022-09-01 00:28:00 +02:00
|
|
|
QList<Area> &areas, QVector<Waypoint> &waypoints)
|
2016-10-24 20:00:29 +02:00
|
|
|
{
|
2022-09-23 02:34:18 +02:00
|
|
|
PointStyleMap pointStyles;
|
|
|
|
PolygonStyleMap polyStyles;
|
|
|
|
LineStyleMap lineStyles;
|
2022-10-03 00:36:38 +02:00
|
|
|
QMap<QString, QString> map;
|
2022-09-02 08:36:58 +02:00
|
|
|
|
2016-10-24 20:00:29 +02:00
|
|
|
while (_reader.readNextStartElement()) {
|
2018-08-27 22:25:55 +02:00
|
|
|
if (_reader.name() == QLatin1String("Document"))
|
2022-09-02 08:36:58 +02:00
|
|
|
document(ctx, tracks, areas, waypoints);
|
2018-08-27 22:25:55 +02:00
|
|
|
else if (_reader.name() == QLatin1String("Folder"))
|
2022-09-23 02:34:18 +02:00
|
|
|
folder(ctx, tracks, areas, waypoints, pointStyles, polyStyles,
|
2022-10-03 00:36:38 +02:00
|
|
|
lineStyles, map);
|
2022-09-02 08:36:58 +02:00
|
|
|
else if (_reader.name() == QLatin1String("Placemark"))
|
2022-09-23 02:34:18 +02:00
|
|
|
placemark(ctx, tracks, areas, waypoints, pointStyles, polyStyles,
|
2022-10-03 00:36:38 +02:00
|
|
|
lineStyles, map);
|
2022-09-02 08:36:58 +02:00
|
|
|
else if (_reader.name() == QLatin1String("PhotoOverlay"))
|
2022-10-03 00:36:38 +02:00
|
|
|
photoOverlay(ctx, waypoints, pointStyles, map);
|
2016-10-24 20:00:29 +02:00
|
|
|
else
|
|
|
|
_reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-27 19:47:46 +02:00
|
|
|
bool KMLParser::parse(QFile *file, QList<TrackData> &tracks,
|
2019-01-31 01:46:53 +01:00
|
|
|
QList<RouteData> &routes, QList<Area> &areas, QVector<Waypoint> &waypoints)
|
2016-10-24 20:00:29 +02:00
|
|
|
{
|
2017-07-27 19:47:46 +02:00
|
|
|
Q_UNUSED(routes);
|
2022-09-01 00:28:00 +02:00
|
|
|
QFileInfo fi(*file);
|
2017-07-27 19:47:46 +02:00
|
|
|
|
|
|
|
_reader.clear();
|
|
|
|
|
2022-09-02 08:36:58 +02:00
|
|
|
if (isZIP(file)) {
|
2022-09-01 00:28:00 +02:00
|
|
|
QZipReader zip(fi.absoluteFilePath(), QIODevice::ReadOnly);
|
|
|
|
QTemporaryDir tempDir;
|
2022-09-02 08:36:58 +02:00
|
|
|
if (!tempDir.isValid() || !zip.extractAll(tempDir.path()))
|
2022-09-01 00:28:00 +02:00
|
|
|
_reader.raiseError("Error extracting ZIP file");
|
|
|
|
else {
|
|
|
|
QDir zipDir(tempDir.path());
|
|
|
|
QFileInfoList files(zipDir.entryInfoList(QStringList("*.kml"),
|
|
|
|
QDir::Files));
|
|
|
|
|
|
|
|
if (files.isEmpty())
|
|
|
|
_reader.raiseError("No KML file found in ZIP file");
|
|
|
|
else {
|
|
|
|
QFile kmlFile(files.first().absoluteFilePath());
|
|
|
|
if (!kmlFile.open(QIODevice::ReadOnly))
|
|
|
|
_reader.raiseError("Error opening KML file");
|
|
|
|
else {
|
|
|
|
_reader.setDevice(&kmlFile);
|
|
|
|
|
|
|
|
if (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == QLatin1String("kml"))
|
2022-09-02 08:36:58 +02:00
|
|
|
kml(Ctx(fi.absoluteFilePath(), zipDir, true),
|
|
|
|
tracks, areas, waypoints);
|
2022-09-01 00:28:00 +02:00
|
|
|
else
|
|
|
|
_reader.raiseError("Not a KML file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
file->reset();
|
|
|
|
_reader.setDevice(file);
|
|
|
|
|
|
|
|
if (_reader.readNextStartElement()) {
|
|
|
|
if (_reader.name() == QLatin1String("kml"))
|
2022-09-02 08:36:58 +02:00
|
|
|
kml(Ctx(fi.absoluteFilePath(), fi.absoluteDir(), false), tracks,
|
|
|
|
areas, waypoints);
|
2022-09-01 00:28:00 +02:00
|
|
|
else
|
|
|
|
_reader.raiseError("Not a KML file");
|
|
|
|
}
|
2016-10-24 20:00:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return !_reader.error();
|
|
|
|
}
|