@@ -4,11 +4,13 @@ use std::io;
4
4
use std:: path:: Path ;
5
5
use std:: { borrow:: Borrow , convert:: TryInto } ;
6
6
7
- use geo:: { point, Contains , Geometry , MultiPolygon , Point , Polygon } ;
7
+ use geo:: { point, Contains , Geometry , MultiPolygon , Point , Polygon , PreparedGeometry , Relate } ;
8
8
use numpy:: { PyArray , PyReadonlyArrayDyn } ;
9
9
use rstar:: { PointDistance , RTree , RTreeObject , AABB } ;
10
+ use static_cell:: StaticCell ;
10
11
11
12
pub static GSHHS_F : & str = "gshhs_f_-180.000000E-90.000000N180.000000E90.000000N.wkb.xz" ;
13
+ static POLYS : StaticCell < Vec < Polygon > > = StaticCell :: new ( ) ;
12
14
13
15
#[ pyclass]
14
16
#[ derive( Clone ) ]
@@ -17,7 +19,7 @@ pub struct Gshhg {
17
19
}
18
20
19
21
#[ derive( Clone ) ]
20
- struct PolW ( Polygon ) ;
22
+ struct PolW ( PreparedGeometry < ' static > ) ;
21
23
22
24
impl RTreeObject for PolW {
23
25
type Envelope = AABB < Point < f64 > > ;
@@ -33,7 +35,7 @@ impl PointDistance for PolW {
33
35
}
34
36
35
37
fn contains_point ( & self , point : & Point < f64 > ) -> bool {
36
- self . 0 . contains ( point)
38
+ self . 0 . relate ( point) . is_contains ( )
37
39
}
38
40
39
41
fn distance_2_if_less_or_equal ( & self , _point : & Point < f64 > , _max_distance : f64 ) -> Option < f64 > {
@@ -45,7 +47,8 @@ impl Gshhg {
45
47
pub fn from_geom ( geom : Geometry ) -> io:: Result < Gshhg > {
46
48
let geom: MultiPolygon = geom. try_into ( ) . unwrap ( ) ;
47
49
assert ! ( geom. 0 . len( ) > 10 ) ;
48
- let geoms = geom. 0 . into_iter ( ) . map ( |p| PolW ( p) ) . collect ( ) ;
50
+ POLYS . init ( geom. into_iter ( ) . collect ( ) ) ;
51
+ let geoms = POLYS . into_iter ( ) . map ( |p| PolW ( PreparedGeometry :: from ( p) ) ) . collect ( ) ;
49
52
50
53
let geom = RTree :: bulk_load ( geoms) ;
51
54
Ok ( Gshhg { geom } )
0 commit comments