This repository has been archived by the owner on Jul 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lowranceusr4.cc
1080 lines (874 loc) · 29.1 KB
/
lowranceusr4.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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Access to Lowrance USR version 4 files.
Contributed to gpsbabel by Kris Beevers (beevek at gmail.com)
Copyright (C) 2011 Robert Lipe, robertlipe+source@gpsbabel.org
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., 59 Temple Place - Suite 330, Boston, MA 02111 USA
HISTORY:
01/06/2012 - Kris Beevers (beevek at gmail.com)
- First pass read-write support
*/
#include "defs.h"
#include <math.h> /* for lat/lon conversion */
#include <time.h> /* for gmtime */
#include <stdio.h> /* for gmtime */
#include <stdlib.h> // atoi
/* from waypt.c, we need to iterate over waypoints when extracting
routes */
#if NEWQ
extern QList<Waypoint*> waypt_list;
#else
extern queue waypt_head;
#endif
static gbfile* file_in;
static gbfile* file_out;
static short_handle mkshort_handle;
static route_head* trk_head;
static route_head* rte_head;
static int reading_version;
static int waypt_uid;
static int route_uid;
static int track_uid;
static Waypoint** waypt_table;
static int waypt_table_sz, waypt_table_ct;
static char* opt_title;
static char* opt_serialnum;
static int opt_serialnum_i;
static char* opt_content_descr;
#define MYNAME "Lowrance USR4"
#define MAXUSRSTRINGSIZE 256
#define SEMIMINOR 6356752.3142
#define DEGREESTORADIANS 0.017453292
typedef struct {
format_specific_data fs;
int uid_unit;
int uid_seq_low;
int uid_seq_high;
} lowranceusr4_fsdata;
static int
lowranceusr4_readstr(char* buf, const int maxlen, gbfile* file, int bytes_per_char)
{
int org, len;
int bytesread = 0;
org = len = gbfgetint32(file); /* bytes */
if (len < 0) {
buf[0] = '\0'; /* seems len=-1 means no string */
return 0;
} else if (len) {
if (len > maxlen*bytes_per_char) {
len = maxlen*bytes_per_char;
}
if (bytes_per_char == 1) {
bytesread += gbfread(buf, 1, len, file);
} else {
/* simple adjustment to read strings where characters are 16
bits (or more). for now let's just project the characters
down onto utf-8 space by ignoring all but the most
significant byte. */
int i, j;
char discard;
for (i = 0; i < len/bytes_per_char; ++i) {
bytesread += gbfread(&buf[i], 1, 1, file);
for (j = 1; j < bytes_per_char; ++j) {
bytesread +=gbfread(&discard, 1, 1, file);
}
}
buf[len/bytes_per_char] = '\0';
}
if (org > bytesread) {
(void) gbfseek(file, (org - bytesread), SEEK_CUR);
}
}
return len;
}
static void
lowranceusr4_writestr(const QString& buf, gbfile* file, unsigned int bytes_per_char)
{
unsigned int len = 0;
len = buf.length();
if (0xffffffff / bytes_per_char < len) {
/* be pedantic and check for the unlikely event that we are asked
to write more than 2^32 bytes */
len = 0xffffffff / bytes_per_char;
}
gbfputint32(len*bytes_per_char, file_out);
if (bytes_per_char == 1) {
(void) gbfwrite(CSTR(buf), 1, len, file);
} else {
for (unsigned int i = 0; i < len; ++i) {
gbfputc(buf[i].cell(), file_out);
for (unsigned int j = 1; j < bytes_per_char; ++j) {
gbfputc('\0', file_out);
}
}
}
}
static
arglist_t lowranceusr4_args[] = {
{
"title", &opt_title, "(output) Output title string",
"", ARGTYPE_STRING, ARG_NOMINMAX
},
{
"serialnum", &opt_serialnum, "(output) Device serial number",
"0", ARGTYPE_INT, ARG_NOMINMAX
},
{
"description", &opt_content_descr, "(output) Content description",
"", ARGTYPE_STRING, ARG_NOMINMAX
},
ARG_TERMINATOR
};
static void
rd_init(const QString& fname)
{
file_in = gbfopen_le(fname, "rb", MYNAME);
}
static void
rd_deinit(void)
{
gbfclose(file_in);
}
static void
wr_init(const QString& fname)
{
file_out = gbfopen_le(fname, "wb", MYNAME);
mkshort_handle = mkshort_new_handle();
}
static void
wr_deinit(void)
{
gbfclose(file_out);
mkshort_del_handle(&mkshort_handle);
}
/**
* Latitude and longitude for USR coords are in the lowrance mercator meter
* format in WGS84. The below code converts them to degrees.
*/
static double
lon_mm_to_deg(double x)
{
return x / (DEGREESTORADIANS * SEMIMINOR);
}
static double
lat_mm_to_deg(double x)
{
return (2.0 * atan(exp(x / SEMIMINOR)) - M_PI / 2.0) / DEGREESTORADIANS;
}
/* will be useful for write support */
static long
lon_deg_to_mm(double x)
{
return (long)(x * SEMIMINOR * DEGREESTORADIANS);
}
static long
lat_deg_to_mm(double x)
{
return (long)(SEMIMINOR * log(tan((x * DEGREESTORADIANS + M_PI / 2.0) / 2.0)));
}
static time_t
lowranceusr4_get_timestamp(int jd_number, time_t t)
{
int a, b, c, d, e, m;
struct tm* ptm, ntm;
time_t out;
/* get UTC time from time_t */
ptm = gmtime(&t);
memset(&ntm, 0, sizeof(ntm));
ntm.tm_hour = ptm->tm_hour;
ntm.tm_min = ptm->tm_min;
ntm.tm_sec = ptm->tm_sec;
/* convert the JD number to get day/month/year */
a = jd_number + 32044;
b = (4*a + 3) / 146097;
c = a - (146097*b) / 4;
d = (4*c + 3) / 1461;
e = c - (1461*d) / 4;
m = (5*e + 2) / 153;
ntm.tm_mday = e + 1 - (153*m + 2) / 5;
ntm.tm_mon = m + 3 - 12 * (m / 10) - 1;
ntm.tm_year = 100 * b + d - 4800 + m / 10 - 1900;
/* put it all back together into a unix timestamp in UTC */
out = mkgmtime(&ntm);
return out;
}
static int
lowranceusr4_jd_from_timestamp(time_t t)
{
return (int)round((float)t / 86400.0 + 2440587.0);
}
static void
lowranceusr4_copy_fsdata(lowranceusr4_fsdata** dest, lowranceusr4_fsdata* src)
{
*dest = (lowranceusr4_fsdata*)xmalloc(sizeof(*src));
** dest = *src;
(*dest)->fs.next = NULL;
}
static void
lowranceusr4_free_fsdata(void* fsdata)
{
xfree(fsdata);
}
static
lowranceusr4_fsdata*
lowranceusr4_alloc_fsdata(void)
{
lowranceusr4_fsdata* fsdata = (lowranceusr4_fsdata*) xcalloc(sizeof(*fsdata), 1);
fsdata->fs.type = FS_LOWRANCEUSR4;
fsdata->fs.copy = (fs_copy) lowranceusr4_copy_fsdata;
fsdata->fs.destroy = lowranceusr4_free_fsdata;
fsdata->fs.convert = NULL;
fsdata->uid_unit = 0;
fsdata->uid_seq_low = 0;
fsdata->uid_seq_high = 0;
return fsdata;
}
/* below couple of functions mostly borrowed from raymarine.c */
/* make waypoint shortnames unique */
static char
same_points(const Waypoint* A, const Waypoint* B)
{
return ( /* !!! We are case-sensitive !!! */
(A->shortname == B->shortname) &&
(A->latitude == B->latitude) &&
(A->longitude == B->longitude));
}
static void
register_waypt(const Waypoint* ref)
{
int i;
Waypoint* wpt = (Waypoint*) ref;
for (i = 0; i < waypt_table_ct; i++) {
Waypoint* cmp = waypt_table[i];
if (same_points(wpt, cmp)) {
return;
}
}
if (waypt_table_ct >= waypt_table_sz) {
waypt_table_sz += 32;
if (waypt_table) {
waypt_table = (Waypoint**) xrealloc(waypt_table, waypt_table_sz * sizeof(wpt));
} else {
waypt_table = (Waypoint**) xmalloc(waypt_table_sz * sizeof(wpt));
}
}
if (global_opts.debug_level >= 2) {
printf(MYNAME " adding waypt %s (%s) to table at index %d\n",
qPrintable(wpt->shortname), qPrintable(wpt->description), waypt_table_ct);
}
waypt_table[waypt_table_ct] = (Waypoint*)wpt;
waypt_table_ct++;
}
/* end borrowed from raymarine.c */
static int
lowranceusr4_find_waypt_index(const Waypoint* wpt)
{
int i;
for (i = 0; i < waypt_table_ct; ++i) {
if (same_points(wpt, (const Waypoint*)waypt_table[i])) {
return i;
}
}
return waypt_table_ct+1; /* should never happen */
}
static void
lowranceusr4_parse_waypoints(void)
{
short int icon_num;
unsigned int i, num_waypts, create_date, create_time;
int text_len;
char buff[MAXUSRSTRINGSIZE + 1];
num_waypts = gbfgetint32(file_in);
if (global_opts.debug_level >= 1) {
printf(MYNAME " parse_waypoints: Num waypoints %d\n", num_waypts);
}
for (i = 0; i < num_waypts; ++i) {
Waypoint* wpt_tmp;
wpt_tmp = new Waypoint;
lowranceusr4_fsdata* fsdata = lowranceusr4_alloc_fsdata();
fs_chain_add(&(wpt_tmp->fs), (format_specific_data*) fsdata);
/* read/parse waypoint, with fields as follows (taken mostly
from http://lowranceusrv4togpxconverter.blogspot.com/):
UID unit number - uint32
UID sequence number - int64
Waypt stream version number - uint16
Waypt name length (bytes) - uint32
Waypoint name - utf-16 string w/above length (w->shortname)
Longitude (mercator meters) - int32 (w->longitude)
Latitude (mercator meters) - int32 (w->latitude)
Flags - uint32
Icon ID - uint16 (to w->icon_descr via conversion)
Color ID - uint16
Description length (bytes) - uint32
Description - utf-16 string w/above length (w->description)
Alarm radius - float (w->proximity)
Creation date - uint32 (w->creation_time)
Creation time - uint32 (w->creation_time)
Unused - uint8
Depth (feet) - float (w->depth)
Loran GRI - int32
Loran TdA - int32
Loran TdB - int32
*/
/* UID unit number */
fsdata->uid_unit = gbfgetint32(file_in);
/* 64-bit UID sequence number */
fsdata->uid_seq_low = gbfgetint32(file_in);
fsdata->uid_seq_high = gbfgetint32(file_in);
/* Waypt stream version number, discard for now */
gbfgetint16(file_in);
/* Waypoint name; input is 2 bytes per char, we convert to 1 */
text_len = lowranceusr4_readstr(&buff[0], MAXUSRSTRINGSIZE, file_in, 2);
if (text_len) {
buff[text_len] = '\0';
wpt_tmp->shortname = buff;
}
/* Long/Lat */
wpt_tmp->longitude = lon_mm_to_deg(gbfgetint32(file_in));
wpt_tmp->latitude = lat_mm_to_deg(gbfgetint32(file_in));
/* Flags, discard for now */
gbfgetint32(file_in);
/* Icon ID; TODO: need to run this through something like
lowranceusr_find_desc_from_icon_number to convert to a gpsbabel
icon description; however it doesn't seem that the icon ids
used in usr4 match those from usr{2,3} so we need a new
mapping. */
icon_num = gbfgetint16(file_in);
(void) icon_num; // Hush warning for now.
/* wpt_tmp->icon_descr = lowranceusr_find_desc_from_icon_number(icon_num); */
/* Color ID, discard for now */
gbfgetint16(file_in);
/* Waypoint descr; input is 2 bytes per char, we convert to 1 */
text_len = lowranceusr4_readstr(&buff[0], MAXUSRSTRINGSIZE, file_in, 2);
if (text_len) {
buff[text_len] = '\0';
wpt_tmp->description = buff;
}
/* Alarm radius; XXX: I'm not sure what the units are here,
assuming meters but may be feet? */
WAYPT_SET(wpt_tmp, proximity, gbfgetflt(file_in));
/* Creation date/time; the date is a Julian day number, and the
time is a unix timestamp. */
create_date = gbfgetint32(file_in);
create_time = gbfgetint32(file_in);
// Julian date 2440487 is 1/1/1970. If that's the date we're working
// with, as a practical matter, we have no date, so don't even compute
// or set it.
if (create_date > 2440587) {
wpt_tmp->SetCreationTime(lowranceusr4_get_timestamp(create_date,
create_time));
}
/* Unused byte */
gbfgetc(file_in);
/* Depth in feet */
WAYPT_SET(wpt_tmp, depth, FEET_TO_METERS(gbfgetflt(file_in)));
/* Loran data, discard for now */
gbfgetint32(file_in);
gbfgetint32(file_in);
gbfgetint32(file_in);
if (global_opts.debug_level >= 1) {
printf(MYNAME " parse_waypoints: name = %s, uid_unit = %u, "
"uid_seq_low = %d, uid_seq_high = %d, lat = %f, lon = %f, depth = %f\n",
qPrintable(wpt_tmp->shortname), fsdata->uid_unit,
fsdata->uid_seq_low, fsdata->uid_seq_high,
wpt_tmp->latitude, wpt_tmp->longitude, wpt_tmp->depth);
}
waypt_add(wpt_tmp);
}
}
static Waypoint*
lowranceusr4_find_waypt(int uid_unit, int uid_seq_low, int uid_seq_high)
{
#if !NEWQ
queue* elem, *tmp;
#endif
lowranceusr4_fsdata* fs = NULL;
#if NEWQ
// Iterate with waypt_disp_all?
foreach(Waypoint* waypointp, waypt_list) {
#else
QUEUE_FOR_EACH(&waypt_head, elem, tmp) {
Waypoint* waypointp = (Waypoint*) elem;
#endif
fs = (lowranceusr4_fsdata*) fs_chain_find(waypointp->fs, FS_LOWRANCEUSR4);
if (fs && fs->uid_unit == uid_unit &&
fs->uid_seq_low == uid_seq_low &&
fs->uid_seq_high == uid_seq_high) {
return waypointp;
}
}
if (global_opts.debug_level >= 1) {
printf(MYNAME " lowranceusr4_find_waypt: warning, failed finding waypoint with ids %d %d %d\n",
uid_unit, uid_seq_low, uid_seq_high);
}
return NULL;
}
static void
lowranceusr4_parse_routes(void)
{
unsigned int num_routes, i, j, text_len;
unsigned int num_legs;
char buff[MAXUSRSTRINGSIZE + 1];
Waypoint* wpt_tmp;
unsigned int uid_unit, uid_seq_low, uid_seq_high;
num_routes = gbfgetint32(file_in);
if (global_opts.debug_level >= 1) {
printf(MYNAME " parse_routes: Num routes = %d\n", num_routes);
}
for (i = 0; i < num_routes; ++i) {
rte_head = route_head_alloc();
route_add_head(rte_head);
rte_head->rte_num = i+1;
lowranceusr4_fsdata* fsdata = lowranceusr4_alloc_fsdata();
fs_chain_add(&(rte_head->fs), (format_specific_data*) fsdata);
/* read/parse route, with fields as follows (taken mostly
from http://lowranceusrv4togpxconverter.blogspot.com/):
UID unit number - uint32
UID sequence number - int64
Route stream version number - uint16
Route name length (bytes) - uint32
Route name - utf-16 string w/above length (r->rte_name)
Number of waypoints - uint32 (N)
Waypoint list - sequence of N (uint32, uint64) waypoint UIDs
*/
/* UID unit number */
fsdata->uid_unit = gbfgetint32(file_in);
/* 64-bit UID sequence number */
fsdata->uid_seq_low = gbfgetint32(file_in);
fsdata->uid_seq_high = gbfgetint32(file_in);
/* Route stream version number, discard for now */
gbfgetint16(file_in);
/* Route name; input is 2 bytes per char, we convert to 1 */
text_len = lowranceusr4_readstr(&buff[0], MAXUSRSTRINGSIZE, file_in, 2);
if (text_len) {
buff[text_len] = '\0';
rte_head->rte_name = buff;
}
num_legs = gbfgetint32(file_in);
if (global_opts.debug_level >= 1) {
printf(MYNAME " parse_routes: route name=%s has %d waypoints\n",
qPrintable(rte_head->rte_name), num_legs);
}
for (j = 0; j < num_legs; ++j) {
uid_unit = gbfgetint32(file_in);
uid_seq_low = gbfgetint32(file_in);
uid_seq_high = gbfgetint32(file_in);
wpt_tmp = lowranceusr4_find_waypt(uid_unit, uid_seq_low, uid_seq_high);
if (wpt_tmp) {
if (global_opts.debug_level >= 2) {
printf(MYNAME " parse_routes: added wpt %s to route %s\n",
qPrintable(wpt_tmp->shortname), qPrintable(rte_head->rte_name));
}
route_add_wpt(rte_head, new Waypoint(*wpt_tmp));
}
}
/* Mystery byte, discard */
gbfgetc(file_in);
}
}
static void
lowranceusr4_parse_trails(void)
{
int num_trails, num_trail_pts, M, i, j, k, trk_num, text_len;
char buff[MAXUSRSTRINGSIZE + 1];
Waypoint* wpt_tmp;
/* num trails */
num_trails = gbfgetint32(file_in);
if (global_opts.debug_level >= 1) {
printf(MYNAME " parse_trails: num trails = %d\n", num_trails);
}
for (i = trk_num = 0; i < num_trails; ++i) {
trk_head = route_head_alloc();
trk_head->rte_num = ++trk_num;
track_add_head(trk_head);
lowranceusr4_fsdata* fsdata = lowranceusr4_alloc_fsdata();
fs_chain_add(&(trk_head->fs), (format_specific_data*) fsdata);
/* read/parse trail, with fields as follows (taken mostly from
http://lowranceusrv4togpxconverter.blogspot.com/):
UID unit number - uint32
UID sequence number - int64
Trail stream version number - uint16
Trail name length (bytes) - uint32
Trail name - utf-16 string w/above length (t->rte_name)
Flags - uint32
Color ID - uint32
Comment length (bytes) - uint32
Comment - utf-16 string w/above length (t->rte_desc)
Creation date - uint32
Creation time - uint32
Unused - uint8
Active flag - uint8
Visible flag - uint8
Data count (?) - uint32
Data type depth (?) - uint8
Data type water temp (?) - uint8
Data type SOG (?) - uint8
Trackpoint count - int32 (N)
Trackpoint list - sequence of N objects as follows:
Unknown (?) - uint16
Unknown (?) - uint8
POSIX timestamp (?) - uint32 (w->creation_time)
Longitude (radians) - double (w->longitude)
Latitude (radians) - double (w->latitude)
Data item count - uint32 (M)
Data items - sequence of M objects as follows:
Unknown (?) - uint8
Unknown (?) - float
*/
/* UID unit number */
fsdata->uid_unit = gbfgetint32(file_in);
/* 64-bit UID sequence number */
fsdata->uid_seq_low = gbfgetint32(file_in);
fsdata->uid_seq_high = gbfgetint32(file_in);
/* Trail stream version number, discard for now */
gbfgetint16(file_in);
/* Trail name; input is 2 bytes per char, we convert to 1 */
text_len = lowranceusr4_readstr(&buff[0], MAXUSRSTRINGSIZE, file_in, 2);
if (text_len) {
buff[text_len] = '\0';
trk_head->rte_name = buff;
}
/* Flags, discard for now */
gbfgetint32(file_in);
/* Color ID, discard for now */
gbfgetint32(file_in);
/* Comment/description */
text_len = lowranceusr4_readstr(&buff[0], MAXUSRSTRINGSIZE, file_in, 2);
if (text_len) {
buff[text_len] = '\0';
trk_head->rte_desc = buff;
}
/* Creation date/time, discard for now */
gbfgetint32(file_in);
gbfgetint32(file_in);
/* Some flag bytes, discard for now */
gbfgetc(file_in);
gbfgetc(file_in);
gbfgetc(file_in);
/* Some mysterious "data count" and "data type" stuff, not sure
what it's for, need dox */
gbfgetint32(file_in);
gbfgetc(file_in);
gbfgetc(file_in);
gbfgetc(file_in);
num_trail_pts = gbfgetint32(file_in);
if (global_opts.debug_level >= 1) {
printf(MYNAME " parse_trails: trail %d name=%s has %d trackpoints\n",
trk_num, qPrintable(trk_head->rte_name), num_trail_pts);
}
for (j = 0; j < num_trail_pts; ++j) {
wpt_tmp = new Waypoint;
/* Some unknown bytes */
gbfgetint16(file_in);
gbfgetc(file_in);
/* POSIX timestamp */
wpt_tmp->SetCreationTime(QDateTime::fromTime_t(gbfgetint32(file_in)));
/* Long/Lat */
wpt_tmp->longitude = gbfgetdbl(file_in) / DEGREESTORADIANS; /* rad to deg */
wpt_tmp->latitude = gbfgetdbl(file_in) / DEGREESTORADIANS;
/* Mysterious per-trackpoint data, toss it for now */
M = gbfgetint32(file_in);
for (k = 0; k < M; ++k) {
gbfgetc(file_in);
gbfgetflt(file_in);
}
track_add_wpt(trk_head, wpt_tmp);
if (global_opts.debug_level >= 2) {
printf(MYNAME " parse_routes: added trackpoint %f,%f to route %s\n",
wpt_tmp->latitude, wpt_tmp->longitude, qPrintable(trk_head->rte_name));
}
}
}
}
static void
data_read(void)
{
short int MajorVersion, MinorVersion;
int text_len, DataStreamVersion;
unsigned int create_date, create_time, serial_num;
unsigned char byte;
char buff[MAXUSRSTRINGSIZE + 1];
MajorVersion = gbfgetint16(file_in);
reading_version = MajorVersion;
MinorVersion = gbfgetint16(file_in);
DataStreamVersion = gbfgetint32(file_in);
if (global_opts.debug_level >= 1) {
printf(MYNAME " data_read: Major Version %d Minor Version %d Data Stream Version %d\n",
MajorVersion, MinorVersion, DataStreamVersion);
}
if (MajorVersion != 4) {
fatal(MYNAME ": input file is from an unsupported version of the USR format (must be version 4)\n");
}
text_len = lowranceusr4_readstr(&buff[0], MAXUSRSTRINGSIZE, file_in, 1);
if (text_len > 0 && global_opts.debug_level >= 1) {
buff[text_len] = '\0';
printf(MYNAME " file title: %s\n", buff);
}
text_len = lowranceusr4_readstr(&buff[0], MAXUSRSTRINGSIZE, file_in, 1);
if (text_len > 0 && global_opts.debug_level >= 1) {
buff[text_len] = '\0';
printf(MYNAME " date string: %s\n", buff);
}
/* for now we won't use these for anything */
create_date = gbfgetint32(file_in);
(void) create_date;
create_time = gbfgetint32(file_in);
(void) create_time;
byte = gbfgetc(file_in); /* unused, apparently */
(void) byte;
serial_num = gbfgetint32(file_in);
if (global_opts.debug_level >= 1) {
printf(MYNAME " device serial number %u\n", (unsigned int)serial_num);
}
text_len = lowranceusr4_readstr(&buff[0], MAXUSRSTRINGSIZE, file_in, 1);
if (text_len > 0 && global_opts.debug_level >= 1) {
buff[text_len] = '\0';
printf(MYNAME " content description: %s\n", buff);
}
lowranceusr4_parse_waypoints();
lowranceusr4_parse_routes();
lowranceusr4_parse_trails();
}
static void
lowranceusr4_waypt_disp(const Waypoint* wpt)
{
/* UID unit number */
gbfputint32(opt_serialnum_i, file_out);
/* 64-bit UID sequence number */
gbfputint32(waypt_uid++, file_out);
gbfputint32(0, file_out);
/* Waypt stream version number: this always seems to be 2 in my data
so that's what I'll use */
gbfputint16(2, file_out);
/* Waypt name */
lowranceusr4_writestr(wpt->shortname, file_out, 2);
/* Long/Lat */
gbfputint32(lon_deg_to_mm(wpt->longitude), file_out);
gbfputint32(lat_deg_to_mm(wpt->latitude), file_out);
/* Flags: this always seems to be 2 or 4 in my data, not sure what
it means */
gbfputint32(2, file_out);
/* Icon ID; TODO: need to invert icon description to an icon number,
see parse_waypoints above */
gbfputint16(0, file_out);
/* Color ID */
gbfputint16(0, file_out);
/* Waypt description */
lowranceusr4_writestr(wpt->description, file_out, 2);
/* Alarm radius */
gbfputflt(WAYPT_GET(wpt, proximity, 0.0), file_out);
/* Creation date/time */
gbfputint32(lowranceusr4_jd_from_timestamp(wpt->GetCreationTime().toTime_t()), file_out);
gbfputint32(wpt->GetCreationTime().toTime_t(), file_out);
/* Unused byte */
gbfputc(0, file_out);
/* Depth in feet */
gbfputflt(METERS_TO_FEET(WAYPT_GET(wpt, depth, 0.0)), file_out);
/* Loran data */
gbfputint32(0, file_out);
gbfputint32(0, file_out);
gbfputint32(0, file_out);
}
static void
lowranceusr4_write_waypoints(void)
{
int i;
/* enumerate all waypoints from both the plain old waypoint list and
also all routes */
waypt_table_sz = 0;
waypt_table_ct = 0;
waypt_table = NULL;
waypt_disp_all(register_waypt);
route_disp_all(NULL, NULL, register_waypt);
if (global_opts.debug_level >= 1) {
printf(MYNAME " writing %d waypoints\n", waypt_table_ct);
}
gbfputint32(waypt_table_ct, file_out);
waypt_uid = 0;
for (i = 0; i < waypt_table_ct; ++i) {
if (global_opts.debug_level >= 2) {
printf(MYNAME " writing out waypt %d (%s - %s)\n",
i, qPrintable(waypt_table[i]->shortname), qPrintable(waypt_table[i]->description));
}
lowranceusr4_waypt_disp((const Waypoint*)waypt_table[i]);
}
}
static void
lowranceusr4_write_route_hdr(const route_head* rte)
{
if (global_opts.debug_level >= 1) {
printf(MYNAME " writing route #%d (%s) with %d waypts\n",
route_uid, qPrintable(rte->rte_name), rte->rte_waypt_ct);
}
/* UID unit number */
gbfputint32(opt_serialnum_i, file_out);
/* 64-bit UID sequence number */
gbfputint32(route_uid++, file_out);
gbfputint32(0, file_out);
/* Route stream version number: seems to be 1 in my data */
gbfputint16(1, file_out);
/* Waypt name */
lowranceusr4_writestr(rte->rte_name, file_out, 2);
/* Num waypoints */
gbfputint32(rte->rte_waypt_ct, file_out);
}
static void
lowranceusr4_write_wpt_uids(const Waypoint* wpt)
{
int waypt_idx;
/* find the index of wpt in our table */
waypt_idx = lowranceusr4_find_waypt_index(wpt);
if (global_opts.debug_level >= 2) {
if (waypt_idx > waypt_table_ct) {
printf(MYNAME " WARNING: failed finding waypoint %s in waypoint table\n",
qPrintable(wpt->shortname));
} else {
printf(MYNAME " adding waypt %d (%s) to route\n",
waypt_idx, qPrintable(waypt_table[waypt_idx]->shortname));
}
}
gbfputint32(opt_serialnum_i, file_out);
gbfputint32(waypt_idx, file_out);
gbfputint32(0, file_out);
}
static void
lowranceusr4_write_route_trl(const route_head* rte)
{
/* Mystery byte */
gbfputc(0, file_out);
}
static void
lowranceusr4_write_routes(void)
{
if (global_opts.debug_level >= 1) {
printf(MYNAME " writing %d routes\n", route_count());
}
gbfputint32(route_count(), file_out);
route_uid = 0;
route_disp_all(lowranceusr4_write_route_hdr,
lowranceusr4_write_route_trl,
lowranceusr4_write_wpt_uids);
}
static void
lowranceusr4_write_track_hdr(const route_head* trk)
{
if (global_opts.debug_level >= 1) {
printf(MYNAME " writing track %d (%s) with %d trackpoints\n",
track_uid, qPrintable(trk->rte_name), trk->rte_waypt_ct);
}
/* UID unit number */
gbfputint32(opt_serialnum_i, file_out);
/* 64-bit UID sequence number */
gbfputint32(track_uid++, file_out);
gbfputint32(0, file_out);
/* Route stream version number: always seems to be 3 in my data */
gbfputint16(3, file_out);
/* Track name */
lowranceusr4_writestr(trk->rte_name, file_out, 2);
/* Flags: always seems to be 2 in my data */
gbfputint32(2, file_out);
/* Color ID */
gbfputint32(0, file_out);
/* Comment */
lowranceusr4_writestr(trk->rte_desc, file_out, 2);
/* Creation date/time */
gbfputint32(0, file_out);
gbfputint32(0, file_out);
/* Unused byte */
gbfputc(0, file_out);
/* Active flag */
gbfputc(0, file_out);
/* Visible flag; I'll just assume all tracks should be visible for
now */
gbfputc(1, file_out);
/* Mysterious "data count" and "data type" stuff */
gbfputint32(0, file_out);
gbfputc(0, file_out);
gbfputc(0, file_out);
gbfputc(0, file_out);
/* Trackpoint count */
gbfputint32(trk->rte_waypt_ct, file_out);
}
static void
lowranceusr4_write_track_waypt(const Waypoint* wpt)
{
/* Some unknown bytes */
gbfputint16(0, file_out);
gbfputc(0, file_out);
/* Timestamp */
gbfputint32(wpt->GetCreationTime().toTime_t(), file_out);
/* Long/Lat */