forked from sduclos/S52
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS57data.c
1861 lines (1457 loc) · 44.6 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-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 "S57data.h" // S57_geo
#include "S52utils.h" // PRINTF()
#include <math.h> // INFINITY, nearbyint()
#ifdef S52_USE_PROJ
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 _S57ID = 1; // start at 1, the number of object loaded
typedef struct _pt3 { double x,y,z; } pt3;
typedef struct _pt2 { double x,y; } pt2;
// assume: extent canonical: x1 < x2, y1 < y2
typedef struct _rect {
double x1; // W LL
double y1; // S LL
double x2; // E UR
double y2; // N UR
} _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'
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
#define S57_ATT_NM_LN 6 // S57 Class Attribute Name lenght
#define S57_GEO_NM_LN 13 // GDAL/OGR primitive name: "ConnectedNode"
typedef struct _S57_geo {
guint S57ID; // record ID / S52ObjectHandle use as index in S52_obj GPtrArray
//guint s52objID; // optimisation: numeric value of OBCL string
char name[S57_GEO_NM_LN+1]; // 6 - object name + '\0'
// 8 - WOLDNM + '\0'
// 13 - ConnectedNode + '\0'
S57_Obj_t obj_t; // PL & S57 - P/L/A
_rect rect; // lat/lon extent of this S75 object
// length of geo data (POINT, LINE, AREA) currently in buffer
guint geoSize; // max is 1 point / linexyznbr / ringxyznbr[0]
// hold coordinate before and after projection
// FIXME: why alloc xyz*1, easy to handle like the reste, but fragment mem !?!
geocoord *pointxyz; // point (alloc)
guint linexyznbr; // line number of point XYZ (alloc)
geocoord *linexyz; // line coordinate
// area
guint ringnbr; // number of ring
guint *ringxyznbr; // number coords per ring (alloc)
geocoord **ringxyz; // coords of rings (alloc)
// 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;
// FIXME: SCAMAX
#ifdef S52_USE_SUPP_LINE_OVERLAP
// only for object "Edge"
gchar *name_rcidstr; // optimisation: point to Att NAME_RCID str value
S57_geo *geoOwner; // s57 obj that use this edge
//S57_AW_t origAW; // debug - original Area Winding, CW: area < 0, CCW: area > 0
#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
gboolean highlight; // highlight this geo object (cursor pick / hazard - experimental)
gboolean hazard; // TRUE if a Safety Contour / hazard - use by leglin and GUARDZONE
} _S57_geo;
static GString *_attList = NULL;
static int _doneGeoData(_S57_geo *geo)
// delete the geo data it self - data from OGR is a copy
{
#ifdef S52_USE_GV
// not GV geo data
return TRUE;
#endif
// POINT
if (NULL != geo->pointxyz) {
g_free((geocoord*)geo->pointxyz);
geo->pointxyz = NULL;
}
// LINES
if (NULL != geo->linexyz) {
g_free((geocoord*)geo->linexyz);
geo->linexyz = NULL;
}
// AREAS
if (NULL != geo->ringxyz){
unsigned int i;
for(i = 0; i < geo->ringnbr; ++i) {
if (NULL != geo->ringxyz[i])
g_free((geocoord*)geo->ringxyz[i]);
geo->ringxyz[i] = NULL;
}
g_free((geocoord*)geo->ringxyz);
geo->ringxyz = NULL;
}
if (NULL != geo->ringxyznbr) {
g_free(geo->ringxyznbr);
geo->ringxyznbr = NULL;
}
geo->linexyznbr = 0;
geo->ringnbr = 0;
return TRUE;
}
int S57_doneData (_S57_geo *geo, gpointer user_data)
{
// quiet line overlap analysis that trigger a bunch of harmless warning
if (NULL!=user_data && NULL==geo)
return FALSE;
#ifdef S52_USE_WORLD
{
S57_geo *geoNext = NULL;
if (NULL != (geoNext = S57_getNextPoly(geo))) {
S57_doneData(geoNext, user_data);
}
}
#endif
_doneGeoData(geo);
S57_donePrimGeo(geo);
if (NULL != geo->attribs)
g_datalist_clear(&geo->attribs);
if (NULL != geo->centroid)
g_array_free(geo->centroid, TRUE);
g_free(geo);
return TRUE;
}
int S57_initPROJ()
// NOTE: corrected for PROJ 4.6.0 ("datum=WGS84")
{
if (FALSE == _doInit)
return FALSE;
#ifdef S52_USE_PROJ
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();
g_assert(0);
return FALSE;
}
#endif
if (NULL == _attList)
_attList = g_string_new("");
// FIXME: will need resetting for different projection
_doInit = FALSE;
return TRUE;
}
int S57_donePROJ()
{
#ifdef S52_USE_PROJ
if (NULL != _pjsrc) pj_free(_pjsrc);
if (NULL != _pjdst) pj_free(_pjdst);
#endif
_pjsrc = NULL;
_pjdst = NULL;
_doInit = TRUE;
if (NULL != _attList)
g_string_free(_attList, TRUE);
_attList = NULL;
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]
/* http://en.wikipedia.org/wiki/Web_Mercator
PROJCS["WGS 84 / Pseudo-Mercator",
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,
AUTHORITY["EPSG","9122"]],
AUTHORITY["EPSG","4326"]],
PROJECTION["Mercator_1SP"],
PARAMETER["central_meridian",0],
PARAMETER["scale_factor",1],
PARAMETER["false_easting",0],
PARAMETER["false_northing",0],
UNIT["metre",1,
AUTHORITY["EPSG","9001"]],
AXIS["X",EAST],
AXIS["Y",NORTH],
EXTENSION["PROJ4","+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs"],
AUTHORITY["EPSG","3857"]]
*/
const char *templ = "+proj=merc +lat_ts=%.6f +lon_0=%.6f +ellps=WGS84 +datum=WGS84 +unit=m";
// FIXME: utm tilt ENC .. why?
//const char *templ = "+proj=utm +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);
#ifdef S52_USE_PROJ
if (NULL != _pjdst)
pj_free(_pjdst);
_pjdst = pj_init_plus(_pjstr);
if (FALSE == _pjdst) {
PRINTF("ERROR: init pjdst PROJ4 (lat:%f) [%s]\n", lat, pj_strerrno(pj_errno));
g_assert(0);
return FALSE;
}
#endif
return TRUE;
}
GCPTR 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;
#ifdef S52_USE_PROJ
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;
#endif
return uv;
}
int S57_geo2prj3dv(guint npt, double *data)
// convert to XY 'in-place'
{
#ifdef S52_USE_GV
return TRUE;
#endif
return_if_null(data);
pt3 *pt = (pt3*)data;
if (TRUE == _doInit) {
S57_initPROJ();
}
if (NULL == _pjdst) {
PRINTF("WARNING: nothing to project to .. load a chart first!\n");
return FALSE;
}
#ifdef S52_USE_PROJ
// 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("WARNING: in transform (%i): %s (%f,%f)\n", ret, pj_strerrno(pj_errno), pt->x, pt->y);
g_assert(0);
return FALSE;
}
/*
// FIXME: heuristic to reduce the number of point (for LOD):
//try to (and check) reduce the number of points by flushing decimal
// then libtess should remove coincident points.
// Other trick, try to reduce more by rounding using cell scale
// pt->x = nearbyint(pt->x / (? * 10)) / (? * 10);
//
// test - 1km
pt = (pt3*)data;
for (guint i=0; i<npt; ++i, ++pt) {
pt->x = nearbyint(pt->x / 1000.0) * 1000.0;
pt->y = nearbyint(pt->y / 1000.0) * 1000.0;
}
//*/
#endif
return TRUE;
}
int S57_geo2prj(_S57_geo *geo)
{
return_if_null(geo);
if (TRUE == _doInit)
S57_initPROJ();
#ifdef S52_USE_PROJ
guint nr = S57_getRingNbr(geo);
for (guint i=0; i<nr; ++i) {
guint npt;
double *ppt;
if (TRUE == S57_getGeoData(geo, i, &npt, &ppt)) {
if (FALSE == S57_geo2prj3dv(npt, ppt))
return FALSE;
}
}
#endif // S52_USE_PROJ
return TRUE;
}
S57_geo *S57_setPOINT(geocoord *xyz)
{
return_if_null(xyz);
_S57_geo *geo = g_new0(_S57_geo, 1);
//_S57_geo *geo = g_try_new0(_S57_geo, 1);
if (NULL == geo)
g_assert(0);
geo->S57ID = _S57ID++;
geo->obj_t = S57_POINT_T;
geo->pointxyz = xyz;
geo->rect.x1 = INFINITY;
geo->rect.y1 = INFINITY;
geo->rect.x2 = -INFINITY;
geo->rect.y2 = -INFINITY;
geo->scamin = INFINITY;
#ifdef S52_USE_WORLD
geo->nextPoly = NULL;
#endif
return geo;
}
#ifdef S52_USE_SUPP_LINE_OVERLAP
// experimental
S57_geo *S57_setGeoLine(_S57_geo *geo, guint xyznbr, geocoord *xyz)
{
return_if_null(geo);
geo->obj_t = S57_LINES_T; // because some Edge objet default to _META_T when no geo yet
geo->linexyznbr = xyznbr;
geo->linexyz = xyz;
return geo;
}
#endif // S52_USE_SUPP_LINE_OVERLAP
S57_geo *S57_setLINES(guint xyznbr, geocoord *xyz)
{
_S57_geo *geo = g_new0(_S57_geo, 1);
//_S57_geo *geo = g_try_new0(_S57_geo, 1);
if (NULL == geo)
g_assert(0);
return_if_null(geo);
geo->S57ID = _S57ID++;
geo->obj_t = S57_LINES_T;
geo->linexyznbr = xyznbr;
geo->linexyz = xyz;
geo->rect.x1 = INFINITY;
geo->rect.y1 = INFINITY;
geo->rect.x2 = -INFINITY;
geo->rect.y2 = -INFINITY;
geo->scamin = INFINITY;
#ifdef S52_USE_WORLD
geo->nextPoly = NULL;
#endif
return geo;
}
#if 0
S57_geo *S57_setMLINE(guint nLineCount, guint *linexyznbr, geocoord **linexyz)
{
_S57_geo *geo = g_new0(_S57_geo, 1);
//_S57_geo *geo = g_try_new0(_S57_geo, 1);
if (NULL == geo)
g_assert(0);
geo->ID = _ID++;
geo->obj_t = MLINE_T;
geo->linenbr = nLineCount;
geo->linexyznbr = linexyznbr;
geo->linexyz = linexyz;
#ifdef S52_USE_WORLD
geo->nextPoly = NULL;
#endif
return geo;
}
#endif // 0
//S57_geo *S57_setAREAS(guint ringnbr, guint *ringxyznbr, geocoord **ringxyz, S57_AW_t origAW)
S57_geo *S57_setAREAS(guint ringnbr, guint *ringxyznbr, geocoord **ringxyz)
{
return_if_null(ringxyznbr);
return_if_null(ringxyz);
_S57_geo *geo = g_new0(_S57_geo, 1);
//_S57_geo *geo = g_try_new0(_S57_geo, 1);
if (NULL == geo)
g_assert(0);
geo->S57ID = _S57ID++;
geo->obj_t = S57_AREAS_T;
geo->ringnbr = ringnbr;
geo->ringxyznbr = ringxyznbr;
geo->ringxyz = ringxyz;
geo->rect.x1 = INFINITY;
geo->rect.y1 = INFINITY;
geo->rect.x2 = -INFINITY;
geo->rect.y2 = -INFINITY;
geo->scamin = INFINITY;
#ifdef S52_USE_WORLD
geo->nextPoly = NULL;
#endif
//#ifdef S52_USE_SUPP_LINE_OVERLAP
// geo->origAW = origAW;
//#else
// (void)origAW;
//#endif
return geo;
}
S57_geo *S57_set_META()
{
_S57_geo *geo = g_new0(_S57_geo, 1);
//_S57_geo *geo = g_try_new0(_S57_geo, 1);
if (NULL == geo)
g_assert(0);
geo->S57ID = _S57ID++;
geo->obj_t = S57__META_T;
geo->rect.x1 = INFINITY;
geo->rect.y1 = INFINITY;
geo->rect.x2 = -INFINITY;
geo->rect.y2 = -INFINITY;
geo->scamin = INFINITY;
#ifdef S52_USE_WORLD
geo->nextPoly = NULL;
#endif
return geo;
}
int S57_setName(_S57_geo *geo, const char *name)
// FIXME: name come from GDAL/OGR s57objectclasses.csv
{
return_if_null(geo);
return_if_null(name);
//*
if (S57_GEO_NM_LN < strlen(name)) {
PRINTF("DEBUG: S57_geo name overflow max S57_GEO_NM_LN : %s\n", name);
g_assert(0);
}
//*/
int len = strlen(name);
len = (S57_GEO_NM_LN < len) ? S57_GEO_NM_LN : len;
memcpy(geo->name, name, len);
geo->name[len] = '\0';
return TRUE;
}
GCPTR S57_getName(_S57_geo *geo)
{
return_if_null(geo);
return_if_null(geo->name);
return geo->name;
}
guint S57_getRingNbr(_S57_geo *geo)
{
return_if_null(geo);
// since this is used with S57_getGeoData
// META object don't need to be projected for rendering
switch (geo->obj_t) {
case S57_POINT_T:
case S57_LINES_T:
return 1;
case S57_AREAS_T:
return geo->ringnbr;
default:
return 0;
}
}
int S57_getGeoData(_S57_geo *geo, guint ringNo, guint *npt, double **ppt)
// helper providing uniform access to geo
// WARNING: npt is the allocated mem (capacity)
{
return_if_null(geo);
if (S57_AREAS_T==geo->obj_t && geo->ringnbr<ringNo) {
PRINTF("WARNING: invalid ring number requested! \n");
*npt = 0;
g_assert(0);
return FALSE;
}
switch (geo->obj_t) {
case S57__META_T: *npt = 0; break; // meta geo stuff (ex: C_AGGR)
case S57_POINT_T:
if (NULL != geo->pointxyz) {
*npt = 1;
*ppt = geo->pointxyz;
} else {
*npt = 0;
}
break;
case S57_LINES_T:
if (NULL != geo->linexyz) {
*npt = geo->linexyznbr;
*ppt = geo->linexyz;
} else {
*npt = 0;
}
break;
case S57_AREAS_T:
if (NULL != geo->ringxyznbr) {
*npt = geo->ringxyznbr[ringNo];
*ppt = geo->ringxyz[ringNo];
} else {
*npt = 0;
}
// debug
//if (geodata->ringnbr > 1) {
// PRINTF("DEBUG: AREA_T ringnbr:%i only exterior ring used\n", geodata->ringnbr);
//}
break;
default:
PRINTF("ERROR: object type invalid (%i)\n", geo->obj_t);
g_assert(0);
return FALSE;
}
// alloc'ed mem for xyz vs xyz size
if (*npt < geo->geoSize) {
PRINTF("ERROR: geo lenght greater then npt - internal error\n");
g_assert(0);
return FALSE;
}
if (0 == *npt) {
//PRINTF("DEBUG: npt == 0\n");
return FALSE;
}
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);
// failsafe
prim->list = NULL;
prim->vertex = NULL;
g_free(prim);
return NULL;
}
S57_prim *S57_initPrimGeo(_S57_geo *geo)
{
return_if_null(geo);
geo->prim = S57_initPrim(geo->prim);
return geo->prim;
}
S57_geo *S57_donePrimGeo(_S57_geo *geo)
{
return_if_null(geo);
if (NULL != geo->prim) {
S57_donePrim(geo->prim);
geo->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 *geo)
{
return_if_null(geo);
return geo->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;
}
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 *geo, double x1, double y1, double x2, double y2)
// assume: extent canonical - W, S, E, N
{
return_if_null(geo);
// debug
//if (0 == g_strncasecmp(geo->name->str, "M_COVR", 6)) {
// PRINTF("DEBUG: %s: %f, %f UR: %f, %f\n", geo->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", geo->name->str, x1, y1, x2, y2);
g_assert(0);
}
*/
/* newVRMEBL pass here now, useless anyway
if (isinf(x1) && isinf(x2)) {
//PRINTF("DEBUG: %s: LL: %f, %f UR: %f, %f\n", geo->name->str, x1, y1, x2, y2);
PRINTF("DEBUG: %s: LL: %f, %f UR: %f, %f\n", geo->name, x1, y1, x2, y2);
g_assert(0);
return FALSE;
}
*/
geo->rect.x1 = x1;
geo->rect.y1 = y1;
geo->rect.x2 = x2;
geo->rect.y2 = y2;
return TRUE;
}
int S57_getExt(_S57_geo *geo, double *x1, double *y1, double *x2, double *y2)
// assume: extent canonical
{
// inside cull loop this check is useless
// but other are not
return_if_null(geo);
*x1 = geo->rect.x1; // W
*y1 = geo->rect.y1; // S
*x2 = geo->rect.x2; // E
*y2 = geo->rect.y2; // N
// no extent: "m_covr", "$CSYMB", ..
if (TRUE == isinf(geo->rect.x1))
return FALSE;
return TRUE;
}
S57_Obj_t S57_getObjtype(_S57_geo *geo)
{
if (NULL == geo)
return S57__META_T;
return geo->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 *geo)
{
int cnt = 0;
g_datalist_foreach(&geo->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 (S57_ATT_NM_LN == 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);
}
}