Skip to content

Commit 60bc592

Browse files
committed
wrap polygon and implement contains
1 parent b8e3a21 commit 60bc592

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

src/shapes.rs

+31-8
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,53 @@
11
use pyo3::prelude::*;
2-
use std::{borrow::Borrow, convert::TryInto};
32
use std::fs::File;
43
use std::io;
54
use std::path::Path;
5+
use std::{borrow::Borrow, convert::TryInto};
66

7-
use geo::{point, Contains, Geometry, Polygon, MultiPolygon};
8-
use rstar::RTree;
7+
use geo::{point, Contains, Geometry, MultiPolygon, Point, Polygon};
98
use numpy::{PyArray, PyReadonlyArrayDyn};
9+
use rstar::{PointDistance, RTree, RTreeObject, AABB};
1010

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

1313
#[pyclass]
1414
#[derive(Clone)]
1515
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+
}
1742
}
1843

1944
impl Gshhg {
2045
pub fn from_geom(geom: Geometry) -> io::Result<Gshhg> {
2146
let geom: MultiPolygon = geom.try_into().unwrap();
2247
assert!(geom.0.len() > 10);
48+
let geoms = geom.0.into_iter().map(|p| PolW(p)).collect();
2349

24-
let geom = RTree::bulk_load(geom.0);
50+
let geom = RTree::bulk_load(geoms);
2551
Ok(Gshhg { geom })
2652
}
2753

@@ -71,10 +97,7 @@ impl Gshhg {
7197
/// Same as `contains`, but does not check for bounds.
7298
pub(crate) fn contains_unchecked(&self, x: f64, y: f64) -> bool {
7399
let p = point!(x: x, y: y);
74-
// let po = self.geom.nearest_neighbor(&p);
75-
// true
76100
self.geom.locate_at_point(&p).is_some()
77-
// self.geom.contains(&p)
78101
}
79102

80103
pub fn contains_many(

0 commit comments

Comments
 (0)