forked from OpenCPN/OpenCPN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cm93.cpp
7037 lines (5371 loc) · 246 KB
/
cm93.cpp
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
/***************************************************************************
*
* Project: OpenCPN
* Purpose: cm93 Chart Object
* Author: David Register
*
***************************************************************************
* Copyright (C) 2010 by David S. Register *
* *
* 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. *
**************************************************************************/
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif //precompiled headers
#include <wx/textfile.h>
#include <wx/tokenzr.h>
#include <wx/arrstr.h>
#include <wx/mstream.h>
#include <wx/spinctrl.h>
#include <wx/listctrl.h>
#include <wx/regex.h>
#include <algorithm>
#include "mygdal/ogr_api.h"
#include "s57chart.h"
#include "cm93.h"
#include "s52plib.h"
#include "georef.h"
#include "mygeom.h"
#include "cutil.h"
#include "navutil.h" // for LogMessageOnce
#include "ocpn_pixel.h" // for ocpnUSE_DIBSECTION
#include "ocpndc.h"
#include "pluginmanager.h" // for PlugInManager
#include "OCPNPlatform.h"
#include "wx28compat.h"
#include "ChartDataInputStream.h"
#include <stdio.h>
#ifdef ocpnUSE_GL
#include "glChartCanvas.h"
extern ocpnGLOptions g_GLOptions;
#endif
#ifdef __MSVC__
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__ )
#define new DEBUG_NEW
#endif
extern ChartCanvas *cc1;
extern CM93OffsetDialog *g_pCM93OffsetDialog;
extern OCPNPlatform *g_Platform;
extern wxString g_SENCPrefix;
extern s52plib *ps52plib;
extern MyConfig *pConfig;
extern bool g_bDebugCM93;
extern int g_cm93_zoom_factor;
extern PopUpDSlide *pPopupDetailSlider;
extern int g_detailslider_dialog_x, g_detailslider_dialog_y;
extern bool g_bShowDetailSlider;
extern wxString g_PrivateDataDir;
// Flav add for CM93Offset manual setup
extern double g_CM93Maps_Offset_x;
extern double g_CM93Maps_Offset_y;
extern bool g_CM93Maps_Offset_on;
extern bool g_bopengl;
extern PlugInManager *g_pi_manager;
extern float g_GLMinSymbolLineWidth;
extern bool g_b_EnableVBO;
extern PFNGLBINDBUFFERPROC s_glBindBuffer;
// TODO These should be gotten from the ctor
extern MyFrame *gFrame;
#include <wx/arrimpl.cpp>
WX_DEFINE_OBJARRAY ( Array_Of_M_COVR_Desc );
#include <wx/listimpl.cpp>
WX_DEFINE_LIST ( List_Of_M_COVR_Desc );
void appendOSDirSep ( wxString* pString )
{
wxChar sep = wxFileName::GetPathSeparator();
if ( pString->Last() != sep )
pString->Append ( sep );
}
//----------------------------------------------------------------------------
// M_COVR_Desc Implementation
//----------------------------------------------------------------------------
M_COVR_Desc::M_COVR_Desc()
{
pvertices = NULL;
gl_screen_vertices = NULL;
gl_screen_projection_type = PROJECTION_UNKNOWN;
user_xoff = 0.;
user_yoff = 0.;
m_centerlat_cos = 1.0;
m_buser_offsets = false;
m_ngl_vertices = 0;
gl_screen_vertices = NULL;
}
M_COVR_Desc::~M_COVR_Desc()
{
delete[] pvertices;
delete[] gl_screen_vertices;
}
int M_COVR_Desc::GetWKBSize()
{
int size = 0;
size = sizeof ( int ); // size itself
size += sizeof ( int ); // m_cell_index;
size += sizeof ( int ); // m_object_id;
size += sizeof ( int ); // m_subcell
size += sizeof ( int ); // m_nvertices;
size += m_nvertices * sizeof ( float_2Dpt ); // pvertices;
size += sizeof ( int ); // m_npub_year;
size += 8 * sizeof ( double ); // all the rest
return size;
}
bool M_COVR_Desc:: WriteWKB ( void *p )
{
if ( p )
{
int *pr = ( int * ) p;
*pr++ = GetWKBSize();
*pr++ = m_cell_index;
*pr++ = m_object_id;
*pr++ = m_subcell;
*pr++ = m_nvertices;
float_2Dpt *pfo = ( float_2Dpt * ) pr;
float_2Dpt *pfi = pvertices;
for ( int i=0 ; i < m_nvertices ; i++ )
*pfo++ = *pfi++;
int *pi = ( int * ) pfo;
*pi++ = m_npub_year;
double *pd = ( double * ) pi;
*pd++ = transform_WGS84_offset_x;
*pd++ = transform_WGS84_offset_y;
*pd++ = m_covr_lat_min;
*pd++ = m_covr_lat_max;
*pd++ = m_covr_lon_min;
*pd++ = m_covr_lon_max;
double centerlat_cos = cos( ((m_covr_lat_min + m_covr_lat_max)/2.) * PI/180. );
*pd++ = user_xoff * centerlat_cos;
*pd++ = user_yoff * centerlat_cos;
}
return true;
}
int M_COVR_Desc:: ReadWKB ( wxFFileInputStream &ifs )
{
// Read the length of the WKB
int length = 0;
if ( !ifs.Read ( &length, sizeof ( int ) ).Eof() )
{
ifs.Read ( &m_cell_index, sizeof ( int ) );
ifs.Read ( &m_object_id, sizeof ( int ) );
ifs.Read ( &m_subcell, sizeof ( int ) );
ifs.Read ( &m_nvertices, sizeof ( int ) );
assert(m_nvertices > 0);
pvertices = new float_2Dpt[m_nvertices];
ifs.Read ( pvertices,m_nvertices * sizeof ( float_2Dpt ) );
ifs.Read ( &m_npub_year, sizeof ( int ) );
ifs.Read ( &transform_WGS84_offset_x, sizeof ( double ) );
ifs.Read ( &transform_WGS84_offset_y, sizeof ( double ) );
ifs.Read ( &m_covr_lat_min, sizeof ( double ) );
ifs.Read ( &m_covr_lat_max, sizeof ( double ) );
ifs.Read ( &m_covr_lon_min, sizeof ( double ) );
ifs.Read ( &m_covr_lon_max, sizeof ( double ) );
m_centerlat_cos = cos( ((m_covr_lat_min + m_covr_lat_max)/2.) * PI/180. );
ifs.Read ( &user_xoff, sizeof ( double ) );
ifs.Read ( &user_yoff, sizeof ( double ) );
user_xoff /= m_centerlat_cos;
user_yoff /= m_centerlat_cos;
if ( ( fabs ( user_xoff ) > 1. ) || ( fabs ( user_yoff ) > 1. ) )
m_buser_offsets = true;
else
m_buser_offsets = false;
m_covr_bbox.Set( m_covr_lat_min, m_covr_lon_min,
m_covr_lat_max, m_covr_lon_max );
}
return length;
}
OCPNRegion M_COVR_Desc::GetRegion ( const ViewPort &vp, wxPoint *pwp )
{
float_2Dpt *p = pvertices;
for ( int ip = 0 ; ip < m_nvertices ; ip++ )
{
double plon = p->x;
if ( fabs ( plon - vp.clon ) > 180. )
{
if ( plon > vp.clon )
plon -= 360.;
else
plon += 360.;
}
double easting, northing, epix, npix;
toSM ( p->y, plon + 360., vp.clat, vp.clon + 360, &easting, &northing );
// easting -= transform_WGS84_offset_x;
easting -= user_xoff;
// northing -= transform_WGS84_offset_y;
northing -= user_yoff;
epix = easting * vp.view_scale_ppm;
npix = northing * vp.view_scale_ppm;
pwp[ip].x = ( int ) round ( ( vp.pix_width / 2 ) + epix );
pwp[ip].y = ( int ) round ( ( vp.pix_height / 2 ) - npix );
p++;
}
return OCPNRegion ( m_nvertices, pwp );
}
//----------------------------------------------------------------------------
// cm93 covr_set object class
// This is a helper class which holds all the known information
// relating to cm93 cell MCOVR objects of a particular scale
//----------------------------------------------------------------------------
WX_DECLARE_HASH_MAP ( int, int, wxIntegerHash, wxIntegerEqual, cm93cell_hash );
char sig_version[] = "COVR1002";
class covr_set
{
public:
covr_set ( cm93chart *parent );
~covr_set();
bool Init ( wxChar scale_char, wxString &prefix );
unsigned int GetCoverCount() { return m_covr_array_outlines.GetCount(); }
M_COVR_Desc *GetCover ( unsigned int im ) { return &m_covr_array_outlines[im]; }
void Add_MCD ( M_COVR_Desc *pmcd );
bool Add_Update_MCD ( M_COVR_Desc *pmcd );
bool IsCovrLoaded ( int cell_index );
int Find_MCD ( M_COVR_Desc *pmcd );
M_COVR_Desc *Find_MCD ( int cell_index, int object_id, int sbcell );
cm93chart *m_pParent;
wxChar m_scale_char;
int m_scale;
wxString m_cachefile;
Array_Of_M_COVR_Desc m_covr_array_outlines; // array, for chart outline rendering
cm93cell_hash m_cell_hash; // This is a hash, indexed by cell index, elements contain the number of M_COVRs
// found on this particular cell
};
covr_set::covr_set ( cm93chart *parent )
{
m_pParent = parent;
}
covr_set::~covr_set()
{
// Create/Update the cache
if(m_cachefile.IsEmpty())
return; // presumably for Z scale charts
// for which we create no cache
if ( m_covr_array_outlines.GetCount() )
{
wxFFileOutputStream ofs ( m_cachefile );
if ( ofs.IsOk() )
{
ofs.Write ( sig_version, 8 ); // write signature
for ( unsigned int i=0 ; i < m_covr_array_outlines.GetCount() ; i++ )
{
int wkbsize = m_covr_array_outlines[i].GetWKBSize();
if ( wkbsize )
{
char *p = ( char* ) malloc ( wkbsize * sizeof ( char ) );
m_covr_array_outlines[i].WriteWKB ( p );
ofs.Write ( p, wkbsize );
free ( p );
}
}
ofs.Close();
}
}
}
bool covr_set::Init ( wxChar scale_char, wxString &prefix )
{
m_scale_char = scale_char;
switch ( m_scale_char )
{
case 'Z': m_scale = 20000000; break;
case 'A': m_scale = 3000000; break;
case 'B': m_scale = 1000000; break;
case 'C': m_scale = 200000; break;
case 'D': m_scale = 100000; break;
case 'E': m_scale = 50000; break;
case 'F': m_scale = 20000; break;
case 'G': m_scale = 7500; break;
default: m_scale = 20000000; break;
}
// Create the cache file name
wxString prefix_string = prefix;
wxString sep ( wxFileName::GetPathSeparator() );
prefix_string.Replace ( sep, _T ( "_" ) );
prefix_string.Replace ( _T ( ":" ), _T ( "_" ) ); // for Windows
m_cachefile = g_Platform->GetPrivateDataDir();
appendOSDirSep ( &m_cachefile );
m_cachefile += _T ( "cm93" );
appendOSDirSep ( &m_cachefile );
m_cachefile += prefix_string; // include the cm93 prefix string in the cache file name
m_cachefile += _T ( "_" ); // to support multiple cm93 data sets
wxString cache_old_old_name = m_cachefile;
cache_old_old_name += _T ( "coverset." );
cache_old_old_name += m_scale_char;
wxString cache_old_name = m_cachefile;
cache_old_name += _T ( "coverset_sig." );
cache_old_name += m_scale_char;
m_cachefile += _T ( "coverset_sigp." );
m_cachefile += m_scale_char;
wxFileName fn ( m_cachefile );
if ( !fn.DirExists() )
wxFileName::Mkdir ( fn.GetPath(), 0777, wxPATH_MKDIR_FULL );
// Preload the cache
if ( !wxFileName::FileExists ( m_cachefile ) )
{
// The signed file does not exist
// Check for an old style file, and delete if found.
if ( wxFileName::FileExists ( cache_old_name ) )
::wxRemoveFile ( cache_old_name );
if ( wxFileName::FileExists ( cache_old_old_name ) )
::wxRemoveFile ( cache_old_old_name );
return false;
}
wxFFileInputStream ifs ( m_cachefile );
if ( ifs.IsOk() )
{
char sig_bytes[9];
// Validate the file signature
if ( !ifs.Read ( &sig_bytes, 8 ).Eof() )
{
if ( strncmp ( sig_bytes, sig_version, 8 ) )
{
return false; // bad signature match
}
}
else
return false; // short file
bool b_cont = true;
while ( b_cont )
{
M_COVR_Desc *pmcd = new M_COVR_Desc;
int length = pmcd->ReadWKB ( ifs );
if ( length )
{
m_covr_array_outlines.Add ( pmcd );
if ( m_cell_hash.find ( pmcd->m_cell_index ) == m_cell_hash.end() )
m_cell_hash[pmcd->m_cell_index] = 0; // initialize the element
m_cell_hash[pmcd->m_cell_index]++; // add this M_COVR to the hash map
}
else
{
delete pmcd;
b_cont = false;
}
}
}
return true;
}
void covr_set::Add_MCD ( M_COVR_Desc *pmcd )
{
m_covr_array_outlines.Add ( pmcd );
if ( m_cell_hash.find ( pmcd->m_cell_index ) == m_cell_hash.end() ) // not present yet?
m_cell_hash[pmcd->m_cell_index] = 0; // initialize
m_cell_hash[pmcd->m_cell_index]++; // add this M_COVR to the hash map
}
bool covr_set::IsCovrLoaded ( int cell_index )
{
return ( m_cell_hash.find ( cell_index ) != m_cell_hash.end() );
}
bool covr_set::Add_Update_MCD ( M_COVR_Desc *pmcd )
{
if ( m_cell_hash.find ( pmcd->m_cell_index ) == m_cell_hash.end() ) // not present yet?
{
m_covr_array_outlines.Add ( pmcd );
m_cell_hash[pmcd->m_cell_index] = 1; // initialize
return true;
}
// There is at least one MCD already in place for this cell index
// We need to search the entire table to see if any of those MCD's
// correspond to this MCD's object identifier and subcell, as well as cell index
else
{
bool b_found = false;
for ( unsigned int i=0 ; i < m_covr_array_outlines.GetCount() ; i++ )
{
M_COVR_Desc *pmcd_candidate = &m_covr_array_outlines[i];
if ( ( pmcd_candidate->m_cell_index == pmcd->m_cell_index ) &&
( pmcd_candidate->m_object_id == pmcd->m_object_id ) &&
( pmcd_candidate->m_subcell == pmcd->m_subcell ) )
{
b_found = true;
break;
}
}
if ( !b_found )
{
m_covr_array_outlines.Add ( pmcd );
m_cell_hash[pmcd->m_cell_index]++; // add this M_COVR to the hash map
return true;
}
else
return false;
}
}
int covr_set::Find_MCD ( M_COVR_Desc *pmcd )
{
if ( m_cell_hash.find ( pmcd->m_cell_index ) == m_cell_hash.end() ) // not present?
return -1;
else
{
// There is at least one MCD already in place for this cell index
// We need to search the entire table to see if any of those MCD's
// correspond to this MCD's object identifier as well as cell index
for ( unsigned int i=0 ; i < m_covr_array_outlines.GetCount() ; i++ )
{
M_COVR_Desc *pmcd_candidate = &m_covr_array_outlines[i];
if ( ( pmcd_candidate->m_cell_index == pmcd->m_cell_index ) &&
( pmcd_candidate->m_object_id == pmcd->m_object_id ) &&
( pmcd_candidate->m_subcell == pmcd->m_subcell ) )
{
return ( int ) i;
}
}
}
return -1;
}
M_COVR_Desc *covr_set::Find_MCD ( int cell_index, int object_id, int subcell )
{
if ( m_cell_hash.find ( cell_index ) == m_cell_hash.end() ) // not present?
return NULL;
for ( unsigned int i=0 ; i < m_covr_array_outlines.GetCount() ; i++ )
{
M_COVR_Desc *pmcd_candidate = &m_covr_array_outlines[i];
if ( ( pmcd_candidate->m_cell_index == cell_index ) &&
( pmcd_candidate->m_object_id == object_id ) &&
( pmcd_candidate->m_subcell == subcell ) )
return pmcd_candidate;
}
return NULL;
}
// CM93 Encode/Decode support tables
static unsigned char Table_0[] =
{
0x0CD,0x0EA,0x0DC,0x048,0x03E,0x06D,0x0CA,0x07B,0x052,0x0E1,0x0A4,0x08E,0x0AB,0x005,0x0A7,0x097,
0x0B9,0x060,0x039,0x085,0x07C,0x056,0x07A,0x0BA,0x068,0x06E,0x0F5,0x05D,0x002,0x04E,0x00F,0x0A1,
0x027,0x024,0x041,0x034,0x000,0x05A,0x0FE,0x0CB,0x0D0,0x0FA,0x0F8,0x06C,0x074,0x096,0x09E,0x00E,
0x0C2,0x049,0x0E3,0x0E5,0x0C0,0x03B,0x059,0x018,0x0A9,0x086,0x08F,0x030,0x0C3,0x0A8,0x022,0x00A,
0x014,0x01A,0x0B2,0x0C9,0x0C7,0x0ED,0x0AA,0x029,0x094,0x075,0x00D,0x0AC,0x00C,0x0F4,0x0BB,0x0C5,
0x03F,0x0FD,0x0D9,0x09C,0x04F,0x0D5,0x084,0x01E,0x0B1,0x081,0x069,0x0B4,0x009,0x0B8,0x03C,0x0AF,
0x0A3,0x008,0x0BF,0x0E0,0x09A,0x0D7,0x0F7,0x08C,0x067,0x066,0x0AE,0x0D4,0x04C,0x0A5,0x0EC,0x0F9,
0x0B6,0x064,0x078,0x006,0x05B,0x09B,0x0F2,0x099,0x0CE,0x0DB,0x053,0x055,0x065,0x08D,0x007,0x033,
0x004,0x037,0x092,0x026,0x023,0x0B5,0x058,0x0DA,0x02F,0x0B3,0x040,0x05E,0x07F,0x04B,0x062,0x080,
0x0E4,0x06F,0x073,0x01D,0x0DF,0x017,0x0CC,0x028,0x025,0x02D,0x0EE,0x03A,0x098,0x0E2,0x001,0x0EB,
0x0DD,0x0BC,0x090,0x0B0,0x0FC,0x095,0x076,0x093,0x046,0x057,0x02C,0x02B,0x050,0x011,0x00B,0x0C1,
0x0F0,0x0E7,0x0D6,0x021,0x031,0x0DE,0x0FF,0x0D8,0x012,0x0A6,0x04D,0x08A,0x013,0x043,0x045,0x038,
0x0D2,0x087,0x0A0,0x0EF,0x082,0x0F1,0x047,0x089,0x06A,0x0C8,0x054,0x01B,0x016,0x07E,0x079,0x0BD,
0x06B,0x091,0x0A2,0x071,0x036,0x0B7,0x003,0x03D,0x072,0x0C6,0x044,0x08B,0x0CF,0x015,0x09F,0x032,
0x0C4,0x077,0x083,0x063,0x020,0x088,0x0F6,0x0AD,0x0F3,0x0E8,0x04A,0x0E9,0x035,0x01C,0x05F,0x019,
0x01F,0x07D,0x070,0x0FB,0x0D1,0x051,0x010,0x0D3,0x02E,0x061,0x09D,0x05C,0x02A,0x042,0x0BE,0x0E6
};
static unsigned char Encode_table[256];
static unsigned char Decode_table[256];
static bool cm93_decode_table_created;
// Case-insensitive cm93 directory tree depth-first traversal to find the dictionary...
// This could be made simpler, but matches the old code better as is
class FindCM93Dictionary : public wxDirTraverser
{
public:
FindCM93Dictionary ( wxString& path ) : m_path ( path ) {}
virtual wxDirTraverseResult OnFile ( const wxString& filename )
{
wxString name = filename.AfterLast ( wxFileName::GetPathSeparator() ).Lower();
if ( name == wxT ( "cm93obj.dic" ) )
{
m_path = filename;
return wxDIR_STOP;
}
return wxDIR_CONTINUE;
}
virtual wxDirTraverseResult OnDir ( const wxString& WXUNUSED ( dirname ) )
{
return wxDIR_CONTINUE;
}
private:
wxString& m_path;
};
cm93_dictionary::cm93_dictionary()
{
m_S57ClassArray = NULL;
m_AttrArray = NULL;
m_GeomTypeArray = NULL;;
m_ValTypeArray = NULL;
m_max_class = 0;
m_ok = false;
}
bool cm93_dictionary::LoadDictionary(const wxString & dictionary_dir)
{
int i, nline;
wxString line;
wxString dir ( dictionary_dir ); // a copy
bool ret_val = false;
wxChar sep = wxFileName::GetPathSeparator();
if ( dir.Last() != sep )
dir.Append ( sep );
m_dict_dir = dir;
// Build some array strings for Feature decoding
wxString sf ( dir );
sf.Append ( _T ( "CM93OBJ.DIC" ) );
if ( !wxFileName::FileExists ( sf ) )
{
sf = dir;
sf.Append ( _T ( "cm93obj.dic" ) );
if ( !wxFileName::FileExists ( sf ) )
return false;
}
wxTextFile file;
if ( !file.Open ( sf ) )
return false;
nline = file.GetLineCount();
if ( !nline )
return false;
// Read the file once to get the max class number
int iclass_max = 0;
for ( i=0 ; i<nline ; i++ )
{
line = file.GetLine ( i );
wxStringTokenizer tkz ( line, wxT ( "|" ) );
// while ( tkz.HasMoreTokens() )
{
// 6 char class name
wxString class_name = tkz.GetNextToken();
// class number, ascii
wxString token = tkz.GetNextToken();
long liclass;
token.ToLong ( &liclass );
int iclass = liclass;
if ( iclass > iclass_max )
iclass_max = iclass;
// geom type, ascii
wxString geo_type = tkz.GetNextToken();
}
}
m_max_class = iclass_max;
// Create the class name array
m_S57ClassArray = new wxArrayString;
m_S57ClassArray->Add ( _T ( "NULLNM" ), iclass_max+1 );
// And an array of ints describing the geometry type per class
m_GeomTypeArray = ( int * ) malloc ( ( iclass_max + 1 ) * sizeof ( int ) );
// Iterate over the file, filling in the values
for ( i=0 ; i<nline ; i++ )
{
line = file.GetLine ( i );
wxStringTokenizer tkz ( line, wxT ( "|" ) );
// while ( tkz.HasMoreTokens() )
{
// 6 char class name
wxString class_name = tkz.GetNextToken();
// class number, ascii
wxString token = tkz.GetNextToken();
long liclass;
token.ToLong ( &liclass );
int iclass = liclass;
// geom type, ascii
wxString geo_type = tkz.GetNextToken();
m_S57ClassArray->Insert ( class_name, iclass );
m_S57ClassArray->RemoveAt ( iclass + 1 );
int igeom_type = -1; // default unknown
wxChar geo_type_primary = geo_type[0];
if ( geo_type_primary == 'A' )
igeom_type = 3;
else if ( geo_type_primary == 'L' )
igeom_type = 2;
else if ( geo_type_primary == 'P' )
igeom_type = 1;
// Note: there are other types in the file, e.g. 'C'. Dunno what this is
// Also, some object classes want multiple geometries, like PA, PLA, etc.
// Take only primary, ignore the rest
m_GeomTypeArray[iclass] = igeom_type;
}
}
file.Close();
// Build some array strings for Attribute decoding
wxString sfa ( dir );
sfa.Append ( _T ( "ATTRLUT.DIC" ) );
if ( !wxFileName::FileExists ( sfa ) )
{
sfa = dir;
sfa.Append ( _T ( "attrlut.dic" ) );
}
if ( wxFileName::FileExists ( sfa ) )
{
wxFFileInputStream filea ( sfa );
if ( filea.IsOk() )
{
// Read the file once to get the max attr number
int iattr_max = 0;
while ( !filea.Eof() )
{
//read a line
line.Empty();
while ( 1 )
{
char a = filea.GetC();
if ( filea.Eof() )
break;
line.Append ( a );
if ( a == 0x0a )
break;
}
if ( !line.StartsWith ( ( const wxChar * ) wxT ( ";" ) ) )
{
wxStringTokenizer tkz ( line, wxT ( "|" ) );
{
// 6 attribute label
wxString class_name = tkz.GetNextToken();
// attribute number, ascii
wxString token = tkz.GetNextToken();
long liattr;
token.ToLong ( &liattr );
int iattr = liattr;
if ( iattr > iattr_max )
iattr_max = iattr;
}
}
}
m_max_attr = iattr_max;
filea.SeekI ( 0 );
// Create the attribute label array
m_AttrArray = new wxArrayString;
m_AttrArray->Add ( _T ( "NULLNM" ), iattr_max+1 );
// And an array of chars describing the attribute value type
m_ValTypeArray = ( char * ) malloc ( ( iattr_max + 1 ) * sizeof ( char ) );
// Iterate over the file, filling in the values
while ( !filea.Eof() )
{
//read a line
line.Empty();
while ( 1 )
{
char a = filea.GetC();
if ( filea.Eof() )
break;
line.Append ( a );
if ( a == 0x0a )
break;
}
if ( !line.StartsWith ( ( const wxChar * ) wxT ( ";" ) ) )
{
wxStringTokenizer tkz ( line, wxT ( "|" ) );
{
// 6 char class name
wxString attr_name = tkz.GetNextToken();
// class number, ascii
wxString token = tkz.GetNextToken();
long liattr;
token.ToLong ( &liattr );
int iattr = liattr;
m_AttrArray->Insert ( attr_name, iattr );
m_AttrArray->RemoveAt ( iattr + 1 );
// Skip some
token = tkz.GetNextToken();
token = tkz.GetNextToken();
token = tkz.GetNextToken();
token = tkz.GetNextToken().Trim();
char atype = '?';
if ( token.IsSameAs ( _T ( "aFLOAT" ) ) )
atype = 'R';
else if ( token.IsSameAs ( _T ( "aBYTE" ) ) )
atype = 'B';
else if ( token.IsSameAs ( _T ( "aSTRING" ) ) )
atype = 'S';
else if ( token.IsSameAs ( _T ( "aCMPLX" ) ) )
atype = 'C';
else if ( token.IsSameAs ( _T ( "aLIST" ) ) )
atype = 'L';
else if ( token.IsSameAs ( _T ( "aWORD10" ) ) )
atype = 'W';
else if ( token.IsSameAs ( _T ( "aLONG" ) ) )
atype = 'G';
m_ValTypeArray[iattr] = atype;
}
}
}
ret_val = true;
}
else // stream IsOK
{
ret_val = false;
}
}
else // Look for alternate file
{
sfa = dir;
sfa.Append ( _T ( "CM93ATTR.DIC" ) );
if ( !wxFileName::FileExists ( sfa ) )
{
sfa = dir;
sfa.Append ( _T ( "cm93attr.dic" ) );
}
if ( wxFileName::FileExists ( sfa ) )
{
wxFFileInputStream filea ( sfa );
if ( filea.IsOk() )
{
// Read the file once to get the max attr number
int iattr_max = 0;
while ( !filea.Eof() )
{
//read a line
line.Empty();
while ( 1 )
{
char a = filea.GetC();
if ( filea.Eof() )
break;
line.Append ( a );
if ( a == 0x0a )
break;
}
if ( !line.StartsWith ( ( const wxChar * ) wxT ( ";" ) ) )
{
wxStringTokenizer tkz ( line, wxT ( "|" ) );
if ( tkz.CountTokens() )
{
// 6 attribute label
wxString class_name = tkz.GetNextToken();
// attribute number, ascii
wxString token = tkz.GetNextToken();
long liattr;
token.ToLong ( &liattr );
int iattr = liattr;
if ( iattr > iattr_max )
iattr_max = iattr;
}
}
}
m_max_attr = iattr_max;
filea.SeekI ( 0 );
// Create the attribute label array
m_AttrArray = new wxArrayString;
m_AttrArray->Add ( _T ( "NULLNM" ), iattr_max+1 );
// And an array of chars describing the attribute value type
m_ValTypeArray = ( char * ) malloc ( ( iattr_max + 1 ) * sizeof ( char ) );
for ( int iat=0 ; iat < iattr_max + 1 ; iat++ )
m_ValTypeArray[iat] = '?';
// Iterate over the file, filling in the values
while ( !filea.Eof() )
{
//read a line
line.Empty();
while ( 1 )
{
char a = filea.GetC();
if ( filea.Eof() )
break;
line.Append ( a );
if ( a == 0x0a )
break;
}
if ( !line.StartsWith ( ( const wxChar * ) wxT ( ";" ) ) )
{
wxStringTokenizer tkz ( line, wxT ( "|\r\n" ) );
if ( tkz.CountTokens() >= 3 )
{
// 6 char class name
wxString attr_name = tkz.GetNextToken();
// class number, ascii
wxString token = tkz.GetNextToken();
long liattr;
token.ToLong ( &liattr );
int iattr = liattr;
m_AttrArray->Insert ( attr_name, iattr );
m_AttrArray->RemoveAt ( iattr + 1 );
token = tkz.GetNextToken().Trim();
char atype = '?';
if ( token.IsSameAs ( _T ( "aFLOAT" ) ) )
atype = 'R';
else if ( token.IsSameAs ( _T ( "aBYTE" ) ) )
atype = 'B';
else if ( token.IsSameAs ( _T ( "aSTRING" ) ) )
atype = 'S';
else if ( token.IsSameAs ( _T ( "aCMPLX" ) ) )
atype = 'C';
else if ( token.IsSameAs ( _T ( "aLIST" ) ) )
atype = 'L';
else if ( token.IsSameAs ( _T ( "aWORD10" ) ) )
atype = 'W';
else if ( token.IsSameAs ( _T ( "aLONG" ) ) )
atype = 'G';
m_ValTypeArray[iattr] = atype;