-
Notifications
You must be signed in to change notification settings - Fork 560
/
ufuncs.c
3404 lines (3022 loc) · 106 KB
/
ufuncs.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 <Python.h>
#include <math.h>
#define NO_IMPORT_ARRAY
#define NO_IMPORT_UFUNC
#define PY_ARRAY_UNIQUE_SYMBOL shapely_ARRAY_API
#define PY_UFUNC_UNIQUE_SYMBOL shapely_UFUNC_API
#include <numpy/ndarraytypes.h>
#include <numpy/npy_3kcompat.h>
#include <numpy/ufuncobject.h>
#include "fast_loop_macros.h"
#include "geos.h"
#include "pygeom.h"
#define OUTPUT_Y \
PyObject* ret = GeometryObject_FromGEOS(ret_ptr, ctx); \
PyObject** out = (PyObject**)op1; \
Py_XDECREF(*out); \
*out = ret
#define OUTPUT_Y_I(I, RET_PTR) \
PyObject* ret##I = GeometryObject_FromGEOS(RET_PTR, ctx); \
PyObject** out##I = (PyObject**)op##I; \
Py_XDECREF(*out##I); \
*out##I = ret##I
// Fail if inputs output multiple times on the same place in memory. That would
// lead to segfaults as the same GEOSGeometry would be 'owned' by multiple PyObjects.
#define CHECK_NO_INPLACE_OUTPUT(N) \
if ((steps[N] == 0) && (dimensions[0] > 1)) { \
PyErr_Format(PyExc_NotImplementedError, \
"Zero-strided output detected. Ufunc mode with args[0]=%p, " \
"args[N]=%p, steps[0]=%ld, steps[N]=%ld, dimensions[0]=%ld.", \
args[0], args[N], steps[0], steps[N], dimensions[0]); \
return; \
}
#define CHECK_ALLOC(ARR) \
if (ARR == NULL) { \
PyErr_SetString(PyExc_MemoryError, "Could not allocate memory"); \
return; \
}
static void geom_arr_to_npy(GEOSGeometry** array, char* ptr, npy_intp stride,
npy_intp count) {
npy_intp i;
PyObject* ret;
PyObject** out;
GEOS_INIT;
for (i = 0; i < count; i++, ptr += stride) {
ret = GeometryObject_FromGEOS(array[i], ctx);
out = (PyObject**)ptr;
Py_XDECREF(*out);
*out = ret;
}
GEOS_FINISH;
}
/* Define the geom -> bool functions (Y_b) */
static void* is_empty_data[1] = {GEOSisEmpty_r};
/* the GEOSisSimple_r function fails on geometrycollections */
static char GEOSisSimpleAllTypes_r(void* context, void* geom) {
int type = GEOSGeomTypeId_r(context, geom);
if (type == -1) {
return 2; // Predicates use a return value of 2 for errors
} else if (type == 7) {
return 0;
} else {
return GEOSisSimple_r(context, geom);
}
}
static void* is_simple_data[1] = {GEOSisSimpleAllTypes_r};
static void* is_ring_data[1] = {GEOSisRing_r};
static void* has_z_data[1] = {GEOSHasZ_r};
/* the GEOSisClosed_r function fails on non-linestrings */
static char GEOSisClosedAllTypes_r(void* context, void* geom) {
int type = GEOSGeomTypeId_r(context, geom);
if (type == -1) {
return 2; // Predicates use a return value of 2 for errors
} else if ((type == 1) || (type == 2) || (type == 5)) {
return GEOSisClosed_r(context, geom);
} else {
return 0;
}
}
static void* is_closed_data[1] = {GEOSisClosedAllTypes_r};
static void* is_valid_data[1] = {GEOSisValid_r};
#if GEOS_SINCE_3_7_0
static char GEOSGeom_isCCW_r(void* context, void* geom) {
const GEOSCoordSequence* coord_seq;
char is_ccw = 2; // return value of 2 means GEOSException
int i;
// Return False for non-linear geometries
i = GEOSGeomTypeId_r(context, geom);
if (i == -1) {
return 2;
}
if ((i != GEOS_LINEARRING) && (i != GEOS_LINESTRING)) {
return 0;
}
// Return False for lines with fewer than 4 points
i = GEOSGeomGetNumPoints_r(context, geom);
if (i == -1) {
return 2;
}
if (i < 4) {
return 0;
}
// Get the coordinatesequence and call isCCW()
coord_seq = GEOSGeom_getCoordSeq_r(context, geom);
if (coord_seq == NULL) {
return 2;
}
if (!GEOSCoordSeq_isCCW_r(context, coord_seq, &is_ccw)) {
return 2;
}
return is_ccw;
}
static void* is_ccw_data[1] = {GEOSGeom_isCCW_r};
#endif
typedef char FuncGEOS_Y_b(void* context, void* a);
static char Y_b_dtypes[2] = {NPY_OBJECT, NPY_BOOL};
static void Y_b_func(char** args, npy_intp* dimensions, npy_intp* steps, void* data) {
FuncGEOS_Y_b* func = (FuncGEOS_Y_b*)data;
GEOSGeometry* in1 = NULL;
char ret;
GEOS_INIT_THREADS;
UNARY_LOOP {
/* get the geometry; return on error */
if (!get_geom(*(GeometryObject**)ip1, &in1)) {
errstate = PGERR_NOT_A_GEOMETRY;
goto finish;
}
if (in1 == NULL) {
/* in case of a missing value: return 0 (False) */
ret = 0;
} else {
/* call the GEOS function */
ret = func(ctx, in1);
/* finish for illegal values */
if (ret == 2) {
errstate = PGERR_GEOS_EXCEPTION;
goto finish;
}
}
*(npy_bool*)op1 = ret;
}
finish:
GEOS_FINISH_THREADS;
}
static PyUFuncGenericFunction Y_b_funcs[1] = {&Y_b_func};
/* Define the object -> bool functions (O_b) which do not raise on non-geom objects*/
static char IsMissing(void* context, PyObject* obj) {
GEOSGeometry* g = NULL;
if (!get_geom((GeometryObject*)obj, &g)) {
return 0;
};
return g == NULL; // get_geom sets g to NULL for None input
}
static void* is_missing_data[1] = {IsMissing};
static char IsGeometry(void* context, PyObject* obj) {
GEOSGeometry* g = NULL;
if (!get_geom((GeometryObject*)obj, &g)) {
return 0;
}
return g != NULL;
}
static void* is_geometry_data[1] = {IsGeometry};
static char IsValidInput(void* context, PyObject* obj) {
GEOSGeometry* g = NULL;
return get_geom((GeometryObject*)obj, &g);
}
static void* is_valid_input_data[1] = {IsValidInput};
typedef char FuncGEOS_O_b(void* context, PyObject* obj);
static char O_b_dtypes[2] = {NPY_OBJECT, NPY_BOOL};
static void O_b_func(char** args, npy_intp* dimensions, npy_intp* steps, void* data) {
FuncGEOS_O_b* func = (FuncGEOS_O_b*)data;
GEOS_INIT_THREADS;
UNARY_LOOP { *(npy_bool*)op1 = func(ctx, *(PyObject**)ip1); }
GEOS_FINISH_THREADS;
}
static PyUFuncGenericFunction O_b_funcs[1] = {&O_b_func};
/* Define the geom, geom -> bool functions (YY_b) */
static void* equals_data[1] = {GEOSEquals_r};
typedef char FuncGEOS_YY_b(void* context, void* a, void* b);
static char YY_b_dtypes[3] = {NPY_OBJECT, NPY_OBJECT, NPY_BOOL};
static void YY_b_func(char** args, npy_intp* dimensions, npy_intp* steps, void* data) {
FuncGEOS_YY_b* func = (FuncGEOS_YY_b*)data;
GEOSGeometry *in1 = NULL, *in2 = NULL;
char ret;
GEOS_INIT_THREADS;
BINARY_LOOP {
/* get the geometries: return on error */
if (!get_geom(*(GeometryObject**)ip1, &in1)) {
errstate = PGERR_NOT_A_GEOMETRY;
goto finish;
}
if (!get_geom(*(GeometryObject**)ip2, &in2)) {
errstate = PGERR_NOT_A_GEOMETRY;
goto finish;
}
if ((in1 == NULL) || (in2 == NULL)) {
/* in case of a missing value: return 0 (False) */
ret = 0;
} else {
/* call the GEOS function */
ret = func(ctx, in1, in2);
/* return for illegal values */
if (ret == 2) {
errstate = PGERR_GEOS_EXCEPTION;
goto finish;
}
}
*(npy_bool*)op1 = ret;
}
finish:
GEOS_FINISH_THREADS;
}
static PyUFuncGenericFunction YY_b_funcs[1] = {&YY_b_func};
/* Define the geom, geom -> bool functions (YY_b) prepared */
static void* contains_func_tuple[2] = {GEOSContains_r, GEOSPreparedContains_r};
static void* contains_data[1] = {contains_func_tuple};
static char GEOSContainsProperly(void* context, void* g1, void* g2) {
const GEOSPreparedGeometry* prepared_geom_tmp = NULL;
char ret;
prepared_geom_tmp = GEOSPrepare_r(context, g1);
if (prepared_geom_tmp == NULL) {
return 2;
}
ret = GEOSPreparedContainsProperly_r(context, prepared_geom_tmp, g2);
GEOSPreparedGeom_destroy_r(context, prepared_geom_tmp);
return ret;
}
static void* contains_properly_func_tuple[2] = {GEOSContainsProperly,
GEOSPreparedContainsProperly_r};
static void* contains_properly_data[1] = {contains_properly_func_tuple};
static void* covered_by_func_tuple[2] = {GEOSCoveredBy_r, GEOSPreparedCoveredBy_r};
static void* covered_by_data[1] = {covered_by_func_tuple};
static void* covers_func_tuple[2] = {GEOSCovers_r, GEOSPreparedCovers_r};
static void* covers_data[1] = {covers_func_tuple};
static void* crosses_func_tuple[2] = {GEOSCrosses_r, GEOSPreparedCrosses_r};
static void* crosses_data[1] = {crosses_func_tuple};
static void* disjoint_func_tuple[2] = {GEOSDisjoint_r, GEOSPreparedDisjoint_r};
static void* disjoint_data[1] = {disjoint_func_tuple};
static void* intersects_func_tuple[2] = {GEOSIntersects_r, GEOSPreparedIntersects_r};
static void* intersects_data[1] = {intersects_func_tuple};
static void* overlaps_func_tuple[2] = {GEOSOverlaps_r, GEOSPreparedOverlaps_r};
static void* overlaps_data[1] = {overlaps_func_tuple};
static void* touches_func_tuple[2] = {GEOSTouches_r, GEOSPreparedTouches_r};
static void* touches_data[1] = {touches_func_tuple};
static void* within_func_tuple[2] = {GEOSWithin_r, GEOSPreparedWithin_r};
static void* within_data[1] = {within_func_tuple};
static char YY_b_p_dtypes[3] = {NPY_OBJECT, NPY_OBJECT, NPY_BOOL};
static void YY_b_p_func(char** args, npy_intp* dimensions, npy_intp* steps, void* data) {
FuncGEOS_YY_b* func = ((FuncGEOS_YY_b**)data)[0];
FuncGEOS_YY_b* func_prepared = ((FuncGEOS_YY_b**)data)[1];
GEOSGeometry *in1 = NULL, *in2 = NULL;
GEOSPreparedGeometry* in1_prepared = NULL;
char ret;
GEOS_INIT_THREADS;
BINARY_LOOP {
/* get the geometries: return on error */
if (!get_geom_with_prepared(*(GeometryObject**)ip1, &in1, &in1_prepared)) {
errstate = PGERR_NOT_A_GEOMETRY;
goto finish;
}
if (!get_geom(*(GeometryObject**)ip2, &in2)) {
errstate = PGERR_NOT_A_GEOMETRY;
goto finish;
}
if ((in1 == NULL) || (in2 == NULL)) {
/* in case of a missing value: return 0 (False) */
ret = 0;
} else {
if (in1_prepared == NULL) {
/* call the GEOS function */
ret = func(ctx, in1, in2);
} else {
/* call the prepared GEOS function */
ret = func_prepared(ctx, in1_prepared, in2);
}
/* return for illegal values */
if (ret == 2) {
errstate = PGERR_GEOS_EXCEPTION;
goto finish;
}
}
*(npy_bool*)op1 = ret;
}
finish:
GEOS_FINISH_THREADS;
}
static PyUFuncGenericFunction YY_b_p_funcs[1] = {&YY_b_p_func};
static char is_prepared_dtypes[2] = {NPY_OBJECT, NPY_BOOL};
static void is_prepared_func(char** args, npy_intp* dimensions, npy_intp* steps,
void* data) {
GEOSGeometry* in1 = NULL;
GEOSPreparedGeometry* in1_prepared = NULL;
GEOS_INIT_THREADS;
UNARY_LOOP {
/* get the geometry: return on error */
if (!get_geom_with_prepared(*(GeometryObject**)ip1, &in1, &in1_prepared)) {
errstate = PGERR_NOT_A_GEOMETRY;
break;
}
*(npy_bool*)op1 = (in1_prepared != NULL);
}
GEOS_FINISH_THREADS;
}
static PyUFuncGenericFunction is_prepared_funcs[1] = {&is_prepared_func};
/* Define the geom -> geom functions (Y_Y) */
static void* envelope_data[1] = {GEOSEnvelope_r};
static void* convex_hull_data[1] = {GEOSConvexHull_r};
static void* GEOSBoundaryAllTypes_r(void* context, void* geom) {
char typ = GEOSGeomTypeId_r(context, geom);
if (typ == 7) {
/* return None for geometrycollections */
return NULL;
} else {
return GEOSBoundary_r(context, geom);
}
}
static void* boundary_data[1] = {GEOSBoundaryAllTypes_r};
static void* unary_union_data[1] = {GEOSUnaryUnion_r};
static void* point_on_surface_data[1] = {GEOSPointOnSurface_r};
static void* centroid_data[1] = {GEOSGetCentroid_r};
static void* line_merge_data[1] = {GEOSLineMerge_r};
static void* extract_unique_points_data[1] = {GEOSGeom_extractUniquePoints_r};
static void* GetExteriorRing(void* context, void* geom) {
char typ = GEOSGeomTypeId_r(context, geom);
if (typ != 3) {
return NULL;
}
void* ret = (void*)GEOSGetExteriorRing_r(context, geom);
/* Create a copy of the obtained geometry */
if (ret != NULL) {
ret = GEOSGeom_clone_r(context, ret);
}
return ret;
}
static void* get_exterior_ring_data[1] = {GetExteriorRing};
/* the normalize funcion acts inplace */
static void* GEOSNormalize_r_with_clone(void* context, void* geom) {
int ret;
void* new_geom = GEOSGeom_clone_r(context, geom);
if (new_geom == NULL) {
return NULL;
}
ret = GEOSNormalize_r(context, new_geom);
if (ret == -1) {
GEOSGeom_destroy_r(context, new_geom);
return NULL;
}
return new_geom;
}
static void* normalize_data[1] = {GEOSNormalize_r_with_clone};
static void* force_2d_data[1] = {PyGEOSForce2D};
#if GEOS_SINCE_3_8_0
static void* build_area_data[1] = {GEOSBuildArea_r};
static void* make_valid_data[1] = {GEOSMakeValid_r};
static void* coverage_union_data[1] = {GEOSCoverageUnion_r};
static void* GEOSMinimumBoundingCircleWithReturn(void* context, void* geom) {
GEOSGeometry* center = NULL;
double radius;
GEOSGeometry* ret = GEOSMinimumBoundingCircle_r(context, geom, &radius, ¢er);
if (ret == NULL) {
return NULL;
}
GEOSGeom_destroy_r(context, center);
return ret;
}
static void* minimum_bounding_circle_data[1] = {GEOSMinimumBoundingCircleWithReturn};
#endif
#if GEOS_SINCE_3_7_0
static void* reverse_data[1] = {GEOSReverse_r};
#endif
#if GEOS_SINCE_3_6_0
static void* oriented_envelope_data[1] = {GEOSMinimumRotatedRectangle_r};
#endif
typedef void* FuncGEOS_Y_Y(void* context, void* a);
static char Y_Y_dtypes[2] = {NPY_OBJECT, NPY_OBJECT};
static void Y_Y_func(char** args, npy_intp* dimensions, npy_intp* steps, void* data) {
FuncGEOS_Y_Y* func = (FuncGEOS_Y_Y*)data;
GEOSGeometry* in1 = NULL;
GEOSGeometry** geom_arr;
CHECK_NO_INPLACE_OUTPUT(1);
// allocate a temporary array to store output GEOSGeometry objects
geom_arr = malloc(sizeof(void*) * dimensions[0]);
CHECK_ALLOC(geom_arr);
GEOS_INIT_THREADS;
UNARY_LOOP {
// get the geometry: return on error
if (!get_geom(*(GeometryObject**)ip1, &in1)) {
errstate = PGERR_NOT_A_GEOMETRY;
destroy_geom_arr(ctx, geom_arr, i - 1);
break;
}
if (in1 == NULL) {
// in case of a missing value: return NULL (None)
geom_arr[i] = NULL;
} else {
geom_arr[i] = func(ctx, in1);
// NULL means: exception, but for some functions it may also indicate a
// "missing value" (None) (GetExteriorRing, GEOSBoundaryAllTypes_r)
// So: check the last_error before setting error state
if ((geom_arr[i] == NULL) && (last_error[0] != 0)) {
errstate = PGERR_GEOS_EXCEPTION;
destroy_geom_arr(ctx, geom_arr, i - 1);
break;
}
}
}
GEOS_FINISH_THREADS;
// fill the numpy array with PyObjects while holding the GIL
if (errstate == PGERR_SUCCESS) {
geom_arr_to_npy(geom_arr, args[1], steps[1], dimensions[0]);
}
free(geom_arr);
}
static PyUFuncGenericFunction Y_Y_funcs[1] = {&Y_Y_func};
/* Define the geom -> no return value functions (Y) */
static char PrepareGeometryObject(void* ctx, GeometryObject* geom) {
if (geom->ptr_prepared == NULL) {
geom->ptr_prepared = (GEOSPreparedGeometry*)GEOSPrepare_r(ctx, geom->ptr);
if (geom->ptr_prepared == NULL) {
return PGERR_GEOS_EXCEPTION;
}
}
return PGERR_SUCCESS;
}
static char DestroyPreparedGeometryObject(void* ctx, GeometryObject* geom) {
if (geom->ptr_prepared != NULL) {
GEOSPreparedGeom_destroy_r(ctx, geom->ptr_prepared);
geom->ptr_prepared = NULL;
}
return PGERR_SUCCESS;
}
static void* prepare_data[1] = {PrepareGeometryObject};
static void* destroy_prepared_data[1] = {DestroyPreparedGeometryObject};
typedef char FuncPyGEOS_Y(void* ctx, GeometryObject* geom);
static char Y_dtypes[1] = {NPY_OBJECT};
static void Y_func(char** args, npy_intp* dimensions, npy_intp* steps, void* data) {
FuncPyGEOS_Y* func = (FuncPyGEOS_Y*)data;
GEOSGeometry* in1 = NULL;
GeometryObject* geom_obj = NULL;
GEOS_INIT;
NO_OUTPUT_LOOP {
geom_obj = *(GeometryObject**)ip1;
if (!get_geom(geom_obj, &in1)) {
errstate = PGERR_GEOS_EXCEPTION;
goto finish;
}
if (in1 != NULL) {
errstate = func(ctx, geom_obj);
if (errstate != PGERR_SUCCESS) {
goto finish;
}
}
}
finish:
GEOS_FINISH;
}
static PyUFuncGenericFunction Y_funcs[1] = {&Y_func};
/* Define the geom, double -> geom functions (Yd_Y) */
static void* GEOSInterpolateProtectEmpty_r(void* context, void* geom, double d) {
char errstate = geos_interpolate_checker(context, geom);
if (errstate == PGERR_SUCCESS) {
return GEOSInterpolate_r(context, geom, d);
} else if (errstate == PGERR_EMPTY_GEOMETRY) {
return GEOSGeom_createEmptyPoint_r(context);
} else {
return NULL;
}
}
static void* line_interpolate_point_data[1] = {GEOSInterpolateProtectEmpty_r};
static void* GEOSInterpolateNormalizedProtectEmpty_r(void* context, void* geom,
double d) {
char errstate = geos_interpolate_checker(context, geom);
if (errstate == PGERR_SUCCESS) {
return GEOSInterpolateNormalized_r(context, geom, d);
} else if (errstate == PGERR_EMPTY_GEOMETRY) {
return GEOSGeom_createEmptyPoint_r(context);
} else {
return NULL;
}
}
static void* line_interpolate_point_normalized_data[1] = {
GEOSInterpolateNormalizedProtectEmpty_r};
static void* simplify_data[1] = {GEOSSimplify_r};
static void* simplify_preserve_topology_data[1] = {GEOSTopologyPreserveSimplify_r};
static void* force_3d_data[1] = {PyGEOSForce3D};
#if GEOS_SINCE_3_9_0
static void* unary_union_prec_data[1] = {GEOSUnaryUnionPrec_r};
#endif
#if GEOS_SINCE_3_10_0
static void* segmentize_data[1] = {GEOSDensify_r};
#endif
typedef void* FuncGEOS_Yd_Y(void* context, void* a, double b);
static char Yd_Y_dtypes[3] = {NPY_OBJECT, NPY_DOUBLE, NPY_OBJECT};
static void Yd_Y_func(char** args, npy_intp* dimensions, npy_intp* steps, void* data) {
FuncGEOS_Yd_Y* func = (FuncGEOS_Yd_Y*)data;
GEOSGeometry* in1 = NULL;
GEOSGeometry** geom_arr;
CHECK_NO_INPLACE_OUTPUT(2);
// allocate a temporary array to store output GEOSGeometry objects
geom_arr = malloc(sizeof(void*) * dimensions[0]);
CHECK_ALLOC(geom_arr);
GEOS_INIT_THREADS;
BINARY_LOOP {
// get the geometry: return on error
if (!get_geom(*(GeometryObject**)ip1, &in1)) {
errstate = PGERR_NOT_A_GEOMETRY;
destroy_geom_arr(ctx, geom_arr, i - 1);
break;
}
double in2 = *(double*)ip2;
if ((in1 == NULL) || (npy_isnan(in2))) {
// in case of a missing value: return NULL (None)
geom_arr[i] = NULL;
} else {
geom_arr[i] = func(ctx, in1, in2);
if (geom_arr[i] == NULL) {
// Interpolate functions return NULL on PGERR_GEOMETRY_TYPE and on
// PGERR_GEOS_EXCEPTION. Distinguish these by the state of last_error.
errstate = last_error[0] == 0 ? PGERR_GEOMETRY_TYPE : PGERR_GEOS_EXCEPTION;
destroy_geom_arr(ctx, geom_arr, i - 1);
break;
}
}
}
GEOS_FINISH_THREADS;
// fill the numpy array with PyObjects while holding the GIL
if (errstate == PGERR_SUCCESS) {
geom_arr_to_npy(geom_arr, args[2], steps[2], dimensions[0]);
}
free(geom_arr);
}
static PyUFuncGenericFunction Yd_Y_funcs[1] = {&Yd_Y_func};
/* Define the geom, int -> geom functions (Yi_Y) */
/* We add bound and type checking to the various indexing functions */
static void* GetPointN(void* context, void* geom, int n) {
char typ = GEOSGeomTypeId_r(context, geom);
int size, i;
if ((typ != 1) && (typ != 2)) {
return NULL;
}
size = GEOSGeomGetNumPoints_r(context, geom);
if (size == -1) {
return NULL;
}
if (n < 0) {
/* Negative indexing: we get it for free */
i = size + n;
} else {
i = n;
}
if ((i < 0) || (i >= size)) {
/* Important, could give segfaults else */
return NULL;
}
return GEOSGeomGetPointN_r(context, geom, i);
}
static void* get_point_data[1] = {GetPointN};
static void* GetInteriorRingN(void* context, void* geom, int n) {
char typ = GEOSGeomTypeId_r(context, geom);
int size, i;
if (typ != 3) {
return NULL;
}
size = GEOSGetNumInteriorRings_r(context, geom);
if (size == -1) {
return NULL;
}
if (n < 0) {
/* Negative indexing: we get it for free */
i = size + n;
} else {
i = n;
}
if ((i < 0) || (i >= size)) {
/* Important, could give segfaults else */
return NULL;
}
void* ret = (void*)GEOSGetInteriorRingN_r(context, geom, i);
/* Create a copy of the obtained geometry */
if (ret != NULL) {
ret = GEOSGeom_clone_r(context, ret);
}
return ret;
}
static void* get_interior_ring_data[1] = {GetInteriorRingN};
static void* GetGeometryN(void* context, void* geom, int n) {
int size, i;
size = GEOSGetNumGeometries_r(context, geom);
if (size == -1) {
return NULL;
}
if (n < 0) {
/* Negative indexing: we get it for free */
i = size + n;
} else {
i = n;
}
if ((i < 0) || (i >= size)) {
/* Important, could give segfaults else */
return NULL;
}
void* ret = (void*)GEOSGetGeometryN_r(context, geom, i);
/* Create a copy of the obtained geometry */
if (ret != NULL) {
ret = GEOSGeom_clone_r(context, ret);
}
return ret;
}
static void* get_geometry_data[1] = {GetGeometryN};
/* the set srid funcion acts inplace */
static void* GEOSSetSRID_r_with_clone(void* context, void* geom, int srid) {
void* ret = GEOSGeom_clone_r(context, geom);
if (ret == NULL) {
return NULL;
}
GEOSSetSRID_r(context, ret, srid);
return ret;
}
static void* set_srid_data[1] = {GEOSSetSRID_r_with_clone};
typedef void* FuncGEOS_Yi_Y(void* context, void* a, int b);
static char Yi_Y_dtypes[3] = {NPY_OBJECT, NPY_INT, NPY_OBJECT};
static void Yi_Y_func(char** args, npy_intp* dimensions, npy_intp* steps, void* data) {
FuncGEOS_Yi_Y* func = (FuncGEOS_Yi_Y*)data;
GEOSGeometry* in1 = NULL;
GEOSGeometry** geom_arr;
CHECK_NO_INPLACE_OUTPUT(2);
// allocate a temporary array to store output GEOSGeometry objects
geom_arr = malloc(sizeof(void*) * dimensions[0]);
CHECK_ALLOC(geom_arr);
GEOS_INIT_THREADS;
BINARY_LOOP {
// get the geometry: return on error
if (!get_geom(*(GeometryObject**)ip1, &in1)) {
errstate = PGERR_NOT_A_GEOMETRY;
destroy_geom_arr(ctx, geom_arr, i - 1);
break;
}
int in2 = *(int*)ip2;
if (in1 == NULL) {
// in case of a missing value: return NULL (None)
geom_arr[i] = NULL;
} else {
geom_arr[i] = func(ctx, in1, in2);
// NULL means: exception, but for some functions it may also indicate a
// "missing value" (None) (GetPointN, GetInteriorRingN, GetGeometryN)
// So: check the last_error before setting error state
if ((geom_arr[i] == NULL) && (last_error[0] != 0)) {
errstate = PGERR_GEOS_EXCEPTION;
destroy_geom_arr(ctx, geom_arr, i - 1);
break;
}
}
}
GEOS_FINISH_THREADS;
// fill the numpy array with PyObjects while holding the GIL
if (errstate == PGERR_SUCCESS) {
geom_arr_to_npy(geom_arr, args[2], steps[2], dimensions[0]);
}
free(geom_arr);
}
static PyUFuncGenericFunction Yi_Y_funcs[1] = {&Yi_Y_func};
/* Define the geom, geom -> geom functions (YY_Y) */
static void* intersection_data[1] = {GEOSIntersection_r};
static void* difference_data[1] = {GEOSDifference_r};
static void* symmetric_difference_data[1] = {GEOSSymDifference_r};
static void* union_data[1] = {GEOSUnion_r};
static void* shared_paths_data[1] = {GEOSSharedPaths_r};
typedef void* FuncGEOS_YY_Y(void* context, void* a, void* b);
static char YY_Y_dtypes[3] = {NPY_OBJECT, NPY_OBJECT, NPY_OBJECT};
/* There are two inner loop functions for the YY_Y. This is because this
* pattern allows for ufunc.reduce.
* A reduce operation requires special attention, because we output the
* result in a temporary array. NumPy expects that the output array is
* filled during the inner loop, so that it can take the first input of
* the reduction operation to be the output array. Easiest to show by
* example:
* function called: out = intersection.reduce(in)
* initialization by numpy: out[0] = in[0]; in1 = out; in2[:] = in[:]
* first loop: out[0] = func(in1[0], in2[1]) [ = func(out[0], in2[1]) ]
* second loop: out[0] = func(in1[0], in2[2]) [ = func(out[0], in2[2]) ]
*/
static void YY_Y_func_reduce(char** args, npy_intp* dimensions, npy_intp* steps,
void* data) {
FuncGEOS_YY_Y* func = (FuncGEOS_YY_Y*)data;
GEOSGeometry *in1 = NULL, *in2 = NULL, *out = NULL;
// Whether to destroy a temporary intermediate value of `out`:
char do_destroy = 0;
GEOS_INIT_THREADS;
if (!get_geom(*(GeometryObject**)args[0], &out)) {
errstate = PGERR_NOT_A_GEOMETRY;
} else {
BINARY_LOOP {
// Get the geometry inputs; in1 from previous iteration, in2 from array
in1 = out;
if (!get_geom(*(GeometryObject**)ip2, &in2)) {
errstate = PGERR_NOT_A_GEOMETRY;
break;
}
/* Either (or both) in1 and in2 could be NULL (Python: None).
* Reduction operations should skip None values. We have 4 possible combinations:
*/
// 1. (not NULL, not NULL); run the GEOS function
if ((in1 != NULL) && (in2 != NULL)) {
out = func(ctx, in1, in2);
// Discard in1 if it was a temporary intermediate
if (do_destroy) {
GEOSGeom_destroy_r(ctx, in1);
}
// Mark the newly generated geometry as intermediate. Note: out will become in1.
do_destroy = 1;
// Break on error (we do this after discarding in1 to avoid memleaks)
if (out == NULL) {
errstate = PGERR_GEOS_EXCEPTION;
break;
}
}
// 2. (NULL, not NULL); When the first element of the reduction axis is None
else if ((in1 == NULL) && (in2 != NULL)) {
// Keep in2 as 'outcome' of the operation.
out = in2;
// Ensure that it will not be destroyed (it is owned by python)
do_destroy = 0;
}
// 3. (not NULL, NULL); When a None value is encountered after a not-None
// Don't do `out = in1`, as that is already the case.
// 4. (NULL, NULL); When we have not yet encountered any not-None
// Do nothing; out will remain NULL
}
}
GEOS_FINISH_THREADS;
// fill the numpy array with a single PyObject while holding the GIL
if (errstate == PGERR_SUCCESS) {
geom_arr_to_npy(&out, args[2], steps[2], 1);
}
}
static void YY_Y_func(char** args, npy_intp* dimensions, npy_intp* steps, void* data) {
// A reduce is characterized by multiple iterations (dimension[0] > 1) that
// are output on the same place in memory (steps[2] == 0).
if ((steps[2] == 0) && (dimensions[0] > 1)) {
if (args[0] == args[2]) {
YY_Y_func_reduce(args, dimensions, steps, data);
return;
} else {
// Fail if inputs do not have the expected structure.
PyErr_Format(PyExc_NotImplementedError,
"Unknown ufunc mode with args=[%p, %p, %p], steps=[%ld, %ld, %ld], "
"dimensions=[%ld].",
args[0], args[1], args[2], steps[0], steps[1], steps[2],
dimensions[0]);
return;
}
}
FuncGEOS_YY_Y* func = (FuncGEOS_YY_Y*)data;
GEOSGeometry *in1 = NULL, *in2 = NULL;
GEOSGeometry** geom_arr;
// allocate a temporary array to store output GEOSGeometry objects
geom_arr = malloc(sizeof(void*) * dimensions[0]);
CHECK_ALLOC(geom_arr);
GEOS_INIT_THREADS;
BINARY_LOOP {
// get the geometries: return on error
if (!get_geom(*(GeometryObject**)ip1, &in1) ||
!get_geom(*(GeometryObject**)ip2, &in2)) {
errstate = PGERR_NOT_A_GEOMETRY;
destroy_geom_arr(ctx, geom_arr, i - 1);
break;
}
if ((in1 == NULL) || (in2 == NULL)) {
// in case of a missing value: return NULL (None)
geom_arr[i] = NULL;
} else {
geom_arr[i] = func(ctx, in1, in2);
if (geom_arr[i] == NULL) {
errstate = PGERR_GEOS_EXCEPTION;
destroy_geom_arr(ctx, geom_arr, i - 1);
break;
}
}
}
GEOS_FINISH_THREADS;
// fill the numpy array with PyObjects while holding the GIL
if (errstate == PGERR_SUCCESS) {
geom_arr_to_npy(geom_arr, args[2], steps[2], dimensions[0]);
}
free(geom_arr);
}
static PyUFuncGenericFunction YY_Y_funcs[1] = {&YY_Y_func};
/* Define the geom -> double functions (Y_d) */
static int GetX(void* context, void* a, double* b) {
char typ = GEOSGeomTypeId_r(context, a);
if (typ != 0) {
*(double*)b = NPY_NAN;
return 1;
} else {
return GEOSGeomGetX_r(context, a, b);
}
}
static void* get_x_data[1] = {GetX};
static int GetY(void* context, void* a, double* b) {
char typ = GEOSGeomTypeId_r(context, a);
if (typ != 0) {
*(double*)b = NPY_NAN;
return 1;
} else {
return GEOSGeomGetY_r(context, a, b);
}
}
static void* get_y_data[1] = {GetY};
#if GEOS_SINCE_3_7_0
static int GetZ(void* context, void* a, double* b) {
char typ = GEOSGeomTypeId_r(context, a);
if (typ != 0) {
*(double*)b = NPY_NAN;
return 1;
} else {
return GEOSGeomGetZ_r(context, a, b);
}
}
static void* get_z_data[1] = {GetZ};
#endif
static void* area_data[1] = {GEOSArea_r};
static void* length_data[1] = {GEOSLength_r};
#if GEOS_SINCE_3_6_0
static int GetPrecision(void* context, void* a, double* b) {
// GEOS returns -1 on error; 0 indicates double precision; > 0 indicates a precision
// grid size was set for this geometry.
double out = GEOSGeom_getPrecision_r(context, a);
if (out == -1) {
return 0;
}
*(double*)b = out;
return 1;
}
static void* get_precision_data[1] = {GetPrecision};
static int MinimumClearance(void* context, void* a, double* b) {
// GEOSMinimumClearance deviates from the pattern of returning 0 on exception and 1 on
// success for functions that return an int (it follows pattern for boolean functions
// returning char 0/1 and 2 on exception)
int retcode = GEOSMinimumClearance_r(context, a, b);
if (retcode == 2) {
return 0;
} else {
return 1;
}
}
static void* minimum_clearance_data[1] = {MinimumClearance};
#endif
#if GEOS_SINCE_3_8_0
static int GEOSMinimumBoundingRadius(void* context, GEOSGeometry* geom, double* radius) {
GEOSGeometry* center = NULL;
GEOSGeometry* ret = GEOSMinimumBoundingCircle_r(context, geom, radius, ¢er);
if (ret == NULL) {
return 0; // exception code
}
GEOSGeom_destroy_r(context, center);
GEOSGeom_destroy_r(context, ret);
return 1; // success code
}
static void* minimum_bounding_radius_data[1] = {GEOSMinimumBoundingRadius};
#endif
typedef int FuncGEOS_Y_d(void* context, void* a, double* b);
static char Y_d_dtypes[2] = {NPY_OBJECT, NPY_DOUBLE};
static void Y_d_func(char** args, npy_intp* dimensions, npy_intp* steps, void* data) {
FuncGEOS_Y_d* func = (FuncGEOS_Y_d*)data;
GEOSGeometry* in1 = NULL;
GEOS_INIT_THREADS;
UNARY_LOOP {
/* get the geometry: return on error */
if (!get_geom(*(GeometryObject**)ip1, &in1)) {
errstate = PGERR_NOT_A_GEOMETRY;
goto finish;
}
if (in1 == NULL) {
*(double*)op1 = NPY_NAN;
} else {
/* let the GEOS function set op1; return on error */
if (func(ctx, in1, (npy_double*)op1) == 0) {
errstate = PGERR_GEOS_EXCEPTION;
goto finish;
}
}
}
finish:
GEOS_FINISH_THREADS;
}
static PyUFuncGenericFunction Y_d_funcs[1] = {&Y_d_func};
/* Define the geom -> int functions (Y_i) */
/* data values are GEOS func, GEOS error code, return value when input is None */
static void* get_type_id_func_tuple[3] = {GEOSGeomTypeId_r, (void*)-1, (void*)-1};
static void* get_type_id_data[1] = {get_type_id_func_tuple};
static void* get_dimensions_func_tuple[3] = {GEOSGeom_getDimensions_r, (void*)0,
(void*)-1};
static void* get_dimensions_data[1] = {get_dimensions_func_tuple};
static void* get_coordinate_dimension_func_tuple[3] = {GEOSGeom_getCoordinateDimension_r,
(void*)-1, (void*)-1};
static void* get_coordinate_dimension_data[1] = {get_coordinate_dimension_func_tuple};
static void* get_srid_func_tuple[3] = {GEOSGetSRID_r, (void*)0, (void*)-1};
static void* get_srid_data[1] = {get_srid_func_tuple};
static int GetNumPoints(void* context, void* geom, int n) {
char typ = GEOSGeomTypeId_r(context, geom);