forked from sduclos/S52
/
S57data.c
1777 lines (1377 loc) · 42.9 KB
/
S57data.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
// S57data.c: interface to S57 geo data
//
// Project: OpENCview
/*
This file is part of the OpENCview project, a viewer of ENC.
Copyright (C) 2000-2014 Sylvain Duclos sduclos@users.sourceforge.net
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 "S57data.h" // S57_geo
#include "S52utils.h" // PRINTF(), S52_strlen()
#include <math.h> // INFINITY
#ifdef S52_USE_PROJ
#include <proj_api.h> // projUV, projXY, projPJ
static projPJ _pjsrc = NULL; // projection source
static projPJ _pjdst = NULL; // projection destination
static char *_pjstr = NULL;
static int _doInit = TRUE; // will set new src projection
static const char *_argssrc = "+proj=latlong +ellps=WGS84 +datum=WGS84";
//static const char *_argsdst = "+proj=merc +ellps=WGS84 +datum=WGS84 +unit=m +no_defs";
// Note: ../../../FWTools/FWTools-2.0.6/bin/gdalwarp -t_srs "+proj=merc +ellps=WGS84 +datum=WGS84 +unit=m +no_defs" 46307260_LOD2.tif 46307260_LOD2.merc.tif
#endif
// 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 */
#define UNKNOWN (1.0/0.0) //HUGE_VAL // INFINITY/NAN
// debug: current object's internal ID
static unsigned int _id = 1; // start at 1, the number of object loaded
typedef struct _pt3 { double x,y,z; } pt3;
typedef struct _pt2 { double x,y; } pt2;
//struct _pt3{ double x,y,z; } pt3;
//struct pt3 { double x,y,z; };
// assume: extent canonical: x1 < x2, y1 < y2
typedef struct _rect {
double x1; // W
double y1; // S
double x2; // E
double y2; // N
} _rect;
// data for glDrawArrays()
typedef struct _prim {
int mode;
int first;
int count;
//guint mode;
//guint first;
//guint count;
} _prim;
typedef struct _S57_prim {
GArray *list; // list of _prim in 'vertex'
// optimisation: use XY, so that the GPU move, internaly, 1/3 less data
// but this is useless if no LOD for ENC - zooming out will add 10x more data
GArray *vertex; // XYZ geographic coordinate (bouble or float for GLES2 since some go right in the GPU - ie line)
guint DList; // display list of the above
} _S57_prim;
// S57 object geo data
typedef struct _S57_geo {
guint id; // record id (debug)
//guint s52objID; // optimisation: numeric value of OBCL string
GString *name; // object name 6/8 + '\0'; used for S52 LUP
S52_Obj_t obj_t; // used in CS
//S57_Obj_t obj_t; // used in CS
_rect rect; // lat/lon extent of object
gboolean sup; // geo sup - TRUE if outside view
// length of geo data (POINT, LINE, AREA) currently in buffer
guint dataSize; // max is 1, linexyznbr, ringxyznbr[0]
// hold coordinate before and after projection
geocoord *pointxyz; // point
guint linexyznbr; // line
geocoord *linexyz;
guint ringnbr; // area
guint *ringxyznbr;
geocoord **ringxyz;
// hold tessalated geographic and projected coordinate of area
// in a format suitable for OpenGL
S57_prim *prim;
GData *attribs;
#ifdef S52_USE_C_AGGR_C_ASSO
// point to the S57 relationship object C_AGGR / C_ASSO this S57_geo belong
S57_geo *relation;
#endif
// for CS - object "touched" by this object
union {
S57_geo *TOPMAR; // break out objet class "touched"
S57_geo *LIGHTS; // break out objet class "touched"
S57_geo *DEPARE; // break out objet class "touched"
S57_geo *DEPVAL; // break out objet class "touched"
} touch;
double scamin;
#ifdef S52_USE_SUPP_LINE_OVERLAP
GString *rcidstr; // optimisation point to rcid str value
S57_geo *link; // experimetal, link to auxiliary
#endif
// centroid - save current centroids of this object
// optimisation mostly for layer 9 AREA
guint centroidIdx;
GArray *centroid;
#ifdef S52_USE_WORLD
S57_geo *nextPoly;
#endif
int highlight; // highlight this geo object (cursor pick - experimental)
} _S57_geo;
static GString *_attList = NULL;
static void _string_free(gpointer data)
{
g_string_free((GString*)data, TRUE);
}
static int _doneGeoData(_S57_geo *geoData)
// delete the geo data it self
{
// POINT
if (NULL != geoData->pointxyz) {
g_free((geocoord*)geoData->pointxyz);
geoData->pointxyz = NULL;
}
// LINES
if (NULL != geoData->linexyz) {
g_free((geocoord*)geoData->linexyz);
geoData->linexyz = NULL;
}
// AREAS
if (NULL != geoData->ringxyz){
unsigned int i;
for(i = 0; i < geoData->ringnbr; ++i) {
if (NULL != geoData->ringxyz[i])
g_free((geocoord*)geoData->ringxyz[i]);
geoData->ringxyz[i] = NULL;
}
g_free((geocoord*)geoData->ringxyz);
geoData->ringxyz = NULL;
}
if (NULL != geoData->ringxyznbr) {
g_free(geoData->ringxyznbr);
geoData->ringxyznbr = NULL;
}
geoData->linexyznbr = 0;
geoData->ringnbr = 0;
return TRUE;
}
int S57_doneData (_S57_geo *geoData, gpointer user_data)
{
// quiet line overlap analysis that trigger a bunch of harmless warning
if (NULL!=user_data && NULL==geoData)
return FALSE;
#ifdef S52_USE_WORLD
{
S57_geo *geoDataNext = NULL;
if (NULL != (geoDataNext = S57_getNextPoly(geoData))) {
S57_doneData(geoDataNext, user_data);
}
}
#endif
#ifndef S52_USE_GV
// data from OGR is a copy --so this need to be deleted
_doneGeoData(geoData);
#endif
S57_donePrimGeo(geoData);
if (NULL != geoData->name)
g_string_free(geoData->name, TRUE);
if (NULL != geoData->attribs)
g_datalist_clear(&geoData->attribs);
if (NULL != geoData->centroid)
g_array_free(geoData->centroid, TRUE);
g_free(geoData);
return TRUE;
}
#ifdef S52_USE_PROJ
int S57_initPROJ()
// NOTE: corrected for PROJ 4.6.0 ("datum=WGS84")
{
if (FALSE == _doInit)
return FALSE;
const char *pj_ver = pj_get_release();
if (NULL != pj_ver)
PRINTF("PROJ4 VERSION: %s\n", pj_ver);
// setup source projection
if (!(_pjsrc = pj_init_plus(_argssrc))){
PRINTF("error init src PROJ4\n");
S57_donePROJ();
return FALSE;
}
// FIXME: will need resetting for different projection
_doInit = FALSE;
if (NULL == _attList)
_attList = g_string_new("");
return TRUE;
}
int S57_donePROJ()
{
if (NULL != _pjsrc) pj_free(_pjsrc);
if (NULL != _pjdst) pj_free(_pjdst);
_pjsrc = NULL;
_pjdst = NULL;
_doInit = TRUE;
if (NULL != _attList)
g_string_free(_attList, TRUE);
if (NULL != _pjstr)
g_free(_pjstr);
_pjstr = NULL;
return TRUE;
}
int S57_setMercPrj(double lat, double lon)
{
// From: http://trac.osgeo.org/proj/wiki/GenParms (and other link from that page)
// Note: For merc, PROJ.4 does not support a latitude of natural origin other than the equator (lat_0=0).
// Note: true scale using the +lat_ts parameter, which is the latitude at which the scale is 1.
// Note: +lon_wrap=180.0 convert clamp [-180..180] to clamp [0..360]
const char *templ = "+proj=merc +lat_ts=%.6f +lon_0=%.6f +ellps=WGS84 +datum=WGS84 +unit=m";
if (NULL != _pjstr) {
PRINTF("WARNING: Merc projection allready set\n");
return FALSE;
}
_pjstr = g_strdup_printf(templ, lat, lon);
PRINTF("DEBUG: lat:%f, lon:%f [%s]\n", lat, lon, _pjstr);
if (NULL != _pjdst)
pj_free(_pjdst);
if (!(_pjdst = pj_init_plus(_pjstr))) {
PRINTF("ERROR: init pjdst PROJ4 (lat:%f) [%s]\n", lat, pj_strerrno(pj_errno));
g_assert(0);
return FALSE;
}
return TRUE;
}
cchar *S57_getPrjStr(void)
{
return _pjstr;
}
projXY S57_prj2geo(projUV uv)
// convert PROJ to geographic (LL)
{
if (TRUE == _doInit) return uv;
if (NULL == _pjdst) return uv;
uv = pj_inv(uv, _pjdst);
if (0 != pj_errno) {
PRINTF("ERROR: x=%f y=%f %s\n", uv.u, uv.v, pj_strerrno(pj_errno));
g_assert(0);
return uv;
}
uv.u /= DEG_TO_RAD;
uv.v /= DEG_TO_RAD;
return uv;
}
int S57_geo2prj3dv(guint npt, double *data)
// convert to XY 'in-place'
{
return_if_null(data);
pt3 *pt = (pt3*)data;
if (TRUE == _doInit)
S57_initPROJ();
if (NULL == _pjdst) {
PRINTF("ERROR: nothing to project to .. load a chart frist!\n");
// debug
g_assert(0);
return FALSE;
}
// deg to rad --latlon
for (guint i=0; i<npt; ++i, ++pt) {
pt->x *= DEG_TO_RAD,
pt->y *= DEG_TO_RAD;
// debug
//if (-10.0 == pt->z) {
// PRINTF("DEBUG: overlap found\n");
// //g_assert(0);
//}
}
// reset to beginning
pt = (pt3*)data;
// rad to cartesian --mercator
int ret = pj_transform(_pjsrc, _pjdst, npt, 3, &pt->x, &pt->y, &pt->z);
if (0 != ret) {
PRINTF("ERROR: in transform (%i): %s (%f,%f)\n", ret, pj_strerrno(pj_errno), pt->x, pt->y);
g_assert(0);
return FALSE;
}
return TRUE;
}
int S57_geo2prj(_S57_geo *geoData)
{
return_if_null(geoData);
if (TRUE == _doInit)
S57_initPROJ();
guint nr = S57_getRingNbr(geoData);
for (guint i=0; i<nr; ++i) {
guint npt;
double *ppt;
if (TRUE == S57_getGeoData(geoData, i, &npt, &ppt))
if (FALSE == S57_geo2prj3dv(npt, ppt))
return FALSE;
}
return TRUE;
}
#endif
S57_geo *S57_setPOINT(geocoord *xyz)
{
return_if_null(xyz);
_S57_geo *geoData = g_new0(_S57_geo, 1);
//_S57_geo *geoData = g_try_new0(_S57_geo, 1);
if (NULL == geoData)
g_assert(0);
geoData->id = _id++;
geoData->obj_t = POINT_T;
geoData->pointxyz = xyz;
geoData->rect.x1 = INFINITY;
geoData->rect.y1 = INFINITY;
geoData->rect.x2 = -INFINITY;
geoData->rect.y2 = -INFINITY;
geoData->scamin = INFINITY;
#ifdef S52_USE_WORLD
geoData->nextPoly = NULL;
#endif
return geoData;
}
// experimental
S57_geo *S57_setGeoLine(_S57_geo *geoData, guint xyznbr, geocoord *xyz)
{
return_if_null(geoData);
geoData->obj_t = LINES_T; // because some Edge objet default to _META_T when no geo yet
geoData->linexyznbr = xyznbr;
geoData->linexyz = xyz;
return geoData;
}
S57_geo *S57_setLINES(guint xyznbr, geocoord *xyz)
{
_S57_geo *geoData = g_new0(_S57_geo, 1);
//_S57_geo *geoData = g_try_new0(_S57_geo, 1);
if (NULL == geoData)
g_assert(0);
return_if_null(geoData);
geoData->id = _id++;
geoData->obj_t = LINES_T;
geoData->linexyznbr = xyznbr;
geoData->linexyz = xyz;
geoData->rect.x1 = INFINITY;
geoData->rect.y1 = INFINITY;
geoData->rect.x2 = -INFINITY;
geoData->rect.y2 = -INFINITY;
geoData->scamin = INFINITY;
#ifdef S52_USE_WORLD
geoData->nextPoly = NULL;
#endif
return geoData;
}
#if 0
S57_geo *S57_setMLINE(guint nLineCount, guint *linexyznbr, geocoord **linexyz)
{
_S57_geo *geoData = g_new0(_S57_geo, 1);
//_S57_geo *geoData = g_try_new0(_S57_geo, 1);
if (NULL == geoData)
g_assert(0);
geoData->id = _id++;
geoData->obj_t = MLINE_T;
geoData->linenbr = nLineCount;
geoData->linexyznbr = linexyznbr;
geoData->linexyz = linexyz;
#ifdef S52_USE_WORLD
geoData->nextPoly = NULL;
#endif
return geoData;
}
#endif
S57_geo *S57_setAREAS(guint ringnbr, guint *ringxyznbr, geocoord **ringxyz)
{
return_if_null(ringxyznbr);
return_if_null(ringxyz);
_S57_geo *geoData = g_new0(_S57_geo, 1);
//_S57_geo *geoData = g_try_new0(_S57_geo, 1);
if (NULL == geoData)
g_assert(0);
geoData->id = _id++;
geoData->obj_t = AREAS_T;
geoData->ringnbr = ringnbr;
geoData->ringxyznbr = ringxyznbr;
geoData->ringxyz = ringxyz;
geoData->rect.x1 = INFINITY;
geoData->rect.y1 = INFINITY;
geoData->rect.x2 = -INFINITY;
geoData->rect.y2 = -INFINITY;
geoData->scamin = INFINITY;
#ifdef S52_USE_WORLD
geoData->nextPoly = NULL;
#endif
return geoData;
}
S57_geo *S57_set_META()
{
_S57_geo *geoData = g_new0(_S57_geo, 1);
//_S57_geo *geoData = g_try_new0(_S57_geo, 1);
if (NULL == geoData)
g_assert(0);
geoData->id = _id++;
geoData->obj_t = _META_T;
geoData->rect.x1 = INFINITY;
geoData->rect.y1 = INFINITY;
geoData->rect.x2 = -INFINITY;
geoData->rect.y2 = -INFINITY;
geoData->scamin = INFINITY;
#ifdef S52_USE_WORLD
geoData->nextPoly = NULL;
#endif
return geoData;
}
int S57_setName(_S57_geo *geoData, const char *name)
// NOTE: this is a S57 object name .. UTF-16 or UTF-8
// use g_string to handle that
{
return_if_null(geoData);
return_if_null(name);
geoData->name = g_string_new(name);
return TRUE;
}
cchar *S57_getName(_S57_geo *geoData)
{
return_if_null(geoData);
return_if_null(geoData->name);
return geoData->name->str;
}
guint S57_getRingNbr(_S57_geo *geoData)
{
return_if_null(geoData);
// since this is used with S57_getGeoData
// META object don't need to be projected for rendering
switch (geoData->obj_t) {
case POINT_T:
case LINES_T:
return 1;
case AREAS_T:
return geoData->ringnbr;
default:
return 0;
}
}
int S57_getGeoData(_S57_geo *geoData, guint ringNo, guint *npt, double **ppt)
// helper providing uniform access to geoData
// WARNING: npt is the allocated mem (capacity)
{
return_if_null(geoData);
if (AREAS_T==geoData->obj_t && geoData->ringnbr<ringNo) {
PRINTF("WARNING: invalid ring number requested! \n");
*npt = 0;
g_assert(0);
}
switch (geoData->obj_t) {
case _META_T: *npt = 0; break; // meta geo stuff (ex: C_AGGR)
case POINT_T:
if (NULL != geoData->pointxyz) {
*npt = 1;
*ppt = geoData->pointxyz;
} else {
*npt = 0;
}
break;
case LINES_T:
if (NULL != geoData->linexyz) {
*npt = geoData->linexyznbr;
*ppt = geoData->linexyz;
} else {
*npt = 0;
}
break;
case AREAS_T:
if (NULL != geoData->ringxyznbr) {
*npt = geoData->ringxyznbr[ringNo];
*ppt = geoData->ringxyz[ringNo];
} else {
*npt = 0;
}
//else
// PRINTF("ERROR: atempt to access a tessed area .. \n"
// "(there is no geo anymore!!)\n");
// WARNING: check if begin/end vertex are the same
// (OpenGIS: closed and simple ring)
// If so shorten it to help trace tesss (in combineCB)
/*
{
int n = 3 * (*npt-1);
double *p = *ppt;
if (p[0] == p[n+0] &&
p[1] == p[n+1] &&
p[2] == p[n+2]) {
//--(*npt);
// *npt -= 1; // <<< shorten
} else {
PRINTF("not close/simple ring\n");
//PRINTF(" START: x=%f y=%f z=%f\n", p[0],p[1],p[2]);
//PRINTF(" END: x=%f y=%f z=%f\n", p[n+0],p[n+1],p[n+2]);
//exit(0);
}
}
*/
//if (geodata->ringnbr > 1) {
// PRINTF("WARNING!!! AREA_T ringnbr:%i only exterior ring used\n", geodata->ringnbr);
//}
break;
default:
PRINTF("ERROR: object type invalid (%i)\n", geoData->obj_t);
g_assert(0);
return FALSE;
}
if (*npt < geoData->dataSize) {
PRINTF("ERROR: geo lenght greater then npt - internal error\n");
g_assert(0);
return FALSE;
}
if (0==*npt)
return FALSE;
else
return TRUE;
}
S57_prim *S57_initPrim(_S57_prim *prim)
// set/reset primitive holder
{
if (NULL == prim) {
S57_prim *p = g_new0(S57_prim, 1);
//_S57_prim *p = g_try_new0(_S57_prim, 1);
if (NULL == p)
g_assert(0);
p->list = g_array_new(FALSE, FALSE, sizeof(_prim));
p->vertex = g_array_new(FALSE, FALSE, sizeof(vertex_t)*3);
return p;
} else {
g_array_set_size(prim->list, 0);
g_array_set_size(prim->vertex, 0);
return prim;
}
}
S57_prim *S57_donePrim(_S57_prim *prim)
{
//return_if_null(prim);
// some symbol (ex Mariners' Object) dont use primitive since
// not in OpenGL retained mode .. so this warning is a false alarme
if (NULL == prim)
return NULL;
if (NULL != prim->list) g_array_free(prim->list, TRUE);
if (NULL != prim->vertex) g_array_free(prim->vertex, TRUE);
//if (NULL != prim->list) g_array_unref(prim->list);
//if (NULL != prim->vertex) g_array_unref(prim->vertex);
// failsafe
prim->list = NULL;
prim->vertex = NULL;
g_free(prim);
return NULL;
}
S57_prim *S57_initPrimGeo(_S57_geo *geoData)
{
return_if_null(geoData);
geoData->prim = S57_initPrim(geoData->prim);
return geoData->prim;
}
S57_geo *S57_donePrimGeo(_S57_geo *geoData)
{
return_if_null(geoData);
if (NULL != geoData->prim) {
S57_donePrim(geoData->prim);
geoData->prim = NULL;
}
return NULL;
}
int S57_begPrim(_S57_prim *prim, int mode)
{
_prim p;
return_if_null(prim);
p.mode = mode;
p.first = prim->vertex->len;
g_array_append_val(prim->list, p);
return TRUE;
}
int S57_endPrim(_S57_prim *prim)
{
return_if_null(prim);
_prim *p = &g_array_index(prim->list, _prim, prim->list->len-1);
if (NULL == p) {
PRINTF("ERROR: no primitive at index: %i\n", prim->list->len-1);
g_assert(0);
return FALSE;
}
p->count = prim->vertex->len - p->first;
// debug
//if (p->count < 0) {
// g_assert(0);
//}
return TRUE;
}
int S57_addPrimVertex(_S57_prim *prim, vertex_t *ptr)
// add one xyz coord (3 vertex_t)
{
return_if_null(prim);
return_if_null(ptr);
//g_array_append_val(prim->vertex, *ptr);
g_array_append_vals(prim->vertex, ptr, 1);
return TRUE;
}
S57_prim *S57_getPrimGeo(_S57_geo *geoData)
{
return_if_null(geoData);
return geoData->prim;
}
guint S57_getPrimData(_S57_prim *prim, guint *primNbr, vertex_t **vert, guint *vertNbr, guint *vboID)
{
return_if_null(prim);
//return_if_null(prim->list);
*primNbr = prim->list->len;
*vert = (vertex_t*)prim->vertex->data;
*vertNbr = prim->vertex->len;
*vboID = prim->DList;
return TRUE;
}
GArray *S57_getPrimVertex(_S57_prim *prim)
{
return_if_null(prim);
return prim->vertex;
}
/*
S57_prim *S57_setPrimSize(_S57_prim *prim, int sz)
{
return_if_null(prim);
g_array_set_size(prim->list, sz);
g_array_set_size(prim->vertex, sz);
return prim;
}
*/
int S57_setPrimDList (_S57_prim *prim, guint DList)
{
return_if_null(prim);
prim->DList = DList;
return TRUE;
}
int S57_getPrimIdx(_S57_prim *prim, unsigned int i, int *mode, int *first, int *count)
//int S57_getPrimIdx(_S57_prim *prim, guint i, guint *mode, guint *first, guint *count)
{
return_if_null(prim);
if (i>=prim->list->len)
return FALSE;
_prim *p = &g_array_index(prim->list, _prim, i);
if (NULL == p) {
PRINTF("ERROR: no primitive at index: %i\n", i);
g_assert(0);
return FALSE;
}
*mode = p->mode;
*first = p->first;
*count = p->count;
return TRUE;
}
int S57_setExt(_S57_geo *geoData, double x1, double y1, double x2, double y2)
// assume: extent canonical
{
return_if_null(geoData);
//if (0 == g_strncasecmp(geoData->name->str, "M_COVR", 6)) {
// PRINTF("%s: %f, %f UR: %f, %f\n", geoData->name->str, x1, y1, x2, y2);
//}
// canonize lng
//x1 = (x1 < -180.0) ? 0.0 : (x1 > 180.0) ? 0.0 : x1;
//x2 = (x2 < -180.0) ? 0.0 : (x2 > 180.0) ? 0.0 : x2;
// canonize lat
//y1 = (y1 < -90.0) ? 0.0 : (y1 > 90.0) ? 0.0 : y1;
//y2 = (y2 < -90.0) ? 0.0 : (y2 > 90.0) ? 0.0 : y2;
/*
// check prime-meridian crossing
if ((x1 < 0.0) && (0.0 < x2)) {
PRINTF("DEBUG: prime-meridian crossing %s: LL: %f, %f UR: %f, %f\n", geoData->name->str, x1, y1, x2, y2);
g_assert(0);
}
*/
if (isinf(x1) && isinf(x2)) {
PRINTF("DEBUG: %s: LL: %f, %f UR: %f, %f\n", geoData->name->str, x1, y1, x2, y2);
g_assert(0);
}
geoData->rect.x1 = x1;
geoData->rect.y1 = y1;
geoData->rect.x2 = x2;
geoData->rect.y2 = y2;
return TRUE;
}
int S57_getExt(_S57_geo *geoData, double *x1, double *y1, double *x2, double *y2)
// assume: extent canonical
{
return_if_null(geoData);
*x1 = geoData->rect.x1; // W
*y1 = geoData->rect.y1; // S
*x2 = geoData->rect.x2; // E
*y2 = geoData->rect.y2; // N
return TRUE;
}
S52_Obj_t S57_getObjtype(_S57_geo *geoData)
//S57_Obj_t S57_getObjtype(_S57_geo *geoData)
{
if (NULL == geoData)
return _META_T;
return geoData->obj_t;
}
#if 0
// return the number of attributes.
static void _countItems(GQuark key_id, gpointer data, gpointer user_data)
{
const gchar *attName = g_quark_to_string(key_id);
if (6 == strlen(attName)){
int *cnt = (int*)user_data;
*cnt = *cnt + 1;
}
}
int S57_getNumAtt(S57_geo *geoData)
{
int cnt = 0;
g_datalist_foreach(&geoData->attribs, _countItems, &cnt);
return cnt;
}
struct _qwerty {
int currentIdx;
char featureName[20];
char **name;
char **value;
};
static void _getAttValues(GQuark key_id, gpointer data, gpointer user_data)
{
struct _qwerty *attData = (struct _qwerty*)user_data;
GString *attValue = (GString*) data;
const gchar *attName = g_quark_to_string(key_id);
if (6 == S52_strlen(attName)){
strcpy(attData->value[attData->currentIdx], attValue->str);
strcpy(attData->name [attData->currentIdx], attName );
PRINTF("inserting %s %s %d", attName, attValue->str, attData->currentIdx);
attData->currentIdx += 1;
} else {
;// PRINTF("sjov Att: %s = %s \n",attName, attValue->str);
}
}
// recommend you count number of attributes in advance, to allocate the
// propper amount of **. each char *name should be allocated 7 and the char
// *val 20 ????
int S57_getAttributes(_S57_geo *geoData, char **name, char **val)
{
struct _qwerty tmp;
tmp.currentIdx = 0;
tmp.name = name;
tmp.value = val;
g_datalist_foreach(&geoData->attribs, _getAttValues, &tmp);
// strcpy(name[tmp.currentIdx], "x");
// strcpy(val[tmp.currentIdx], "y");
return tmp.currentIdx;
}
#endif
GString *S57_getAttVal(_S57_geo *geoData, const char *att_name)
// return attribute string value or NULL if:
// 1- attribute name abscent
// 2- its a mandatory attribute but its value is not define (EMPTY_NUMBER_MARKER)
{
return_if_null(geoData);
return_if_null(att_name);
//GString *att = (GString*) g_datalist_get_data(&geoData->attribs, att_name);
//GString *att = (GString*) g_dataset_id_get_data(&geoData->attribs, g_quark_try_string(att_name));
GQuark q = g_quark_from_string(att_name);
//GQuark q = g_quark_from_static_string(att_name);
GString *att = (GString*) g_datalist_id_get_data(&geoData->attribs, q);
if (NULL!=att && (0==g_strcmp0(att->str, EMPTY_NUMBER_MARKER))) {
//PRINTF("NOTE: mandatory attribute (%s) with ommited value\n", att_name);
return NULL;
}
// display this NOTE once (because of to many warning)
static int silent = FALSE;
if (!silent && NULL!=att && 0==att->len) {
PRINTF("NOTE: attribute (%s) has no value [obj:%s]\n", att_name, geoData->name->str);
PRINTF(" (this msg will not repeat)\n");
silent = TRUE;
return NULL;
}
return att;
}
GData *S57_setAtt(_S57_geo *geoData, const char *name, const char *val)
{
return_if_null(geoData);
return_if_null(name);
return_if_null(val);
GQuark qname = g_quark_from_string(name);
GString *value = g_string_new(val);