My favorites | Sign in
Project Home Source
Checkout   Browse   Changes  
Changes to /trunk/gpsbabel/gui/gpx.cpp
r3767 vs. r4161 Compare: vs.  Format:
Revision r4161
Go to: 
/trunk/gpsbabel/gui/gpx.cpp   r3767 /trunk/gpsbabel/gui/gpx.cpp   r4161
Properties
svn:executable
1 *   
  
Contents
1 // -*- C++ -*- 1 // -*- C++ -*-
2 // $Id: gpx.cpp,v 1.2 2009-08-28 17:08:55 robertl Exp $ 2 // $Id: gpx.cpp,v 1.2 2009-08-28 17:08:55 robertl Exp $
3 //------------------------------------------------------------------------ 3 //------------------------------------------------------------------------
4 // 4 //
5 // Copyright (C) 2009 S. Khai Mong <khai@mangrai.com>. 5 // Copyright (C) 2009 S. Khai Mong <khai@mangrai.com>.
6 // 6 //
7 // This program is free software; you can redistribute it and/or 7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as 8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the 9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version. 10 // License, or (at your option) any later version.
11 // 11 //
12 // This program is distributed in the hope that it will be useful, 12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details. 15 // General Public License for more details.
16 // 16 //
17 // You should have received a copy of the GNU General Public License 17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software 18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111
20 // USA 20 // USA
21 // 21 //
22 //------------------------------------------------------------------------ 22 //------------------------------------------------------------------------
23 23
24 24
25 #include <QFile> 25 #include <QFile>
26 #include <QXmlInputSource> 26 #include <QXmlInputSource>
27 #include <QXmlSimpleReader> 27 #include <QXmlSimpleReader>
28 #include <QXmlDefaultHandler> 28 #include <QXmlDefaultHandler>
29 #include "gpx.h" 29 #include "gpx.h"
30 30
31 31
32 static QDateTime decodeDateTime(const QString s) 32 static QDateTime decodeDateTime(const QString s)
33 { 33 {
34 QDateTime utc = QDateTime::fromString(s, "yyyy-MM-dd'T'HH:mm:ss'Z'"); 34 QDateTime utc = QDateTime::fromString(s, "yyyy-MM-dd'T'HH:mm:ss'Z'");
35 return utc; 35 return utc;
36 } 36 }
37 37
38 static bool trackIsEmpty(const GpxTrack &trk){ 38 static bool trackIsEmpty(const GpxTrack &trk){
39 int count = 0; 39 int count = 0;
40 for (int i=0; i< trk.getTrackSegments().size(); i++) { 40 for (int i=0; i< trk.getTrackSegments().size(); i++) {
41 for (int j=0; j<trk.getTrackSegments()[i].getTrackPoints().size(); j++) { 41 for (int j=0; j<trk.getTrackSegments()[i].getTrackPoints().size(); j++) {
42 count++; 42 count++;
43 } 43 }
44 } 44 }
45 return count <=2 ; 45 return count <=2 ;
46 } 46 }
47 47
48 class GpxHandler: public QXmlDefaultHandler 48 class GpxHandler: public QXmlDefaultHandler
49 { 49 {
50 public: 50 public:
51 GpxHandler(): QXmlDefaultHandler() 51 GpxHandler(): QXmlDefaultHandler()
52 52
53 { 53 {
54 state = e_noop; 54 state = e_noop;
55 } 55 }
56 56
57 typedef enum {e_noop, e_wpt, e_trk, 57 typedef enum {e_noop, e_wpt, e_trk,
58 e_trkpt, e_trkseg, e_rte, e_rtept} elementState; 58 e_trkpt, e_trkseg, e_rte, e_rtept} elementState;
59 QString textChars; 59 QString textChars;
60 GpxWaypoint currentWpt; 60 GpxWaypoint currentWpt;
61 QList <GpxWaypoint> wptList; 61 QList <GpxWaypoint> wptList;
62 62
63 QList <GpxTrack> trkList; 63 QList <GpxTrack> trkList;
64 GpxTrack currentTrk; 64 GpxTrack currentTrk;
65 GpxTrackPoint currentTrkPt; 65 GpxTrackPoint currentTrkPt;
66 GpxTrackSegment currentTrkSeg; 66 GpxTrackSegment currentTrkSeg;
67 67
68 QList <GpxRoute> rteList; 68 QList <GpxRoute> rteList;
69 GpxRoute currentRte; 69 GpxRoute currentRte;
70 GpxRoutePoint currentRtePt; 70 GpxRoutePoint currentRtePt;
71 71
72 elementState state; 72 elementState state;
73 QList <elementState> stateStack; 73 QList <elementState> stateStack;
74 74
75 virtual bool startElement (const QString & , 75 virtual bool startElement (const QString & ,
76 const QString & localName, const QString & qName, 76 const QString & localName, const QString & qName,
77 const QXmlAttributes & atts ) 77 const QXmlAttributes & atts )
78 { 78 {
79 if (localName == "wpt") { 79 if (localName == "wpt") {
80 currentWpt = GpxWaypoint(); 80 currentWpt = GpxWaypoint();
81 double lat = atts.value("lat").toDouble();; 81 double lat = atts.value("lat").toDouble();;
82 double lon = atts.value("lon").toDouble();; 82 double lon = atts.value("lon").toDouble();;
83 currentWpt.setLocation(LatLng(lat, lon)); 83 currentWpt.setLocation(LatLng(lat, lon));
84 stateStack << state; 84 stateStack << state;
85 state = e_wpt; 85 state = e_wpt;
86 } 86 }
87 87
88 else if (localName == "trk") { 88 else if (localName == "trk") {
89 stateStack << state; 89 stateStack << state;
90 state = e_trk; 90 state = e_trk;
91 currentTrk.clear(); 91 currentTrk.clear();
92 } 92 }
93 93
94 else if (localName == "trkseg") { 94 else if (localName == "trkseg") {
95 stateStack << state; 95 stateStack << state;
96 state = e_trkseg; 96 state = e_trkseg;
97 currentTrkSeg.clear(); 97 currentTrkSeg.clear();
98 } 98 }
99 99
100 else if (localName == "trkpt") { 100 else if (localName == "trkpt") {
101 currentTrkPt = GpxTrackPoint(); 101 currentTrkPt = GpxTrackPoint();
102 double lat = atts.value("lat").toDouble();; 102 double lat = atts.value("lat").toDouble();;
103 double lon = atts.value("lon").toDouble();; 103 double lon = atts.value("lon").toDouble();;
104 currentTrkPt.setLocation(LatLng(lat, lon)); 104 currentTrkPt.setLocation(LatLng(lat, lon));
105 stateStack << state; 105 stateStack << state;
106 state = e_trkpt; 106 state = e_trkpt;
107 } 107 }
108 108
109 else if (localName == "rte") { 109 else if (localName == "rte") {
110 stateStack << state; 110 stateStack << state;
111 state = e_rte; 111 state = e_rte;
112 currentRte.clear(); 112 currentRte.clear();
113 } 113 }
114 114
115 else if (localName == "rtept") { 115 else if (localName == "rtept") {
116 currentRtePt = GpxRoutePoint(); 116 currentRtePt = GpxRoutePoint();
117 double lat = atts.value("lat").toDouble();; 117 double lat = atts.value("lat").toDouble();;
118 double lon = atts.value("lon").toDouble();; 118 double lon = atts.value("lon").toDouble();;
119 currentRtePt.setLocation(LatLng(lat, lon)); 119 currentRtePt.setLocation(LatLng(lat, lon));
120 stateStack << state; 120 stateStack << state;
121 state = e_rtept; 121 state = e_rtept;
122 } 122 }
123 123
124 124
125 else if (state == e_wpt || 125 else if (state == e_wpt ||
126 state == e_trkpt || state == e_trkseg || state == e_trk || 126 state == e_trkpt || state == e_trkseg || state == e_trk ||
127 state == e_rte || state == e_rtept) { 127 state == e_rte || state == e_rtept) {
128 } 128 }
129 else { 129 else {
130 //fprintf(stderr, "localName: %s name: %s\n", 130 //fprintf(stderr, "localName: %s name: %s\n",
131 //localName.toStdString().c_str(), qName.toStdString().c_str()); 131 //localName.toStdString().c_str(), qName.toStdString().c_str());
132 } 132 }
133 return true; 133 return true;
134 }; 134 };
135 135
136 virtual bool endElement (const QString & , 136 virtual bool endElement (const QString & ,
137 const QString & localName, 137 const QString & localName,
138 const QString & qName) 138 const QString & qName)
139 { 139 {
140 if (localName == "wpt") { 140 if (localName == "wpt") {
141 state = stateStack.takeLast(); 141 state = stateStack.takeLast();
142 wptList << currentWpt; 142 wptList << currentWpt;
143 } 143 }
144 else if (localName == "ele" && state == e_wpt) { 144 else if (localName == "ele" && state == e_wpt) {
145 currentWpt.setElevation(textChars.toDouble()); 145 currentWpt.setElevation(textChars.toDouble());
146 } 146 }
147 else if (localName == "name" && state == e_wpt) { 147 else if (localName == "name" && state == e_wpt) {
148 currentWpt.setName(textChars); 148 currentWpt.setName(textChars);
149 } 149 }
150 else if (localName == "cmt" && state == e_wpt) { 150 else if (localName == "cmt" && state == e_wpt) {
151 currentWpt.setComment(textChars); 151 currentWpt.setComment(textChars);
152 } 152 }
153 else if (localName == "desc" && state == e_wpt) { 153 else if (localName == "desc" && state == e_wpt) {
154 currentWpt.setDescription(textChars); 154 currentWpt.setDescription(textChars);
155 } 155 }
156 else if (localName == "sym" && state == e_wpt) { 156 else if (localName == "sym" && state == e_wpt) {
157 currentWpt.setSymbol(textChars); 157 currentWpt.setSymbol(textChars);
158 } 158 }
159 159
160 else if (localName == "trkpt") { 160 else if (localName == "trkpt") {
161 state = stateStack.takeLast(); 161 state = stateStack.takeLast();
162 currentTrkSeg.addPoint(currentTrkPt); 162 currentTrkSeg.addPoint(currentTrkPt);
163 } 163 }
164 else if (localName == "ele" && state == e_trkpt) { 164 else if (localName == "ele" && state == e_trkpt) {
165 currentTrkPt.setElevation(textChars.toDouble()); 165 currentTrkPt.setElevation(textChars.toDouble());
166 } 166 }
167 else if (localName == "time" && state == e_trkpt) { 167 else if (localName == "time" && state == e_trkpt) {
168 currentTrkPt.setDateTime(decodeDateTime(textChars)); 168 currentTrkPt.setDateTime(decodeDateTime(textChars));
169 } 169 }
170 170
171 else if (localName == "trkseg") { 171 else if (localName == "trkseg") {
172 state = stateStack.takeLast(); 172 state = stateStack.takeLast();
173 currentTrk.addSegment(currentTrkSeg); 173 currentTrk.addSegment(currentTrkSeg);
174 } 174 }
175 175
176 else if (localName == "trk") { 176 else if (localName == "trk") {
177 state = stateStack.takeLast(); 177 state = stateStack.takeLast();
178 if (!trackIsEmpty(currentTrk)) 178 if (!trackIsEmpty(currentTrk))
179 trkList << currentTrk; 179 trkList << currentTrk;
180 } 180 }
181 181
182 else if (localName == "name" && state == e_trk) { 182 else if (localName == "name" && state == e_trk) {
183 currentTrk.setName(textChars); 183 currentTrk.setName(textChars);
184 } 184 }
185 else if (localName == "number" && state == e_trk) { 185 else if (localName == "number" && state == e_trk) {
186 currentTrk.setNumber(textChars.toInt()); 186 currentTrk.setNumber(textChars.toInt());
187 } 187 }
188 188
189 else if (localName == "rte") { 189 else if (localName == "rte") {
190 state = stateStack.takeLast(); 190 state = stateStack.takeLast();
191 if (currentRte.getRoutePoints().size()>=2) 191 if (currentRte.getRoutePoints().size()>=2)
192 rteList << currentRte; 192 rteList << currentRte;
193 } 193 }
194 194
195 else if (localName == "rtept") { 195 else if (localName == "rtept") {
196 state = stateStack.takeLast(); 196 state = stateStack.takeLast();
197 currentRte.addPoint(currentRtePt); 197 currentRte.addPoint(currentRtePt);
198 } 198 }
199 199
200 else if (localName == "name" && state == e_rtept) { 200 else if (localName == "name" && state == e_rtept) {
201 currentRtePt.setName(textChars); 201 currentRtePt.setName(textChars);
202 } 202 }
203 203
204 else if (localName == "name" && state == e_rte) { 204 else if (localName == "name" && state == e_rte) {
205 currentRte.setName(textChars); 205 currentRte.setName(textChars);
206 } 206 }
207 207
208 else { 208 else {
209 //fprintf(stderr, "end ---- localName: %s name: %s\n\n", 209 //fprintf(stderr, "end ---- localName: %s name: %s\n\n",
210 //localName.toStdString().c_str(), qName.toStdString().c_str()); 210 //localName.toStdString().c_str(), qName.toStdString().c_str());
211 } 211 }
212 return true; 212 return true;
213 }; 213 };
214 214
215 virtual bool characters(const QString &x) 215 virtual bool characters(const QString &x)
216 { 216 {
217 textChars = x; 217 textChars = x;
218 return true; 218 return true;
219 }; 219 };
220 }; 220 };
221 221
222 222
223 //------------------------------------------------------------------------ 223 //------------------------------------------------------------------------
224 224
225 bool Gpx::read(const QString & fileName) 225 bool Gpx::read(const QString & fileName)
226 { 226 {
227 QFile file(fileName); 227 QFile file(fileName);
228 if (!file.open(QIODevice::ReadOnly)) 228 if (!file.open(QIODevice::ReadOnly))
229 return false; 229 return false;
230 230
231 QXmlInputSource xmlIn(&file); 231 QXmlInputSource xmlIn(&file);
232 232
233 QXmlSimpleReader reader; 233 QXmlSimpleReader reader;
234 GpxHandler gpxHandler; 234 GpxHandler gpxHandler;
235 reader.setContentHandler(&gpxHandler); 235 reader.setContentHandler(&gpxHandler);
236 236
237 if (reader.parse(xmlIn)) { 237 if (reader.parse(xmlIn)) {
238 wayPoints = gpxHandler.wptList; 238 wayPoints = gpxHandler.wptList;
239 tracks = gpxHandler.trkList; 239 tracks = gpxHandler.trkList;
240 routes = gpxHandler.rteList; 240 routes = gpxHandler.rteList;
241 return true; 241 return true;
242 } 242 }
243 else 243 else
244 return false; 244 return false;
245 } 245 }
Powered by Google Project Hosting