forked from shapely/shapely
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geos.c
1249 lines (1116 loc) · 35.4 KB
/
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
#define PY_SSIZE_T_CLEAN
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "geos.h"
#include <Python.h>
#include <numpy/ndarraytypes.h>
#include <numpy/npy_math.h>
#include <structmember.h>
/* This initializes a globally accessible GEOS Context, only to be used when holding the
* GIL */
void* geos_context[1] = {NULL};
/* This initializes a globally accessible GEOSException object */
PyObject* geos_exception[1] = {NULL};
int init_geos(PyObject* m) {
PyObject* base_class = PyErr_NewException("shapely.errors.ShapelyError", NULL, NULL);
PyModule_AddObject(m, "ShapelyError", base_class);
geos_exception[0] =
PyErr_NewException("shapely.errors.GEOSException", base_class, NULL);
PyModule_AddObject(m, "GEOSException", geos_exception[0]);
void* context_handle = GEOS_init_r();
// TODO: the error handling is not yet set up for the global context (it is right now
// only used where error handling is not used)
// GEOSContext_setErrorMessageHandler_r(context_handle, geos_error_handler, last_error);
geos_context[0] = context_handle;
return 0;
}
void destroy_geom_arr(void* context, GEOSGeometry** array, int length) {
int i;
for (i = 0; i < length; i++) {
if (array[i] != NULL) {
GEOSGeom_destroy_r(context, array[i]);
}
}
}
/* These functions are used to workaround two GEOS issues (in WKB writer for
* GEOS < 3.9, in WKT writer for GEOS < 3.9 and in GeoJSON writer for GEOS 3.10.0):
* - POINT EMPTY was not handled correctly (we do it ourselves)
* - MULTIPOINT (EMPTY) resulted in segfault (we check for it and raise)
*/
/* Returns 1 if a multipoint has an empty point, 0 otherwise, 2 on error.
*/
char multipoint_has_point_empty(GEOSContextHandle_t ctx, GEOSGeometry* geom) {
int n, i;
char is_empty;
const GEOSGeometry* sub_geom;
n = GEOSGetNumGeometries_r(ctx, geom);
if (n == -1) {
return 2;
}
for (i = 0; i < n; i++) {
sub_geom = GEOSGetGeometryN_r(ctx, geom, i);
if (sub_geom == NULL) {
return 2;
}
is_empty = GEOSisEmpty_r(ctx, sub_geom);
if (is_empty != 0) {
// If empty encountered, or on exception, return:
return is_empty;
}
}
return 0;
}
/* Returns 1 if geometry is an empty point, 0 otherwise, 2 on error.
*/
char is_point_empty(GEOSContextHandle_t ctx, GEOSGeometry* geom) {
int geom_type;
geom_type = GEOSGeomTypeId_r(ctx, geom);
if (geom_type == GEOS_POINT) {
return GEOSisEmpty_r(ctx, geom);
} else if (geom_type == -1) {
return 2; // GEOS exception
} else {
return 0; // No empty point
}
}
/* Returns 1 if a geometrycollection has an empty point, 0 otherwise, 2 on error.
Checks recursively (geometrycollections may contain multipoints / geometrycollections)
*/
char geometrycollection_has_point_empty(GEOSContextHandle_t ctx, GEOSGeometry* geom) {
int n, i;
char has_empty;
const GEOSGeometry* sub_geom;
n = GEOSGetNumGeometries_r(ctx, geom);
if (n == -1) {
return 2;
}
for (i = 0; i < n; i++) {
sub_geom = GEOSGetGeometryN_r(ctx, geom, i);
if (sub_geom == NULL) {
return 2;
}
has_empty = has_point_empty(ctx, (GEOSGeometry*)sub_geom);
if (has_empty != 0) {
// If empty encountered, or on exception, return:
return has_empty;
}
}
return 0;
}
/* Returns 1 if geometry is / has an empty point, 0 otherwise, 2 on error.
*/
char has_point_empty(GEOSContextHandle_t ctx, GEOSGeometry* geom) {
int geom_type;
geom_type = GEOSGeomTypeId_r(ctx, geom);
if (geom_type == GEOS_POINT) {
return GEOSisEmpty_r(ctx, geom);
} else if (geom_type == GEOS_MULTIPOINT) {
return multipoint_has_point_empty(ctx, geom);
} else if (geom_type == GEOS_GEOMETRYCOLLECTION) {
return geometrycollection_has_point_empty(ctx, geom);
} else if (geom_type == -1) {
return 2; // GEOS exception
} else {
return 0; // No empty point
}
}
/* Creates a POINT (nan, nan[, nan)] from a POINT EMPTY template
Returns NULL on error
*/
GEOSGeometry* point_empty_to_nan(GEOSContextHandle_t ctx, GEOSGeometry* geom) {
int j, ndim;
GEOSCoordSequence* coord_seq;
GEOSGeometry* result;
ndim = GEOSGeom_getCoordinateDimension_r(ctx, geom);
if (ndim == 0) {
return NULL;
}
coord_seq = GEOSCoordSeq_create_r(ctx, 1, ndim);
if (coord_seq == NULL) {
return NULL;
}
for (j = 0; j < ndim; j++) {
if (!GEOSCoordSeq_setOrdinate_r(ctx, coord_seq, 0, j, Py_NAN)) {
GEOSCoordSeq_destroy_r(ctx, coord_seq);
return NULL;
}
}
result = GEOSGeom_createPoint_r(ctx, coord_seq);
if (result == NULL) {
GEOSCoordSeq_destroy_r(ctx, coord_seq);
return NULL;
}
GEOSSetSRID_r(ctx, result, GEOSGetSRID_r(ctx, geom));
return result;
}
/* Creates a new multipoint, replacing empty points with POINT (nan, nan[, nan)]
Returns NULL on error
*/
GEOSGeometry* multipoint_empty_to_nan(GEOSContextHandle_t ctx, GEOSGeometry* geom) {
int n, i;
GEOSGeometry* result;
const GEOSGeometry* sub_geom;
n = GEOSGetNumGeometries_r(ctx, geom);
if (n == -1) {
return NULL;
}
GEOSGeometry** geoms = malloc(sizeof(void*) * n);
for (i = 0; i < n; i++) {
sub_geom = GEOSGetGeometryN_r(ctx, geom, i);
if (GEOSisEmpty_r(ctx, sub_geom)) {
geoms[i] = point_empty_to_nan(ctx, (GEOSGeometry*)sub_geom);
} else {
geoms[i] = GEOSGeom_clone_r(ctx, (GEOSGeometry*)sub_geom);
}
// If the function errored: cleanup and return
if (geoms[i] == NULL) {
destroy_geom_arr(ctx, geoms, i);
free(geoms);
return NULL;
}
}
result = GEOSGeom_createCollection_r(ctx, GEOS_MULTIPOINT, geoms, n);
// If the function errored: cleanup and return
if (result == NULL) {
destroy_geom_arr(ctx, geoms, i);
free(geoms);
return NULL;
}
free(geoms);
GEOSSetSRID_r(ctx, result, GEOSGetSRID_r(ctx, geom));
return result;
}
/* Creates a new geometrycollection, replacing all empty points with POINT (nan, nan[,
nan)]
Returns NULL on error
*/
GEOSGeometry* geometrycollection_empty_to_nan(GEOSContextHandle_t ctx,
GEOSGeometry* geom) {
int n, i;
GEOSGeometry* result = NULL;
const GEOSGeometry* sub_geom;
n = GEOSGetNumGeometries_r(ctx, geom);
if (n == -1) {
return NULL;
}
GEOSGeometry** geoms = malloc(sizeof(void*) * n);
for (i = 0; i < n; i++) {
sub_geom = GEOSGetGeometryN_r(ctx, geom, i);
geoms[i] = point_empty_to_nan_all_geoms(ctx, (GEOSGeometry*)sub_geom);
// If the function errored: cleanup and return
if (geoms[i] == NULL) {
goto finish;
}
}
result = GEOSGeom_createCollection_r(ctx, GEOS_GEOMETRYCOLLECTION, geoms, n);
finish:
// If the function errored: cleanup, else set SRID
if (result == NULL) {
destroy_geom_arr(ctx, geoms, i);
} else {
GEOSSetSRID_r(ctx, result, GEOSGetSRID_r(ctx, geom));
}
free(geoms);
return result;
}
/* Creates a new geometry, replacing empty points with POINT (nan, nan[, nan)]
Returns NULL on error.
*/
GEOSGeometry* point_empty_to_nan_all_geoms(GEOSContextHandle_t ctx, GEOSGeometry* geom) {
int geom_type;
GEOSGeometry* result;
geom_type = GEOSGeomTypeId_r(ctx, geom);
if (geom_type == -1) {
result = NULL;
} else if (is_point_empty(ctx, geom)) {
result = point_empty_to_nan(ctx, geom);
} else if (geom_type == GEOS_MULTIPOINT) {
result = multipoint_empty_to_nan(ctx, geom);
} else if (geom_type == GEOS_GEOMETRYCOLLECTION) {
result = geometrycollection_empty_to_nan(ctx, geom);
} else {
result = GEOSGeom_clone_r(ctx, geom);
}
GEOSSetSRID_r(ctx, result, GEOSGetSRID_r(ctx, geom));
return result;
}
/* Checks whether the geometry is a multipoint with an empty point in it
*
* According to https://github.com/libgeos/geos/issues/305, this check is not
* necessary for GEOS 3.7.3, 3.8.2, or 3.9. When these versions are out, we
* should add version conditionals and test.
*
* The return value is one of:
* - PGERR_SUCCESS
* - PGERR_MULTIPOINT_WITH_POINT_EMPTY
* - PGERR_GEOS_EXCEPTION
*/
char check_to_wkt_compatible(GEOSContextHandle_t ctx, GEOSGeometry* geom) {
char geom_type, is_empty;
geom_type = GEOSGeomTypeId_r(ctx, geom);
if (geom_type == -1) {
return PGERR_GEOS_EXCEPTION;
}
if (geom_type != GEOS_MULTIPOINT) {
return PGERR_SUCCESS;
}
is_empty = multipoint_has_point_empty(ctx, geom);
if (is_empty == 0) {
return PGERR_SUCCESS;
} else if (is_empty == 1) {
return PGERR_MULTIPOINT_WITH_POINT_EMPTY;
} else {
return PGERR_GEOS_EXCEPTION;
}
}
/* Checks whether the geometry contains a coordinate greater than 1E+100
*
* See also:
*
* https://github.com/shapely/shapely/issues/1903
*/
char get_zmax(GEOSContextHandle_t, const GEOSGeometry*, double*);
char get_zmax_simple(GEOSContextHandle_t, const GEOSGeometry*, double*);
char get_zmax_polygon(GEOSContextHandle_t, const GEOSGeometry*, double*);
char get_zmax_collection(GEOSContextHandle_t, const GEOSGeometry*, double*);
char get_zmax(GEOSContextHandle_t ctx, const GEOSGeometry* geom, double* zmax) {
int type = GEOSGeomTypeId_r(ctx, geom);
if ((type == 0) || (type == 1) || (type == 2)) {
return get_zmax_simple(ctx, geom, zmax);
} else if (type == 3) {
return get_zmax_polygon(ctx, geom, zmax);
} else if ((type >= 4) && (type <= 7)) {
return get_zmax_collection(ctx, geom, zmax);
} else {
return 0;
}
}
char get_zmax_simple(GEOSContextHandle_t ctx, const GEOSGeometry* geom, double* zmax) {
const GEOSCoordSequence* seq;
unsigned int n, i;
double coord;
seq = GEOSGeom_getCoordSeq_r(ctx, geom);
if (seq == NULL) {
return 0;
}
if (GEOSCoordSeq_getSize_r(ctx, seq, &n) == 0) {
return 0;
}
for (i = 0; i < n; i++) {
if (!GEOSCoordSeq_getZ_r(ctx, seq, i, &coord)) {
return 0;
}
if (npy_isfinite(coord) && (coord > *zmax)) {
*zmax = coord;
}
}
return 1;
}
char get_zmax_polygon(GEOSContextHandle_t ctx, const GEOSGeometry* geom, double* zmax) {
const GEOSGeometry* ring;
int n, i;
ring = GEOSGetExteriorRing_r(ctx, geom);
if (ring == NULL) {
return 0;
}
if (!get_zmax_simple(ctx, ring, zmax)) {
return 0;
}
n = GEOSGetNumInteriorRings_r(ctx, geom);
if (n == -1) {
return 0;
}
for (i = 0; i < n; i++) {
ring = GEOSGetInteriorRingN_r(ctx, geom, i);
if (ring == NULL) {
return 0;
}
if (!get_zmax_simple(ctx, ring, zmax)) {
return 0;
}
}
return 1;
}
char get_zmax_collection(GEOSContextHandle_t ctx, const GEOSGeometry* geom,
double* zmax) {
const GEOSGeometry* elem;
int n, i;
n = GEOSGetNumGeometries_r(ctx, geom);
if (n == -1) {
return 0;
}
for (i = 0; i < n; i++) {
elem = GEOSGetGeometryN_r(ctx, geom, i);
if (elem == NULL) {
return 0;
}
if (!get_zmax(ctx, elem, zmax)) {
return 0;
}
}
return 1;
}
#if !GEOS_SINCE_3_13_0
char check_to_wkt_trim_compatible(GEOSContextHandle_t ctx, const GEOSGeometry* geom,
int dimension) {
char is_empty;
double xmax = 0.0;
double ymax = 0.0;
double zmax = 0.0;
if (GEOSisEmpty_r(ctx, geom)) {
return PGERR_SUCCESS;
}
// use max coordinates to check if any coordinate is too large
if (!(GEOSGeom_getXMax_r(ctx, geom, &xmax) && GEOSGeom_getYMax_r(ctx, geom, &ymax))) {
return PGERR_GEOS_EXCEPTION;
}
if ((dimension > 2) && GEOSHasZ_r(ctx, geom)) {
if (!get_zmax(ctx, geom, &zmax)) {
return PGERR_GEOS_EXCEPTION;
}
}
if ((npy_isfinite(xmax) && (xmax > 1E100)) || (npy_isfinite(ymax) && (ymax > 1E100)) ||
(npy_isfinite(zmax) && (zmax > 1E100))) {
return PGERR_COORD_OUT_OF_BOUNDS;
}
return PGERR_SUCCESS;
}
#endif // !GEOS_SINCE_3_13_0
#if GEOS_SINCE_3_9_0
/* Checks whether the geometry is a 3D empty geometry and, if so, create the WKT string
*
* GEOS 3.9.* is able to distiguish 2D and 3D simple geometries (non-collections). But the
* but the WKT serialization never writes a 3D empty geometry. This function fixes that.
* It only makes sense to use this for GEOS versions >= 3.9.
*
* Pending GEOS ticket: https://trac.osgeo.org/geos/ticket/1129
*
* If the geometry is a 3D empty, then the (char**) wkt argument is filled with the
* correct WKT string. Else, wkt becomes NULL and the GEOS WKT writer should be used.
*
* The return value is one of:
* - PGERR_SUCCESS
* - PGERR_GEOS_EXCEPTION
*/
char wkt_empty_3d_geometry(GEOSContextHandle_t ctx, GEOSGeometry* geom, char** wkt) {
char is_empty;
int geom_type;
is_empty = GEOSisEmpty_r(ctx, geom);
if (is_empty == 2) {
return PGERR_GEOS_EXCEPTION;
} else if (is_empty == 0) {
*wkt = NULL;
return PGERR_SUCCESS;
}
if (GEOSGeom_getCoordinateDimension_r(ctx, geom) == 2) {
*wkt = NULL;
return PGERR_SUCCESS;
}
geom_type = GEOSGeomTypeId_r(ctx, geom);
switch (geom_type) {
case GEOS_POINT:
*wkt = "POINT Z EMPTY";
return PGERR_SUCCESS;
case GEOS_LINESTRING:
*wkt = "LINESTRING Z EMPTY";
break;
case GEOS_LINEARRING:
*wkt = "LINEARRING Z EMPTY";
break;
case GEOS_POLYGON:
*wkt = "POLYGON Z EMPTY";
break;
// Note: Empty collections cannot be 3D in GEOS.
// We do include the options in case of future support.
case GEOS_MULTIPOINT:
*wkt = "MULTIPOINT Z EMPTY";
break;
case GEOS_MULTILINESTRING:
*wkt = "MULTILINESTRING Z EMPTY";
break;
case GEOS_MULTIPOLYGON:
*wkt = "MULTIPOLYGON Z EMPTY";
break;
case GEOS_GEOMETRYCOLLECTION:
*wkt = "GEOMETRYCOLLECTION Z EMPTY";
break;
default:
return PGERR_GEOS_EXCEPTION;
}
return PGERR_SUCCESS;
}
#endif // GEOS_SINCE_3_9_0
/* GEOSInterpolate_r and GEOSInterpolateNormalized_r segfault on empty
* geometries and also on collections with the first geometry empty.
*
* This function returns:
* - PGERR_GEOMETRY_TYPE on non-linear geometries
* - PGERR_EMPTY_GEOMETRY on empty linear geometries
* - PGERR_EXCEPTIONS on GEOS exceptions
* - PGERR_SUCCESS on a non-empty and linear geometry
*
* Note that GEOS 3.8 fixed this situation for empty LINESTRING/LINEARRING,
* but it still segfaults on other empty geometries.
*/
char geos_interpolate_checker(GEOSContextHandle_t ctx, GEOSGeometry* geom) {
char type;
char is_empty;
const GEOSGeometry* sub_geom;
// Check if the geometry is linear
type = GEOSGeomTypeId_r(ctx, geom);
if (type == -1) {
return PGERR_GEOS_EXCEPTION;
} else if ((type == GEOS_POINT) || (type == GEOS_POLYGON) ||
(type == GEOS_MULTIPOINT) || (type == GEOS_MULTIPOLYGON)) {
return PGERR_GEOMETRY_TYPE;
}
// Check if the geometry is empty
is_empty = GEOSisEmpty_r(ctx, geom);
if (is_empty == 1) {
return PGERR_EMPTY_GEOMETRY;
} else if (is_empty == 2) {
return PGERR_GEOS_EXCEPTION;
}
// For collections: also check the type and emptyness of the first geometry
if ((type == GEOS_MULTILINESTRING) || (type == GEOS_GEOMETRYCOLLECTION)) {
sub_geom = GEOSGetGeometryN_r(ctx, geom, 0);
if (sub_geom == NULL) {
return PGERR_GEOS_EXCEPTION; // GEOSException
}
type = GEOSGeomTypeId_r(ctx, sub_geom);
if (type == -1) {
return PGERR_GEOS_EXCEPTION;
} else if ((type != GEOS_LINESTRING) && (type != GEOS_LINEARRING)) {
return PGERR_GEOMETRY_TYPE;
}
is_empty = GEOSisEmpty_r(ctx, sub_geom);
if (is_empty == 1) {
return PGERR_EMPTY_GEOMETRY;
} else if (is_empty == 2) {
return PGERR_GEOS_EXCEPTION;
}
}
return PGERR_SUCCESS;
}
/* Define the GEOS error handler. See GEOS_INIT / GEOS_FINISH macros in geos.h*/
void geos_error_handler(const char* message, void* userdata) {
snprintf(userdata, 1024, "%s", message);
}
/* Extract bounds from geometry.
*
* Bounds coordinates will be set to NPY_NAN if geom is NULL, empty, or does not have an
* envelope.
*
* Parameters
* ----------
* ctx: GEOS context handle
* geom: pointer to GEOSGeometry; can be NULL
* xmin: pointer to xmin value
* ymin: pointer to ymin value
* xmax: pointer to xmax value
* ymax: pointer to ymax value
*
* Must be called from within a GEOS_INIT_THREADS / GEOS_FINISH_THREADS
* or GEOS_INIT / GEOS_FINISH block.
*
* Returns
* -------
* 1 on success; 0 on error
*/
int get_bounds(GEOSContextHandle_t ctx, GEOSGeometry* geom, double* xmin, double* ymin,
double* xmax, double* ymax) {
int retval = 1;
if (geom == NULL || GEOSisEmpty_r(ctx, geom)) {
*xmin = *ymin = *xmax = *ymax = NPY_NAN;
return 1;
}
// use min / max coordinates
if (!(GEOSGeom_getXMin_r(ctx, geom, xmin) && GEOSGeom_getYMin_r(ctx, geom, ymin) &&
GEOSGeom_getXMax_r(ctx, geom, xmax) && GEOSGeom_getYMax_r(ctx, geom, ymax))) {
return 0;
}
return retval;
}
/* Create a Polygon from bounding coordinates.
*
* Must be called from within a GEOS_INIT_THREADS / GEOS_FINISH_THREADS
* or GEOS_INIT / GEOS_FINISH block.
*
* Parameters
* ----------
* ctx: GEOS context handle
* xmin: minimum X value
* ymin: minimum Y value
* xmax: maximum X value
* ymax: maximum Y value
* ccw: if 1, box will be created in counterclockwise direction from bottom right;
* otherwise will be created in clockwise direction from bottom left.
*
* Returns
* -------
* GEOSGeometry* on success (owned by caller) or
* NULL on failure or NPY_NAN coordinates
*/
GEOSGeometry* create_box(GEOSContextHandle_t ctx, double xmin, double ymin, double xmax,
double ymax, char ccw) {
if (npy_isnan(xmin) || npy_isnan(ymin) || npy_isnan(xmax) || npy_isnan(ymax)) {
return NULL;
}
GEOSCoordSequence* coords = NULL;
GEOSGeometry* geom = NULL;
GEOSGeometry* ring = NULL;
// Construct coordinate sequence and set vertices
coords = GEOSCoordSeq_create_r(ctx, 5, 2);
if (coords == NULL) {
return NULL;
}
if (ccw) {
// Start from bottom right (xmax, ymin) to match shapely
if (!(GEOSCoordSeq_setX_r(ctx, coords, 0, xmax) &&
GEOSCoordSeq_setY_r(ctx, coords, 0, ymin) &&
GEOSCoordSeq_setX_r(ctx, coords, 1, xmax) &&
GEOSCoordSeq_setY_r(ctx, coords, 1, ymax) &&
GEOSCoordSeq_setX_r(ctx, coords, 2, xmin) &&
GEOSCoordSeq_setY_r(ctx, coords, 2, ymax) &&
GEOSCoordSeq_setX_r(ctx, coords, 3, xmin) &&
GEOSCoordSeq_setY_r(ctx, coords, 3, ymin) &&
GEOSCoordSeq_setX_r(ctx, coords, 4, xmax) &&
GEOSCoordSeq_setY_r(ctx, coords, 4, ymin))) {
if (coords != NULL) {
GEOSCoordSeq_destroy_r(ctx, coords);
}
return NULL;
}
} else {
// Start from bottom left (min, ymin) to match shapely
if (!(GEOSCoordSeq_setX_r(ctx, coords, 0, xmin) &&
GEOSCoordSeq_setY_r(ctx, coords, 0, ymin) &&
GEOSCoordSeq_setX_r(ctx, coords, 1, xmin) &&
GEOSCoordSeq_setY_r(ctx, coords, 1, ymax) &&
GEOSCoordSeq_setX_r(ctx, coords, 2, xmax) &&
GEOSCoordSeq_setY_r(ctx, coords, 2, ymax) &&
GEOSCoordSeq_setX_r(ctx, coords, 3, xmax) &&
GEOSCoordSeq_setY_r(ctx, coords, 3, ymin) &&
GEOSCoordSeq_setX_r(ctx, coords, 4, xmin) &&
GEOSCoordSeq_setY_r(ctx, coords, 4, ymin))) {
if (coords != NULL) {
GEOSCoordSeq_destroy_r(ctx, coords);
}
return NULL;
}
}
// Construct linear ring then use to construct polygon
// Note: coords are owned by ring; if ring fails to construct, it will
// automatically clean up the coords
ring = GEOSGeom_createLinearRing_r(ctx, coords);
if (ring == NULL) {
return NULL;
}
// Note: ring is owned by polygon; if polygon fails to construct, it will
// automatically clean up the ring
geom = GEOSGeom_createPolygon_r(ctx, ring, NULL, 0);
if (geom == NULL) {
return NULL;
}
return geom;
}
/* Create a 3D empty Point
*
* Works around a limitation of the GEOS < 3.8 C API by constructing the point
* from its WKT representation (POINT Z EMPTY).
* (the limitation is that we can't have length-0 coord seqs)
*
* Returns
* -------
* GEOSGeometry* on success (owned by caller) or NULL on failure
*/
GEOSGeometry* PyGEOS_create3DEmptyPoint(GEOSContextHandle_t ctx) {
GEOSGeometry* geom;
#if GEOS_SINCE_3_8_0
coord_seq = GEOSCoordSeq_create_r(ctx, 0, 3);
if (coord_seq == NULL) {
return NULL;
}
geom = GEOSGeom_createPoint_r(ctx, coord_seq);
#else // GEOS_SINCE_3_8_0
const char* wkt = "POINT Z EMPTY";
GEOSWKTReader* reader;
reader = GEOSWKTReader_create_r(ctx);
if (reader == NULL) {
return NULL;
}
geom = GEOSWKTReader_read_r(ctx, reader, wkt);
GEOSWKTReader_destroy_r(ctx, reader);
#endif // GEOS_SINCE_3_8_0
return geom;
}
/* Create a non-empty Point from x, y and optionally z coordinates.
*
* Parameters
* ----------
* ctx: GEOS context handle
* x: X value
* y: Y value
* z: Z value pointer (point will be 2D if this is NULL)
*
* Returns
* -------
* GEOSGeometry* on success (owned by caller) or NULL on failure
*/
GEOSGeometry* PyGEOS_createPoint(GEOSContextHandle_t ctx, double x, double y, double* z) {
#if GEOS_SINCE_3_8_0
if (z == NULL) {
// There is no 3D equivalent for GEOSGeom_createPointFromXY_r
// instead, it is constructed from a coord seq.
return GEOSGeom_createPointFromXY_r(ctx, x, y);
}
#endif
// Fallback point construction (3D or GEOS < 3.8.0)
GEOSCoordSequence* coord_seq = NULL;
coord_seq = GEOSCoordSeq_create_r(ctx, 1, z == NULL ? 2 : 3);
if (coord_seq == NULL) {
return NULL;
}
if (!GEOSCoordSeq_setX_r(ctx, coord_seq, 0, x)) {
GEOSCoordSeq_destroy_r(ctx, coord_seq);
return NULL;
}
if (!GEOSCoordSeq_setY_r(ctx, coord_seq, 0, y)) {
GEOSCoordSeq_destroy_r(ctx, coord_seq);
return NULL;
}
if (z != NULL) {
if (!GEOSCoordSeq_setZ_r(ctx, coord_seq, 0, *z)) {
GEOSCoordSeq_destroy_r(ctx, coord_seq);
return NULL;
}
}
// Note: coordinate sequence is owned by point; if point fails to construct, it will
// automatically clean up the coordinate sequence
return GEOSGeom_createPoint_r(ctx, coord_seq);
}
/* Create a Point from x, y and optionally z coordinates, optionally handling NaN.
*
* Parameters
* ----------
* ctx: GEOS context handle
* x: X value
* y: Y value
* z: Z value pointer (point will be 2D if this is NULL)
* handle_nan: 0 means 'allow', 1 means 'skip', 2 means 'error'
* out: pointer to the resulting point geometry
*
* Returns
* -------
* Returns an error state (PGERR_SUCCESS / PGERR_GEOS_EXCEPTION / PGERR_NAN_COORD)
*/
enum ShapelyErrorCode create_point(GEOSContextHandle_t ctx, double x, double y, double* z,
int handle_nan, GEOSGeometry** out) {
if (handle_nan != SHAPELY_HANDLE_NAN_ALLOW) {
// Check here whether any of (x, y, z) is nonfinite
if ((!npy_isfinite(x)) || (!npy_isfinite(y)) || (z != NULL ? !npy_isfinite(*z) : 0)) {
if (handle_nan == SHAPELY_HANDLE_NAN_SKIP) {
// Construct an empty point
if (z == NULL) {
*out = GEOSGeom_createEmptyPoint_r(ctx);
} else {
*out = PyGEOS_create3DEmptyPoint(ctx);
}
return (*out != NULL) ? PGERR_SUCCESS : PGERR_GEOS_EXCEPTION;
} else {
// Raise exception
return PGERR_NAN_COORD;
}
}
}
*out = PyGEOS_createPoint(ctx, x, y, z);
return (*out != NULL) ? PGERR_SUCCESS : PGERR_GEOS_EXCEPTION;
}
/* Force the coordinate dimensionality (2D / 3D) of any geometry
*
* Parameters
* ----------
* ctx: GEOS context handle
* geom: geometry
* dims: dimensions to force (2 or 3)
* z: Z coordinate (ignored if dims==2)
*
* Returns
* -------
* GEOSGeometry* on success (owned by caller) or NULL on failure
*/
GEOSGeometry* force_dims(GEOSContextHandle_t, GEOSGeometry*, unsigned int, double);
GEOSGeometry* force_dims_simple(GEOSContextHandle_t ctx, GEOSGeometry* geom, int type,
unsigned int dims, double z) {
unsigned int actual_dims, n, i, j;
double coord;
const GEOSCoordSequence* seq = GEOSGeom_getCoordSeq_r(ctx, geom);
/* Special case for POINT EMPTY (on GEOS < 3.8, point coordinate list cannot be 0-length)
*/
#if !GEOS_SINCE_3_8_0
if ((type == 0) && (GEOSisEmpty_r(ctx, geom) == 1)) {
if (dims == 2) {
return GEOSGeom_createEmptyPoint_r(ctx);
} else if (dims == 3) {
return PyGEOS_create3DEmptyPoint(ctx);
} else {
return NULL;
}
}
#endif // !GEOS_SINCE_3_8_0
/* Investigate the coordinate sequence, return when already of correct dimensionality */
if (GEOSCoordSeq_getDimensions_r(ctx, seq, &actual_dims) == 0) {
return NULL;
}
if (actual_dims == dims) {
return GEOSGeom_clone_r(ctx, geom);
}
if (GEOSCoordSeq_getSize_r(ctx, seq, &n) == 0) {
return NULL;
}
/* Create a new one to fill with the new coordinates */
GEOSCoordSequence* seq_new = GEOSCoordSeq_create_r(ctx, n, dims);
if (seq_new == NULL) {
return NULL;
}
for (i = 0; i < n; i++) {
for (j = 0; j < 2; j++) {
if (!GEOSCoordSeq_getOrdinate_r(ctx, seq, i, j, &coord)) {
GEOSCoordSeq_destroy_r(ctx, seq_new);
return NULL;
}
if (!GEOSCoordSeq_setOrdinate_r(ctx, seq_new, i, j, coord)) {
GEOSCoordSeq_destroy_r(ctx, seq_new);
return NULL;
}
}
if (dims == 3) {
if (!GEOSCoordSeq_setZ_r(ctx, seq_new, i, z)) {
GEOSCoordSeq_destroy_r(ctx, seq_new);
return NULL;
}
}
}
/* Construct a new geometry */
if (type == 0) {
return GEOSGeom_createPoint_r(ctx, seq_new);
} else if (type == 1) {
return GEOSGeom_createLineString_r(ctx, seq_new);
} else if (type == 2) {
return GEOSGeom_createLinearRing_r(ctx, seq_new);
} else {
return NULL;
}
}
GEOSGeometry* force_dims_polygon(GEOSContextHandle_t ctx, GEOSGeometry* geom,
unsigned int dims, double z) {
int i, n;
const GEOSGeometry *shell, *hole;
GEOSGeometry *new_shell, *new_hole, *result = NULL;
GEOSGeometry** new_holes;
n = GEOSGetNumInteriorRings_r(ctx, geom);
if (n == -1) {
return NULL;
}
/* create the exterior ring */
shell = GEOSGetExteriorRing_r(ctx, geom);
if (shell == NULL) {
return NULL;
}
new_shell = force_dims_simple(ctx, (GEOSGeometry*)shell, 2, dims, z);
if (new_shell == NULL) {
return NULL;
}
new_holes = malloc(sizeof(void*) * n);
if (new_holes == NULL) {
GEOSGeom_destroy_r(ctx, new_shell);
return NULL;
}
for (i = 0; i < n; i++) {
hole = GEOSGetInteriorRingN_r(ctx, geom, i);
if (hole == NULL) {
GEOSGeom_destroy_r(ctx, new_shell);
destroy_geom_arr(ctx, new_holes, i - 1);
goto finish;
}
new_hole = force_dims_simple(ctx, (GEOSGeometry*)hole, 2, dims, z);
if (hole == NULL) {
GEOSGeom_destroy_r(ctx, new_shell);
destroy_geom_arr(ctx, new_holes, i - 1);
goto finish;
}
new_holes[i] = new_hole;
}
result = GEOSGeom_createPolygon_r(ctx, new_shell, new_holes, n);
finish:
if (new_holes != NULL) {
free(new_holes);
}
return result;
}
GEOSGeometry* force_dims_collection(GEOSContextHandle_t ctx, GEOSGeometry* geom, int type,
unsigned int dims, double z) {
int i, n;
const GEOSGeometry* sub_geom;
GEOSGeometry *new_sub_geom, *result = NULL;
GEOSGeometry** geoms;
n = GEOSGetNumGeometries_r(ctx, geom);
if (n == -1) {
return NULL;
}
geoms = malloc(sizeof(void*) * n);
if (geoms == NULL) {
return NULL;
}
for (i = 0; i < n; i++) {
sub_geom = GEOSGetGeometryN_r(ctx, geom, i);
if (sub_geom == NULL) {
destroy_geom_arr(ctx, geoms, i - i);
goto finish;
}
new_sub_geom = force_dims(ctx, (GEOSGeometry*)sub_geom, dims, z);
if (new_sub_geom == NULL) {
destroy_geom_arr(ctx, geoms, i - i);
goto finish;
}
geoms[i] = new_sub_geom;
}
result = GEOSGeom_createCollection_r(ctx, type, geoms, n);
finish:
if (geoms != NULL) {
free(geoms);
}
return result;
}
GEOSGeometry* force_dims(GEOSContextHandle_t ctx, GEOSGeometry* geom, unsigned int dims,
double z) {
int type = GEOSGeomTypeId_r(ctx, geom);
if ((type == 0) || (type == 1) || (type == 2)) {
return force_dims_simple(ctx, geom, type, dims, z);
} else if (type == 3) {
return force_dims_polygon(ctx, geom, dims, z);
} else if ((type >= 4) && (type <= 7)) {