forked from sduclos/S52
-
Notifications
You must be signed in to change notification settings - Fork 0
/
S52PL.c
4342 lines (3433 loc) · 120 KB
/
S52PL.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
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
// S52PLib.c: S52 Presentation Library parser/manager
//
// Project: OpENCview
/*
This file is part of the OpENCview project, a viewer of ENC.
Copyright (C) 2000-2016 Sylvain Duclos [email protected]
OpENCview is free software: you can redistribute it and/or modify
it under the terms of the Lesser GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpENCview 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
Lesser GNU General Public License for more details.
You should have received a copy of the Lesser GNU General Public License
along with OpENCview. If not, see <http://www.gnu.org/licenses/>.
*/
#include "S52PL.h" // --
#include "S52CS.h" // S52_CS_condTable[]
#include "S52MP.h" // S52_MP_get/set()
#include "S52utils.h" // PRINTF(), S52_atoi(), S52_atof()
#include "S57data.h" // geocoord, S57_obj_t
#include <glib.h>
#include <math.h> // INFINITY
#define S52_SMB_NMLN 8 // symbology name lenght
//-- PLIB ID MODULE STRUCTURE ---------------------------------------
typedef struct _LBID {
int RCID;
gchar EXPP; // 'N' (new) or 'R' (revision)
GString *ID;
// PTYP string
// ESID string
// EDTN string
// CODT char[8]
// COTI char[6]
// VRDT char[8]
// PROF char[2] // 'PN' (new) or 'PR' (revision)
// OCDT char[8]
// COMT string
struct _LBID *next;
} _LBID;
static _LBID *_plibID = NULL;
//-- COLOR MODULE STRUCTURE ---------------------------------------
typedef struct _colTable {
GString *tableName; // debug
GArray *colors;
} _colTable;
static GArray *_colTables = NULL;
static GTree *_colref = NULL; // fast indexing of color array
typedef enum _colorTableStat {
_COL_TBL_NOSTAT = 0 , // unknown color table status
_COL_TBL_NIL = 'N', // new edition
_COL_TBL_ADD = 'A', // insert
_COL_TBL_MOD = 'M', // replace
_COL_TBL_DEL = 'D', // deletion
_COL_TBL_NUM = 4 // number of color table status
} _colorTableStat;
#define S52_COL_NMLN 5 // color name lenght
#define S52_COL_NUM 63 // number of color (#64 is transparent)
// Following 4 additional colors could be made available as alternative colors for non-charted items.
// Draft PLib 4.0 has 4 more color of MIO's: "MARBL", "MARCY", "MARMG", "MARWH"
static const char *_colorName[] = {
"NODTA", "CURSR", "CHBLK", "CHGRD", "CHGRF", "CHRED", "CHGRN", "CHYLW",
"CHMGD", "CHMGF", "CHBRN", "CHWHT", "SCLBR", "CHCOR", "LITRD", "LITGN",
"LITYW", "ISDNG", "DNGHL", "TRFCD", "TRFCF", "LANDA", "LANDF", "CSTLN",
"SNDG1", "SNDG2", "DEPSC", "DEPCN", "DEPDW", "DEPMD", "DEPMS", "DEPVS",
"DEPIT", "RADHI", "RADLO", "ARPAT", "NINFO", "RESBL", "ADINF", "RESGR",
"SHIPS", "PSTRK", "SYTRK", "PLRTE", "APLRT", "UINFD", "UINFF", "UIBCK",
"UIAFD", "UINFR", "UINFG", "UINFO", "UINFB", "UINFM", "UIBDR", "UIAFF",
"OUTLW", "OUTLL", "RES01", "RES02", "RES03", "BKAJ1", "BKAJ2"
// ,"MARBL", "MARCY", "MARMG", "MARWH"
};
//-- SYMBOLISATION MODULE STRUCTURE -----------------------------
// position parameter: LINE, PATTERN, SYMBOL
typedef struct _Position {
union {int dummy1, PAMI, dummy2; } minDist;
union {int dummy1, PAMA, dummy2; } maxDist;
union {int LICL, PACL, SYCL; } pivot_x;
union {int LIRW, PARW, SYRW; } pivot_y;
union {int LIHL, PAHL, SYHL; } bbox_w;
union {int LIVL, PAVL, SYVL; } bbox_h;
union {int LBXC, PBXC, SBXC; } bbox_x; // UL crnr
union {int LBXR, PBXR, SBXR; } bbox_y; // UL crnr
} _Position;
typedef struct _Shape {
// note: bitmap/vector mutually exclusive
union { GString *dummy, *PBTM, *SBTM; } bitmap; // unused
union { GString *LVCT, *PVCT, *SVCT; } vector; //
} Shape;
// symbology definition: LINE, PATTERN, SYMBOL
typedef struct _S52_cmdDef {
int RCID;
union {char LINM[S52_SMB_NMLN+1], // symbology name
PANM[S52_SMB_NMLN+1], // '\0' teminated
SYNM[S52_SMB_NMLN+1];
} name;
union {char dummy, PADF, SYDF; } definition;
union {char dummy1, PATP, dummy2; } fillType;
union {char dummy1, PASP, dummy2; } spacing;
union {_Position line, patt, symb; } pos;
union {GString *LXPO, *PXPO, *SXPO; } exposition;
union {Shape line, patt, symb; } shape;
union {GString *LCRF, *PCRF, *SCRF; } colRef;
// ---- not a S52 fields ------------------------------------
S52_SMBtblName symType; // debug LINE,PATT,SYMB
S52_DList DListData; // GL Display List / VBO
#if (defined(S52_USE_GL2) || defined(S52_USE_GLES2))
guint mask_texID; // texture ID of pattern after running VBO
int potW; // tex widht
int potH; // tex height
#endif
} _S52_cmdDef;
typedef struct _Text {
GString *frmtd; // formated text string (could be NULL)
char hjust;
char vjust;
char space;
char style; // CHARS
char weight; // CHARS
char width; // CHARS
int bsize; // CHARS -body size
int xoffs; // pica (1 = 0.351mm)
int yoffs; // pica (1 = 0.351mm)
S52_Color *col; // colour
int dis; // display (view group)
} _Text;
// this 'union' is to highlight that *cmdDef is a pointer to a
// 1) PLib symbole definition or 2) C function (CS) or 3) text struct or 4) light sector
typedef union _cmdDef {
_S52_cmdDef *def;
_Text *text; // after parsing this could de NULL
S52_CS_condSymb *CS;
// because there is no cmdDef for light sector, so put VBO here (ie no need struct _S52_cmdDef)
S52_DList *DList; // for pattern in GLES2 this DL will create a texture
} _cmdDef;
// command word list
typedef struct _cmdWL {
S52_CmdWrd cmdWord; // Command Word type
char *param; // start of parameter for this command
_cmdDef cmd; // command word definition or conditional symb func call
#ifdef S52_USE_FREETYPE_GL
guint vboID; // ID if the OpenGL VBO text
guint len; // VBO text length
#endif
guchar crntPal; // optimisation: this 'cmd' is setup for 'palette N' colors
struct _cmdWL *next;
} _cmdWL;
// S52 lookup table name (fifth letter)
typedef enum _LUPtnm {
_LUP_NONAM = 0 , // unknown LUP (META)
_LUP_SIMPL = 'L', // points --SIMPLIFIED
_LUP_PAPER = 'R', // points --PAPER_CHART
_LUP_LINES = 'S', // lines --LINES
_LUP_PLAIN = 'N', // areas --PLAIN_BOUNDARIES
_LUP_SYMBO = 'O', // areas --SYMBOLIZED_BOUNDARIES
_LUP_NUM = 5 // number of lookup name
} _LUPtnm;
//-- LOOKUP MODULE STRUCTURE ----------------------------------------
typedef struct _LUP {
int RCID; // record identifier
char OBCL[S52_PL_NMLN+1]; // LUP name --'\0' terminated
S57_Obj_t FTYP; // 'A' Area, 'L' Line, 'P' Point
S52_disPrio DPRI; // Display Priority
S52_RadPrio RPRI; // 'O' or 'S', Radar Priority
_LUPtnm TNAM; // FTYP: areas, points, lines
GString *ATTC; // Attribute Code/Value (repeat)
GString *INST; // Instruction Field (rules)
S52_DisCat DISC; // Display Categorie: B/S/O, Base, Standard, Other
int LUCM; // Look-Up Comment (PLib3.x put 'groupes' here,
// hense 'int', but its a string in the specs)
// ---- not a S52 fields ------------------------------------
S52_objSupp supp; // suppress display of this object type
struct _LUP *OBCLnext; // next LUP with name OBCL
} _LUP;
typedef enum _poly_mode {
PM_BEG = '0', // begin a poly (enter poly mode)
PM_SUB = '1', // begin sub poly
PM_END = '2' // end a poly (leave poly mode)
} _poly_mode;
// hold state of vector command parser
typedef struct _S52_vec {
char *name; // name of this symbology definition
char *str; // VCT field
char *colRef; // CRF field
S57_prim *prim; // x,y,z,x,y,z,... (DrawArray format)
int bbx; // def->pos.line.bbox_x.LBXC;
int bby; // def->pos.line.bbox_y.LBXR;
int pivot_x; // def->pos.line.pivot_x.LICL;
int pivot_y; // def->pos.line.pivot_y.LIRW;
double radius; // disk radius
_poly_mode pm; // polygon mode
} _S52_vec;
// NOTE: order important --index to '_table[]'
// agreegated tables name
typedef enum _table_t {
//S52_PL_ID, // S52 Library Identification Module DB
//S52_PL_COL, // Colour Table (6)
// Look-Up Table for:
LUP_PT_SIMPL, // simplified point symbol
LUP_PT_PAPER, // paper chart point symbol
LUP_LINE, // line
LUP_AREA_PLN, // plain boundaries area
LUP_AREA_SYM, // symbolized bound. area
// Symbolisation Table for:
SMB_LINE, // Complex Linestyle
SMB_PATT, // Pattern
SMB_SYMB, // Symbol
SMB_COND, // Conditional Symbology
TBL_NUM // number of Tables
} _table_t;
typedef struct _S52_obj {
// 2 set of LUP: normal and alternate
_LUP *LUP; // common data for the 2 set of LUP
_cmdWL *cmdLorig[2]; // instruction list command (parsed LUP.INST)
GString *CSinst[2]; // expanded (resolved) cond. symb. instruction list
_cmdWL *CScmdL[2]; // parsed cond. symb. instruction list command
// final command list (array):
// normal command word + those once CS has been resolve and parsed
GArray *cmdAfinal[2]; // command array: normal symbol and alternate
GArray *crntA; // point to the current (active) command array (normal or alternate)
guint crntAidx; // index in command array
gint textParsed[2]; // TRUE if parsed, need two flag because there is text for
// two type of point and area
// CS override
int prioOveride; // CS overide display priority
S52_disPrio DPRI; // Display Priority
S52_RadPrio RPRI; // 'O' or 'S', Radar Priority
S52_DisCat DISC; // Display Categorie: B/S/O, Base, Standard, Other
int LUCM; // Look-Up Comment (PLib3.x put 'groupes' here,
// hense 'int', but its a string in the specs)
S57_geo *geoData; // S-57
// --- Auxiliary Info --------------------------------
// FIXME: make that a struct
//char LOD; // optimisation: chart purpose: cell->filename->str[2]
// NOTE: this is a general holder for orient/speed depending on
// the object type. So it could be for current, ship, AIS, ...
gdouble orient; // LIGHT angle (after parsing), heading of 'ownshp'
gdouble speed; // 'ownshp' speed for drawing vertor lenght
GTimeVal time; // store time (use to find age of AIS)
gboolean supp; // display suppression set by user
// LEGLIN
struct _S52_obj *nextLeg; // link to next leglin (need to draw arc)
struct _S52_obj *prevLeg; // link to previous leg so that we can clip the start of this leg
// of the amout of wholin dist of the previous leg
// WHeel-Over-LINe
struct _S52_obj *wholin; // link to wholin obj
} _S52_obj;
// Tables (LUP+symbology) --BBTree holder
static gboolean _initPLib = TRUE; // will init PLib
static GTree *_table[TBL_NUM] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
#define CR '\r' // carriage return
#define EOL '\037' // 31/037/0x1F/CTRL-_: ASCII Unit Separator
// used in S52 as an EOL (also ATTC field separator)
#define APOS '\047'
#define DEFOBJ "######" // default object (name)
// MAXINT-6 is how OGR tag an UNKNOWN value
// see gdal/ogr/ogrsf_frmts/s57/s57.h:126
// it is then turn into a string in gv_properties
//#define EMPTY_NUMBER_MARKER "2147483641" /* MAXINT-6 */
// MAX_BUF == 1024 --for buffer overflow
//#define LNFMT "%1024[^\n]" // line format
#define LNFMT "%[^\n]" // line format
#define FIELD(str) if(0==strncmp(#str, _pBuf, 4))
typedef unsigned char u8;
// used to parse the PLib
#define MAX_BUF 1024 // working buffer length
static char _pBuf[MAX_BUF];
typedef struct _PL {
gchar *data; // current position in PL
gsize cnt; // current offset of 'data'
gsize sz; // total size of PL
} _PL;
#if 0
char _S52AuxSymb[] =
0001 0
LUPT 34LU01056NIL$CSYMBP00009OSIMPLIFIED
ATTC 15$SCODESCALEB10
INST 13SY(SCALEB10)
DISC 12DISPLAYBASE
LUCM 611030
**** 0
0001 501057
LUPT 34LU01057NIL$CSYMBP00009OSIMPLIFIED
ATTC 15$SCODESCALEB11
INST 13SY(SCALEB11)
DISC 12DISPLAYBASE
LUCM 611030
**** 0
#endif
// 1 col: S-52 pslb03_2.pdf 13.4.3
// 2 col: S-57 name for 'nature of surface' attribute --NATSUR (113)
// 3 col: S-57 attribute remark
static const char *natsur[] = {
"", // 0 : no value in ENC (filler)
"M ", //"mud", // 1 : mud IJ 2,20;
"Cy ", //"clay", // 2 : clay IJ 3;
"Si ", //"silt", // 3 : silt IJ 4;
"S ", //"sand", // 4 : sand IC 6; IJ 1,20; 312.2;
"St ", //"stone", // 5 : stone IC 7; IJ 5,20; 312.2; 425.5-6;
"G ", //"gravel", // 6 : gravel IJ 6,20;
"P ", //"pebbles", // 7 : pebbles IJ 7;
"Cb ", //"cobbles", // 8 : cobbles IJ 8;
"R ", //"rock", // 9 : rock IJ 9,21; 426.2
"marsh ", // 10 : marsh
"R ", //"lava", // 11 : lava
"snow ", // 12 : snow
"ice ", // 13 : ice
"Co ", //"coral", // 14 : coral IJ 10,22; 425.5; 426.3;
"swamp ", // 15 : swamp
"bog/moor ", // 16 : bog/moor
"Sh ", //"shells", // 17 : shells IJ 11; 425.5-6;
"R " //"boulder" // 18 : boulder
};
#define N_NATSUR 19 // number of natsur
// CMS: Color Management System --> lcms
#include "lcms.h"
static cmsHPROFILE _xyzp = NULL;
static cmsHPROFILE _rgbp = NULL;
static cmsHTRANSFORM _2RGB = NULL;
static int _lcmsError = FALSE; // TRUE an error occur in lcms
// optimisation: indexed
static GPtrArray *_objList = NULL;
//------------------------
//
// MODULES LINKING SECTION
//
//------------------------
#if 0
static int _readS52Line(_PL *fp, char *buf)
// copy a line from fp into buf return number of
// char in buf or -1 on EOF. buf is null/null terminated.
{
int len = 0;
//int ret = 0;
//buf[0] = '\0';
return_if_null(fp);
return_if_null(buf);
while ( (fp->cnt < fp->sz) && ('\n' != *fp->data) ) {
*buf++ = *fp->data++;
fp->cnt++;
len++;
}
// EOF
if (fp->cnt >= fp->sz)
return -1;
// skip EOL
if ('\n' == *fp->data) {
fp->data++;
fp->cnt++;
*buf = '\0';
}
// FIXME: remove this hack
if ( len > 0 && *(buf-1) == EOL)
*(buf-1) = '\0'; // chop trailing \037 --string is \0\0 terminated
return len;
}
#endif
static int _readS52Line(_PL *fp, char *buf)
// copy a line from fp into buf return number of
// char in buf or -1 on EOF. buf is null/null terminated.
{
int linelen = 0;
char *b = buf;
return_if_null(fp);
return_if_null(buf);
while ( (fp->cnt < fp->sz) && ('\n' != *fp->data) && (linelen<MAX_BUF-1)) {
*b++ = *fp->data++;
fp->cnt++;
++linelen;
}
*b = '\0';
if (fp->cnt < fp->sz) {
fp->data++;
fp->cnt++;
}
// skip comment - because # is used to indicate default rule
if (';' == *buf)
return linelen;
// use the record lenght
char dst[6];
strncpy(dst, buf+4, 5);
dst[5] = '\0';
//int reclen = g_ascii_strtoll(dst, NULL, 10);
int reclen = S52_atoi(dst);
for (int i=reclen+9; i<=linelen; ++i)
buf[i] = '\0';
if (EOL == buf[reclen+8])
buf[reclen+8] = '\0';
// EOF
if (fp->cnt >= fp->sz)
return -1;
return linelen;
}
static int _chopAtEOL(char *pBuffer, char c)
// replace S52 EOL (field separator) with char 'c'
{
/*
int i;
for (i=0; pBuffer[i] != '\0'; ++i)
if ( pBuffer[i] == EOL )
pBuffer[i] = c;
*/
while (*pBuffer != '\0') {
if (*pBuffer == EOL )
*pBuffer = c;
++pBuffer;
}
return TRUE;
}
static GTree *_selLUP(_LUPtnm TNAM)
// select lookup table from its name
{
switch (TNAM) {
case _LUP_SIMPL: return _table[LUP_PT_SIMPL];
case _LUP_PAPER: return _table[LUP_PT_PAPER];
case _LUP_LINES: return _table[LUP_LINE];
case _LUP_PLAIN: return _table[LUP_AREA_PLN];
case _LUP_SYMBO: return _table[LUP_AREA_SYM];
case _LUP_NONAM: return NULL;
case _LUP_NUM:
default:
PRINTF("ERROR unknown lookup table (%i)\n", TNAM);
g_assert(0);
}
return NULL;
}
static GTree *_selSMB(S52_SMBtblName name)
// select symbology table
{
switch (name) {
case S52_SMB_LINE: return _table[SMB_LINE];
case S52_SMB_PATT: return _table[SMB_PATT];
case S52_SMB_SYMB: return _table[SMB_SYMB];
case S52_SMB_COND: return _table[SMB_COND];
default:
PRINTF("ERROR unknown symbology table!!\n");
g_assert(0);
}
return NULL;
}
static gint _cmpCOL(gconstpointer nameA, gconstpointer nameB)
// compare color name
{
//PRINTF("%s - %s\n",(char*)nameA,(char*)nameB);
return strncmp((char*)nameA, (char*)nameB, S52_COL_NMLN);
}
static gint _cmpLUP(gconstpointer nameA, gconstpointer nameB, gpointer user_data)
// compare lookup name
{
// 'user_data' useless warning
(void) user_data;
//PRINTF("%s - %s\n",(char*)nameA,(char*)nameB);
return strncmp((char*)nameA, (char*)nameB, S52_PL_NMLN);
}
static gint _cmpSMB(gconstpointer nameA, gconstpointer nameB, gpointer user_data)
// compare Symbology name
{
// 'user_data' useless warning
(void) user_data;
return strncmp((char*)nameA, (char*)nameB, S52_SMB_NMLN);
}
static gint _cmpCOND(gconstpointer nameA, gconstpointer nameB)
// compare Cond Symbology name
{
return strncmp((char*)nameA, (char*)nameB, S52_SMB_NMLN);
}
static void _delLUP(gpointer value)
// delete lookup
{
_LUP *LUP = (_LUP*) value;
//_doneCmdList(LUP->cmdList);
while (NULL != LUP) {
_LUP *crntLUP = LUP->OBCLnext;
if (NULL != LUP->ATTC) g_string_free(LUP->ATTC, TRUE);
if (NULL != LUP->INST) g_string_free(LUP->INST, TRUE);
g_free(LUP);
LUP = crntLUP;
}
}
static void _delSMB(gpointer value)
// delete symbol
{
_S52_cmdDef *def = (_S52_cmdDef*) value;
// debug
//PRINTF("del %s\n", def->name.SYNM);
g_string_free(def->exposition.LXPO, TRUE);
g_string_free(def->shape.line.vector.LVCT, TRUE);
g_string_free(def->colRef.LCRF, TRUE);
g_free(def);
}
static gint _loadCondSymb()
// load Conditional Symbology in BBtree
{
if (NULL == S52_CS_condTable)
return FALSE;
for (int i=0; NULL!=S52_CS_condTable[i].CScb; ++i) {
g_tree_insert(_selSMB(S52_SMB_COND),
(gpointer) S52_CS_condTable[i].name,
(gpointer) &S52_CS_condTable[i]);
}
return TRUE;
}
static int _dumpATT(char *str)
// debug
{
int len = strlen(str);
//g_print("LUP ATT:");
printf("LUP ATT:");
while (len != 0) {
//g_print(" %s", str);
printf(" %s", str);
str += len+1;
len = strlen(str);
}
//g_print("\n");
printf("\n");
return TRUE;
}
static _LUP *_lookUpLUP(_LUP *LUPlist, S57_geo *geoData)
// Get the LUP with maximum Object attribute match.
//
// Note: reference are maide to section "8.3 How to use the look-up table"
// of IHO ECDIS PRESENTATION LIBRARY USER'S MANUAL Ed/Rev 3.2 March 2000
// (IHO Special Publication No. 52 ANNEX A of APPENDIX 2 --S52-A-2)
{
int trace = FALSE; //TRUE = debug
int best_nATTmatch = 0; // best attribute value match
return_if_null(LUPlist);
return_if_null(geoData);
// setup default LUP to the first LUP
_LUP *LUP = LUPlist;
// debug
//if (0 == strncmp(LUPlist->OBCL, "SBDARE", 6)) {
// trace = 1;
// S57_dumpData(geoData, FALSE);
//}
//GString *FIDNstr = S57_getAttVal(geoData, "FIDN");
//if (0==strcmp("2135158878", FIDNstr->str)) {
// trace = 1;
// S57_dumpData(geoData);
// PRINTF("%s\n", FIDNstr->str);
//}
// default LUP [ref S52-A-2:8.3.3.2]
if (NULL == LUP->OBCLnext) {
if (NULL == LUP->ATTC)
return LUP;
else {
// debug
PRINTF("DEBUG: single look-up non-empty attribute, RCID:%i\n", LUPlist->RCID);
g_assert(0);
}
}
// special case [S52-A-2:8.3.3.4(iii)]
if (0 == strncmp(LUP->OBCL, "TSSLPT", S52_PL_NMLN)){
if (NULL == S57_getAttVal(geoData, "ORIENT")) {
// FIXME: hit this in S-64 ENC
PRINTF("FIXME: TSSLPT found ... check this ... no ORIENT\n");
//g_assert(0);
return LUP;
}
}
// Get next LUP --the first one is alway empty.
LUPlist = LUPlist->OBCLnext;
// Scan all LUP found for this S57 object
// for the one that have the complet attribute name/value match.
// [S52-A-2:8.3.3.3]
while (LUPlist) {
int skipLUP = 0; //
int nATTmatch = 0; // nbr of att value match for this LUP
char *attlv = (NULL == LUPlist->ATTC) ? NULL : LUPlist->ATTC->str; // ATTL+ATTV
if (NULL == attlv) {
LUPlist = LUPlist->OBCLnext;
continue;
}
if (trace)
_dumpATT(attlv);
while (*attlv != '\0' && !skipLUP) {
char attl[7] = {'\0'}; // attribute name
GString *attv = NULL; // attribute value
// scan object attribute name (ie propertie name in OGR)
// for a name match
strncat(attl, attlv, 6);
attv = S57_getAttVal(geoData, attl);
if (NULL != attv) {
//PRINTF("attv: %s\n", attv->str);
// Check for a attribute value match.
// All attribute value must match.
// So if attribute name doesn't matche try next LUP.
// OK here we have an attribute name match
// checking now for attribute value match
// special case [S52-A-2:8.3.3.4(i)]
// ie. use any attribute value (except value unknown)
if ( (attlv[6] == ' ') && (0!=g_strcmp0(attv->str, EMPTY_NUMBER_MARKER)) ) {
++nATTmatch;
} else {
// special case [S52-A-2:8.3.3.4(ii)]
// ie. match if value is unknown
if ( (attlv[6] == '?') && (0==g_strcmp0(attv->str, EMPTY_NUMBER_MARKER)) ) {
//if ( (attlv[6] == '?') ) {
++nATTmatch;
// give DRVAL1 = 0.0
// but should be unknown
// see CA49995A FIDN:327146 FIDS:9
// FIX: export PRESERVE_EMPTY_NUMBERS:ON
// value check
} else {
// no attribut value in LUP (ex ORIENT in TSSLPT)
if (attlv[6] == '\0')
++nATTmatch;
else {
char *tmpVal = strstr(attv->str, attlv+6);
// must match *exacly*
// so '4,3,4' match '4,3,4,7' but not 3,4,3 (4,3 match)
// the trick is to use the lenght of of the value of
// the PLib *not* from S57
//char *tmpVal = strncmp(attv->str, attlv+6, strlen(attlv)-6);
// FIX: record the max lenght of att val str of a match
if (NULL != tmpVal) {
//int len = strlen(tmpVal);
//if (len < attv->len
//if (len >= best_attValLen) {
// best_attValLen = len;
// ++nATTmatch;
//}
int valS57 = atoi(attv->str);
int valLUP = atoi(attlv+6);
if (valS57 == valLUP)
++nATTmatch;
}
// skip this lookup
else
skipLUP = 1;
}
}
}
// get next attribute name/value for this LUP
while (*attlv != '\0')
attlv++; // find end of attribue name/value pair
attlv++; // skip end of field --witch is now a '\0'
} else {
skipLUP = 1;
nATTmatch = 0;
//nATTmatch = 1;
//PRINTF("SKIP\n");
}
} // while
// BUG: the first match found is returned!
//if (nATTmatch > best_nATTmatch) {
// OR no match at all are discarded
//if ((0 != nATTmatch) && (nATTmatch > best_nATTmatch)) {
// FIX: last best match found, but zero match discarded
// this seem like a S52 bug
if ((0 != nATTmatch) && (nATTmatch >= best_nATTmatch)) {
best_nATTmatch = nATTmatch;
LUP = LUPlist;
if (trace)
PRINTF("CANDIDATE(%i): %s\n", best_nATTmatch, LUPlist->ATTC->str);
}
LUPlist = LUPlist->OBCLnext;
//nATTmatch = 0;
} // while
if (trace)
PRINTF("SELECTED LUP: %s\n", LUP->INST->str);
return LUP;
}
// command word
#define CMDWRD(s,t) if (0==strncmp(#s, str, 2)) { \
str += 3; \
cmd->cmdWord = t; \
cmd->param = str;
#define LOOKUP(dbnm) cmd->cmd.def = (_S52_cmdDef*)g_tree_lookup(_selSMB(dbnm), str); \
if (cmd->cmd.def == NULL) { \
cmd->cmd.def = (_S52_cmdDef*)g_tree_lookup(_selSMB(dbnm), (void*) "QUESMRK1"); \
PRINTF("WARNING: no lookup %s, %i, default to QUESMRK1\n", str, dbnm); \
}
// scan foward stop on ; or end-of-line
#define SCANFWRD while ( !(*str == ';' || *str == '\0')) str++; }
static _cmdWL *_parseINST(GString *inst)
// Parse "Symbology Instruction" (LUP) and link them to rendering rules
{
char *str = inst->str;
_cmdWL *top = NULL;
_cmdWL *last = NULL;
// assume that the previous object that used this CS lookup
// will have saved the instruction command word.
// object of same classe can have different command word
// because of the Cond. Symb.
//PRINTF("_LUP2cmd str:%s\n", str);
while (*str != '\0') {
// 'marfea' end with ';'
if (*str == ';') {
str++; // skip ';'
continue;
}
_cmdWL *cmd = g_new0(_cmdWL, 1);
//_cmdWL *cmd = g_try_new0(_cmdWL, 1);
if (NULL == cmd)
g_assert(0);
////////////////////////////////
// parse Symbology Command Word
//
// NOTE: command might repeat except:
// -S52_CMD_COM_LN: complex line,
// -S52_CMD_ARE_CO: area color,
// -S52_CMD_CND_SY: conditional symbology
// SHOWTEXT
CMDWRD(TX, S52_CMD_TXT_TX) SCANFWRD
else CMDWRD(TE, S52_CMD_TXT_TE) SCANFWRD
// SHOWPOINT
else CMDWRD(SY, S52_CMD_SYM_PT) LOOKUP(S52_SMB_SYMB) SCANFWRD
// SHOWLINE
else CMDWRD(LS, S52_CMD_SIM_LN) SCANFWRD
else CMDWRD(LC, S52_CMD_COM_LN) LOOKUP(S52_SMB_LINE) SCANFWRD
// SHOWAREA
else CMDWRD(AC, S52_CMD_ARE_CO) SCANFWRD
else CMDWRD(AP, S52_CMD_ARE_PA) LOOKUP(S52_SMB_PATT) SCANFWRD
// CALLSYMPROC
else CMDWRD(CS, S52_CMD_CND_SY) LOOKUP(S52_SMB_COND) SCANFWRD
// OVERRIDE PRIORITY (not in S52 specs.)
else CMDWRD(OP, S52_CMD_OVR_PR) SCANFWRD
// failsafe
else {
PRINTF("ERROR: parsing Command Word:%s\n", str);
g_assert(0);
return FALSE;
}
// append command
if (top == NULL) {
top = cmd;
last= top;
} else {
last->next = cmd;
last= cmd;
}
} /* while */
// case when PLib say to draw nothing
//if (NULL == top) {
// PRINTF("NOTE: LUP for %s\n", str);
// g_assert(0);
//}
return top;
}
static _cmdWL *_initCmdA(_S52_obj *obj, int alt)
// return the CS command in cmd list, else NULL
// start to fill the command array upto the first CS
{
_cmdWL *cmd = obj->cmdLorig[alt];
g_array_set_size(obj->cmdAfinal[alt], 0);
// scan for a CS
while (NULL != cmd) {
// there can only be one CS (CND_SY) per LUP
if (S52_CMD_CND_SY == cmd->cmdWord) {
return cmd;
//break;
}
g_array_append_val(obj->cmdAfinal[alt], *cmd);
cmd = cmd->next;
}
return cmd;
}
static int _freeCmdList(_cmdWL *top)
// free command list
{
while (top != NULL) {
_cmdWL *cmd = top->next;
g_free(top);
top = cmd;
}
return TRUE;
}
static gint _freeTXT(_Text *text)
{
if (NULL != text->frmtd) {
g_string_free(text->frmtd, TRUE);
text->frmtd = NULL;
}
g_free(text);
return TRUE;
}
static gint _freeAllTXT(GArray *cmdArray)
// free all text
{
for (guint i=0; i<cmdArray->len; ++i) {
_cmdWL *cmd = &g_array_index(cmdArray, _cmdWL, i);
if ((S52_CMD_TXT_TX==cmd->cmdWord) || (S52_CMD_TXT_TE==cmd->cmdWord)) {
if (NULL != cmd->cmd.text) {
_freeTXT(cmd->cmd.text);
cmd->cmd.text = NULL;
}
}
}
return TRUE;
}
static int _resolveSMB(_S52_obj *obj, int alt)
// return TRUE if there is a CS in the INST file for this LUP (alt)
// also fill the command Array
{
// debug
//S57_geo *geoData = S52_PL_getGeo(obj);
//GString *FIDNstr = S57_getAttVal(geoData, "FIDN");
//if (NULL != FIDNstr && 0==strcmp("2135161787", FIDNstr->str)) {
// PRINTF("%s\n",FIDNstr->str);
//}
// clear old CS instruction
if (NULL != obj->CSinst[alt])
g_string_free(obj->CSinst[alt], TRUE);
obj->CSinst[alt] = NULL;
// clear old CS command list