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

Added support for KML gx:Track

This commit is contained in:
Martin Tůma 2016-10-28 19:12:40 +02:00
parent 10b903c129
commit 5693375a3f
2 changed files with 84 additions and 0 deletions

View File

@ -23,6 +23,52 @@ QDateTime KMLParser::time()
return d; return d;
} }
bool KMLParser::coord(Trackpoint &trackpoint)
{
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->isSpace() || cp->isNull()) {
if (c > 2)
return false;
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
val[c] = QString(vp, cp - vp).toDouble(&res);
#else // QT_VERSION < 5
val[c] = QStringRef(&data, vp - sp, cp - vp).toDouble(&res);
#endif // QT_VERSION < 5
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;
}
bool KMLParser::pointCoordinates(Waypoint &waypoint) bool KMLParser::pointCoordinates(Waypoint &waypoint)
{ {
QString data = _reader.readElementText(); QString data = _reader.readElementText();
@ -67,6 +113,8 @@ bool KMLParser::pointCoordinates(Waypoint &waypoint)
return false; return false;
waypoint.setCoordinates(Coordinates(val[0], val[1])); waypoint.setCoordinates(Coordinates(val[0], val[1]));
if (!waypoint.coordinates().isValid())
return false;
if (c == 2) if (c == 2)
waypoint.setElevation(val[2]); waypoint.setElevation(val[2]);
@ -123,6 +171,8 @@ bool KMLParser::lineCoordinates(TrackData &track)
return false; return false;
track.append(Trackpoint(Coordinates(val[0], val[1]))); track.append(Trackpoint(Coordinates(val[0], val[1])));
if (!track.last().coordinates().isValid())
return false;
if (c == 2) if (c == 2)
track.last().setElevation(val[2]); track.last().setElevation(val[2]);
@ -172,6 +222,32 @@ void KMLParser::point(Waypoint &waypoint)
} }
} }
void KMLParser::Track(TrackData &track)
{
const char mismatchError[] = "gx:coord/when element count mismatch.";
int i = track.size();
while (_reader.readNextStartElement()) {
if (_reader.name() == "when") {
track.append(Trackpoint());
track.last().setTimestamp(time());
} else if (_reader.name() == "coord") {
if (i == track.size()) {
_reader.raiseError(mismatchError);
return;
} else if (!coord(track[i])) {
_reader.raiseError("Invalid coordinates.");
return;
}
i++;
} else
_reader.skipCurrentElement();
}
if (i != track.size())
_reader.raiseError(mismatchError);
}
void KMLParser::placemark() void KMLParser::placemark()
{ {
QString name, desc; QString name, desc;
@ -197,6 +273,12 @@ void KMLParser::placemark()
track.setName(name); track.setName(name);
track.setDescription(desc); track.setDescription(desc);
lineString(track); lineString(track);
} else if (_reader.name() == "Track") {
_tracks.append(TrackData());
TrackData &track = _tracks.last();
track.setName(name);
track.setDescription(desc);
Track(track);
} else } else
_reader.skipCurrentElement(); _reader.skipCurrentElement();
} }

View File

@ -22,10 +22,12 @@ private:
void document(); void document();
void folder(); void folder();
void placemark(); void placemark();
void Track(TrackData &track);
void lineString(TrackData &track); void lineString(TrackData &track);
void point(Waypoint &waypoint); void point(Waypoint &waypoint);
bool pointCoordinates(Waypoint &waypoint); bool pointCoordinates(Waypoint &waypoint);
bool lineCoordinates(TrackData &track); bool lineCoordinates(TrackData &track);
bool coord(Trackpoint &trackpoint);
QDateTime timeStamp(); QDateTime timeStamp();
qreal number(); qreal number();
QDateTime time(); QDateTime time();