forked from GPSBabel/gpsbabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compegps.cc
698 lines (613 loc) · 15.6 KB
/
compegps.cc
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/*
Support for CompeGPS waypoint (.wpt), route (.rte) and track (.trk) files,
Copyright (C) 2005 Olaf Klein, [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
History:
10/23/2005: First release; only a reader
10/25/2005: becomes a writer too
10/26/2005: received documentation from CompeGPS team
added fatals for "G" and "U" if not WGS84 and lat/lon
08/13/2006: switch to gbfile api
*/
/*
the meaning of leading characters in CompeGPS data lines (enhanced PCX):
header lines:
"G": WGS 84 - Datum of the map
"N": Anybody - Name of the user
"L": -02:00:00 - Difference to UTC
"M": ... - Any comments
"R": 16711680 , xxxx , 1 - Route header
"U": 1 - System of coordinates (0=UTM 1=Latitude/Longitude)
"C": 0 0 255 2 -1.000000 - ???
"V": 0.0 0.0 0 0 0 0 0.0 - ???
"E": 0|1|00-NUL-00 00:00:00|00:00:00|0 - ???
data lines:
"W": if(route) routepoint; else waypoint
"T": trackpoint
"t": if(track) additionally track info
if(!track) additionally trackpoint info
"a": link to ...
"w": waypoint additional info
*/
#include "defs.h"
#include "cet_util.h"
#include "csv_util.h"
#if CSVFMTS_ENABLED
#include <cmath>
#include "jeeps/gpsmath.h"
#include <cstdlib>
#include <cstdio>
#define MYNAME "CompeGPS"
#define SHORT_NAME_LENGTH 16
static gbfile* fin, *fout;
static int target_index, curr_index;
static int track_info_flag;
static short_handle sh;
static int snlen;
static int radius;
static int input_datum;
static const route_head* curr_track;
/* placeholders for options */
static char* option_icon;
static char* option_index;
static char* option_radius;
static char* option_snlen;
static
QVector<arglist_t> compegps_args = {
{
"deficon", &option_icon, "Default icon name",
nullptr, ARGTYPE_STRING, ARG_NOMINMAX, nullptr
},
{
"index", &option_index, "Index of route/track to write (if more than one in source)",
nullptr, ARGTYPE_INT, "1", nullptr, nullptr
},
{
"radius", &option_radius, "Give points (waypoints/route points) a default radius (proximity)",
nullptr, ARGTYPE_FLOAT, "0", nullptr, nullptr
},
{
"snlen", &option_snlen, "Length of generated shortnames (default 16)",
"16", ARGTYPE_INT, "1", nullptr, nullptr
},
};
static
void fix_datum(double* lat, double* lon)
{
double amt;
/*
* Avoid FP jitter in the common case.
*/
if (input_datum != DATUM_WGS84) {
GPS_Math_Known_Datum_To_WGS84_M(*lat, *lon, 0.0, lat, lon,
&amt, input_datum);
}
}
static void
compegps_parse_date(const char* c, struct tm* tm)
{
char month[4];
tm->tm_mday = atoi(c);
strncpy(month, c+3, 3);
month[3] = 0;
tm->tm_mon = month_lookup(month);
int year = atoi(c + 7);
if (year < 70) {
year += 100;
}
if (year > 1900) {
year -= 1900;
}
tm->tm_year = year;
// if (tm->tm_year < 70) tm->tm_year += 100;
}
static void
compegps_parse_time(const char* c, struct tm* tm)
{
tm->tm_hour = atoi(c);
tm->tm_min = atoi(c+3);
tm->tm_sec = atoi(c+6);
}
/* specialized readers */
static Waypoint*
parse_wpt(char* buff)
{
int col = -1;
char* cx;
Waypoint* wpt = new Waypoint;
struct tm tm;
int has_time = 0;
memset(&tm, 0, sizeof(tm));
char* c = strstr(buff, "A ");
if (c == buff) {
col++;
}
c = csv_lineparse(buff, " ", "", col++);
while (c != nullptr) {
c = lrtrim(c);
if (*c != '\0') {
#if 0
printf(MYNAME "_read_wpt: col(%d)=%s\n", col, c);
#endif
switch (col) {
case 0:
cx = c + strlen(c) - 1; /* trim trailing underscores */
while ((cx >= c) && (*cx == '_')) {
*cx-- = '\0';
}
if (*c != '\0') {
wpt->shortname = c;
}
break;
case 2:
human_to_dec(c, &wpt->latitude, nullptr, 1);
break;
case 3:
human_to_dec(c, nullptr, &wpt->longitude, 2);
break;
// Older compegps used a dumb constant.
// Report are that 2010-era writes a sensible
// value here.
/* always "27-MAR-62 00:00:00" */
case 4:
if (strcmp(c, "27-MAR-62")) {
has_time = 1;
compegps_parse_date(c, &tm);
}
break;
case 5:
if (has_time) {
compegps_parse_time(c, &tm);
wpt->SetCreationTime(mkgmtime(&tm));
}
break;
case 6:
wpt->altitude = atof(c);
break;
case 7:
wpt->description = c;
break;
default:
if (col > 7) {
wpt->description += " ";
wpt->description += c;
}
}
}
c = csv_lineparse(nullptr, " ", "", col++);
}
fix_datum(&wpt->latitude, &wpt->longitude);
return wpt;
}
static void
parse_wpt_info(const char* buff, Waypoint* wpt) /* "w" */
{
int col = -1;
double fx;
char* c = csv_lineparse(buff, ",", "", col++);
while (c != nullptr) {
c = lrtrim(c);
if (*c != '\0') {
#if 0
printf(MYNAME "_read_wpt_info: col(%d)=%s\n", col, c);
#endif
switch (col) {
case 0:
wpt->icon_descr = c;
break;
case 1:
break; /* Text postion */
case 2:
break; /* Lens zoom level */
case 3:
break; /* Text colour */
case 4:
break; /* Background colour */
case 5:
break; /* Transparent text (0=transparent, 1=no transparent) */
case 6:
break; /* ??? */
case 7:
break; /* ??? */
case 8: /* radius */
fx = atof(c);
if (fx > 0) {
WAYPT_SET(wpt, proximity, fx);
}
break;
}
}
c = csv_lineparse(nullptr, ",", "", col++);
}
}
static Waypoint*
parse_trkpt(char* buff)
{
int col = -1;
struct tm tm;
Waypoint* wpt = new Waypoint;
char* c = strstr(buff, "A ");
if (c == buff) {
col++;
}
memset(&tm, 0, sizeof(tm));
c = csv_lineparse(buff, " ", "", col++);
while (c != nullptr) {
c = lrtrim(c);
if (*c != '\0') {
#if 0
printf(MYNAME "_read_trkpt: col(%d)=%s\n", col, c);
#endif
switch (col) {
case 2:
human_to_dec(c, &wpt->latitude, nullptr, 1);
break;
case 3:
human_to_dec(c, nullptr, &wpt->longitude, 2);
break;
case 4:
compegps_parse_date(c, &tm);
break;
case 5:
compegps_parse_time(c, &tm);
wpt->SetCreationTime(mkgmtime(&tm));
break;
case 7:
wpt->altitude = atof(c);
break;
}
}
c = csv_lineparse(nullptr, " ", "", col++);
}
fix_datum(&wpt->latitude, &wpt->longitude);
return wpt;
}
static void
parse_track_info(const char* buff, route_head* track) /* "t" */
{
int col = -1;
char* c = csv_lineparse(buff, "|", "", col++);
while (c != nullptr) {
c = lrtrim(c);
if (*c != '\0') {
#if 0
printf(MYNAME "_read_track_info: col(%d)=%s\n", col, c);
#endif
switch (col) {
case 0:
break; /* unknown field */
case 1:
track->rte_name = c;
break;
case 2:
break; /* unknown field */
case 3:
break; /* unknown field */
}
}
c = csv_lineparse(nullptr, "|", "", col++);
}
}
static void
parse_rte_info(const char* buff, route_head* route) /* "R" */
{
int col = -1;
char* c = csv_lineparse(buff, ",", "", col++);
while (c != nullptr) {
c = lrtrim(c);
if (*c != '\0') {
#if 0
printf(MYNAME "_read_rte_info: col(%d)=%s\n", col, c);
#endif
switch (col) {
case 0:
break; /* unknown field (colour?) */
case 1:
route->rte_name = c;
break;
case 2:
break; /* unknown field */
}
}
c = csv_lineparse(nullptr, ",", "", col++);
}
}
/* main functions */
static void
compegps_rd_init(const QString& fname)
{
fin = gbfopen(fname, "rb", MYNAME);
input_datum = DATUM_WGS84;
}
static void
compegps_rd_deinit()
{
gbfclose(fin);
}
static void
compegps_data_read()
{
char* buff;
int line = 0;
int input_datum;
Waypoint* wpt = nullptr;
route_head* route = nullptr;
route_head* track = nullptr;
while ((buff = gbfgetstr(fin))) {
if ((line++ == 0) && fin->unicode) {
cet_convert_init(CET_CHARSET_UTF8, 1);
}
char* cin = lrtrim(buff);
if (strlen(cin) == 0) {
continue;
}
char* ctail = strchr(cin, ' ');
if (ctail == nullptr) {
continue;
}
ctail = lrtrim(ctail);
switch (*cin) {
case 'G':
input_datum = GPS_Lookup_Datum_Index(ctail);
if (input_datum < 0) {
fatal(MYNAME ": Unsupported datum \"%s\"!", ctail);
}
break;
case 'U':
switch (*ctail) {
case '1': /* lat/lon, that's we want to see */
break;
case '0': /* UTM not supported yet */
fatal(MYNAME "Sorry, UTM is not supported yet!\n");
default:
fatal(MYNAME "Invalid system of coordinates (%s)!\n", cin);
}
break;
case 'R':
route = route_head_alloc();
route_add_head(route);
parse_rte_info(ctail, route);
break;
case 'M':
break;
case 'W':
wpt = parse_wpt(ctail);
if (wpt != nullptr) {
if (route != nullptr) {
route_add_wpt(route, wpt);
} else {
waypt_add(wpt);
}
}
break;
case 'w':
is_fatal((wpt == nullptr), MYNAME ": No waypoint data before \"%s\"!", cin);
parse_wpt_info(ctail, wpt);
break;
case 'T':
wpt = parse_trkpt(ctail);
if (wpt != nullptr) {
if (track == nullptr) {
track = route_head_alloc();
track_add_head(track);
}
track_add_wpt(track, wpt);
}
break;
case 't':
if (track != nullptr) {
parse_track_info(ctail, track);
}
break;
}
}
}
/* ----------------------------------------------------------- */
static void
write_waypt_cb(const Waypoint* wpt)
{
if (curr_index != target_index) {
return;
}
// Our only output cleansing is to replace
QString cleaned_name(wpt->shortname);
cleaned_name.replace(' ', '_');
QString name = (snlen > 0) ? mkshort(sh, cleaned_name) : cleaned_name;
gbfprintf(fout, "W %s A ", CSTR(name));
gbfprintf(fout, "%.10f%c%c ",
fabs(wpt->latitude), 0xBA, (wpt->latitude >= 0) ? 'N' : 'S');
gbfprintf(fout, "%.10f%c%c ",
fabs(wpt->longitude), 0xBA, (wpt->longitude >= 0) ? 'E' : 'W');
gbfprintf(fout, "27-MAR-62 00:00:00 %.6f",
(wpt->altitude != unknown_alt) ? wpt->altitude : 0.0);
if (wpt->description != nullptr) {
gbfprintf(fout, " %s", CSTRc(wpt->description));
}
gbfprintf(fout, "\n");
if ((!wpt->icon_descr.isNull()) || (wpt->wpt_flags.proximity) || \
(option_icon != nullptr)) {
gbfprintf(fout, "w %s,0,0.0,16777215,255,1,7,,%.1f\n",
wpt->icon_descr.isNull() ? "Waypoint" : CSTR(wpt->icon_descr),
WAYPT_GET(wpt, proximity, 0));
}
}
static void
write_route_hdr_cb(const route_head* rte)
{
curr_index++;
if (curr_index != target_index) {
return;
}
QString name = rte->rte_name;
if (name != nullptr) {
name = csv_stringclean(name, ",");
} else {
name = " ";
}
gbfprintf(fout, "R 16711680,%s,1,-1\n", CSTR(name));
}
static void
write_route()
{
curr_index = 0;
route_disp_all(write_route_hdr_cb, nullptr, write_waypt_cb);
}
static void
write_track_hdr_cb(const route_head* trk)
{
track_info_flag = 0;
curr_track = trk;
curr_index++;
if (curr_index != target_index) {
return;
}
track_info_flag = 1;
}
static void
write_trkpt_cb(const Waypoint* wpt)
{
char buff[128];
if ((curr_index != target_index) || (wpt == nullptr)) {
return;
}
buff[0] = '\0';
// TOOD: This should probably attempt a gmtime and then fall back to the 1-1-1970
// case or bypass the time_t completely and build string representations directly.
if (wpt->creation_time.isValid()) {
const time_t tt = wpt->GetCreationTime().toTime_t();
struct tm tm = *gmtime(&tt);
strftime(buff, sizeof(buff), "%d-%b-%y %H:%M:%S", &tm);
strupper(buff);
} else {
strncpy(buff, "01-JAN-70 00:00:00", sizeof(buff));
}
gbfprintf(fout, "T A %.10f%c%c %.10f%c%c ",
fabs(wpt->latitude), 0xBA, (wpt->latitude >= 0) ? 'N' : 'S',
fabs(wpt->longitude), 0xBA, (wpt->longitude >= 0) ? 'E' : 'W');
gbfprintf(fout, "%s s %.1f %.1f %.1f %.1f %d ",
buff,
wpt->altitude,
0.0,
0.0,
0.0,
0);
gbfprintf(fout, "%.1f %.1f %.1f %.1f %.1f\n",
-1000.0,
-1.0,
-1.0,
-1.0,
-1.0);
if (track_info_flag != 0) {
track_info_flag = 0;
if (curr_track->rte_name != nullptr) {
QString name = csv_stringclean(curr_track->rte_name, "|");
gbfprintf(fout, "t 4294967295|%s|-1|-1\n", CSTR(name));
}
}
}
static void
write_track()
{
curr_index = 0;
// gbfprintf(fout, "L +02:00:00\n");
track_disp_all(write_track_hdr_cb, nullptr, write_trkpt_cb);
gbfprintf(fout, "F 1234\n");
}
static void
write_waypoints()
{
waypt_disp_all(write_waypt_cb);
}
/* --------------------------------------------------------------------------- */
static void
compegps_wr_init(const QString& fname)
{
fout = gbfopen(fname, "w", MYNAME);
sh = mkshort_new_handle();
}
static void
compegps_wr_deinit()
{
mkshort_del_handle(&sh);
gbfclose(fout);
}
static void
compegps_data_write()
{
/* because of different file extensions we can only write one GPS data type at time */
gbfprintf(fout, "G WGS 84\n");
gbfprintf(fout, "U 1\n");
/* process options */
target_index = 1;
if (option_index != nullptr) {
target_index = atoi(option_index);
}
snlen = 0;
if (global_opts.synthesize_shortnames != 0) {
if (option_snlen != nullptr) {
snlen = atoi(option_snlen);
} else {
snlen = SHORT_NAME_LENGTH;
}
is_fatal((snlen < 1), MYNAME "Invalid length for generated shortnames!");
setshort_whitespace_ok(sh, 0);
setshort_length(sh, snlen);
}
radius = -1;
if (option_radius != nullptr) {
radius = atof(option_radius);
is_fatal((radius <= 0.0), MYNAME "Invalid value for radius!");
}
if (option_icon != nullptr) {
if (*option_icon == '\0') {
option_icon = nullptr;
} else if (case_ignore_strcmp(option_icon, "deficon") == 0) {
option_icon = nullptr;
}
}
switch (global_opts.objective) {
case wptdata:
case unknown_gpsdata:
curr_index = target_index = 0;
write_waypoints();
break;
case trkdata:
write_track();
break;
case rtedata:
write_route();
break;
case posndata:
fatal(MYNAME ": Realtime positioning not supported.\n");
break;
}
}
/* --------------------------------------------------------------------------- */
ff_vecs_t compegps_vecs = {
ff_type_file,
FF_CAP_RW_ALL,
compegps_rd_init,
compegps_wr_init,
compegps_rd_deinit,
compegps_wr_deinit,
compegps_data_read,
compegps_data_write,
nullptr,
&compegps_args,
CET_CHARSET_MS_ANSI, 1
, NULL_POS_OPS,
nullptr
};
#endif /* CSVFMTS_ENABLED */