/
lwgeom_geos.c
1850 lines (1508 loc) · 43.3 KB
/
lwgeom_geos.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
/**********************************************************************
*
* PostGIS - Spatial Types for PostgreSQL
* http://postgis.net
*
* PostGIS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* PostGIS 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
*
**********************************************************************
*
* Copyright 2011-2014 Sandro Santilli <strk@kbt.io>
* Copyright 2015-2018 Daniel Baston <dbaston@gmail.com>
* Copyright 2017-2018 Darafei Praliaskouski <me@komzpa.net>
*
**********************************************************************/
#include "lwgeom_geos.h"
#include "liblwgeom.h"
#include "liblwgeom_internal.h"
#include "lwgeom_log.h"
#include "lwrandom.h"
#include <stdarg.h>
#include <stdlib.h>
LWTIN* lwtin_from_geos(const GEOSGeometry* geom, uint8_t want3d);
#define AUTOFIX LW_TRUE
#define LWGEOM_GEOS_ERRMSG_MAXSIZE 256
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE];
extern void
lwgeom_geos_error(const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
/* Call the supplied function */
if (LWGEOM_GEOS_ERRMSG_MAXSIZE - 1 < vsnprintf(lwgeom_geos_errmsg, LWGEOM_GEOS_ERRMSG_MAXSIZE - 1, fmt, ap))
lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE - 1] = '\0';
va_end(ap);
}
/* Destroy any non-null GEOSGeometry* pointers passed as arguments */
#define GEOS_FREE(...) \
do { \
geos_destroy((sizeof((void*[]){__VA_ARGS__})/sizeof(void*)), __VA_ARGS__); \
} while (0)
/* Pass the latest GEOS error to lwerror, then return NULL */
#define GEOS_FAIL() \
do { \
lwerror("%s: GEOS Error: %s", __func__, lwgeom_geos_errmsg); \
return NULL; \
} while (0)
#define GEOS_FREE_AND_FAIL(...) \
do { \
GEOS_FREE(__VA_ARGS__); \
GEOS_FAIL(); \
} while (0)
/* Return the consistent SRID of all inputs, or call lwerror
* in case of SRID mismatch. */
#define RESULT_SRID(...) \
(get_result_srid((sizeof((const void*[]){__VA_ARGS__})/sizeof(void*)), __func__, __VA_ARGS__))
/* Free any non-null GEOSGeometry* pointers passed as arguments *
* Called by GEOS_FREE, which populates 'count' */
static void geos_destroy(size_t count, ...) {
va_list ap;
va_start(ap, count);
while (count--)
{
GEOSGeometry* g = va_arg(ap, GEOSGeometry*);
if (g)
{
GEOSGeom_destroy(g);
}
}
}
/*
** GEOS <==> PostGIS conversion functions
**
** Default conversion creates a GEOS point array, then iterates through the
** PostGIS points, setting each value in the GEOS array one at a time.
**
*/
/* Return a POINTARRAY from a GEOSCoordSeq */
POINTARRAY*
ptarray_from_GEOSCoordSeq(const GEOSCoordSequence* cs, uint8_t want3d)
{
uint32_t dims = 2;
uint32_t size = 0, i;
POINTARRAY* pa;
POINT4D point = { 0.0, 0.0, 0.0, 0.0 };
LWDEBUG(2, "ptarray_fromGEOSCoordSeq called");
if (!GEOSCoordSeq_getSize(cs, &size)) lwerror("Exception thrown");
LWDEBUGF(4, " GEOSCoordSeq size: %d", size);
if (want3d)
{
if (!GEOSCoordSeq_getDimensions(cs, &dims)) lwerror("Exception thrown");
LWDEBUGF(4, " GEOSCoordSeq dimensions: %d", dims);
/* forget higher dimensions (if any) */
if (dims > 3) dims = 3;
}
LWDEBUGF(4, " output dimensions: %d", dims);
pa = ptarray_construct((dims == 3), 0, size);
for (i = 0; i < size; i++)
{
GEOSCoordSeq_getX(cs, i, &(point.x));
GEOSCoordSeq_getY(cs, i, &(point.y));
if (dims >= 3) GEOSCoordSeq_getZ(cs, i, &(point.z));
ptarray_set_point4d(pa, i, &point);
}
return pa;
}
/* Return an LWGEOM from a Geometry */
LWGEOM*
GEOS2LWGEOM(const GEOSGeometry* geom, uint8_t want3d)
{
int type = GEOSGeomTypeId(geom);
int SRID = GEOSGetSRID(geom);
/* GEOS's 0 is equivalent to our unknown as for SRID values */
if (SRID == 0) SRID = SRID_UNKNOWN;
if (want3d && !GEOSHasZ(geom))
{
LWDEBUG(3, "Geometry has no Z, won't provide one");
want3d = 0;
}
switch (type)
{
const GEOSCoordSequence* cs;
POINTARRAY *pa, **ppaa;
const GEOSGeometry* g;
LWGEOM** geoms;
uint32_t i, ngeoms;
case GEOS_POINT:
LWDEBUG(4, "lwgeom_from_geometry: it's a Point");
cs = GEOSGeom_getCoordSeq(geom);
if (GEOSisEmpty(geom)) return (LWGEOM*)lwpoint_construct_empty(SRID, want3d, 0);
pa = ptarray_from_GEOSCoordSeq(cs, want3d);
return (LWGEOM*)lwpoint_construct(SRID, NULL, pa);
case GEOS_LINESTRING:
case GEOS_LINEARRING:
LWDEBUG(4, "lwgeom_from_geometry: it's a LineString or LinearRing");
if (GEOSisEmpty(geom)) return (LWGEOM*)lwline_construct_empty(SRID, want3d, 0);
cs = GEOSGeom_getCoordSeq(geom);
pa = ptarray_from_GEOSCoordSeq(cs, want3d);
return (LWGEOM*)lwline_construct(SRID, NULL, pa);
case GEOS_POLYGON:
LWDEBUG(4, "lwgeom_from_geometry: it's a Polygon");
if (GEOSisEmpty(geom)) return (LWGEOM*)lwpoly_construct_empty(SRID, want3d, 0);
ngeoms = GEOSGetNumInteriorRings(geom);
ppaa = lwalloc(sizeof(POINTARRAY*) * (ngeoms + 1));
g = GEOSGetExteriorRing(geom);
cs = GEOSGeom_getCoordSeq(g);
ppaa[0] = ptarray_from_GEOSCoordSeq(cs, want3d);
for (i = 0; i < ngeoms; i++)
{
g = GEOSGetInteriorRingN(geom, i);
cs = GEOSGeom_getCoordSeq(g);
ppaa[i + 1] = ptarray_from_GEOSCoordSeq(cs, want3d);
}
return (LWGEOM*)lwpoly_construct(SRID, NULL, ngeoms + 1, ppaa);
case GEOS_MULTIPOINT:
case GEOS_MULTILINESTRING:
case GEOS_MULTIPOLYGON:
case GEOS_GEOMETRYCOLLECTION:
LWDEBUG(4, "lwgeom_from_geometry: it's a Collection or Multi");
ngeoms = GEOSGetNumGeometries(geom);
geoms = NULL;
if (ngeoms)
{
geoms = lwalloc(sizeof(LWGEOM*) * ngeoms);
for (i = 0; i < ngeoms; i++)
{
g = GEOSGetGeometryN(geom, i);
geoms[i] = GEOS2LWGEOM(g, want3d);
}
}
return (LWGEOM*)lwcollection_construct(type, SRID, NULL, ngeoms, geoms);
default:
lwerror("GEOS2LWGEOM: unknown geometry type: %d", type);
return NULL;
}
}
GEOSCoordSeq ptarray_to_GEOSCoordSeq(const POINTARRAY*, uint8_t fix_ring);
GEOSCoordSeq
ptarray_to_GEOSCoordSeq(const POINTARRAY* pa, uint8_t fix_ring)
{
uint32_t dims = 2;
uint32_t i;
int append_points = 0;
const POINT3DZ* p3d;
const POINT2D* p2d;
GEOSCoordSeq sq;
if (FLAGS_GET_Z(pa->flags)) dims = 3;
if (fix_ring)
{
if (pa->npoints < 1)
{
lwerror("ptarray_to_GEOSCoordSeq called with fix_ring and 0 vertices in ring, cannot fix");
return NULL;
}
else
{
if (pa->npoints < 4) append_points = 4 - pa->npoints;
if (!ptarray_is_closed_2d(pa) && append_points == 0) append_points = 1;
}
}
if (!(sq = GEOSCoordSeq_create(pa->npoints + append_points, dims)))
{
lwerror("Error creating GEOS Coordinate Sequence");
return NULL;
}
for (i = 0; i < pa->npoints; i++)
{
if (dims == 3)
{
p3d = getPoint3dz_cp(pa, i);
p2d = (const POINT2D*)p3d;
LWDEBUGF(4, "Point: %g,%g,%g", p3d->x, p3d->y, p3d->z);
}
else
{
p2d = getPoint2d_cp(pa, i);
LWDEBUGF(4, "Point: %g,%g", p2d->x, p2d->y);
}
GEOSCoordSeq_setX(sq, i, p2d->x);
GEOSCoordSeq_setY(sq, i, p2d->y);
if (dims == 3) GEOSCoordSeq_setZ(sq, i, p3d->z);
}
if (append_points)
{
if (dims == 3)
{
p3d = getPoint3dz_cp(pa, 0);
p2d = (const POINT2D*)p3d;
}
else
p2d = getPoint2d_cp(pa, 0);
for (i = pa->npoints; i < pa->npoints + append_points; i++)
{
GEOSCoordSeq_setX(sq, i, p2d->x);
GEOSCoordSeq_setY(sq, i, p2d->y);
if (dims == 3) GEOSCoordSeq_setZ(sq, i, p3d->z);
}
}
return sq;
}
static inline GEOSGeometry*
ptarray_to_GEOSLinearRing(const POINTARRAY* pa, uint8_t autofix)
{
GEOSCoordSeq sq;
GEOSGeom g;
sq = ptarray_to_GEOSCoordSeq(pa, autofix);
g = GEOSGeom_createLinearRing(sq);
return g;
}
GEOSGeometry*
GBOX2GEOS(const GBOX* box)
{
GEOSGeometry* envelope;
GEOSGeometry* ring;
GEOSCoordSequence* seq = GEOSCoordSeq_create(5, 2);
if (!seq) return NULL;
GEOSCoordSeq_setX(seq, 0, box->xmin);
GEOSCoordSeq_setY(seq, 0, box->ymin);
GEOSCoordSeq_setX(seq, 1, box->xmax);
GEOSCoordSeq_setY(seq, 1, box->ymin);
GEOSCoordSeq_setX(seq, 2, box->xmax);
GEOSCoordSeq_setY(seq, 2, box->ymax);
GEOSCoordSeq_setX(seq, 3, box->xmin);
GEOSCoordSeq_setY(seq, 3, box->ymax);
GEOSCoordSeq_setX(seq, 4, box->xmin);
GEOSCoordSeq_setY(seq, 4, box->ymin);
ring = GEOSGeom_createLinearRing(seq);
if (!ring)
{
GEOSCoordSeq_destroy(seq);
return NULL;
}
envelope = GEOSGeom_createPolygon(ring, NULL, 0);
if (!envelope)
{
GEOSGeom_destroy(ring);
return NULL;
}
return envelope;
}
GEOSGeometry*
LWGEOM2GEOS(const LWGEOM* lwgeom, uint8_t autofix)
{
GEOSCoordSeq sq;
GEOSGeom g, shell;
GEOSGeom* geoms = NULL;
uint32_t ngeoms, i, j;
int geostype;
#if LWDEBUG_LEVEL >= 4
char* wkt;
#endif
if (autofix)
{
/* cross fingers and try without autofix, maybe it'll work? */
g = LWGEOM2GEOS(lwgeom, LW_FALSE);
if (g) return g;
}
LWDEBUGF(4, "LWGEOM2GEOS got a %s", lwtype_name(lwgeom->type));
if (lwgeom_has_arc(lwgeom))
{
LWGEOM* lwgeom_stroked = lwgeom_stroke(lwgeom, 32);
GEOSGeometry* g = LWGEOM2GEOS(lwgeom_stroked, autofix);
lwgeom_free(lwgeom_stroked);
return g;
}
LWPOINT* lwp = NULL;
LWPOLY* lwpoly = NULL;
LWLINE* lwl = NULL;
LWCOLLECTION* lwc = NULL;
switch (lwgeom->type)
{
case POINTTYPE:
lwp = (LWPOINT*)lwgeom;
if (lwgeom_is_empty(lwgeom))
g = GEOSGeom_createEmptyPolygon();
else
{
sq = ptarray_to_GEOSCoordSeq(lwp->point, 0);
g = GEOSGeom_createPoint(sq);
}
if (!g) return NULL;
break;
case LINETYPE:
lwl = (LWLINE*)lwgeom;
/* TODO: if (autofix) */
if (lwl->points->npoints == 1)
{
/* Duplicate point, to make geos-friendly */
lwl->points = ptarray_addPoint(lwl->points,
getPoint_internal(lwl->points, 0),
FLAGS_NDIMS(lwl->points->flags),
lwl->points->npoints);
}
sq = ptarray_to_GEOSCoordSeq(lwl->points, 0);
g = GEOSGeom_createLineString(sq);
if (!g) return NULL;
break;
case POLYGONTYPE:
lwpoly = (LWPOLY*)lwgeom;
if (lwgeom_is_empty(lwgeom))
g = GEOSGeom_createEmptyPolygon();
else
{
shell = ptarray_to_GEOSLinearRing(lwpoly->rings[0], autofix);
if (!shell) return NULL;
ngeoms = lwpoly->nrings - 1;
if (ngeoms > 0) geoms = lwalloc(sizeof(GEOSGeom) * ngeoms);
for (i = 1; i < lwpoly->nrings; i++)
{
geoms[i - 1] = ptarray_to_GEOSLinearRing(lwpoly->rings[i], autofix);
if (!geoms[i - 1])
{
uint32_t k;
for (k = 0; k < i - 1; k++)
GEOSGeom_destroy(geoms[k]);
lwfree(geoms);
GEOSGeom_destroy(shell);
return NULL;
}
}
g = GEOSGeom_createPolygon(shell, geoms, ngeoms);
if (geoms) lwfree(geoms);
}
if (!g) return NULL;
break;
case MULTIPOINTTYPE:
case MULTILINETYPE:
case MULTIPOLYGONTYPE:
case COLLECTIONTYPE:
if (lwgeom->type == MULTIPOINTTYPE)
geostype = GEOS_MULTIPOINT;
else if (lwgeom->type == MULTILINETYPE)
geostype = GEOS_MULTILINESTRING;
else if (lwgeom->type == MULTIPOLYGONTYPE)
geostype = GEOS_MULTIPOLYGON;
else
geostype = GEOS_GEOMETRYCOLLECTION;
lwc = (LWCOLLECTION*)lwgeom;
ngeoms = lwc->ngeoms;
if (ngeoms > 0) geoms = lwalloc(sizeof(GEOSGeom) * ngeoms);
j = 0;
for (i = 0; i < ngeoms; ++i)
{
GEOSGeometry* g;
if (lwgeom_is_empty(lwc->geoms[i])) continue;
g = LWGEOM2GEOS(lwc->geoms[i], 0);
if (!g)
{
uint32_t k;
for (k = 0; k < j; k++)
GEOSGeom_destroy(geoms[k]);
lwfree(geoms);
return NULL;
}
geoms[j++] = g;
}
g = GEOSGeom_createCollection(geostype, geoms, j);
if (ngeoms > 0) lwfree(geoms);
if (!g) return NULL;
break;
default:
lwerror("Unknown geometry type: %d - %s", lwgeom->type, lwtype_name(lwgeom->type));
return NULL;
}
GEOSSetSRID(g, lwgeom->srid);
#if LWDEBUG_LEVEL >= 4
wkt = GEOSGeomToWKT(g);
LWDEBUGF(4, "LWGEOM2GEOS: GEOSGeom: %s", wkt);
free(wkt);
#endif
return g;
}
GEOSGeometry*
make_geos_point(double x, double y)
{
GEOSCoordSequence* seq = GEOSCoordSeq_create(1, 2);
GEOSGeometry* geom = NULL;
if (!seq) return NULL;
GEOSCoordSeq_setX(seq, 0, x);
GEOSCoordSeq_setY(seq, 0, y);
geom = GEOSGeom_createPoint(seq);
if (!geom) GEOSCoordSeq_destroy(seq);
return geom;
}
GEOSGeometry*
make_geos_segment(double x1, double y1, double x2, double y2)
{
GEOSCoordSequence* seq = GEOSCoordSeq_create(2, 2);
GEOSGeometry* geom = NULL;
if (!seq) return NULL;
GEOSCoordSeq_setX(seq, 0, x1);
GEOSCoordSeq_setY(seq, 0, y1);
GEOSCoordSeq_setX(seq, 1, x2);
GEOSCoordSeq_setY(seq, 1, y2);
geom = GEOSGeom_createLineString(seq);
if (!geom) GEOSCoordSeq_destroy(seq);
return geom;
}
const char*
lwgeom_geos_version()
{
const char* ver = GEOSversion();
return ver;
}
/* Return the consistent SRID of all input.
* Intended to be called from RESULT_SRID macro */
static int32_t
get_result_srid(size_t count, const char* funcname, ...)
{
va_list ap;
va_start(ap, funcname);
int32_t srid = SRID_INVALID;
size_t i;
for(i = 0; i < count; i++)
{
LWGEOM* g = va_arg(ap, LWGEOM*);
if (!g)
{
lwerror("%s: Geometry is null", funcname);
return SRID_INVALID;
}
if (i == 0)
{
srid = g->srid;
}
else
{
if (g->srid != srid)
{
lwerror("%s: Operation on mixed SRID geometries (%d != %d)", funcname, srid, g->srid);
return SRID_INVALID;
}
}
}
return srid;
}
LWGEOM*
lwgeom_normalize(const LWGEOM* geom)
{
LWGEOM* result;
int32_t srid = RESULT_SRID(geom);
uint8_t is3d = FLAGS_GET_Z(geom->flags);
GEOSGeometry* g;
if (srid == SRID_INVALID) return NULL;
initGEOS(lwnotice, lwgeom_geos_error);
if (!(g = LWGEOM2GEOS(geom, AUTOFIX))) GEOS_FAIL();
if (GEOSNormalize(g) == -1) GEOS_FREE_AND_FAIL(g);
GEOSSetSRID(g, srid);
if (!(result = GEOS2LWGEOM(g, is3d))) GEOS_FREE_AND_FAIL(g);
GEOSGeom_destroy(g);
return result;
}
LWGEOM*
lwgeom_intersection(const LWGEOM* geom1, const LWGEOM* geom2)
{
LWGEOM* result;
int32_t srid = RESULT_SRID(geom1, geom2);
uint8_t is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags));
GEOSGeometry* g1;
GEOSGeometry* g2;
GEOSGeometry* g3;
if (srid == SRID_INVALID) return NULL;
/* A.Intersection(Empty) == Empty */
if (lwgeom_is_empty(geom2)) return lwgeom_clone_deep(geom2); /* match empty type? */
/* Empty.Intersection(A) == Empty */
if (lwgeom_is_empty(geom1)) return lwgeom_clone_deep(geom1); /* match empty type? */
initGEOS(lwnotice, lwgeom_geos_error);
if (!(g1 = LWGEOM2GEOS(geom1, AUTOFIX))) GEOS_FAIL();
if (!(g2 = LWGEOM2GEOS(geom2, AUTOFIX))) GEOS_FREE_AND_FAIL(g1);
g3 = GEOSIntersection(g1, g2);
if (!g3) GEOS_FREE_AND_FAIL(g1);
GEOSSetSRID(g3, srid);
if (!(result = GEOS2LWGEOM(g3, is3d))) GEOS_FREE_AND_FAIL(g1, g2, g3);
GEOS_FREE(g1, g2, g3);
return result;
}
LWGEOM*
lwgeom_linemerge(const LWGEOM* geom)
{
LWGEOM* result;
int32_t srid = RESULT_SRID(geom);
uint8_t is3d = FLAGS_GET_Z(geom->flags);
GEOSGeometry* g1;
GEOSGeometry* g3;
if (srid == SRID_INVALID) return NULL;
/* Empty.Linemerge() == Empty */
if (lwgeom_is_empty(geom)) return lwgeom_clone_deep(geom); /* match empty type to linestring? */
initGEOS(lwnotice, lwgeom_geos_error);
if (!(g1 = LWGEOM2GEOS(geom, AUTOFIX))) GEOS_FAIL();
g3 = GEOSLineMerge(g1);
if (!g3) GEOS_FREE_AND_FAIL(g1);
GEOSSetSRID(g3, srid);
if (!(result = GEOS2LWGEOM(g3, is3d)))
GEOS_FREE_AND_FAIL(g1, g3);
GEOS_FREE(g1, g3);
return result;
}
LWGEOM*
lwgeom_unaryunion(const LWGEOM* geom)
{
LWGEOM* result;
int32_t srid = RESULT_SRID(geom);
uint8_t is3d = FLAGS_GET_Z(geom->flags);
GEOSGeometry* g1;
GEOSGeometry* g3;
if (srid == SRID_INVALID) return NULL;
/* Empty.UnaryUnion() == Empty */
if (lwgeom_is_empty(geom)) return lwgeom_clone_deep(geom);
initGEOS(lwnotice, lwgeom_geos_error);
if (!(g1 = LWGEOM2GEOS(geom, AUTOFIX))) GEOS_FAIL();
g3 = GEOSUnaryUnion(g1);
if (!g3) GEOS_FREE_AND_FAIL(g1);
GEOSSetSRID(g3, srid);
if (!(result = GEOS2LWGEOM(g3, is3d)))
GEOS_FREE_AND_FAIL(g1, g3);
GEOS_FREE(g1, g3);
return result;
}
LWGEOM*
lwgeom_difference(const LWGEOM* geom1, const LWGEOM* geom2)
{
LWGEOM* result;
int32_t srid = RESULT_SRID(geom1, geom2);
uint8_t is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags));
GEOSGeometry *g1, *g2, *g3;
if (srid == SRID_INVALID) return NULL;
/* A.Intersection(Empty) == Empty */
if (lwgeom_is_empty(geom2)) return lwgeom_clone_deep(geom1); /* match empty type? */
/* Empty.Intersection(A) == Empty */
if (lwgeom_is_empty(geom1)) return lwgeom_clone_deep(geom1); /* match empty type? */
initGEOS(lwnotice, lwgeom_geos_error);
if (!(g1 = LWGEOM2GEOS(geom1, AUTOFIX))) GEOS_FAIL();
if (!(g2 = LWGEOM2GEOS(geom2, AUTOFIX))) GEOS_FREE_AND_FAIL(g1);
g3 = GEOSDifference(g1, g2);
if (!g3) GEOS_FREE_AND_FAIL(g1, g2);
GEOSSetSRID(g3, srid);
if (!(result = GEOS2LWGEOM(g3, is3d)))
GEOS_FREE_AND_FAIL(g1, g2, g3);
GEOS_FREE(g1, g2, g3);
return result;
}
LWGEOM*
lwgeom_symdifference(const LWGEOM* geom1, const LWGEOM* geom2)
{
LWGEOM* result;
int32_t srid = RESULT_SRID(geom1, geom2);
uint8_t is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags));
GEOSGeometry *g1, *g2, *g3;
if (srid == SRID_INVALID) return NULL;
/* A.SymDifference(Empty) == A */
if (lwgeom_is_empty(geom2)) return lwgeom_clone_deep(geom1);
/* Empty.DymDifference(B) == B */
if (lwgeom_is_empty(geom1)) return lwgeom_clone_deep(geom2);
initGEOS(lwnotice, lwgeom_geos_error);
if (!(g1 = LWGEOM2GEOS(geom1, AUTOFIX))) GEOS_FAIL();
if (!(g2 = LWGEOM2GEOS(geom2, AUTOFIX))) GEOS_FREE_AND_FAIL(g1);
g3 = GEOSSymDifference(g1, g2);
if (!g3) GEOS_FREE_AND_FAIL(g1, g2);
GEOSSetSRID(g3, srid);
if (!(result = GEOS2LWGEOM(g3, is3d)))
GEOS_FREE_AND_FAIL(g1, g2, g3);
GEOS_FREE(g1, g2, g3);
return result;
}
LWGEOM*
lwgeom_centroid(const LWGEOM* geom)
{
LWGEOM* result;
int32_t srid = RESULT_SRID(geom);
uint8_t is3d = FLAGS_GET_Z(geom->flags);
GEOSGeometry *g1, *g3;
if (srid == SRID_INVALID) return NULL;
if (lwgeom_is_empty(geom))
{
LWPOINT* lwp = lwpoint_construct_empty(srid, is3d, lwgeom_has_m(geom));
return lwpoint_as_lwgeom(lwp);
}
initGEOS(lwnotice, lwgeom_geos_error);
if (!(g1 = LWGEOM2GEOS(geom, AUTOFIX))) GEOS_FAIL();
g3 = GEOSGetCentroid(g1);
if (!g3) GEOS_FREE_AND_FAIL(g1);
GEOSSetSRID(g3, srid);
if (!(result = GEOS2LWGEOM(g3, is3d)))
GEOS_FREE_AND_FAIL(g1);
GEOS_FREE(g1, g3);
return result;
}
LWGEOM *
lwgeom_pointonsurface(const LWGEOM *geom)
{
LWGEOM *result;
int32_t srid = RESULT_SRID(geom);
uint8_t is3d = FLAGS_GET_Z(geom->flags);
GEOSGeometry *g1, *g3;
if (srid == SRID_INVALID) return NULL;
if (lwgeom_is_empty(geom))
{
LWPOINT *lwp = lwpoint_construct_empty(srid, is3d, lwgeom_has_m(geom));
return lwpoint_as_lwgeom(lwp);
}
initGEOS(lwnotice, lwgeom_geos_error);
if (!(g1 = LWGEOM2GEOS(geom, AUTOFIX))) GEOS_FAIL();
g3 = GEOSPointOnSurface(g1);
if (!g3) GEOS_FREE_AND_FAIL(g1);
GEOSSetSRID(g3, srid);
if (!(result = GEOS2LWGEOM(g3, is3d)))
GEOS_FREE_AND_FAIL(g1, g3);
GEOS_FREE(g1, g3);
return result;
}
LWGEOM*
lwgeom_union(const LWGEOM* geom1, const LWGEOM* geom2)
{
LWGEOM* result;
int32_t srid = RESULT_SRID(geom1, geom2);
uint8_t is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags));
GEOSGeometry *g1, *g2, *g3;
if (srid == SRID_INVALID) return NULL;
/* A.Union(empty) == A */
if (lwgeom_is_empty(geom1)) return lwgeom_clone_deep(geom2);
/* B.Union(empty) == B */
if (lwgeom_is_empty(geom2)) return lwgeom_clone_deep(geom1);
initGEOS(lwnotice, lwgeom_geos_error);
if (!(g1 = LWGEOM2GEOS(geom1, AUTOFIX))) GEOS_FAIL();
if (!(g2 = LWGEOM2GEOS(geom2, AUTOFIX))) GEOS_FREE_AND_FAIL(g1);
g3 = GEOSUnion(g1, g2);
if (!g3) GEOS_FREE_AND_FAIL(g1, g2);
GEOSSetSRID(g3, srid);
if (!(result = GEOS2LWGEOM(g3, is3d)))
GEOS_FREE_AND_FAIL(g1, g2, g3);
GEOS_FREE(g1, g2, g3);
return result;
}
LWGEOM *
lwgeom_clip_by_rect(const LWGEOM *geom1, double x1, double y1, double x2, double y2)
{
LWGEOM *result;
GEOSGeometry *g1, *g3;
int is3d;
/* A.Intersection(Empty) == Empty */
if ( lwgeom_is_empty(geom1) )
return lwgeom_clone_deep(geom1);
is3d = FLAGS_GET_Z(geom1->flags);
initGEOS(lwnotice, lwgeom_geos_error);
if (!(g1 = LWGEOM2GEOS(geom1, AUTOFIX)))
GEOS_FAIL();
if (!(g3 = GEOSClipByRect(g1, x1, y1, x2, y2)))
GEOS_FREE_AND_FAIL(g1);
GEOS_FREE(g1);
result = GEOS2LWGEOM(g3, is3d);
GEOS_FREE(g3);
if (!result)
GEOS_FAIL();
result->srid = geom1->srid;
return result;
}
/* ------------ BuildArea stuff ---------------------------------------------------------------------{ */
typedef struct Face_t
{
const GEOSGeometry* geom;
GEOSGeometry* env;
double envarea;
struct Face_t* parent; /* if this face is an hole of another one, or NULL */
} Face;
static Face* newFace(const GEOSGeometry* g);
static void delFace(Face* f);
static unsigned int countParens(const Face* f);
static void findFaceHoles(Face** faces, int nfaces);
static Face*
newFace(const GEOSGeometry* g)
{
Face* f = lwalloc(sizeof(Face));
f->geom = g;
f->env = GEOSEnvelope(f->geom);
GEOSArea(f->env, &f->envarea);
f->parent = NULL;
return f;
}
static unsigned int
countParens(const Face* f)
{
unsigned int pcount = 0;
while (f->parent)
{
++pcount;
f = f->parent;
}
return pcount;
}
/* Destroy the face and release memory associated with it */
static void
delFace(Face* f)
{
GEOSGeom_destroy(f->env);
lwfree(f);
}
static int
compare_by_envarea(const void* g1, const void* g2)
{
Face* f1 = *(Face**)g1;
Face* f2 = *(Face**)g2;
double n1 = f1->envarea;
double n2 = f2->envarea;
if (n1 < n2) return 1;
if (n1 > n2) return -1;
return 0;
}
/* Find holes of each face */
static void
findFaceHoles(Face** faces, int nfaces)
{
int i, j, h;
/* We sort by envelope area so that we know holes are only after their shells */
qsort(faces, nfaces, sizeof(Face*), compare_by_envarea);
for (i = 0; i < nfaces; ++i)
{
Face* f = faces[i];
int nholes = GEOSGetNumInteriorRings(f->geom);
LWDEBUGF(2, "Scanning face %d with env area %g and %d holes", i, f->envarea, nholes);
for (h = 0; h < nholes; ++h)
{
const GEOSGeometry* hole = GEOSGetInteriorRingN(f->geom, h);
LWDEBUGF(2,
"Looking for hole %d/%d of face %d among %d other faces",
h + 1,
nholes,
i,
nfaces - i - 1);
for (j = i + 1; j < nfaces; ++j)
{
const GEOSGeometry* f2er;
Face* f2 = faces[j];
if (f2->parent) continue; /* hole already assigned */
f2er = GEOSGetExteriorRing(f2->geom);
/* TODO: can be optimized as the ring would have the same vertices, possibly in
* different order. Maybe comparing number of points could already be useful. */
if (GEOSEquals(f2er, hole))
{
LWDEBUGF(2, "Hole %d/%d of face %d is face %d", h + 1, nholes, i, j);
f2->parent = f;
break;
}
}
}
}
}
static GEOSGeometry*
collectFacesWithEvenAncestors(Face** faces, int nfaces)
{
GEOSGeometry** geoms = lwalloc(sizeof(GEOSGeometry*) * nfaces);
GEOSGeometry* ret;
unsigned int ngeoms = 0;
int i;
for (i = 0; i < nfaces; ++i)
{
Face* f = faces[i];