-
Notifications
You must be signed in to change notification settings - Fork 43
/
strtree.c
1333 lines (1123 loc) · 40.7 KB
/
strtree.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 <float.h>
#include <numpy/npy_math.h>
#include <structmember.h>
#define NO_IMPORT_ARRAY
#define PY_ARRAY_UNIQUE_SYMBOL pygeos_ARRAY_API
#include <numpy/arrayobject.h>
#include <numpy/ndarraytypes.h>
#include <numpy/npy_3kcompat.h>
#include "coords.h"
#include "geos.h"
#include "kvec.h"
#include "pygeom.h"
#include "strtree.h"
#include "vector.h"
/* GEOS function that takes a prepared geometry and a regular geometry
* and returns bool value */
typedef char FuncGEOS_YpY_b(void* context, const GEOSPreparedGeometry* a,
const GEOSGeometry* b);
/* get predicate function based on ID. See strtree.py::BinaryPredicate for
* lookup table of id to function name */
FuncGEOS_YpY_b* get_predicate_func(int predicate_id) {
switch (predicate_id) {
case 1: { // intersects
return (FuncGEOS_YpY_b*)GEOSPreparedIntersects_r;
}
case 2: { // within
return (FuncGEOS_YpY_b*)GEOSPreparedWithin_r;
}
case 3: { // contains
return (FuncGEOS_YpY_b*)GEOSPreparedContains_r;
}
case 4: { // overlaps
return (FuncGEOS_YpY_b*)GEOSPreparedOverlaps_r;
}
case 5: { // crosses
return (FuncGEOS_YpY_b*)GEOSPreparedCrosses_r;
}
case 6: { // touches
return (FuncGEOS_YpY_b*)GEOSPreparedTouches_r;
}
case 7: { // covers
return (FuncGEOS_YpY_b*)GEOSPreparedCovers_r;
}
case 8: { // covered_by
return (FuncGEOS_YpY_b*)GEOSPreparedCoveredBy_r;
}
case 9: { // contains_properly
return (FuncGEOS_YpY_b*)GEOSPreparedContainsProperly_r;
}
default: { // unknown predicate
PyErr_SetString(PyExc_ValueError, "Invalid query predicate");
return NULL;
}
}
}
/* Calculate indices of tree geometries.
* This uses pointer offsets of geometries from the head of tree geometries to calculate
* corresponding indices.
*
* Parameters
* ----------
* tree_geoms: array of tree geometries
*
* arr: dynamic vector of addresses of geometries within tree geometries array
*/
static PyArrayObject* tree_geom_offsets_to_npy_arr(GeometryObject** tree_geoms,
tree_geom_vec_t* geoms) {
size_t i;
size_t size = kv_size(*geoms);
npy_intp geom_index;
char* head_ptr = (char*)tree_geoms; // head of tree geometry array
npy_intp dims[1] = {size};
// the following raises a compiler warning based on how the macro is defined
// in numpy. There doesn't appear to be anything we can do to avoid it.
PyArrayObject* result = (PyArrayObject*)PyArray_SimpleNew(1, dims, NPY_INTP);
if (result == NULL) {
PyErr_SetString(PyExc_RuntimeError, "could not allocate numpy array");
return NULL;
}
for (i = 0; i < size; i++) {
// Calculate index using offset of its address compared to head of tree geometries
geom_index =
(npy_intp)(((char*)kv_A(*geoms, i) - head_ptr) / sizeof(GeometryObject*));
// assign value into numpy array
*(npy_intp*)PyArray_GETPTR1(result, i) = geom_index;
}
return (PyArrayObject*)result;
}
static void STRtree_dealloc(STRtreeObject* self) {
size_t i;
// free the tree
if (self->ptr != NULL) {
GEOS_INIT;
GEOSSTRtree_destroy_r(ctx, self->ptr);
GEOS_FINISH;
}
// free the geometries
for (i = 0; i < self->_geoms_size; i++) {
Py_XDECREF(self->_geoms[i]);
}
free(self->_geoms);
// free the PyObject
Py_TYPE(self)->tp_free((PyObject*)self);
}
void dummy_query_callback(void* item, void* user_data) {}
static PyObject* STRtree_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
int node_capacity;
PyObject* arr;
void *tree, *ptr;
npy_intp n, i, counter = 0, count_indexed = 0;
GEOSGeometry* geom;
GeometryObject* obj;
GeometryObject** _geoms;
if (!PyArg_ParseTuple(args, "Oi", &arr, &node_capacity)) {
return NULL;
}
if (!PyArray_Check(arr)) {
PyErr_SetString(PyExc_TypeError, "Not an ndarray");
return NULL;
}
if (!PyArray_ISOBJECT((PyArrayObject*)arr)) {
PyErr_SetString(PyExc_TypeError, "Array should be of object dtype");
return NULL;
}
if (PyArray_NDIM((PyArrayObject*)arr) != 1) {
PyErr_SetString(PyExc_TypeError, "Array should be one dimensional");
return NULL;
}
GEOS_INIT;
tree = GEOSSTRtree_create_r(ctx, (size_t)node_capacity);
if (tree == NULL) {
errstate = PGERR_GEOS_EXCEPTION;
return NULL;
GEOS_FINISH;
}
n = PyArray_SIZE((PyArrayObject*)arr);
_geoms = (GeometryObject**)malloc(n * sizeof(GeometryObject*));
for (i = 0; i < n; i++) {
/* get the geometry */
ptr = PyArray_GETPTR1((PyArrayObject*)arr, i);
obj = *(GeometryObject**)ptr;
/* fail and cleanup incase obj was no geometry */
if (!get_geom(obj, &geom)) {
errstate = PGERR_NOT_A_GEOMETRY;
GEOSSTRtree_destroy_r(ctx, tree);
// free the geometries
for (i = 0; i < counter; i++) {
Py_XDECREF(_geoms[i]);
}
free(_geoms);
GEOS_FINISH;
return NULL;
}
/* If geometry is None or empty, do not add it to the tree or count.
* Set it as NULL for the internal geometries used for predicate tests.
*/
if (geom == NULL || GEOSisEmpty_r(ctx, geom)) {
_geoms[i] = NULL;
} else {
// NOTE: we must keep a reference to the GeometryObject added to the tree in order
// to avoid segfaults later. See: https://github.com/pygeos/pygeos/pull/100.
Py_INCREF(obj);
_geoms[i] = obj;
count_indexed++;
// Store the address of this geometry within _geoms array as the item data in the
// tree. This address is used to calculate the original index of the geometry in
// the input array.
// NOTE: the type of item data we store is GeometryObject**.
GEOSSTRtree_insert_r(ctx, tree, geom, &(_geoms[i]));
}
counter++;
}
// A dummy query to trigger the build of the tree (only if the tree is not empty)
if (count_indexed > 0) {
GEOSGeometry* dummy = create_point(ctx, 0.0, 0.0);
if (dummy == NULL) {
GEOSSTRtree_destroy_r(ctx, tree);
GEOS_FINISH;
return NULL;
}
GEOSSTRtree_query_r(ctx, tree, dummy, dummy_query_callback, NULL);
GEOSGeom_destroy_r(ctx, dummy);
}
STRtreeObject* self = (STRtreeObject*)type->tp_alloc(type, 0);
if (self == NULL) {
GEOSSTRtree_destroy_r(ctx, tree);
GEOS_FINISH;
return NULL;
}
GEOS_FINISH;
self->ptr = tree;
self->count = count_indexed;
self->_geoms_size = n;
self->_geoms = _geoms;
return (PyObject*)self;
}
/* Callback called by strtree_query with item data of each intersecting geometry
* and a dynamic vector to push that item onto.
*
* Item data is the address of that geometry within the tree geometries (_geoms) array.
*
* Parameters
* ----------
* item: index of intersected geometry in the tree
*
* user_data: pointer to dynamic vector
* */
void query_callback(void* item, void* user_data) {
kv_push(GeometryObject**, *(tree_geom_vec_t*)user_data, item);
}
/* Evaluate the predicate function against a prepared version of geom
* for each geometry in the tree specified by indexes in out_indexes.
* out_indexes is updated in place with the indexes of the geometries in the
* tree that meet the predicate.
*
* Parameters
* ----------
* predicate_func: pointer to a prepared predicate function, e.g.,
* GEOSPreparedIntersects_r
*
* geom: input geometry to prepare and test against each geometry in the tree specified by
* in_indexes.
*
* prepared_geom: input prepared geometry, only if previously created. If NULL, geom
* will be prepared instead.
*
* in_geoms: pointer to dynamic vector of addresses in tree geometries (_geoms) that have
* overlapping envelopes with envelope of input geometry.
*
* out_geoms: pointer to dynamic vector of addresses in tree geometries (_geoms) that meet
* predicate function.
*
* count: pointer to an integer where the number of geometries that met the predicate will
* be written.
*
* Returns PGERR_GEOS_EXCEPTION if an error was encountered or PGERR_SUCCESS otherwise
* */
static char evaluate_predicate(void* context, FuncGEOS_YpY_b* predicate_func,
GEOSGeometry* geom, GEOSPreparedGeometry* prepared_geom,
tree_geom_vec_t* in_geoms, tree_geom_vec_t* out_geoms,
npy_intp* count) {
char errstate = PGERR_SUCCESS;
GeometryObject* pg_geom;
GeometryObject** pg_geom_loc; // address of geometry in tree geometries (_geoms)
GEOSGeometry* target_geom;
const GEOSPreparedGeometry* prepared_geom_tmp;
npy_intp i, size;
char predicate_result;
if (prepared_geom == NULL) {
// geom was not previously prepared, prepare it now
prepared_geom_tmp = GEOSPrepare_r(context, geom);
if (prepared_geom_tmp == NULL) {
return PGERR_GEOS_EXCEPTION;
}
} else {
// cast to const only needed until larger refactor of all geom pointers to const
prepared_geom_tmp = (const GEOSPreparedGeometry*)prepared_geom;
}
size = kv_size(*in_geoms);
*count = 0;
for (i = 0; i < size; i++) {
// get address of geometry in tree geometries, then use that to get associated
// GEOS geometry
pg_geom_loc = kv_A(*in_geoms, i);
pg_geom = *pg_geom_loc;
if (pg_geom == NULL) {
continue;
}
get_geom(pg_geom, &target_geom);
// keep the geometry if it passes the predicate
predicate_result = predicate_func(context, prepared_geom_tmp, target_geom);
if (predicate_result == 2) {
// error evaluating predicate; break and cleanup prepared geometry below
errstate = PGERR_GEOS_EXCEPTION;
break;
} else if (predicate_result == 1) {
kv_push(GeometryObject**, *out_geoms, pg_geom_loc);
(*count)++;
}
}
if (prepared_geom == NULL) {
// only if we created prepared_geom_tmp here, destroy it
GEOSPreparedGeom_destroy_r(context, prepared_geom_tmp);
prepared_geom_tmp = NULL;
}
return errstate;
}
/* Query the tree based on input geometry and predicate function.
* The index of each geometry in the tree whose envelope intersects the
* envelope of the input geometry is returned by default.
* If predicate function is provided, only the index of those geometries that
* satisfy the predicate function are returned.
*
* args must be:
* - pygeos geometry object
* - predicate id (see strtree.py for list of ids)
* */
static PyObject* STRtree_query(STRtreeObject* self, PyObject* args) {
GeometryObject* geometry = NULL;
int predicate_id = 0; // default no predicate
GEOSGeometry* geom = NULL;
GEOSPreparedGeometry* prepared_geom = NULL;
npy_intp count;
FuncGEOS_YpY_b* predicate_func = NULL;
PyArrayObject* result;
// Addresses in tree geometries (_geoms) that match tree
tree_geom_vec_t query_geoms;
// Addresses in tree geometries (_geoms) that meet predicate (if present)
tree_geom_vec_t predicate_geoms;
if (self->ptr == NULL) {
PyErr_SetString(PyExc_RuntimeError, "Tree is uninitialized");
return NULL;
}
if (!PyArg_ParseTuple(args, "O!i", &GeometryType, &geometry, &predicate_id)) {
return NULL;
}
if (!get_geom_with_prepared(geometry, &geom, &prepared_geom)) {
PyErr_SetString(PyExc_TypeError, "Invalid geometry");
return NULL;
}
if (self->count == 0) {
npy_intp dims[1] = {0};
return PyArray_SimpleNew(1, dims, NPY_INTP);
}
if (predicate_id != 0) {
predicate_func = get_predicate_func(predicate_id);
if (predicate_func == NULL) {
return NULL;
}
}
GEOS_INIT;
// query the tree for addresses of tree geometries (_geoms) in the tree with
// envelopes that intersect the geometry.
kv_init(query_geoms);
if (geom != NULL && !GEOSisEmpty_r(ctx, geom)) {
GEOSSTRtree_query_r(ctx, self->ptr, geom, query_callback, &query_geoms);
}
if (predicate_id == 0 || kv_size(query_geoms) == 0) {
// No predicate function provided, return all geometry indexes from
// query. If array is empty, return an empty numpy array
result = tree_geom_offsets_to_npy_arr(self->_geoms, &query_geoms);
kv_destroy(query_geoms);
GEOS_FINISH;
return (PyObject*)result;
}
kv_init(predicate_geoms);
errstate = evaluate_predicate(ctx, predicate_func, geom, prepared_geom, &query_geoms,
&predicate_geoms, &count);
if (errstate != PGERR_SUCCESS) {
// error performing predicate
kv_destroy(query_geoms);
kv_destroy(predicate_geoms);
GEOS_FINISH;
return NULL;
}
// calculate indices of tree geometries and output to array
result = tree_geom_offsets_to_npy_arr(self->_geoms, &predicate_geoms);
kv_destroy(query_geoms);
kv_destroy(predicate_geoms);
GEOS_FINISH;
return (PyObject*)result;
}
/* Query the tree based on input geometries and predicate function.
* The index of each geometry in the tree whose envelope intersects the
* envelope of the input geometry is returned by default.
* If predicate function is provided, only the index of those geometries that
* satisfy the predicate function are returned.
* Returns two arrays of equal length: first is indexes of the source geometries
* and second is indexes of tree geometries that meet the above conditions.
*
* args must be:
* - ndarray of pygeos geometries
* - predicate id (see strtree.py for list of ids)
*
* */
static PyObject* STRtree_query_bulk(STRtreeObject* self, PyObject* args) {
PyObject* arr;
PyArrayObject* pg_geoms;
GeometryObject* pg_geom = NULL;
int predicate_id = 0; // default no predicate
GEOSGeometry* geom = NULL;
GEOSPreparedGeometry* prepared_geom = NULL;
index_vec_t src_indexes; // Indices of input geometries
npy_intp i, j, n, size, geom_index;
FuncGEOS_YpY_b* predicate_func = NULL;
char* head_ptr = (char*)self->_geoms;
PyArrayObject* result;
// Addresses in tree geometries (_geoms) that match tree
tree_geom_vec_t query_geoms;
// Aggregated addresses in tree geometries (_geoms) that also meet predicate (if
// present)
tree_geom_vec_t target_geoms;
if (self->ptr == NULL) {
PyErr_SetString(PyExc_RuntimeError, "Tree is uninitialized");
return NULL;
}
if (!PyArg_ParseTuple(args, "Oi", &arr, &predicate_id)) {
return NULL;
}
if (!PyArray_Check(arr)) {
PyErr_SetString(PyExc_TypeError, "Not an ndarray");
return NULL;
}
pg_geoms = (PyArrayObject*)arr;
if (!PyArray_ISOBJECT(pg_geoms)) {
PyErr_SetString(PyExc_TypeError, "Array should be of object dtype");
return NULL;
}
if (PyArray_NDIM(pg_geoms) != 1) {
PyErr_SetString(PyExc_TypeError, "Array should be one dimensional");
return NULL;
}
if (predicate_id != 0) {
predicate_func = get_predicate_func(predicate_id);
if (predicate_func == NULL) {
return NULL;
}
}
n = PyArray_SIZE(pg_geoms);
if (self->count == 0 || n == 0) {
npy_intp dims[2] = {2, 0};
return PyArray_SimpleNew(2, dims, NPY_INTP);
}
kv_init(src_indexes);
kv_init(target_geoms);
GEOS_INIT_THREADS;
for (i = 0; i < n; i++) {
// get pygeos geometry from input geometry array
pg_geom = *(GeometryObject**)PyArray_GETPTR1(pg_geoms, i);
if (!get_geom_with_prepared(pg_geom, &geom, &prepared_geom)) {
errstate = PGERR_NOT_A_GEOMETRY;
break;
}
if (geom == NULL || GEOSisEmpty_r(ctx, geom)) {
continue;
}
kv_init(query_geoms);
GEOSSTRtree_query_r(ctx, self->ptr, geom, query_callback, &query_geoms);
if (kv_size(query_geoms) == 0) {
// no target geoms in query window, skip this source geom
kv_destroy(query_geoms);
continue;
}
if (predicate_id == 0) {
// no predicate, push results directly onto target_geoms
size = kv_size(query_geoms);
for (j = 0; j < size; j++) {
// push index of source geometry onto src_indexes
kv_push(npy_intp, src_indexes, i);
// push geometry that matched tree onto target_geoms
kv_push(GeometryObject**, target_geoms, kv_A(query_geoms, j));
}
} else {
// Tree geometries that meet the predicate are pushed onto target_geoms
errstate = evaluate_predicate(ctx, predicate_func, geom, prepared_geom,
&query_geoms, &target_geoms, &size);
if (errstate != PGERR_SUCCESS) {
kv_destroy(query_geoms);
break;
}
for (j = 0; j < size; j++) {
kv_push(npy_intp, src_indexes, i);
}
}
kv_destroy(query_geoms);
}
GEOS_FINISH_THREADS;
if (errstate != PGERR_SUCCESS) {
kv_destroy(src_indexes);
kv_destroy(target_geoms);
return NULL;
}
size = kv_size(src_indexes);
npy_intp dims[2] = {2, size};
// the following raises a compiler warning based on how the macro is defined
// in numpy. There doesn't appear to be anything we can do to avoid it.
result = (PyArrayObject*)PyArray_SimpleNew(2, dims, NPY_INTP);
if (result == NULL) {
PyErr_SetString(PyExc_RuntimeError, "could not allocate numpy array");
kv_destroy(src_indexes);
kv_destroy(target_geoms);
return NULL;
}
for (i = 0; i < size; i++) {
// assign value into numpy arrays
*(npy_intp*)PyArray_GETPTR2(result, 0, i) = kv_A(src_indexes, i);
// Calculate index using offset of its address compared to head of _geoms
geom_index =
(npy_intp)(((char*)kv_A(target_geoms, i) - head_ptr) / sizeof(GeometryObject*));
*(npy_intp*)PyArray_GETPTR2(result, 1, i) = geom_index;
}
kv_destroy(src_indexes);
kv_destroy(target_geoms);
return (PyObject*)result;
}
/* Callback called by strtree_query with item data of each intersecting geometry
* and a counter to increment each time this function is called. Used to prescreen
* geometries for intersections with the tree.
*
* Parameters
* ----------
* item: address of intersected geometry in the tree geometries (_geoms) array.
*
* user_data: pointer to size_t counter incremented on every call to this function
* */
void prescreen_query_callback(void* item, void* user_data) { (*(size_t*)user_data)++; }
/* Calculate the distance between items in the tree and the src_geom.
* Note: this is only called by the tree after first evaluating the overlap between
* the the envelope of a tree node and the query geometry. It may not be called for
* all equidistant results.
*
* Parameters
* ----------
* item1: address of geometry in tree geometries (_geoms)
*
* item2: pointer to GEOSGeometry* of query geometry
*
* distance: pointer to distance that gets updated in this function
*
* userdata: GEOS context handle.
*
* Returns
* -------
* 0 on error (caller immediately exits and returns NULL); 1 on success
* */
int nearest_distance_callback(const void* item1, const void* item2, double* distance,
void* userdata) {
GEOSGeometry* tree_geom = NULL;
const GEOSContextHandle_t* ctx = (GEOSContextHandle_t*)userdata;
GeometryObject* tree_pg_geom = *((GeometryObject**)item1);
// Note: this is guarded for NULL during construction of tree; no need to check here.
get_geom(tree_pg_geom, &tree_geom);
// distance returns 1 on success, 0 on error
return GEOSDistance_r(*ctx, (GEOSGeometry*)item2, tree_geom, distance);
}
/* Calculate the distance between items in the tree and the src_geom.
* Note: this is only called by the tree after first evaluating the overlap between
* the the envelope of a tree node and the query geometry. It may not be called for
* all equidistant results.
*
* In order to force GEOS to check neighbors in adjacent tree nodes, a slight adjustment
* is added to the distance returned to the tree. Otherwise, the tree nearest neighbor
* algorithm terminates if the envelopes of adjacent tree nodes are not further than
* this distance, and not all equidistant or intersected neighbors are checked. The
* accurate distances are stored into the distance pairs in userdata.
*
* Parameters
* ----------
* item1: address of geometry in tree geometries (_geoms)
*
* item2: pointer to GEOSGeometry* of query geometry
*
* distance: pointer to distance that gets updated in this function
*
* userdata: instance of tree_nearest_userdata_t, includes vector to cache nearest
* distance pairs visited by this function, GEOS context handle, and minimum observed
* distance.
*
* Returns
* -------
* 0 on error (caller immediately exits and returns NULL); 1 on success
* */
int nearest_all_distance_callback(const void* item1, const void* item2, double* distance,
void* userdata) {
GEOSGeometry* tree_geom = NULL;
size_t pairs_size;
double calc_distance;
GeometryObject* tree_pg_geom = *((GeometryObject**)item1);
// Note: this is guarded for NULL during construction of tree; no need to check here.
get_geom(tree_pg_geom, &tree_geom);
tree_nearest_userdata_t* params = (tree_nearest_userdata_t*)userdata;
// distance returns 1 on success, 0 on error
if (GEOSDistance_r(params->ctx, (GEOSGeometry*)item2, tree_geom, &calc_distance) == 0) {
return 0;
}
// store any pairs that are smaller than the minimum distance observed so far
// and update min_distance
if (calc_distance <= params->min_distance) {
params->min_distance = calc_distance;
// if smaller than last item in vector, remove that item first
pairs_size = kv_size(*(params->dist_pairs));
if (pairs_size > 0 &&
calc_distance < (kv_A(*(params->dist_pairs), pairs_size - 1)).distance) {
kv_pop(*(params->dist_pairs));
}
tree_geom_dist_vec_item_t dist_pair =
(tree_geom_dist_vec_item_t){(GeometryObject**)item1, calc_distance};
kv_push(tree_geom_dist_vec_item_t, *(params->dist_pairs), dist_pair);
}
// Set distance for callback
// This adds a slight adjustment to force checking of adjacent tree nodes; otherwise
// they are skipped by the GEOS nearest neighbor algorithm check against bounds
// of adjacent nodes.
*distance = calc_distance + 1e-6;
return 1;
}
#if GEOS_SINCE_3_6_0
/* Find the nearest singular item in the tree to each input geometry.
* Returns indices of source array and tree items.
*
* If there are multiple equidistant or intersected items, only one is returned,
* based on whichever nearest tree item is visited first by GEOS.
*
* Returns a tuple of empty arrays of shape (2,0) if tree is empty.
*
*
* Returns
* -------
* ndarray of shape (2, n) with input indexes, tree indexes
* */
static PyObject* STRtree_nearest(STRtreeObject* self, PyObject* arr) {
PyArrayObject* pg_geoms;
GeometryObject* pg_geom = NULL;
GEOSGeometry* geom = NULL;
GeometryObject** nearest_result = NULL;
npy_intp i, n, size, geom_index;
char* head_ptr = (char*)self->_geoms;
PyArrayObject* result;
// Indices of input and tree geometries
index_vec_t src_indexes;
index_vec_t nearest_indexes;
if (self->ptr == NULL) {
PyErr_SetString(PyExc_RuntimeError, "Tree is uninitialized");
return NULL;
}
if (!PyArray_Check(arr)) {
PyErr_SetString(PyExc_TypeError, "Not an ndarray");
return NULL;
}
pg_geoms = (PyArrayObject*)arr;
if (!PyArray_ISOBJECT(pg_geoms)) {
PyErr_SetString(PyExc_TypeError, "Array should be of object dtype");
return NULL;
}
if (PyArray_NDIM(pg_geoms) != 1) {
PyErr_SetString(PyExc_TypeError, "Array should be one dimensional");
return NULL;
}
// If tree is empty, return empty arrays
if (self->count == 0) {
npy_intp index_dims[2] = {2, 0};
result = (PyArrayObject*)PyArray_SimpleNew(2, index_dims, NPY_INTP);
return (PyObject*)result;
}
n = PyArray_SIZE(pg_geoms);
// preallocate arrays to size of input array
kv_init(src_indexes);
kv_resize(npy_intp, src_indexes, n);
kv_init(nearest_indexes);
kv_resize(npy_intp, nearest_indexes, n);
GEOS_INIT_THREADS;
for (i = 0; i < n; i++) {
// get pygeos geometry from input geometry array
pg_geom = *(GeometryObject**)PyArray_GETPTR1(pg_geoms, i);
if (!get_geom(pg_geom, &geom)) {
errstate = PGERR_NOT_A_GEOMETRY;
break;
}
if (geom == NULL || GEOSisEmpty_r(ctx, geom)) {
continue;
}
// pass in ctx as userdata because we need it for distance calculation in
// the callback
nearest_result = (GeometryObject**)GEOSSTRtree_nearest_generic_r(
ctx, self->ptr, geom, geom, nearest_distance_callback, &ctx);
// GEOSSTRtree_nearest_r returns NULL on error
if (nearest_result == NULL) {
errstate = PGERR_GEOS_EXCEPTION;
break;
}
kv_push(npy_intp, src_indexes, i);
// Calculate index using offset of its address compared to head of _geoms
geom_index = (npy_intp)(((char*)nearest_result - head_ptr) / sizeof(GeometryObject*));
kv_push(npy_intp, nearest_indexes, geom_index);
}
GEOS_FINISH_THREADS;
if (errstate != PGERR_SUCCESS) {
kv_destroy(src_indexes);
kv_destroy(nearest_indexes);
return NULL;
}
size = kv_size(src_indexes);
// Create array of indexes
npy_intp index_dims[2] = {2, size};
result = (PyArrayObject*)PyArray_SimpleNew(2, index_dims, NPY_INTP);
if (result == NULL) {
PyErr_SetString(PyExc_RuntimeError, "could not allocate numpy array");
kv_destroy(src_indexes);
kv_destroy(nearest_indexes);
return NULL;
}
for (i = 0; i < size; i++) {
// assign value into numpy arrays
*(npy_intp*)PyArray_GETPTR2(result, 0, i) = kv_A(src_indexes, i);
*(npy_intp*)PyArray_GETPTR2(result, 1, i) = kv_A(nearest_indexes, i);
}
kv_destroy(src_indexes);
kv_destroy(nearest_indexes);
return (PyObject*)result;
}
/* Find the nearest item(s) in the tree to each input geometry.
* Returns indices of source array, tree items, and distance between them.
*
* If there are multiple equidistant or intersected items, all should be returned.
* Tree indexes are returned in the order they are visited, not necessarily in ascending
* order.
*
* Returns a tuple of empty arrays (shape (2,0), shape (0,)) if tree is empty.
*
*
* Returns
* -------
* tuple of ([arr indexes (shape n), tree indexes (shape n)], distances (shape n))
* */
static PyObject* STRtree_nearest_all(STRtreeObject* self, PyObject* args) {
PyObject* arr;
double max_distance = 0; // default of 0 indicates max_distance not set
int use_max_distance = 0; // flag for the above
PyArrayObject* pg_geoms;
GeometryObject* pg_geom = NULL;
GEOSGeometry* geom = NULL;
GEOSGeometry* envelope = NULL;
GeometryObject** nearest_result = NULL;
npy_intp i, n, size, geom_index;
size_t j, query_counter;
double xmin, ymin, xmax, ymax;
char* head_ptr = (char*)self->_geoms;
tree_nearest_userdata_t userdata;
double distance;
PyArrayObject* result_indexes; // array of [source index, tree index]
PyArrayObject* result_distances; // array of distances
PyObject* result; // tuple of (indexes array, distance array)
// Indices of input geometries
index_vec_t src_indexes;
// Pairs of addresses in tree geometries and distances; used in userdata
tree_geom_dist_vec_t dist_pairs;
// Addresses in tree geometries (_geoms) that match tree
tree_geom_vec_t nearest_geoms;
// Distances of nearest items
tree_dist_vec_t nearest_dist;
if (self->ptr == NULL) {
PyErr_SetString(PyExc_RuntimeError, "Tree is uninitialized");
return NULL;
}
if (!PyArg_ParseTuple(args, "Od", &arr, &max_distance)) {
return NULL;
}
if (max_distance > 0) {
use_max_distance = 1;
}
if (!PyArray_Check(arr)) {
PyErr_SetString(PyExc_TypeError, "Not an ndarray");
return NULL;
}
pg_geoms = (PyArrayObject*)arr;
if (!PyArray_ISOBJECT(pg_geoms)) {
PyErr_SetString(PyExc_TypeError, "Array should be of object dtype");
return NULL;
}
if (PyArray_NDIM(pg_geoms) != 1) {
PyErr_SetString(PyExc_TypeError, "Array should be one dimensional");
return NULL;
}
// If tree is empty, return empty arrays
if (self->count == 0) {
npy_intp index_dims[2] = {2, 0};
result_indexes = (PyArrayObject*)PyArray_SimpleNew(2, index_dims, NPY_INTP);
npy_intp distance_dims[1] = {0};
result_distances = (PyArrayObject*)PyArray_SimpleNew(1, distance_dims, NPY_DOUBLE);
result = PyTuple_New(2);
PyTuple_SET_ITEM(result, 0, (PyObject*)result_indexes);
PyTuple_SET_ITEM(result, 1, (PyObject*)result_distances);
return (PyObject*)result;
}
n = PyArray_SIZE(pg_geoms);
// preallocate arrays to size of input array; for a non-empty tree, there should be
// at least 1 nearest item per input geometry
kv_init(src_indexes);
kv_resize(npy_intp, src_indexes, n);
kv_init(nearest_geoms);
kv_resize(GeometryObject**, nearest_geoms, n);
kv_init(nearest_dist);
kv_resize(double, nearest_dist, n);
GEOS_INIT_THREADS;
// initialize userdata context and dist_pairs vector
userdata.ctx = ctx;
userdata.dist_pairs = &dist_pairs;
for (i = 0; i < n; i++) {
// get pygeos geometry from input geometry array
pg_geom = *(GeometryObject**)PyArray_GETPTR1(pg_geoms, i);
if (!get_geom(pg_geom, &geom)) {
errstate = PGERR_NOT_A_GEOMETRY;
break;
}
if (geom == NULL || GEOSisEmpty_r(ctx, geom)) {
continue;
}
if (use_max_distance) {
// if max_distance is defined, prescreen geometries using simple bbox expansion
if (get_bounds(ctx, geom, &xmin, &ymin, &xmax, &ymax) == 0) {
errstate = PGERR_GEOS_EXCEPTION;
break;
}
envelope = create_box(ctx, xmin - max_distance, ymin - max_distance,
xmax + max_distance, ymax + max_distance, 1);
if (envelope == NULL) {
errstate = PGERR_GEOS_EXCEPTION;
break;
}
query_counter = 0;
GEOSSTRtree_query_r(ctx, self->ptr, envelope, prescreen_query_callback,
&query_counter);
GEOSGeom_destroy_r(ctx, envelope);
if (query_counter == 0) {
// no features are within max_distance, skip distance calculations
continue;
}
}
if (errstate == PGERR_GEOS_EXCEPTION) {
// break outer loop
break;
}
// reset loop-dependent values of userdata
kv_init(dist_pairs);
userdata.min_distance = DBL_MAX;
nearest_result = (GeometryObject**)GEOSSTRtree_nearest_generic_r(
ctx, self->ptr, geom, geom, nearest_all_distance_callback, &userdata);
// GEOSSTRtree_nearest_generic_r returns NULL on error
if (nearest_result == NULL) {
errstate = PGERR_GEOS_EXCEPTION;
kv_destroy(dist_pairs);
break;
}
for (j = 0; j < kv_size(dist_pairs); j++) {
distance = kv_A(dist_pairs, j).distance;
// only keep entries from the smallest distances for this input geometry
// only keep entries within max_distance, if nonzero
// Note: there may be multiple equidistant or intersected tree items
if (distance <= userdata.min_distance &&
(!use_max_distance || distance <= max_distance)) {
kv_push(npy_intp, src_indexes, i);
kv_push(GeometryObject**, nearest_geoms, kv_A(dist_pairs, j).geom);
kv_push(double, nearest_dist, distance);
}
}
kv_destroy(dist_pairs);