|
1 | 1 | use pyo3::prelude::*;
|
2 |
| -use std::borrow::Borrow; |
| 2 | +use std::{borrow::Borrow, convert::TryInto}; |
3 | 3 | use std::fs::File;
|
4 | 4 | use std::io;
|
5 | 5 | use std::path::Path;
|
6 | 6 |
|
7 |
| -use geo::{point, Contains, Geometry}; |
| 7 | +use geo::{point, Contains, Geometry, Polygon, MultiPolygon}; |
| 8 | +use rstar::RTree; |
8 | 9 | use numpy::{PyArray, PyReadonlyArrayDyn};
|
9 | 10 |
|
10 | 11 | pub static GSHHS_F: &str = "gshhs_f_-180.000000E-90.000000N180.000000E90.000000N.wkb.xz";
|
11 | 12 |
|
12 | 13 | #[pyclass]
|
13 | 14 | #[derive(Clone)]
|
14 | 15 | pub struct Gshhg {
|
15 |
| - geom: Geometry, |
| 16 | + geom: RTree<Polygon>, |
16 | 17 | }
|
17 | 18 |
|
18 | 19 | impl Gshhg {
|
19 | 20 | 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); |
20 | 25 | Ok(Gshhg { geom })
|
21 | 26 | }
|
22 | 27 |
|
@@ -60,15 +65,16 @@ impl Gshhg {
|
60 | 65 | let x = super::modulate_longitude(x);
|
61 | 66 | debug_assert!(x >= -180. && x <= 180.);
|
62 | 67 | 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) |
66 | 69 | }
|
67 | 70 |
|
68 | 71 | /// Same as `contains`, but does not check for bounds.
|
69 | 72 | pub(crate) fn contains_unchecked(&self, x: f64, y: f64) -> bool {
|
70 | 73 | 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) |
72 | 78 | }
|
73 | 79 |
|
74 | 80 | pub fn contains_many(
|
@@ -120,7 +126,7 @@ mod tests {
|
120 | 126 | }
|
121 | 127 |
|
122 | 128 | #[test]
|
123 |
| - fn test_load() { |
| 129 | + fn test_load_embedded() { |
124 | 130 | pyo3::prepare_freethreaded_python();
|
125 | 131 | Python::with_gil(|py| Gshhg::new(py)).unwrap();
|
126 | 132 | }
|
|
0 commit comments