-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpsgpx.cpp
75 lines (72 loc) · 2.59 KB
/
gpsgpx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "gpsgpx.h"
#include <QFile>
#include <QDebug>
#include "stdlib.h"
GpsGpx::GpsGpx(QString filename)
: CGps(filename) {
}
int GpsGpx::parsefile() {
QFile file(fn);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return FileErr;
}
QDomDocument doc;
doc.setContent(&file);
file.close();
// GPX -> WPT
// -> RTE -> RTEPT
// -> TRK -> TRKSEG -> TRKPT
QDomElement gpx, l1, l2, l3;
QString name;
bool found = false;
double lat, lon, elevation;
QDateTime time;
//GPX
qDebug() << "parseFile";
gpx = doc.documentElement();
if (gpx.tagName() != "gpx") return ParseErr;
//TRK
l1 = gpx.firstChildElement();
while (!l1.isNull()) {
if (l1.tagName() == "wpt") { //waypoint node
readElement(l1, &lat, &lon, &elevation, &time, &name);
addElement(lat, lon, elevation, time, TrackPoint::WAY_POINT, name);
found = true;
} else if (l1.tagName() == "rte") { //route node
l2 = l1.firstChildElement("rtept");
while (!l2.isNull()) {
readElement(l2, &lat, &lon, &elevation, &time, &name);
addElement(lat, lon, elevation, time, TrackPoint::ROUTE_POINT, name);
found = true;
l2 = l2.nextSiblingElement("rtept");
}
} else if (l1.tagName() == "trk") { //track node
l2 = l1.firstChildElement("trkseg");
while (!l2.isNull()) {
l3 = l2.firstChildElement("trkpt");
while (!l3.isNull()) {
readElement(l3, &lat, &lon, &elevation, &time, &name);
addElement(lat, lon, elevation, time, TrackPoint::TRACK_POINT, name);
found = true;
l3 = l3.nextSiblingElement("trkpt");
}
l2 = l2.nextSiblingElement("trkseg");
}
}
l1 = l1.nextSiblingElement();
}
if (!found) return ParseErr; //do something if nothing's read...
loaded = true;
return OK;
}
void GpsGpx::readElement(QDomElement wpt, double *lat, double *lon, double *elev, QDateTime *timestamp, QString *name) {
QDomElement data;
*lat = wpt.attribute("lat").toDouble();
*lon = wpt.attribute("lon").toDouble();
data = wpt.firstChildElement("time");
*timestamp = QDateTime::fromString(data.text(), TimeDate_GPS);
data = wpt.firstChildElement("ele");
*elev = data.text().toDouble();
data = wpt.firstChildElement("name");
*name = data.text();
}