|
1 | 1 | use pyo3::prelude::*;
|
2 |
| -use std::{borrow::Borrow, convert::TryInto}; |
3 | 2 | use std::fs::File;
|
4 | 3 | use std::io;
|
5 | 4 | use std::path::Path;
|
| 5 | +use std::{borrow::Borrow, convert::TryInto}; |
6 | 6 |
|
7 |
| -use geo::{point, Contains, Geometry, Polygon, MultiPolygon}; |
8 |
| -use rstar::RTree; |
| 7 | +use geo::{point, Contains, Geometry, MultiPolygon, Point, Polygon}; |
9 | 8 | use numpy::{PyArray, PyReadonlyArrayDyn};
|
| 9 | +use rstar::{PointDistance, RTree, RTreeObject, AABB}; |
10 | 10 |
|
11 | 11 | pub static GSHHS_F: &str = "gshhs_f_-180.000000E-90.000000N180.000000E90.000000N.wkb.xz";
|
12 | 12 |
|
13 | 13 | #[pyclass]
|
14 | 14 | #[derive(Clone)]
|
15 | 15 | pub struct Gshhg {
|
16 |
| - geom: RTree<Polygon>, |
| 16 | + geom: RTree<PolW>, |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Clone)] |
| 20 | +struct PolW(Polygon); |
| 21 | + |
| 22 | +impl RTreeObject for PolW { |
| 23 | + type Envelope = AABB<Point<f64>>; |
| 24 | + |
| 25 | + fn envelope(&self) -> Self::Envelope { |
| 26 | + self.0.envelope() |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +impl PointDistance for PolW { |
| 31 | + fn distance_2(&self, _point: &Point<f64>) -> f64 { |
| 32 | + panic!("this should only be used for contains, the distance will give the wrong answer"); |
| 33 | + } |
| 34 | + |
| 35 | + fn contains_point(&self, point: &Point<f64>) -> bool { |
| 36 | + self.0.contains(point) |
| 37 | + } |
| 38 | + |
| 39 | + fn distance_2_if_less_or_equal(&self, _point: &Point<f64>, _max_distance: f64) -> Option<f64> { |
| 40 | + panic!("this should only be used for contains, the distance will give the wrong answer"); |
| 41 | + } |
17 | 42 | }
|
18 | 43 |
|
19 | 44 | impl Gshhg {
|
20 | 45 | pub fn from_geom(geom: Geometry) -> io::Result<Gshhg> {
|
21 | 46 | let geom: MultiPolygon = geom.try_into().unwrap();
|
22 | 47 | assert!(geom.0.len() > 10);
|
| 48 | + let geoms = geom.0.into_iter().map(|p| PolW(p)).collect(); |
23 | 49 |
|
24 |
| - let geom = RTree::bulk_load(geom.0); |
| 50 | + let geom = RTree::bulk_load(geoms); |
25 | 51 | Ok(Gshhg { geom })
|
26 | 52 | }
|
27 | 53 |
|
@@ -71,10 +97,7 @@ impl Gshhg {
|
71 | 97 | /// Same as `contains`, but does not check for bounds.
|
72 | 98 | pub(crate) fn contains_unchecked(&self, x: f64, y: f64) -> bool {
|
73 | 99 | let p = point!(x: x, y: y);
|
74 |
| - // let po = self.geom.nearest_neighbor(&p); |
75 |
| - // true |
76 | 100 | self.geom.locate_at_point(&p).is_some()
|
77 |
| - // self.geom.contains(&p) |
78 | 101 | }
|
79 | 102 |
|
80 | 103 | pub fn contains_many(
|
|
0 commit comments