geoarrow2/algorithm/
rstar.rs

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
use crate::array::*;
use arrow_array::OffsetSizeTrait;
use rstar::primitives::CachedEnvelope;

/// Construct an R-Tree from a geometry array.
pub trait RTree<'a> {
    /// The object type to store in the RTree.
    type RTreeObject: rstar::RTreeObject;

    /// Build an [`RTree`] spatial index containing this array's geometries.
    fn rstar_tree(&'a self) -> rstar::RTree<Self::RTreeObject>;
}

impl<'a> RTree<'a> for PointArray {
    type RTreeObject = crate::scalar::Point<'a>;

    fn rstar_tree(&'a self) -> rstar::RTree<Self::RTreeObject> {
        // Note: for points we don't memoize with CachedEnvelope
        rstar::RTree::bulk_load(self.iter().flatten().collect())
    }
}

impl<'a> RTree<'a> for RectArray {
    type RTreeObject = crate::scalar::Rect<'a>;

    fn rstar_tree(&'a self) -> rstar::RTree<Self::RTreeObject> {
        // Note: for rects we don't memoize with CachedEnvelope
        rstar::RTree::bulk_load(self.iter().flatten().collect())
    }
}

macro_rules! iter_cached_impl {
    ($type:ty, $scalar_type:ty) => {
        impl<'a, O: OffsetSizeTrait> RTree<'a> for $type {
            type RTreeObject = CachedEnvelope<$scalar_type>;

            fn rstar_tree(&'a self) -> rstar::RTree<Self::RTreeObject> {
                rstar::RTree::bulk_load(self.iter().flatten().map(CachedEnvelope::new).collect())
            }
        }
    };
}

iter_cached_impl!(LineStringArray<O>, crate::scalar::LineString<'a, O>);
iter_cached_impl!(PolygonArray<O>, crate::scalar::Polygon<'a, O>);
iter_cached_impl!(MultiPointArray<O>, crate::scalar::MultiPoint<'a, O>);
iter_cached_impl!(
    MultiLineStringArray<O>,
    crate::scalar::MultiLineString<'a, O>
);
iter_cached_impl!(MultiPolygonArray<O>, crate::scalar::MultiPolygon<'a, O>);
iter_cached_impl!(WKBArray<O>, crate::scalar::WKB<'a, O>);
iter_cached_impl!(MixedGeometryArray<O>, crate::scalar::Geometry<'a, O>);
iter_cached_impl!(
    GeometryCollectionArray<O>,
    crate::scalar::GeometryCollection<'a, O>
);