-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathS52PL.c
5079 lines (4040 loc) · 148 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-2018 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, ObjExt_t
#include <glib.h>
#include <math.h> // INFINITY
#include <strings.h> // bzero()
#define S52_COL_NUM 63 // number of color (#64 is transparent)
#define S52_LUP_NMLN 6 // lookup 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;
// 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" (resp.: blue, cyan, magenta, white)
// FIXME: how to handle these
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_symDef {
int RCID;
union {char LINM[S52_PL_SMB_NMLN+1], // symbology name
PANM[S52_PL_SMB_NMLN+1], // '\0' teminated
SYNM[S52_PL_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_obj are made of a number symDef
// this symDef is made of a number of fragment
// DList/VBO hold def of each fragment attribute for this sym (color/pen_w/trans)
S52_DListData DListData; // GL Display List / VBO
#if defined(S52_USE_GL2) || defined(S52_USE_GLES2)
// texture def of pattern after running VBO
guint mask_texID; // tex ID
int potW; // tex widht
int potH; // tex height
#endif
} _S52_symDef;
/* exerp of S52 p. I-25
HJUST "horizontal justification" parameter:
1 means CENTRE justified (i.e. pivot point is located at the centre of the overall length of text string)
2 means RIGHT justified (i.e. pivot point is located at the right side of the last character of text string)
3 means LEFT justified. This is the default value. (i.e. pivot point is located at the left side of the first character of text string)
VJUST "vertical justification" parameter:
1 means BOTTOM justified. This is the default value. (i.e. the pivot point is located at the bottom line of the text string)
2 means CENTRE justified (i.e. the pivot point is located at the centre line of the text string)
3 means TOP justified (i.e. the pivot point is located at the top line of the text string)
SPACE "character spacing" parameter:
1 means FIT spacing (i.e. the text string should be expanded or condensed to fit between the first and the last position in a spatial object)
2 means STANDARD spacing. This is the default value. (i.e. the standard spacing in accordance with the typeface given in CHARS should be used)
3 means STANDARD spacing with word wrap (i.e. the standard spacing in accordance with the typeface given in CHARS should be used;
text longer than 8 characters should be broken into separate lines)
XOFFS "x-offset" parameter:
defines the X-offset of the pivot point given in units of BODY SIZE (see CHARS parameter) relative
to the location of the spatial object (0 is default if XOFFS is not given or undefined); positive x-offset
extends to the right (the "units of BODYSIZE" means that if for example, the body size is 10 pica
points each unit of offset is 10 (0.351) = 3.51 mm).
YOFFS "y-offset" parameter:
defines the y-offset of the pivot point given in units of BODY SIZE (see CHARS parameter) relative
to the location of the spatial object (0 is default if YOFFS is not given or undefined); positive y-offset
extends downwards.
*/
typedef struct _Text {
GString *frmtd; // formated text string (could be NULL)
char hjust; // (see above)
char vjust; // (see above)
char space; // (see above)
char style; // CHARS
char weight; // CHARS
char width; // CHARS
int bsize; // CHARS - body size
int xoffs; // pivot point, pica (1 = 0.351mm) (see above)
int yoffs; // pivot point, pica (1 = 0.351mm) (see above)
S52_Color *col; // colour
int dis; // display (text view group)
//int vgroup; // display (text view group)
#ifdef S52_USE_FREETYPE_GL
guint vboID; // ID if the OpenGL VBO text
guint len; // VBO text length
double strWpx; // string width (pixels)
double strHpx; // string height (pixels)
#endif
} _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_symDef *def;
//S52_CMD_SYM_PT, // SY --SHOWPOINT
//S52_CMD_COM_LN, // LC --SHOWLINE COMPLEX
//S52_CMD_ARE_PA, // AP --SHOWAREA PATTERN
_Text *text; // after parsing this could de NULL
//S52_CMD_TXT_TX, // TX --SHOWTEXT (formated)
//S52_CMD_TXT_TE, // TE --SHOWTEXT
S52_CS_condSymb *CS;
//S52_CMD_CND_SY, // CS --CALLSYMPROC (Conditional Symbology)
// because there is no cmdDef for light sector, so put VBO here (ie no need struct _S52_cmdDef)
S52_DListData *DListData; // for pattern in GLES2 this DL will create a texture
//S52_CMD_ARE_CO, // AC --SHOWAREA
//S52_CMD_SIM_LN, // LS --SHOWLINE
} _cmdDef;
// command word list
typedef struct _cmdWL {
S52_CmdWrd cmdWord; // Command Word type
const char *param; // start of parameter for this command
_cmdDef cmd; // command word definition or conditional symb func call
// FIXME: invariant for cmdWord --> cmd
//S52_CMD_TXT_TX, // TX --SHOWTEXT (formated)
//S52_CMD_TXT_TE, // TE --SHOWTEXT
//S52_CMD_SYM_PT, // SY --SHOWPOINT
//S52_CMD_SIM_LN, // LS --SHOWLINE
//S52_CMD_COM_LN, // LC --SHOWLINE COMPLEX
//S52_CMD_ARE_CO, // AC --SHOWAREA
//S52_CMD_ARE_PA, // AP --SHOWAREA PATTERN
//S52_CMD_CND_SY, // CS --CALLSYMPROC (Conditional Symbology)
//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 _prios {
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)
} _prios;
typedef struct _LUP {
int RCID; // record identifier
char OBCL[S52_LUP_NMLN+1]; // LUP name --'\0' terminated
S57_Obj_t FTYP; // 'A' Area, 'L' Line, 'P' Point
_LUPtnm TNAM; // FTYP: areas, points, lines
_prios prios;
GString *ATTC; // Attribute Code/Value (repeat)
GString *INST; // Instruction Field (rules)
// ---- 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_SMBtblName symType; // debug - when PATT use bbox as ref pt
} _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;
// Obj Auxiliary Info
typedef struct _AUX_Info {
// Note: this is a general holder for orient/speed depending on
// the object type. So it could be for current, ship, AIS, ...
// FIXME: doc this in a union
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
S52_obj *nextLeg; // link to next leglin (need to draw arc)
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
//S52_obj *wholin; // link to wholin obj
} _AUX_Info;
typedef struct _S52_obj {
S57_geo *geo; // Note: must be the first member for S52PLGETGEO(S52OBJ)
_LUP *LUP; // common data for the 2 set of LUP (except INST)
//_LUP *LUP[2];
_cmdWL *cmdLorig[2]; // instruction list command (parsed LUP.INST)
// FIXME: optimisation: will resolve to the same string
// Note: RESARE02() call MP S52_MAR_SYMBOLIZED_BND (via _APP_CS == TRUE)
GString *CSinst[2]; // expanded (resolved) cond. symb. instruction list
//GString *CSinst; // expanded (resolved) cond. symb. instruction list
_cmdWL *CScmdL[2]; // parsed cond. symb. instruction list command
//_cmdWL *CScmdL; // 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 hasText[2]; // TRUE if INST has TE/TX commend word
gint textParsed[2]; // TRUE if parsed, need two flag because there is text for
// two type of point and area
// same idea as hasText - instead of looping on cmd word (INST), flag it once in _parseINST
//gint hasLC[2];
//gint hasCS[2];
// CS override
// FIXME: this assume that the same CS is in the 'alternate' LUP - need proof
gboolean prioOverride; // TRUE if CS overide PLib display priority / same meaning as hasCS()!!
_prios oPrios;
_AUX_Info auxInfo;
} _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
#define US '\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;
static GArray *_RGBA = NULL;
static int _crntPalNo = -1; // -1 init
#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 // 0 - confuse editor efte
// 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
//--------------------------
//static GPtrArray *_objList = NULL;
static GHashTable *_objHash = 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 // 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 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])
if (US == buf[reclen+8])
buf[reclen+8] = '\0';
// EOF
if (fp->cnt >= fp->sz)
return -1;
return linelen;
}
//static int _chopAtEOL(char *pBuffer, char c)
static int _chopAtUS(char *pBuffer, char c)
// replace S52 US (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 == US )
*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("WARNING: 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("WARNING: 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_PL_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_LUP_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_PL_SMB_NMLN);
}
static gint _cmpCOND(gconstpointer nameA, gconstpointer nameB)
// compare Cond Symbology name
{
return strncmp((char*)nameA, (char*)nameB, S52_PL_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_symDef *def = (_S52_symDef*) 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
{
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 *geo)
// 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;
//int trace = TRUE; // debug
int best_nATTmatch = 0; // best attribute value match
return_if_null(LUPlist);
return_if_null(geo);
// setup default LUP to the first LUP
_LUP *bestLUP = LUPlist;
/* debug
if (TRUE==trace && 0==g_strcmp0(LUPlist->OBCL, "SBDARE") && 'A'==S57_getObjtype(geo)) {
//trace = TRUE;
S57_dumpData(geo, FALSE);
}
*/
//GString *FIDNstr = S57_getAttVal(geo, "FIDN");
//if (0==strcmp("2135158878", FIDNstr->str)) {
// trace = 1;
// S57_dumpData(geo, FALSE);
// PRINTF("%s\n", FIDNstr->str);
//}
// default LUP [ref S52-A-2:8.3.3.2]
if (NULL == bestLUP->OBCLnext) {
if (NULL == bestLUP->ATTC)
return bestLUP;
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(bestLUP->OBCL, "TSSLPT", S52_LUP_NMLN)){
if (NULL == S57_getAttVal(geo, "ORIENT")) {
// FIXME: hit this in S-64 ENC
PRINTF("FIXME: TSSLPT found ... check this ... no ORIENT\n");
//g_assert(0);
return bestLUP;
}
}
// 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 = FALSE; //
int nATTmatch = 0; // nbr of att value match for this LUP
// Note: ATTC was previously chopped at US (0x1F) replace by '\0' (abc\0def\0\0)
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); // src \0 never reach!
memcpy(attl, attLV, 6);
//#ifdef S52_USE_CA_ENC
//attv = S57_getAttVal(geo, attl); // will NOT return EMPTY_NUMBER_MARKER
//#else
attv = S57_getAttValALL(geo, attl); // will return EMPTY_NUMBER_MARKER if there
//#endif // S52_USE_CA_ENC
// attl name match
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)
// Note: ATTC str has x1F (US) replace by '\0'
// FIXME: SBDARE this match last LUP WATLEV4NATSUR
if ((attLV[6] == '\0') && (0!=g_strcmp0(attv->str, EMPTY_NUMBER_MARKER))){
++nATTmatch;
//PRINTF("DEBUG: ATTC: %s INST: %s\n", LUPlist->ATTC->str, LUPlist->INST->str);
//S57_dumpData(geo, FALSE);
//g_assert(0);
} else {
// special case [S52-A-2:8.3.3.4(ii)]
// ie. match if value is unknown
#ifdef S52_USE_CA_ENC
// debug - CA ENC
if ((attLV[6] == '?') && FALSE ) { // OK for CA & S64 ENC
#else
if ((attLV[6] == '?') && (0==g_strcmp0(attv->str, EMPTY_NUMBER_MARKER)) ) {
#endif
++nATTmatch;
//PRINTF("DEBUG: ATTC: %s INST: %s\n", LUPlist->ATTC->str, LUPlist->INST->str);
//S57_dumpData(geo, FALSE);
//g_assert(0);
} else {
// value check - no need to handle UTF with g_strstr
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
// FIX: record the max lenght of att val str of a match
if (NULL != tmpVal) {
int valS57 = atoi(attv->str);
int valLUP = atoi(attLV+6);
if (valS57 == valLUP)
++nATTmatch;
} else {
// skip this lookup
skipLUP = TRUE;
}
}
}
// get next attribute name/value for this LUP
// Note: str chopped to \0 at US (Unit Separator - 0x1F)
while (*attLV != '\0') {
attLV++; // find end of attribue name/value pair
}
attLV++; // skip end of field --witch is now a '\0'
} else {
// no att value - no match
skipLUP = TRUE;
nATTmatch = 0;
//debug
//PRINTF("DEBUG: NULL attVal for attName: %s\n", attLV);
}
} // 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;
bestLUP = LUPlist;
if (trace) {
PRINTF("DEBUG: %s:CANDIDATE(%i): ----------------\n", bestLUP->OBCL, best_nATTmatch);
if (NULL != bestLUP->ATTC)
_dumpATT(bestLUP->ATTC->str);
}
}
LUPlist = LUPlist->OBCLnext;
//nATTmatch = 0;
} // while
if (trace) {
PRINTF("DEBUG: SELECTED LUP: %s\n", bestLUP->INST->str);
if (NULL != bestLUP->ATTC)
_dumpATT(bestLUP->ATTC->str);
//g_assert(0);
}
return bestLUP;
}
// 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_symDef*)g_tree_lookup(_selSMB(dbnm), str); \
if (cmd->cmd.def == NULL) { \
cmd->cmd.def = (_S52_symDef*)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, gint *hasText)
// Parse "Symbology Instruction" (LUP) and link them to rendering rules
{
char *str = inst->str;
_cmdWL *top = NULL;
_cmdWL *last = NULL;
*hasText = FALSE;
// 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);
////////////////////////////////
// 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) *hasText = TRUE; SCANFWRD
else CMDWRD(TE, S52_CMD_TXT_TE) *hasText = TRUE; 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 NULL;
}