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

KML parser code cleanup

This commit is contained in:
Martin Tůma 2016-10-27 22:33:35 +02:00
parent 7f12c0ca95
commit 09c097cc68
2 changed files with 72 additions and 93 deletions

View File

@ -1,7 +1,29 @@
#include "kmlparser.h" #include "kmlparser.h"
bool KMLParser::pointCoordinates() qreal KMLParser::number()
{
bool res;
qreal ret = _reader.readElementText().toDouble(&res);
if (!res)
_reader.raiseError(QString("Invalid %1.").arg(
_reader.name().toString()));
return ret;
}
QDateTime KMLParser::time()
{
QDateTime d = QDateTime::fromString(_reader.readElementText(),
Qt::ISODate);
if (!d.isValid())
_reader.raiseError(QString("Invalid %1.").arg(
_reader.name().toString()));
return d;
}
bool KMLParser::pointCoordinates(Waypoint &waypoint)
{ {
QString data = _reader.readElementText(); QString data = _reader.readElementText();
const QChar *sp, *ep, *cp, *vp; const QChar *sp, *ep, *cp, *vp;
@ -30,12 +52,6 @@ bool KMLParser::pointCoordinates()
if (!res) if (!res)
return false; return false;
if (c == 2) {
Waypoint w(Coordinates(val[0], val[1]));
w.setElevation(val[2]);
_waypoints.append(w);
}
c++; c++;
vp = cp + 1; vp = cp + 1;
} else if (cp->isSpace() || cp->isNull()) { } else if (cp->isSpace() || cp->isNull()) {
@ -50,10 +66,9 @@ bool KMLParser::pointCoordinates()
if (!res) if (!res)
return false; return false;
Waypoint w(Coordinates(val[0], val[1])); waypoint.setCoordinates(Coordinates(val[0], val[1]));
if (c == 2) if (c == 2)
w.setElevation(val[2]); waypoint.setElevation(val[2]);
_waypoints.append(w);
while (cp->isSpace()) while (cp->isSpace())
cp++; cp++;
@ -64,7 +79,7 @@ bool KMLParser::pointCoordinates()
return true; return true;
} }
bool KMLParser::lineCoordinates() bool KMLParser::lineCoordinates(QVector<Waypoint> &route)
{ {
QString data = _reader.readElementText(); QString data = _reader.readElementText();
const QChar *sp, *ep, *cp, *vp; const QChar *sp, *ep, *cp, *vp;
@ -107,10 +122,9 @@ bool KMLParser::lineCoordinates()
if (!res) if (!res)
return false; return false;
Waypoint w(Coordinates(val[0], val[1])); route.append(Waypoint(Coordinates(val[0], val[1])));
if (c == 2) if (c == 2)
w.setElevation(val[2]); route.last().setElevation(val[2]);
_route->append(w);
while (cp->isSpace()) while (cp->isSpace())
cp++; cp++;
@ -122,87 +136,56 @@ bool KMLParser::lineCoordinates()
return true; return true;
} }
QDateTime KMLParser::timeStamp() void KMLParser::timeStamp(Waypoint &waypoint)
{
QDateTime date;
while (_reader.readNextStartElement()) {
if (_reader.name() == "when") {
date = QDateTime::fromString(_reader.readElementText(),
Qt::ISODate);
} else
_reader.skipCurrentElement();
}
return date;
}
void KMLParser::lineString()
{ {
while (_reader.readNextStartElement()) { while (_reader.readNextStartElement()) {
if (_reader.name() == "coordinates") { if (_reader.name() == "when")
_routes.append(QVector<Waypoint>()); waypoint.setTimestamp(time());
_route = &_routes.back();
if (!lineCoordinates())
_reader.raiseError("Invalid coordinates.");
} else
_reader.skipCurrentElement();
}
}
void KMLParser::point(const QString &name, const QString &desc,
const QDateTime timestamp)
{
while (_reader.readNextStartElement()) {
if (_reader.name() == "coordinates") {
if (!pointCoordinates())
_reader.raiseError("Invalid coordinates.");
else {
Waypoint &w = _waypoints.last();
if (!name.isNull())
w.setName(name);
if (!desc.isNull())
w.setDescription(desc);
if (!timestamp.isNull())
w.setTimestamp(timestamp);
}
} else
_reader.skipCurrentElement();
}
}
void KMLParser::multiGeometry(const QString &name, const QString &desc,
const QDateTime timestamp)
{
while (_reader.readNextStartElement()) {
if (_reader.name() == "Point")
point(name, desc, timestamp);
else if (_reader.name() == "LineString")
lineString();
else else
_reader.skipCurrentElement(); _reader.skipCurrentElement();
} }
} }
void KMLParser::lineString(QVector<Waypoint> &route)
{
while (_reader.readNextStartElement()) {
if (_reader.name() == "coordinates") {
if (!lineCoordinates(route))
_reader.raiseError("Invalid coordinates.");
} else
_reader.skipCurrentElement();
}
}
void KMLParser::point(Waypoint &waypoint)
{
while (_reader.readNextStartElement()) {
if (_reader.name() == "coordinates") {
if (!pointCoordinates(waypoint))
_reader.raiseError("Invalid coordinates.");
} else
_reader.skipCurrentElement();
}
}
void KMLParser::placemark() void KMLParser::placemark()
{ {
QString name, desc; Waypoint waypoint;
QDateTime date;
while (_reader.readNextStartElement()) { while (_reader.readNextStartElement()) {
if (_reader.name() == "name") if (_reader.name() == "name")
name = _reader.readElementText(); waypoint.setName(_reader.readElementText());
else if (_reader.name() == "description") else if (_reader.name() == "description")
desc = _reader.readElementText(); waypoint.setDescription(_reader.readElementText());
else if (_reader.name() == "TimeStamp") else if (_reader.name() == "TimeStamp")
date = timeStamp(); timeStamp(waypoint);
else if (_reader.name() == "Point") else if (_reader.name() == "Point") {
point(name, desc, date); _waypoints.append(waypoint);
else if (_reader.name() == "LineString") point(_waypoints.last());
lineString(); } else if (_reader.name() == "LineString") {
else if (_reader.name() == "MultiGeometry") _routes.append(QVector<Waypoint>());
multiGeometry(name, desc, date); lineString(_routes.back());
else } else
_reader.skipCurrentElement(); _reader.skipCurrentElement();
} }
} }

