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
use geo::algorithm::bounding_rect::BoundingRect;
use geo::{Line, LineString, Point, Polygon, Rect, Triangle};
use rstar::RTree;

use crate::SplitGeoSeq;

type RTreeEnvelope = rstar::AABB<[f64; 2]>;

#[derive(Debug)]
pub struct FakeRegion {
    pub id: usize,
    pub bbox: RTreeEnvelope,
}

impl rstar::RTreeObject for FakeRegion {
    type Envelope = RTreeEnvelope;

    fn envelope(&self) -> Self::Envelope {
        self.bbox
    }
}

impl SplitGeoSeq {
    pub fn to_rtrees(&self, max_distance: f64) -> [RTree<FakeRegion>; 6] {
        // Why duplicate? Because bounding_rect isn't defined for
        // Point and for the geos it is defined for, it sometimes
        // gives you a Rect and sometimes Option<Rect>
        [
            RTree::bulk_load(
                self.geos
                    .points
                    .iter()
                    .enumerate()
                    .map(|(index, pt)| FakeRegion {
                        id: index,
                        bbox: cheap_buffer(pt.to_env(), max_distance),
                    })
                    .collect(),
            ),
            RTree::bulk_load(
                self.geos
                    .lines
                    .iter()
                    .enumerate()
                    .map(|(index, ln)| FakeRegion {
                        id: index,
                        bbox: cheap_buffer(ln.to_env(), max_distance),
                    })
                    .collect(),
            ),
            RTree::bulk_load(
                self.geos
                    .polys
                    .iter()
                    .enumerate()
                    .map(|(index, poly)| FakeRegion {
                        id: index,
                        bbox: cheap_buffer(poly.to_env(), max_distance),
                    })
                    .collect(),
            ),
            RTree::bulk_load(
                self.geos
                    .line_strings
                    .iter()
                    .enumerate()
                    .map(|(index, ls)| FakeRegion {
                        id: index,
                        bbox: cheap_buffer(ls.to_env(), max_distance),
                    })
                    .collect(),
            ),
            RTree::bulk_load(
                self.geos
                    .rects
                    .iter()
                    .enumerate()
                    .map(|(index, rect)| FakeRegion {
                        id: index,
                        bbox: cheap_buffer(rect.to_env(), max_distance),
                    })
                    .collect(),
            ),
            RTree::bulk_load(
                self.geos
                    .tris
                    .iter()
                    .enumerate()
                    .map(|(index, tri)| FakeRegion {
                        id: index,
                        bbox: cheap_buffer(tri.to_env(), max_distance),
                    })
                    .collect(),
            ),
        ]
    }
}

pub trait Envelope {
    fn to_env(&self) -> RTreeEnvelope;
}

impl Envelope for Point<f64> {
    fn to_env(&self) -> RTreeEnvelope {
        RTreeEnvelope::from_point([self.x(), self.y()])
    }
}

impl Envelope for Line<f64> {
    fn to_env(&self) -> RTreeEnvelope {
        let bounds = self.bounding_rect();
        RTreeEnvelope::from_corners(
            [bounds.min().x, bounds.min().y],
            [bounds.max().x, bounds.max().y],
        )
    }
}

impl Envelope for Polygon<f64> {
    fn to_env(&self) -> RTreeEnvelope {
        let bounds = self
            .bounding_rect()
            .expect("invalid bounding_rect for Polygon");
        RTreeEnvelope::from_corners(
            [bounds.min().x, bounds.min().y],
            [bounds.max().x, bounds.max().y],
        )
    }
}

impl Envelope for LineString<f64> {
    fn to_env(&self) -> RTreeEnvelope {
        let bounds = self
            .bounding_rect()
            .expect("invalid bounding_rect for LineString");
        RTreeEnvelope::from_corners(
            [bounds.min().x, bounds.min().y],
            [bounds.max().x, bounds.max().y],
        )
    }
}

impl Envelope for Rect<f64> {
    fn to_env(&self) -> RTreeEnvelope {
        RTreeEnvelope::from_corners([self.min().x, self.min().y], [self.max().x, self.max().y])
    }
}

impl Envelope for Triangle<f64> {
    fn to_env(&self) -> RTreeEnvelope {
        let bounds = self.bounding_rect();
        RTreeEnvelope::from_corners(
            [bounds.min().x, bounds.min().y],
            [bounds.max().x, bounds.max().y],
        )
    }
}

pub fn cheap_buffer(bbox: RTreeEnvelope, distance: f64) -> RTreeEnvelope {
    let lower = bbox.lower();
    let upper = bbox.upper();
    RTreeEnvelope::from_corners(
        [lower[0] - distance, lower[1] - distance],
        [upper[0] + distance, upper[1] + distance],
    )
}