forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapgeos.c
1443 lines (1164 loc) · 39.4 KB
/
mapgeos.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
/******************************************************************************
* $Id$
*
* Project: MapServer
* Purpose: MapServer-GEOS integration.
* Author: Steve Lime and the MapServer team.
*
******************************************************************************
* Copyright (c) 1996-2005 Regents of the University of Minnesota.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#include "mapserver.h"
#ifdef USE_GEOS
// To avoid accidental use of non reentrant GEOS API.
// (check only effective in GEOS >= 3.5)
#define GEOS_USE_ONLY_R_API
#include <geos_c.h>
/*
** Error handling...
*/
static void msGEOSError(const char *format, ...)
{
va_list args;
va_start (args, format);
msSetError(MS_GEOSERR, format, "msGEOSError()", args); /* just pass along to MapServer error handling */
va_end(args);
return;
}
static void msGEOSNotice(const char *fmt, ...)
{
return; /* do nothing with notices at this point */
}
#ifndef USE_THREAD
static GEOSContextHandle_t geos_handle;
static inline GEOSContextHandle_t msGetGeosContextHandle()
{
return geos_handle;
}
#else
#include "mapthread.h"
typedef struct geos_thread_info {
struct geos_thread_info *next;
void* thread_id;
GEOSContextHandle_t geos_handle;
} geos_thread_info_t;
static geos_thread_info_t *geos_list = NULL;
static GEOSContextHandle_t msGetGeosContextHandle()
{
geos_thread_info_t *link;
GEOSContextHandle_t ret_obj;
void* thread_id;
msAcquireLock( TLOCK_GEOS );
thread_id = msGetThreadId();
/* find link for this thread */
for( link = geos_list;
link != NULL && link->thread_id != thread_id
&& link->next != NULL && link->next->thread_id != thread_id;
link = link->next ) {}
/* If the target thread link is already at the head of the list were ok */
if( geos_list != NULL && geos_list->thread_id == thread_id ) {
}
/* We don't have one ... initialize one. */
else if( link == NULL || link->next == NULL ) {
geos_thread_info_t *new_link;
new_link = (geos_thread_info_t *) malloc(sizeof(geos_thread_info_t));
new_link->next = geos_list;
new_link->thread_id = thread_id;
new_link->geos_handle = initGEOS_r(msGEOSNotice, msGEOSError);
geos_list = new_link;
}
/* If the link is not already at the head of the list, promote it */
else if( link != NULL && link->next != NULL ) {
geos_thread_info_t *target = link->next;
link->next = link->next->next;
target->next = geos_list;
geos_list = target;
}
ret_obj = geos_list->geos_handle;
msReleaseLock( TLOCK_GEOS );
return ret_obj;
}
#endif
/*
** Setup/Cleanup wrappers
*/
void msGEOSSetup()
{
#ifndef USE_THREAD
geos_handle = initGEOS_r(msGEOSNotice, msGEOSError);
#endif
}
void msGEOSCleanup()
{
#ifndef USE_THREAD
finishGEOS_r(geos_handle);
geos_handle = NULL;
#else
geos_thread_info_t *link;
msAcquireLock( TLOCK_GEOS );
for( link = geos_list; link != NULL;) {
geos_thread_info_t *cur = link;
link = link->next;
finishGEOS_r(cur->geos_handle);
free(cur);
}
geos_list = NULL;
msReleaseLock( TLOCK_GEOS );
#endif
}
/*
** Translation functions
*/
static GEOSGeom msGEOSShape2Geometry_point(pointObj *point)
{
GEOSCoordSeq coords;
GEOSGeom g;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!point) return NULL;
coords = GEOSCoordSeq_create_r(handle,1, 2); /* todo handle z's */
if(!coords) return NULL;
GEOSCoordSeq_setX_r(handle,coords, 0, point->x);
GEOSCoordSeq_setY_r(handle,coords, 0, point->y);
/* GEOSCoordSeq_setY(coords, 0, point->z); */
g = GEOSGeom_createPoint_r(handle,coords); /* g owns the coordinate in coords */
return g;
}
static GEOSGeom msGEOSShape2Geometry_multipoint(lineObj *multipoint)
{
int i;
GEOSGeom g;
GEOSGeom *points;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!multipoint) return NULL;
points = malloc(multipoint->numpoints*sizeof(GEOSGeom));
if(!points) return NULL;
for(i=0; i<multipoint->numpoints; i++)
points[i] = msGEOSShape2Geometry_point(&(multipoint->point[i]));
g = GEOSGeom_createCollection_r(handle,GEOS_MULTIPOINT, points, multipoint->numpoints);
free(points);
return g;
}
static GEOSGeom msGEOSShape2Geometry_line(lineObj *line)
{
int i;
GEOSGeom g;
GEOSCoordSeq coords;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!line) return NULL;
coords = GEOSCoordSeq_create_r(handle,line->numpoints, 2); /* todo handle z's */
if(!coords) return NULL;
for(i=0; i<line->numpoints; i++) {
GEOSCoordSeq_setX_r(handle,coords, i, line->point[i].x);
GEOSCoordSeq_setY_r(handle,coords, i, line->point[i].y);
/* GEOSCoordSeq_setZ(coords, i, line->point[i].z); */
}
g = GEOSGeom_createLineString_r(handle,coords); /* g owns the coordinates in coords */
return g;
}
static GEOSGeom msGEOSShape2Geometry_multiline(shapeObj *multiline)
{
int i;
GEOSGeom g;
GEOSGeom *lines;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!multiline) return NULL;
lines = malloc(multiline->numlines*sizeof(GEOSGeom));
if(!lines) return NULL;
for(i=0; i<multiline->numlines; i++)
lines[i] = msGEOSShape2Geometry_line(&(multiline->line[i]));
g = GEOSGeom_createCollection_r(handle,GEOS_MULTILINESTRING, lines, multiline->numlines);
free(lines);
return g;
}
static GEOSGeom msGEOSShape2Geometry_simplepolygon(shapeObj *shape, int r, int *outerList)
{
int i, j, k;
GEOSCoordSeq coords;
GEOSGeom g;
GEOSGeom outerRing;
GEOSGeom *innerRings=NULL;
int numInnerRings=0, *innerList;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!shape || !outerList) return NULL;
/* build the outer shell */
coords = GEOSCoordSeq_create_r(handle,shape->line[r].numpoints, 2); /* todo handle z's */
if(!coords) return NULL;
for(i=0; i<shape->line[r].numpoints; i++) {
GEOSCoordSeq_setX_r(handle,coords, i, shape->line[r].point[i].x);
GEOSCoordSeq_setY_r(handle,coords, i, shape->line[r].point[i].y);
/* GEOSCoordSeq_setZ(coords, i, shape->line[r].point[i].z); */
}
outerRing = GEOSGeom_createLinearRing_r(handle,coords); /* outerRing owns the coordinates in coords */
/* build the holes */
innerList = msGetInnerList(shape, r, outerList);
for(j=0; j<shape->numlines; j++)
if(innerList[j] == MS_TRUE) numInnerRings++;
if(numInnerRings > 0) {
k = 0; /* inner ring counter */
innerRings = msSmallMalloc(numInnerRings*sizeof(GEOSGeom));
for(j=0; j<shape->numlines; j++) {
if(innerList[j] == MS_FALSE) continue;
coords = GEOSCoordSeq_create_r(handle,shape->line[j].numpoints, 2); /* todo handle z's */
if(!coords) {
free(innerRings);
free(innerList);
return NULL; /* todo, this will leak memory (shell + allocated holes) */
}
for(i=0; i<shape->line[j].numpoints; i++) {
GEOSCoordSeq_setX_r(handle,coords, i, shape->line[j].point[i].x);
GEOSCoordSeq_setY_r(handle,coords, i, shape->line[j].point[i].y);
/* GEOSCoordSeq_setZ(coords, i, shape->line[j].point[i].z); */
}
innerRings[k] = GEOSGeom_createLinearRing_r(handle,coords); /* innerRings[k] owns the coordinates in coords */
k++;
}
}
g = GEOSGeom_createPolygon_r(handle,outerRing, innerRings, numInnerRings);
free(innerList); /* clean up */
free(innerRings); /* clean up */
return g;
}
static GEOSGeom msGEOSShape2Geometry_polygon(shapeObj *shape)
{
int i, j;
GEOSGeom *polygons;
int *outerList, numOuterRings=0, lastOuterRing=0;
GEOSGeom g;
GEOSContextHandle_t handle = msGetGeosContextHandle();
outerList = msGetOuterList(shape);
for(i=0; i<shape->numlines; i++) {
if(outerList[i] == MS_TRUE) {
numOuterRings++;
lastOuterRing = i; /* save for the simple case */
}
}
if(numOuterRings == 1) {
g = msGEOSShape2Geometry_simplepolygon(shape, lastOuterRing, outerList);
} else { /* a true multipolygon */
polygons = msSmallMalloc(numOuterRings*sizeof(GEOSGeom));
j = 0; /* part counter */
for(i=0; i<shape->numlines; i++) {
if(outerList[i] == MS_FALSE) continue;
polygons[j] = msGEOSShape2Geometry_simplepolygon(shape, i, outerList); /* TODO: account for NULL return values */
j++;
}
g = GEOSGeom_createCollection_r(handle,GEOS_MULTIPOLYGON, polygons, numOuterRings);
free(polygons);
}
free(outerList);
return g;
}
GEOSGeom msGEOSShape2Geometry(shapeObj *shape)
{
if(!shape)
return NULL; /* a NULL shape generates a NULL geometry */
switch(shape->type) {
case MS_SHAPE_POINT:
if(shape->numlines == 0 || shape->line[0].numpoints == 0) /* not enough info for a point */
return NULL;
if(shape->line[0].numpoints == 1) /* simple point */
return msGEOSShape2Geometry_point(&(shape->line[0].point[0]));
else /* multi-point */
return msGEOSShape2Geometry_multipoint(&(shape->line[0]));
break;
case MS_SHAPE_LINE:
if(shape->numlines == 0 || shape->line[0].numpoints < 2) /* not enough info for a line */
return NULL;
if(shape->numlines == 1) /* simple line */
return msGEOSShape2Geometry_line(&(shape->line[0]));
else /* multi-line */
return msGEOSShape2Geometry_multiline(shape);
break;
case MS_SHAPE_POLYGON:
if(shape->numlines == 0 || shape->line[0].numpoints < 4) /* not enough info for a polygon (first=last) */
return NULL;
return msGEOSShape2Geometry_polygon(shape); /* simple and multipolygon cases are addressed */
break;
default:
break;
}
return NULL; /* should not get here */
}
static shapeObj *msGEOSGeometry2Shape_point(GEOSGeom g)
{
GEOSCoordSeq coords;
shapeObj *shape=NULL;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!g) return NULL;
shape = (shapeObj *) malloc(sizeof(shapeObj));
msInitShape(shape);
shape->type = MS_SHAPE_POINT;
shape->line = (lineObj *) malloc(sizeof(lineObj));
shape->numlines = 1;
shape->line[0].point = (pointObj *) malloc(sizeof(pointObj));
shape->line[0].numpoints = 1;
shape->geometry = (GEOSGeom) g;
coords = (GEOSCoordSeq) GEOSGeom_getCoordSeq_r(handle,g);
GEOSCoordSeq_getX_r(handle,coords, 0, &(shape->line[0].point[0].x));
GEOSCoordSeq_getY_r(handle,coords, 0, &(shape->line[0].point[0].y));
/* GEOSCoordSeq_getZ(coords, 0, &(shape->line[0].point[0].z)); */
shape->bounds.minx = shape->bounds.maxx = shape->line[0].point[0].x;
shape->bounds.miny = shape->bounds.maxy = shape->line[0].point[0].y;
return shape;
}
static shapeObj *msGEOSGeometry2Shape_multipoint(GEOSGeom g)
{
int i;
int numPoints;
GEOSCoordSeq coords;
GEOSGeom point;
shapeObj *shape=NULL;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!g) return NULL;
numPoints = GEOSGetNumGeometries_r(handle,g); /* each geometry has 1 point */
shape = (shapeObj *) malloc(sizeof(shapeObj));
msInitShape(shape);
shape->type = MS_SHAPE_POINT;
shape->line = (lineObj *) malloc(sizeof(lineObj));
shape->numlines = 1;
shape->line[0].point = (pointObj *) malloc(sizeof(pointObj)*numPoints);
shape->line[0].numpoints = numPoints;
shape->geometry = (GEOSGeom) g;
for(i=0; i<numPoints; i++) {
point = (GEOSGeom) GEOSGetGeometryN_r(handle,g, i);
coords = (GEOSCoordSeq) GEOSGeom_getCoordSeq_r(handle,point);
GEOSCoordSeq_getX_r(handle,coords, 0, &(shape->line[0].point[i].x));
GEOSCoordSeq_getY_r(handle,coords, 0, &(shape->line[0].point[i].y));
/* GEOSCoordSeq_getZ(coords, 0, &(shape->line[0].point[i].z)); */
}
msComputeBounds(shape);
return shape;
}
static shapeObj *msGEOSGeometry2Shape_line(GEOSGeom g)
{
shapeObj *shape=NULL;
GEOSContextHandle_t handle = msGetGeosContextHandle();
int i;
int numPoints;
GEOSCoordSeq coords;
if(!g) return NULL;
numPoints = GEOSGetNumCoordinates_r(handle,g);
coords = (GEOSCoordSeq) GEOSGeom_getCoordSeq_r(handle,g);
shape = (shapeObj *) malloc(sizeof(shapeObj));
msInitShape(shape);
shape->type = MS_SHAPE_LINE;
shape->line = (lineObj *) malloc(sizeof(lineObj));
shape->numlines = 1;
shape->line[0].point = (pointObj *) malloc(sizeof(pointObj)*numPoints);
shape->line[0].numpoints = numPoints;
shape->geometry = (GEOSGeom) g;
for(i=0; i<numPoints; i++) {
GEOSCoordSeq_getX_r(handle,coords, i, &(shape->line[0].point[i].x));
GEOSCoordSeq_getY_r(handle,coords, i, &(shape->line[0].point[i].y));
/* GEOSCoordSeq_getZ(coords, i, &(shape->line[0].point[i].z)); */
}
msComputeBounds(shape);
return shape;
}
static shapeObj *msGEOSGeometry2Shape_multiline(GEOSGeom g)
{
int i, j;
int numPoints, numLines;
GEOSCoordSeq coords;
GEOSGeom lineString;
shapeObj *shape=NULL;
lineObj line;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!g) return NULL;
numLines = GEOSGetNumGeometries_r(handle,g);
shape = (shapeObj *) malloc(sizeof(shapeObj));
msInitShape(shape);
shape->type = MS_SHAPE_LINE;
shape->geometry = (GEOSGeom) g;
for(j=0; j<numLines; j++) {
lineString = (GEOSGeom) GEOSGetGeometryN_r(handle,g, j);
numPoints = GEOSGetNumCoordinates_r(handle,lineString);
coords = (GEOSCoordSeq) GEOSGeom_getCoordSeq_r(handle,lineString);
line.point = (pointObj *) malloc(sizeof(pointObj)*numPoints);
line.numpoints = numPoints;
for(i=0; i<numPoints; i++) {
GEOSCoordSeq_getX_r(handle,coords, i, &(line.point[i].x));
GEOSCoordSeq_getY_r(handle,coords, i, &(line.point[i].y));
/* GEOSCoordSeq_getZ(coords, i, &(line.point[i].z)); */
}
msAddLineDirectly(shape, &line);
}
msComputeBounds(shape);
return shape;
}
static shapeObj *msGEOSGeometry2Shape_polygon(GEOSGeom g)
{
shapeObj *shape=NULL;
lineObj line;
int numPoints, numRings;
int i, j;
GEOSCoordSeq coords;
GEOSGeom ring;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!g) return NULL;
shape = (shapeObj *) malloc(sizeof(shapeObj));
msInitShape(shape);
shape->type = MS_SHAPE_POLYGON;
shape->geometry = (GEOSGeom) g;
/* exterior ring */
ring = (GEOSGeom) GEOSGetExteriorRing_r(handle,g);
numPoints = GEOSGetNumCoordinates_r(handle,ring);
coords = (GEOSCoordSeq) GEOSGeom_getCoordSeq_r(handle,ring);
line.point = (pointObj *) malloc(sizeof(pointObj)*numPoints);
line.numpoints = numPoints;
for(i=0; i<numPoints; i++) {
GEOSCoordSeq_getX_r(handle,coords, i, &(line.point[i].x));
GEOSCoordSeq_getY_r(handle,coords, i, &(line.point[i].y));
/* GEOSCoordSeq_getZ(coords, i, &(line.point[i].z)); */
}
msAddLineDirectly(shape, &line);
/* interior rings */
numRings = GEOSGetNumInteriorRings_r(handle,g);
for(j=0; j<numRings; j++) {
ring = (GEOSGeom) GEOSGetInteriorRingN_r(handle,g, j);
if(GEOSisRing_r(handle,ring) != 1) continue; /* skip it */
numPoints = GEOSGetNumCoordinates_r(handle,ring);
coords = (GEOSCoordSeq) GEOSGeom_getCoordSeq_r(handle,ring);
line.point = (pointObj *) malloc(sizeof(pointObj)*numPoints);
line.numpoints = numPoints;
for(i=0; i<numPoints; i++) {
GEOSCoordSeq_getX_r(handle,coords, i, &(line.point[i].x));
GEOSCoordSeq_getY_r(handle,coords, i, &(line.point[i].y));
/* GEOSCoordSeq_getZ(coords, i, &(line.point[i].z)); */
}
msAddLineDirectly(shape, &line);
}
msComputeBounds(shape);
return shape;
}
static shapeObj *msGEOSGeometry2Shape_multipolygon(GEOSGeom g)
{
int i, j, k;
shapeObj *shape=NULL;
lineObj line;
int numPoints, numRings, numPolygons;
GEOSCoordSeq coords;
GEOSGeom polygon, ring;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!g) return NULL;
numPolygons = GEOSGetNumGeometries_r(handle,g);
shape = (shapeObj *) malloc(sizeof(shapeObj));
msInitShape(shape);
shape->type = MS_SHAPE_POLYGON;
shape->geometry = (GEOSGeom) g;
for(k=0; k<numPolygons; k++) { /* for each polygon */
polygon = (GEOSGeom) GEOSGetGeometryN_r(handle,g, k);
/* exterior ring */
ring = (GEOSGeom) GEOSGetExteriorRing_r(handle,polygon);
numPoints = GEOSGetNumCoordinates_r(handle,ring);
coords = (GEOSCoordSeq) GEOSGeom_getCoordSeq_r(handle,ring);
line.point = (pointObj *) malloc(sizeof(pointObj)*numPoints);
line.numpoints = numPoints;
for(i=0; i<numPoints; i++) {
GEOSCoordSeq_getX_r(handle,coords, i, &(line.point[i].x));
GEOSCoordSeq_getY_r(handle,coords, i, &(line.point[i].y));
/* GEOSCoordSeq_getZ(coords, i, &(line.point[i].z)); */
}
msAddLineDirectly(shape, &line);
/* interior rings */
numRings = GEOSGetNumInteriorRings_r(handle,polygon);
for(j=0; j<numRings; j++) {
ring = (GEOSGeom) GEOSGetInteriorRingN_r(handle,polygon, j);
if(GEOSisRing_r(handle,ring) != 1) continue; /* skip it */
numPoints = GEOSGetNumCoordinates_r(handle,ring);
coords = (GEOSCoordSeq) GEOSGeom_getCoordSeq_r(handle,ring);
line.point = (pointObj *) malloc(sizeof(pointObj)*numPoints);
line.numpoints = numPoints;
for(i=0; i<numPoints; i++) {
GEOSCoordSeq_getX_r(handle,coords, i, &(line.point[i].x));
GEOSCoordSeq_getY_r(handle,coords, i, &(line.point[i].y));
/* GEOSCoordSeq_getZ(coords, i, &(line.point[i].z)); */
}
msAddLineDirectly(shape, &line);
}
} /* next polygon */
msComputeBounds(shape);
return shape;
}
shapeObj *msGEOSGeometry2Shape(GEOSGeom g)
{
int type;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!g)
return NULL; /* a NULL geometry generates a NULL shape */
type = GEOSGeomTypeId_r(handle,g);
switch(type) {
case GEOS_POINT:
return msGEOSGeometry2Shape_point(g);
break;
case GEOS_MULTIPOINT:
return msGEOSGeometry2Shape_multipoint(g);
break;
case GEOS_LINESTRING:
return msGEOSGeometry2Shape_line(g);
break;
case GEOS_MULTILINESTRING:
return msGEOSGeometry2Shape_multiline(g);
break;
case GEOS_POLYGON:
return msGEOSGeometry2Shape_polygon(g);
break;
case GEOS_MULTIPOLYGON:
return msGEOSGeometry2Shape_multipolygon(g);
break;
case GEOS_GEOMETRYCOLLECTION:
if (!GEOSisEmpty_r(handle,g))
{
int i, j, numGeoms;
shapeObj* shape;
numGeoms = GEOSGetNumGeometries_r(handle,g);
shape = (shapeObj *) malloc(sizeof(shapeObj));
msInitShape(shape);
shape->type = MS_SHAPE_LINE;
shape->geometry = (GEOSGeom) g;
numGeoms = GEOSGetNumGeometries_r(handle,g);
for(i = 0; i < numGeoms; i++) { /* for each geometry */
shapeObj* shape2 = msGEOSGeometry2Shape((GEOSGeom)GEOSGetGeometryN_r(handle,g, i));
if (shape2) {
for (j = 0; j < shape2->numlines; j++)
msAddLineDirectly(shape, &shape2->line[j]);
shape2->numlines = 0;
shape2->geometry = NULL; /* not owned */
msFreeShape(shape2);
}
}
msComputeBounds(shape);
return shape;
}
break;
default:
msSetError(MS_GEOSERR, "Unsupported GEOS geometry type (%d).", "msGEOSGeometry2Shape()", type);
}
return NULL;
}
#endif
/*
** Maintenence functions exposed to MapServer/MapScript.
*/
void msGEOSFreeGeometry(shapeObj *shape)
{
#ifdef USE_GEOS
GEOSGeom g=NULL;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!shape || !shape->geometry)
return;
g = (GEOSGeom) shape->geometry;
GEOSGeom_destroy_r(handle,g);
shape->geometry = NULL;
#else
msSetError(MS_GEOSERR, "GEOS support is not available.", "msGEOSFreeGEOSGeom()");
return;
#endif
}
/*
** WKT input and output functions
*/
shapeObj *msGEOSShapeFromWKT(const char *wkt)
{
#ifdef USE_GEOS
GEOSGeom g;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!wkt)
return NULL;
g = GEOSGeomFromWKT_r(handle,wkt);
if(!g) {
msSetError(MS_GEOSERR, "Error reading WKT geometry \"%s\".", "msGEOSShapeFromWKT()", wkt);
return NULL;
} else {
return msGEOSGeometry2Shape(g);
}
#else
msSetError(MS_GEOSERR, "GEOS support is not available.", "msGEOSShapeFromWKT()");
return NULL;
#endif
}
/* Return should be freed with msGEOSFreeWKT */
char *msGEOSShapeToWKT(shapeObj *shape)
{
#ifdef USE_GEOS
GEOSGeom g;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!shape)
return NULL;
/* if we have a geometry, we should update it*/
msGEOSFreeGeometry(shape);
shape->geometry = (GEOSGeom) msGEOSShape2Geometry(shape);
g = (GEOSGeom) shape->geometry;
if(!g) return NULL;
return GEOSGeomToWKT_r(handle,g);
#else
msSetError(MS_GEOSERR, "GEOS support is not available.", "msGEOSShapeToWKT()");
return NULL;
#endif
}
void msGEOSFreeWKT(char* pszGEOSWKT)
{
#ifdef USE_GEOS
GEOSContextHandle_t handle = msGetGeosContextHandle();
#if GEOS_VERSION_MAJOR > 3 || (GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR >= 2)
GEOSFree_r(handle,pszGEOSWKT);
#endif
#else
msSetError(MS_GEOSERR, "GEOS support is not available.", "msGEOSFreeWKT()");
#endif
}
shapeObj *msGEOSOffsetCurve(shapeObj *p, double offset) {
#if defined USE_GEOS && (GEOS_VERSION_MAJOR > 3 || (GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR >= 3))
GEOSGeom g1, g2;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!p)
return NULL;
if(!p->geometry) /* if no geometry for the shape then build one */
p->geometry = (GEOSGeom) msGEOSShape2Geometry(p);
g1 = (GEOSGeom) p->geometry;
if(!g1) return NULL;
if (GEOSGeomTypeId_r(handle,g1) == GEOS_MULTILINESTRING)
{
GEOSGeom *lines = malloc(p->numlines*sizeof(GEOSGeom));
if (!lines) return NULL;
for(int i=0; i<p->numlines; i++)
{
lines[i] = GEOSOffsetCurve_r(handle, GEOSGetGeometryN_r(handle,g1,i),
offset, 4, GEOSBUF_JOIN_MITRE, fabs(offset*1.5));
}
g2 = GEOSGeom_createCollection_r(handle,GEOS_MULTILINESTRING, lines, p->numlines);
free(lines);
}
else
{
g2 = GEOSOffsetCurve_r(handle,g1, offset, 4, GEOSBUF_JOIN_MITRE, fabs(offset*1.5));
}
return msGEOSGeometry2Shape(g2);
#else
msSetError(MS_GEOSERR, "GEOS Offset Curve support is not available.", "msGEOSingleSidedBuffer()");
return NULL;
#endif
}
/*
** Analytical functions exposed to MapServer/MapScript.
*/
shapeObj *msGEOSBuffer(shapeObj *shape, double width)
{
#ifdef USE_GEOS
GEOSGeom g1, g2;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!shape)
return NULL;
if(!shape->geometry) /* if no geometry for the shape then build one */
shape->geometry = (GEOSGeom) msGEOSShape2Geometry(shape);
g1 = (GEOSGeom) shape->geometry;
if(!g1) return NULL;
g2 = GEOSBuffer_r(handle,g1, width, 30);
return msGEOSGeometry2Shape(g2);
#else
msSetError(MS_GEOSERR, "GEOS support is not available.", "msGEOSBuffer()");
return NULL;
#endif
}
shapeObj *msGEOSSimplify(shapeObj *shape, double tolerance)
{
#ifdef USE_GEOS
GEOSGeom g1, g2;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!shape)
return NULL;
if(!shape->geometry) /* if no geometry for the shape then build one */
shape->geometry = (GEOSGeom) msGEOSShape2Geometry(shape);
g1 = (GEOSGeom) shape->geometry;
if(!g1) return NULL;
g2 = GEOSSimplify_r(handle,g1, tolerance);
return msGEOSGeometry2Shape(g2);
#else
msSetError(MS_GEOSERR, "GEOS Simplify support is not available.", "msGEOSSimplify()");
return NULL;
#endif
}
shapeObj *msGEOSTopologyPreservingSimplify(shapeObj *shape, double tolerance)
{
#ifdef USE_GEOS
GEOSGeom g1, g2;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!shape)
return NULL;
if(!shape->geometry) /* if no geometry for the shape then build one */
shape->geometry = (GEOSGeom) msGEOSShape2Geometry(shape);
g1 = (GEOSGeom) shape->geometry;
if(!g1) return NULL;
g2 = GEOSTopologyPreserveSimplify_r(handle,g1, tolerance);
return msGEOSGeometry2Shape(g2);
#else
msSetError(MS_GEOSERR, "GEOS Simplify support is not available.", "msGEOSTopologyPreservingSimplify()");
return NULL;
#endif
}
shapeObj *msGEOSConvexHull(shapeObj *shape)
{
#ifdef USE_GEOS
GEOSGeom g1, g2;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!shape) return NULL;
if(!shape->geometry) /* if no geometry for the shape then build one */
shape->geometry = (GEOSGeom) msGEOSShape2Geometry(shape);
g1 = (GEOSGeom) shape->geometry;
if(!g1) return NULL;
g2 = GEOSConvexHull_r(handle,g1);
return msGEOSGeometry2Shape(g2);
#else
msSetError(MS_GEOSERR, "GEOS support is not available.", "msGEOSConvexHull()");
return NULL;
#endif
}
shapeObj *msGEOSBoundary(shapeObj *shape)
{
#ifdef USE_GEOS
GEOSGeom g1, g2;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!shape) return NULL;
if(!shape->geometry) /* if no geometry for the shape then build one */
shape->geometry = (GEOSGeom) msGEOSShape2Geometry(shape);
g1 = (GEOSGeom) shape->geometry;
if(!g1) return NULL;
g2 = GEOSBoundary_r(handle,g1);
return msGEOSGeometry2Shape(g2);
#else
msSetError(MS_GEOSERR, "GEOS support is not available.", "msGEOSBoundary()");
return NULL;
#endif
}
pointObj *msGEOSGetCentroid(shapeObj *shape)
{
#ifdef USE_GEOS
GEOSGeom g1, g2;
GEOSCoordSeq coords;
pointObj *point;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!shape) return NULL;
if(!shape->geometry) /* if no geometry for the shape then build one */
shape->geometry = (GEOSGeom) msGEOSShape2Geometry(shape);
g1 = (GEOSGeom) shape->geometry;
if(!g1) return NULL;
g2 = GEOSGetCentroid_r(handle,g1);
if (!g2) return NULL;
point = (pointObj *) malloc(sizeof(pointObj));
coords = (GEOSCoordSeq) GEOSGeom_getCoordSeq_r(handle,g2);
GEOSCoordSeq_getX_r(handle,coords, 0, &(point->x));
GEOSCoordSeq_getY_r(handle,coords, 0, &(point->y));
/* GEOSCoordSeq_getZ(coords, 0, &(point->z)); */
GEOSGeom_destroy_r(handle, g2);
return point;
#else
msSetError(MS_GEOSERR, "GEOS support is not available.", "msGEOSGetCentroid()");
return NULL;
#endif
}
shapeObj *msGEOSUnion(shapeObj *shape1, shapeObj *shape2)
{
#ifdef USE_GEOS
GEOSGeom g1, g2, g3;
GEOSContextHandle_t handle = msGetGeosContextHandle();
if(!shape1 || !shape2)
return NULL;
if(!shape1->geometry) /* if no geometry for the shape then build one */
shape1->geometry = (GEOSGeom) msGEOSShape2Geometry(shape1);
g1 = (GEOSGeom) shape1->geometry;
if(!g1) return NULL;
if(!shape2->geometry) /* if no geometry for the shape then build one */
shape2->geometry = (GEOSGeom) msGEOSShape2Geometry(shape2);
g2 = (GEOSGeom) shape2->geometry;
if(!g2) return NULL;
g3 = GEOSUnion_r(handle,g1, g2);
return msGEOSGeometry2Shape(g3);