-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpxding.c
515 lines (446 loc) · 15.8 KB
/
gpxding.c
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#include <fcntl.h>
#include <getopt.h>
#include <libxml/parser.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#define _STRINGIFY(s) #s
#define STRINGIFY(s) _STRINGIFY(s)
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define VERSION "0.0.5"
#define GPXHEADER_FULL "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gpx version=\"1.1\" creator=\"gpxding "VERSION"\" xmlns=\"http://www.topografix.com/GPX/1/1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\"><trk><trkseg>"
#define GPXHEADER_MIN "<gpx><trk><trkseg>"
#define GPXFOOTER "</trkseg></trk></gpx>"
#define NO_ELE INT_MIN
#define DIGITS 5
#define ELEVATION true
#define EPSILON 2.0
#define NEARBY 0.0
#define QUIET false
#define SPIKE 0.0
#define EARTH_RADIUS 6371008.8 // arithmetic mean radius
#define MPERLAT EARTH_RADIUS * 2 * M_PI / 360
typedef struct GPXPoint {
double lat;
double lon;
int ele;
bool rdp;
} GPXPoint;
// Print help page
static void help() {
puts("gpxding version v"VERSION"\n\
Usage: gpxding [OPTIONS] [FILE ...]\n\
-d number of digits (default "STRINGIFY(DIGITS)")\n\
-e omit elevation info\n\
-h show this help\n\
-m use minimal <gpx> (not compatible with all apps/devices)\n\
-n remove nearby points (default 0 m, disabled)\n\
-p precision in meters (default "STRINGIFY(EPSILON)" m)\n\
-q quiet\n\
-s remove spikes (default 0, disabled)\n\
-t split gpx file into individual tracks\n");
return;
}
// Lose the trailing zeros in combination with printf %.*g
int num_digits(double a, int digits) {
int int_a = (int) abs(a);
if (int_a >= 100) {
digits += 3;
} else if (int_a >= 10) {
digits += 2;
} else if (int_a >= 1) {
digits += 1;
}
return digits;
}
// Concatenate strings
char* concat(const char *s1, const char *s2)
{
char *result = malloc(strlen(s1) + strlen(s2) + 1);
strcpy(result, s1);
strcat(result, s2);
return result;
}
char* int2str(int num) {
int num_len = snprintf(NULL, 0, "%d", num); // Get the length of the string
char *str = (char *)malloc(num_len + 1);
snprintf(str, num_len + 1, "%d", num);
return str;
}
// Split the GPX file in files with individual tracks
// Using mmap as this is faster than libxml2
void splitGPXFile(const char* infilename) {
int fp = open(infilename, O_RDONLY);
if (fp == -1) {
fprintf(stderr, "Error: Could not open file\n");
exit(1);
}
struct stat file_stat;
if (fstat(fp, &file_stat) == -1) {
fprintf(stderr, "Error: Could not determine file size\n");
close(fp);
exit(1);
}
char *file_contents = (char *)mmap(NULL, file_stat.st_size, PROT_READ, MAP_PRIVATE, fp, 0);
if (file_contents == MAP_FAILED) {
fprintf(stderr, "Error: Could not map into memory\n");
close(fp);
exit(1);
}
char *trk_start = file_contents;
char *header_start = file_contents;
char *header_end = NULL;
int count = 1;
// Determine header
while (1) {
trk_start = strstr(trk_start, "<trk>");
if (trk_start == NULL) {
break;
}
header_end = trk_start;
break;
}
if (header_end == NULL) {
printf("No <trk> tag found in the GPX file.\n");
exit(1);
}
// Find and split the individual tracks, <trk>...</trk>
while (1) {
trk_start = strstr(trk_start, "<trk>");
if (trk_start == NULL) {
break;
}
char *trk_end = strstr(trk_start, "</trk>");
if (trk_end == NULL) {
break;
}
trk_end += 6; // Move past the </trk> tag.
char *strcount;
char *outfilename;
strcount = int2str(count);
outfilename = concat(infilename, strcount);
outfilename = concat(outfilename, ".gpx");
FILE *outfile = fopen(outfilename, "w");
if (outfile == NULL) {
fprintf(stderr, "Error: Could not open output file\n");
munmap(file_contents, file_stat.st_size);
close(fp);
exit(1);
}
fwrite(header_start, header_end - header_start, 1, outfile);
fwrite(trk_start, trk_end - trk_start, 1, outfile);
fprintf(outfile, "</gpx>");
fclose(outfile);
trk_start = trk_end;
count++;
}
if (count == 0) {
printf("No <trk> or </trk> tags found in the GPX file.\n");
}
munmap(file_contents, file_stat.st_size);
close(fp);
}
// Read the GPX file
xmlDocPtr readGPXFile(const char* filename) {
xmlDocPtr doc;
FILE *fp;
// Check if file can be opened for reading
if ((fp = fopen(filename, "r")) == NULL) {
fprintf(stderr, "Error: Could not open file %s\n", filename);
exit(1);
}
// Initialize libxml2
xmlInitParser();
// Parse the GPX file
doc = xmlReadFile(filename, NULL, 0);
if (doc == NULL) {
fprintf(stderr, "Error: Could not parse GPX file %s\n", filename);
exit(1);
}
return doc;
}
// Extract the lat,lon and elevation data
void processNode(GPXPoint** points, xmlNodePtr pt_node, int* num_points, bool elevation) {
xmlChar* content;
GPXPoint point;
point.lat = atof((const char*)xmlGetProp(pt_node, (const xmlChar*)"lat"));
point.lon = atof((const char*)xmlGetProp(pt_node, (const xmlChar*)"lon"));
point.ele = NO_ELE;
point.rdp = true;
// Extract the ele(vation) data (if available and desired)
if (elevation) {
xmlNodePtr ele_node = pt_node->children;
while (ele_node != NULL) {
if (xmlStrcmp(ele_node->name, (const xmlChar*)"ele") == 0) {
content = xmlNodeGetContent(ele_node);
point.ele = atoi((const char*)content);
xmlFree(content);
break;
}
ele_node = ele_node->next;
}
}
// Add the point to the dynamic array
*points = (GPXPoint*)realloc(*points, sizeof(GPXPoint) * (*num_points + 1));
(*points)[*num_points] = point;
(*num_points)++;
}
// Parse the GPX file and extract the lat,lon and elevation data
void parseGPXFile(const char* filename, GPXPoint** points, int* num_points, bool elevation) {
xmlDocPtr doc;
xmlNodePtr node, trk_node, trkseg_node, pt_node;
doc = readGPXFile(filename);
node = xmlDocGetRootElement(doc);
if (node == NULL) {
fprintf(stderr, "Error: Empty GPX file %s\n", filename);
xmlFreeDoc(doc);
exit(1);
}
// Find the track point nodes and extract the lat and lon data
*num_points = 0;
for (trk_node = node->children; trk_node != NULL; trk_node = trk_node->next) {
if (xmlStrcmp(trk_node->name, (const xmlChar*)"trk") == 0) {
for (trkseg_node = trk_node->children; trkseg_node != NULL; trkseg_node = trkseg_node->next) {
if (xmlStrcmp(trkseg_node->name, (const xmlChar*)"trkseg") == 0) {
for (pt_node = trkseg_node->children; pt_node != NULL; pt_node = pt_node->next) {
if (xmlStrcmp(pt_node->name, (const xmlChar*)"trkpt") == 0) {
processNode(points, pt_node, num_points, elevation);
}
}
}
}
} else if (xmlStrcmp(trk_node->name, (const xmlChar*)"rte") == 0) {
for (pt_node = trk_node->children; pt_node != NULL; pt_node = pt_node->next) {
if (xmlStrcmp(pt_node->name, (const xmlChar*)"rtept") == 0) {
processNode(points, pt_node, num_points, elevation);
}
}
}
}
xmlFreeDoc(doc);
xmlCleanupParser();
}
// Returns the distance between point a and b
double distance(GPXPoint a, GPXPoint b) {
// lat,lon -> x,y
double lon_d_at_lat = cos(a.lat * M_PI / 180);
a.lon = a.lon * lon_d_at_lat;
b.lon = b.lon * lon_d_at_lat;
double lat = a.lat - b.lat;
double lon = a.lon - b.lon;
return sqrt(lat * lat + lon * lon);
}
// returns the perpendicular distance from p to the line between a and b
double p_distance(GPXPoint p, GPXPoint a, GPXPoint b) {
// lat,lon -> x,y
double lon_d_at_lat = cos(p.lat * M_PI / 180);
p.lon = p.lon * lon_d_at_lat;
a.lon = a.lon * lon_d_at_lat;
b.lon = b.lon * lon_d_at_lat;
double dx = b.lat - a.lat;
double dy = b.lon - a.lon;
double d = sqrt(dx * dx + dy * dy);
return fabs(p.lat * dy - p.lon * dx + b.lat * a.lon - b.lon * a.lat)/d;
}
// Omit spike points
void despike(GPXPoint *points, int n, double epsilon) {
for (int i = 0; i < n - 2; i++) {
double ab = distance(points[i], points[i + 1]);
double bc = distance(points[i + 1], points[i + 2]);
double ac = distance(points[i], points[i + 2]);
if (ac < ab || ac < bc) {
points[i + 1] = points[i];
continue;
}
double pd = p_distance(points[i + 1], points[i], points[i + 2]);
if (pd * epsilon > ac) {
points[i + 1] = points[i];
}
}
}
// Deduplicate nearby points
void reduce_nearby(GPXPoint *points, int n, double epsilon) {
for (int i = 0; i < n - 2; i++) {
if (distance(points[i], points[i + 1]) < epsilon) {
points[i + 1] = points[i];
}
}
}
// Apply Ramer–Douglas–Peucker algorithm to reduce the number of trackpoints
void rdp_simplify(GPXPoint *points, int n, double epsilon) {
int index = 0;
double dmax = 0.0;
for (int i = 1; i < n - 1; i++) {
double d = p_distance(points[i], points[0], points[n-1]);
if (d > dmax) {
index = i;
dmax = d;
}
}
if (dmax > epsilon) {
rdp_simplify(points, index + 1, epsilon);
rdp_simplify(points + index, n - index, epsilon);
} else {
for (int i = 1; i < n - 1; i++) {
points[i].rdp = false; // mark point for removal
}
}
}
// Write the reduced GPX file
void writeGPXFile(const GPXPoint* points, int n, const char* filename, int digits, bool elevation, const char* gpxheader) {
FILE *fp;
fp = fopen(filename, "w");
if (fp != NULL) {
fprintf(fp, gpxheader);
for (int i = 0; i < n; ++i) {
if (points[i].rdp == true) {
int lat_digits = num_digits(points[i].lat, digits);
int lon_digits = num_digits(points[i].lon, digits);
if (elevation && points[i].ele != NO_ELE) {
fprintf(fp, "<trkpt lat=\"%.*g\" lon=\"%.*g\"><ele>%i</ele></trkpt>",
lat_digits, points[i].lat, lon_digits, points[i].lon, points[i].ele);
} else {
fprintf(fp, "<trkpt lat=\"%.*g\" lon=\"%.*g\"></trkpt>",
lat_digits, points[i].lat, lon_digits, points[i].lon);
}
}
}
fprintf(fp, GPXFOOTER);
fclose(fp);
} else {
fprintf(stderr, "Error: Could not write to file %s\n", filename);
exit(1);
}
}
int main(int argc, char *argv[]){
extern char *optarg;
extern int optind;
const char* infilename;
int param;
int digits = DIGITS;
bool elevation = ELEVATION;
double epsilon = EPSILON;
const char* gpxheader = GPXHEADER_FULL;
double nearby = NEARBY;
bool quiet = QUIET;
double spike = SPIKE;
bool split = false;
while ((param = getopt(argc, argv, "d:ehmn:p:qs:t")) != -1)
switch(param) {
case 'd': // number of digits
digits = atoi(optarg) ;
if ((digits < 1) || (digits > 9)) {
fputs("Invalid number of digits\n", stderr);
help();
exit(1);
}
break;
case 'e': // elevation
elevation = false;
break;
case 'h': // help
help();
exit(0);
case 'm': // use minimal <gpx>
gpxheader = GPXHEADER_MIN;
break;
case 'n': // max error in meter
nearby = atof(optarg) ;
if ((nearby < 0) || (nearby > 100)) {
fputs("Invalid nearby value (0-100)\n", stderr);
help();
exit(1);
}
break;
case 'p': // max error in meter
epsilon = atof(optarg) ;
if ((epsilon < 0) || (epsilon > 100)) {
fputs("Invalid precision (0-100)\n", stderr);
help();
exit(1);
}
break;
case 'q':
quiet = true;
break;
case 's': // max error in meter
spike = atof(optarg) ;
if ((spike < 0) || (spike > 10)) {
fputs("Invalid spike factor (0-10)\n", stderr);
help();
exit(1);
}
break;
case 't':
split = true;
break;
case '?':
return 1;
default:
abort();
}
if (argv[optind] == NULL) {
help();
exit(0);
}
// meter / MPERLAT => degree
epsilon /= MPERLAT;
nearby /= MPERLAT;
// Loop over all GPX files
for (int i = optind; i < argc; i++) {
GPXPoint* points = NULL;
char* outfilename;
struct stat filestatus;
int num_points = 0;
infilename = argv[i];
// Parse GPX file, extracting lat, lon and ELEVATION
if (split) {
splitGPXFile(infilename);
break;
}
// Parse GPX file, extracting lat, lon and ELEVATION
parseGPXFile(infilename, &points, &num_points, elevation);
// Omit nearby points
if (num_points == 0) {
fprintf(stderr, "Error: %s does not contain rtept/trkpt/trackpoints\n", infilename);
exit(1);
}
// Omit nearby points
if (nearby > 0.0) reduce_nearby(points, num_points, nearby);
// Omit spike points
if (spike > 0.0) despike(points, num_points, spike);
// First and last point can not be the same
while (points[0].lat == points[num_points-1].lat &&
points[0].lon == points[num_points-1].lon) {
num_points--;
}
// Simplify using RDP algorithm
rdp_simplify(points, num_points, epsilon);
// Write new simplified GPX file
outfilename = concat(infilename, ".gpx");
writeGPXFile(points, num_points, outfilename, digits, elevation, gpxheader);
// Print statistics
if (! quiet) {
int num_rdp_points = 0;
for (int rdp = 0; rdp <= num_points-1; rdp++) {
if (points[rdp].rdp) num_rdp_points++;
}
stat(infilename, &filestatus);
int insize = filestatus.st_size;
stat(outfilename, &filestatus);
int outsize = filestatus.st_size;
printf("%s => %s\n", infilename, outfilename);
printf("%8i => %8i (%.2f%%) trackpoints\n", num_points, num_rdp_points, num_rdp_points * 100.0 / num_points);
printf("%8i => %8i (%.2f%%) bytes\n", insize, outsize, outsize * 100.0 / insize);
}
free(outfilename);
free(points);
}
}