This repository was archived by the owner on Sep 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpxcat.cpp
More file actions
301 lines (242 loc) · 6.6 KB
/
gpxcat.cpp
File metadata and controls
301 lines (242 loc) · 6.6 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <iostream>
#include <cstring>
#include <fstream>
#include <list>
#include <cmath>
#include <limits>
#include <iomanip>
#include "XMLParser.h"
const std::string tool = "gpxcat";
const std::string version= "0.1.0";
// ----------------------------------------------------------------------------
class GpxCat : public XMLParserHandler
{
public:
// -- Constructor -----------------------------------------------------------
GpxCat() :
_distance(-1.0),
_doConcat(false)
{
}
// -- Deconstructor ---------------------------------------------------------
virtual ~GpxCat()
{
}
// -- Properties ------------------------------------------------------------
void setDistance(double distance) { _distance = distance; }
// -- Process a file ----------------------------------------------------------
void processFile(const std::string &inputFilename)
{
std::ifstream stream(inputFilename.c_str());
if (stream.is_open())
{
parseFile(stream);
stream.close();
}
}
static bool getDoubleAttribute(const Attributes &atts, const std::string &key, double &value)
{
auto iter = atts.find(key);
if (iter == atts.end()) return false;
return getDouble(iter->second, value);
}
static bool getDouble(const std::string &str, double &value)
{
try
{
value = std::stod(str);
return true;
}
catch (...)
{
return false;
}
}
private:
// -- Parse a file ----------------------------------------------------------
bool parseFile(std::istream &input)
{
_path.clear();
XMLParser parser(this);
parser.parse(input);
return true;
}
static double deg2rad(double deg)
{
return (deg * M_PI) / 180.0;
}
static double rad2deg(double rad)
{
return (rad * 180.0) / M_PI;
}
// http://www.movable-type.co.uk/scripts/latlong.html
// distance in metres
static double calcDistance(double lat1deg, double lon1deg, double lat2deg, double lon2deg)
{
const double R = 6371E3; // m
double lat1rad = deg2rad(lat1deg);
double lon1rad = deg2rad(lon1deg);
double lat2rad = deg2rad(lat2deg);
double lon2rad = deg2rad(lon2deg);
double dlat = lat2rad - lat1rad;
double dlon = lon2rad - lon1rad;
double a = sin(dlat / 2.0) * sin(dlat / 2.0) + cos(lat1rad) * cos(lat2rad) * sin(dlon / 2.0) * sin(dlon / 2.0);
double c = 2.0 * atan2(sqrt(a), sqrt(1.0 - a));
return c * R;
}
void store(const std::string &text)
{
if (_doConcat)
{
_current.append(text);
}
else
{
std::cout << text;
}
}
void doStartElement(const std::string &name, const Attributes &attributes)
{
_path.append("/");
_path.append(name);
if (_path == "/gpx/trk/trkseg/trkpt")
{
double lat, lon = 0.0;
if (getDoubleAttribute(attributes, "lat", lat) && getDoubleAttribute(attributes, "lon", lon))
{
if (_doConcat)
{
if (_distance >= 0.0 && calcDistance(_lastLat, _lastLon, lat, lon) > _distance)
{
std::cout << _current;
}
_doConcat = false;
}
_lastLat = lat;
_lastLon = lon;
}
}
}
void doEndElement()
{
if (_path == "/gpx/trk")
{
if (_doConcat)
{
std::cout << _current;
_doConcat = false;
}
}
else if (_path == "/gpx/trk/trkseg")
{
_doConcat = true;
_current.clear();
}
size_t i = _path.find_last_of('/');
if (i != std::string::npos) _path.erase(i);
}
public:
// -- Callbacks -------------------------------------------------------------
virtual void xmlDecl(const std::string &text, const Attributes &)
{
store(text);
}
virtual void processingInstruction(const std::string &text, const std::string &, const std::string &)
{
store(text);
}
virtual void docTypeDecl(const std::string &text)
{
store(text);
}
virtual void unhandled(const std::string &text, int lineNumber, int columnNumber)
{
std::cerr << " ERROR: Unexpected gpx info: " << text << " on line: " << lineNumber << " columnNumber: " << columnNumber << std::endl;
exit(1);
}
virtual void cdataDecl(const std::string &text, const std::string &)
{
store(text);
}
virtual void comment(const std::string &text, const std::string &)
{
store(text);
}
virtual void startEndElement(const std::string &text, const std::string &name, const Attributes &attributes)
{
doStartElement(name, attributes);
doEndElement();
store(text);
}
virtual void startElement(const std::string &text, const std::string &name, const Attributes &attributes)
{
doStartElement(name, attributes);
store(text);
}
virtual void text(const std::string &text)
{
store(text);
}
virtual void endElement(const std::string &text, const std::string &)
{
doEndElement();
store(text);
}
private:
// Members
double _distance;
std::string _path;
std::string _current;
double _lastLat;
double _lastLon;
bool _doConcat;
};
// -- Main program ------------------------------------------------------------
int main(int argc, char *argv[])
{
GpxCat gpxCat;
int i = 1;
while (i < argc)
{
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-?") == 0)
{
std::cout << "Usage: " << tool << " [-h] [-v] [-d <distance>] <file.gpx> .." << std::endl;
std::cout << " -h help" << std::endl;
std::cout << " -v show version" << std::endl;
std::cout << " -d <distance> concatenate only if the distance between the end and" << std::endl;
std::cout << " the start of the segments are less than distance (metres)" << std::endl;
std::cout << " file.gpx the input gpx file" << std::endl << std::endl;
std::cout << " Concatenate the segments in the track in the GPX input file." << std::endl;
return 0;
}
else if (strcmp(argv[i], "-v") == 0)
{
std::cout << tool << " v" << version << std::endl;
return 0;
}
else if (strcmp(argv[i], "-d") == 0 && i+1 < argc)
{
double distance;
if (GpxCat::getDouble(argv[++i], distance))
{
gpxCat.setDistance(distance);
}
else
{
std::cerr << "Error: invalid distance for option -s." << std::endl;
return 1;
}
}
else if (argv[i][0] != '-')
{
gpxCat.processFile(argv[i]);
}
else
{
std::cerr << "Error: unknown option:" << argv[i] << std::endl;
return 1;
}
i++;
}
return 0;
}