Skip to content

Commit c192eb4

Browse files
committed
shapes: prepare multipolygon
1 parent f889969 commit c192eb4

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/shapes.rs

+24-15
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ use std::io;
44
use std::path::Path;
55
use std::{borrow::Borrow, convert::TryInto};
66

7-
use geo::{point, Relate, Contains, Geometry, MultiPolygon, Point, Polygon, PreparedGeometry};
7+
use geo::{point, Contains, Geometry, MultiPolygon, Point, Polygon, PreparedGeometry, Relate};
88
use numpy::{PyArray, PyReadonlyArrayDyn};
9-
use rstar::{PointDistance, RTree, RTreeObject, AABB, Envelope};
9+
use rstar::{Envelope, 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<PolW>,
16+
geom: PreparedGeometry<'static>,
1717
}
1818

1919
#[derive(Clone)]
@@ -63,11 +63,13 @@ impl PointDistance for PolW {
6363
impl Gshhg {
6464
pub fn from_geom(geom: Geometry) -> io::Result<Gshhg> {
6565
let geom: MultiPolygon = geom.try_into().unwrap();
66-
assert!(geom.0.len() > 10);
67-
let geoms = geom.0.into_iter().map(|p| PolW::from(p)).collect();
66+
// assert!(geom.0.len() > 10);
67+
// let geoms = geom.0.into_iter().map(|p| PolW::from(p)).collect();
6868

69-
let geom = RTree::bulk_load(geoms);
70-
Ok(Gshhg { geom })
69+
// let geom = RTree::bulk_load(geoms);
70+
Ok(Gshhg {
71+
geom: PreparedGeometry::from(geom),
72+
})
7173
}
7274

7375
pub fn from_compressed<P: AsRef<Path>>(path: P) -> io::Result<Gshhg> {
@@ -83,21 +85,26 @@ impl Gshhg {
8385
let geom = wkb::wkb_to_geom(&mut fd).unwrap();
8486
Ok(geom)
8587
}
86-
}
8788

88-
#[pymethods]
89-
impl Gshhg {
90-
/// Make a new Gshhg shapes instance.
91-
#[staticmethod]
92-
pub fn new(_py: Python) -> io::Result<Self> {
89+
pub fn geom_from_embedded() -> io::Result<Geometry> {
9390
use crate::GsshgData;
9491

9592
let buf = GsshgData::get(&GSHHS_F)
9693
.ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "cannot find shapes"))?;
9794
let buf: &[u8] = buf.data.borrow();
9895
let mut fd = xz2::read::XzDecoder::new(buf);
9996
let geom = wkb::wkb_to_geom(&mut fd).unwrap();
100-
Gshhg::from_geom(geom)
97+
98+
Ok(geom)
99+
}
100+
}
101+
102+
#[pymethods]
103+
impl Gshhg {
104+
/// Make a new Gshhg shapes instance.
105+
#[staticmethod]
106+
pub fn new(_py: Python) -> io::Result<Self> {
107+
Gshhg::from_geom(Gshhg::geom_from_embedded()?)
101108
}
102109

103110
/// Check if point (x, y) is on land.
@@ -116,7 +123,9 @@ impl Gshhg {
116123
/// Same as `contains`, but does not check for bounds.
117124
pub(crate) fn contains_unchecked(&self, x: f64, y: f64) -> bool {
118125
let p = point!(x: x, y: y);
119-
self.geom.locate_at_point(&p).is_some()
126+
// self.geom.locate_at_point(&p).is_some()
127+
// self.geom.relate(&p).is_contains()
128+
self.geom.relate(&p).is_covers()
120129
}
121130

122131
pub fn contains_many(

0 commit comments

Comments
 (0)