View File

@ -2,7 +2,6 @@
#define KMLPARSER_H #define KMLPARSER_H
#include <QXmlStreamReader> #include <QXmlStreamReader>
#include <QVector>
#include <QDateTime> #include <QDateTime>
#include "parser.h" #include "parser.h"
@ -11,7 +10,7 @@ class KMLParser : public Parser
public: public:
KMLParser(QList<QVector<Trackpoint> > &tracks, KMLParser(QList<QVector<Trackpoint> > &tracks,
QList<QVector<Waypoint> > &routes, QList<Waypoint> &waypoints) QList<QVector<Waypoint> > &routes, QList<Waypoint> &waypoints)
: Parser(tracks, routes, waypoints) {_track = 0; _route = 0;} : Parser(tracks, routes, waypoints) {}
~KMLParser() {} ~KMLParser() {}
bool loadFile(QIODevice *device); bool loadFile(QIODevice *device);
@ -24,18 +23,15 @@ private:
void document(); void document();
void folder(); void folder();
void placemark(); void placemark();
void multiGeometry(const QString &name, const QString &desc, void lineString(QVector<Waypoint> &route);
const QDateTime timestamp); void point(Waypoint &waypoint);
void lineString(); bool pointCoordinates(Waypoint &waypoint);
void point(const QString &name, const QString &desc, bool lineCoordinates(QVector<Waypoint> &route);
const QDateTime timestamp); void timeStamp(Waypoint &waypoint);
bool pointCoordinates(); qreal number();
bool lineCoordinates(); QDateTime time();
QDateTime timeStamp();
QXmlStreamReader _reader; QXmlStreamReader _reader;
QVector<Trackpoint> *_track;
QVector<Waypoint> *_route;
}; };
#endif // KMLPARSER_H #endif // KMLPARSER_H