-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpxreader.cpp
120 lines (106 loc) · 3.55 KB
/
gpxreader.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <QDomDocument>
#include <QDomElement>
#include <QDomAttr>
#include <QFile>
#include <QDebug>
#include "gpxreader.h"
GPXReader::GPXReader()
{
}
void GPXReader::clear()
{
m_waypoints.clear();
m_trackpoints.clear();
}
void GPXReader::load(const QString& filename)
{
clear();
QFile file(filename);
QDomDocument document;
if(file.open(QIODevice::ReadOnly))
{
QString error;
int errorLine = 0;
int errorColumn = 0;
if(!document.setContent(&file, &error, &errorLine, &errorColumn))
{
qDebug() << "Cant' read from document" << error << errorLine << " " << errorColumn;
file.close();
return;
}
}
file.close();
QDomNodeList waypointnodelist = document.elementsByTagName("wpt");
for(int loop=0; loop < waypointnodelist.count(); loop++)
{
QDomNode waypointnode = waypointnodelist.at(loop);
WayPoint waypt;
waypt.lat = 0;
waypt.lon = 0;
if(waypointnode.isElement())
{
QDomElement waypointelement = waypointnode.toElement();
waypt.lat = waypointelement.attribute("lat").toDouble();
waypt.lon = waypointelement.attribute("lon").toDouble();
} else
continue;
QDomNodeList waypointchildnodes = waypointnode.childNodes();
for(int loop2=0; loop2 < waypointchildnodes.count(); loop2++)
{
QDomNode waypointchildnode = waypointchildnodes.at(loop2);
if(waypointchildnode.isElement())
{
QDomElement waypointchildelement = waypointchildnode.toElement();
if(waypointchildelement.tagName() == "time")
{
QDateTime time = QDateTime::fromString(waypointchildelement.text(), Qt::ISODate);
time.setTimeSpec(Qt::UTC);
waypt.time = time.toLocalTime();
}
if(waypointchildelement.tagName() == "name")
waypt.name = waypointchildelement.text();
}
}
m_waypoints.push_back(waypt);
}
QDomNodeList trackpointnodelist = document.elementsByTagName("trkpt");
for(int loop=0; loop < trackpointnodelist.count(); loop++)
{
QDomNode trackpointnode = trackpointnodelist.at(loop);
TrackPoint trkpt;
trkpt.lat = 0;
trkpt.lon = 0;
if(trackpointnode.isElement())
{
QDomElement trackpointelement = trackpointnode.toElement();
trkpt.lat = trackpointelement.attribute("lat").toDouble();
trkpt.lon = trackpointelement.attribute("lon").toDouble();
}
else
continue;
QDomNodeList trackpointchildnodes = trackpointnode.childNodes();
for(int loop2=0; loop2 < trackpointchildnodes.count(); loop2++)
{
QDomNode trackpointchildnode = trackpointchildnodes.at(loop2);
if(trackpointchildnode.isElement())
{
QDomElement trackpointchildelement = trackpointchildnode.toElement();
if(trackpointchildelement.tagName() == "time")
{
QDateTime time = QDateTime::fromString(trackpointchildelement.text(), Qt::ISODate);
time.setTimeSpec(Qt::UTC);
trkpt.time = time.toLocalTime();
}
}
}
m_trackpoints.push_back(trkpt);
}
}
QList<WayPoint> GPXReader::getWayPoints()
{
return m_waypoints;
}
QList<TrackPoint> GPXReader::getTrackPoints()
{
return m_trackpoints;
}