ST_Intersects(a, b) — do geometries A and B share any space?
(a: &Geometry, b: &Geometry)
| 15 | |
| 16 | /// ST_Intersects(a, b) — do geometries A and B share any space? |
| 17 | pub fn st_intersects(a: &Geometry, b: &Geometry) -> bool { |
| 18 | // Bbox pre-filter. |
| 19 | let a_bb = geometry_bbox(a); |
| 20 | let b_bb = geometry_bbox(b); |
| 21 | if !a_bb.intersects(&b_bb) { |
| 22 | return false; |
| 23 | } |
| 24 | |
| 25 | match (a, b) { |
| 26 | // Point–Point: identical within tolerance. |
| 27 | (Geometry::Point { coordinates: ca }, Geometry::Point { coordinates: cb }) => { |
| 28 | (ca[0] - cb[0]).abs() < 1e-12 && (ca[1] - cb[1]).abs() < 1e-12 |
| 29 | } |
| 30 | |
| 31 | // Point–LineString: point on any segment. |
| 32 | (Geometry::Point { coordinates: pt }, Geometry::LineString { coordinates: line }) |
| 33 | | (Geometry::LineString { coordinates: line }, Geometry::Point { coordinates: pt }) => { |
| 34 | point_on_ring_boundary(*pt, line) |
| 35 | } |
| 36 | |
| 37 | // Point–Polygon: point inside OR on boundary. |
| 38 | (Geometry::Point { coordinates: pt }, Geometry::Polygon { coordinates: rings }) |
| 39 | | (Geometry::Polygon { coordinates: rings }, Geometry::Point { coordinates: pt }) => { |
| 40 | point_intersects_polygon(*pt, rings) |
| 41 | } |
| 42 | |
| 43 | // LineString–LineString: any edge crossing or shared point. |
| 44 | (Geometry::LineString { coordinates: la }, Geometry::LineString { coordinates: lb }) => { |
| 45 | linestrings_intersect(la, lb) |
| 46 | } |
| 47 | |
| 48 | // LineString–Polygon: any edge crossing, or any line point inside polygon. |
| 49 | (Geometry::LineString { coordinates: line }, Geometry::Polygon { coordinates: rings }) |
| 50 | | (Geometry::Polygon { coordinates: rings }, Geometry::LineString { coordinates: line }) => { |
| 51 | linestring_intersects_polygon(line, rings) |
| 52 | } |
| 53 | |
| 54 | // Polygon–Polygon: edge crossing or one inside the other. |
| 55 | (Geometry::Polygon { coordinates: ra }, Geometry::Polygon { coordinates: rb }) => { |
| 56 | polygons_intersect(ra, rb) |
| 57 | } |
| 58 | |
| 59 | // Multi* types: any component intersects. |
| 60 | (Geometry::MultiPoint { coordinates }, other) |
| 61 | | (other, Geometry::MultiPoint { coordinates }) => coordinates |
| 62 | .iter() |
| 63 | .any(|pt| st_intersects(&Geometry::Point { coordinates: *pt }, other)), |
| 64 | |
| 65 | (Geometry::MultiLineString { coordinates }, other) |
| 66 | | (other, Geometry::MultiLineString { coordinates }) => coordinates.iter().any(|ls| { |
| 67 | st_intersects( |
| 68 | &Geometry::LineString { |
| 69 | coordinates: ls.clone(), |
| 70 | }, |
| 71 | other, |
| 72 | ) |
| 73 | }), |
| 74 |
no test coverage detected