Skip to content

Commit b8e3a21

Browse files
committed
wip: use RTree
1 parent a6c9014 commit b8e3a21

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ rust-embed = "8"
2727
xz2 = "0.1"
2828
ndarray = { version = "0.15", features = [ "rayon" ] }
2929
wkb = "0.7.1"
30+
rstar = "0.12.0"
3031

3132
[dev-dependencies]
3233
rayon = "1"

src/shapes.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
use pyo3::prelude::*;
2-
use std::borrow::Borrow;
2+
use std::{borrow::Borrow, convert::TryInto};
33
use std::fs::File;
44
use std::io;
55
use std::path::Path;
66

7-
use geo::{point, Contains, Geometry};
7+
use geo::{point, Contains, Geometry, Polygon, MultiPolygon};
8+
use rstar::RTree;
89
use numpy::{PyArray, PyReadonlyArrayDyn};
910

1011
pub static GSHHS_F: &str = "gshhs_f_-180.000000E-90.000000N180.000000E90.000000N.wkb.xz";
1112

1213
#[pyclass]
1314
#[derive(Clone)]
1415
pub struct Gshhg {
15-
geom: Geometry,
16+
geom: RTree<Polygon>,
1617
}
1718

1819
impl Gshhg {
1920
pub fn from_geom(geom: Geometry) -> io::Result<Gshhg> {
21+
let geom: MultiPolygon = geom.try_into().unwrap();
22+
assert!(geom.0.len() > 10);
23+
24+
let geom = RTree::bulk_load(geom.0);
2025
Ok(Gshhg { geom })
2126
}
2227

@@ -60,15 +65,16 @@ impl Gshhg {
6065
let x = super::modulate_longitude(x);
6166
debug_assert!(x >= -180. && x <= 180.);
6267
assert!(y > -90. && y <= 90.);
63-
64-
let p = point!(x: x, y: y);
65-
self.geom.contains(&p)
68+
self.contains_unchecked(x, y)
6669
}
6770

6871
/// Same as `contains`, but does not check for bounds.
6972
pub(crate) fn contains_unchecked(&self, x: f64, y: f64) -> bool {
7073
let p = point!(x: x, y: y);
71-
self.geom.contains(&p)
74+
// let po = self.geom.nearest_neighbor(&p);
75+
// true
76+
self.geom.locate_at_point(&p).is_some()
77+
// self.geom.contains(&p)
7278
}
7379

7480
pub fn contains_many(
@@ -120,7 +126,7 @@ mod tests {
120126
}
121127

122128
#[test]
123-
fn test_load() {
129+
fn test_load_embedded() {
124130
pyo3::prepare_freethreaded_python();
125131
Python::with_gil(|py| Gshhg::new(py)).unwrap();
126132
}

0 commit comments

Comments
 (0)