From d1c2d5791fc9cccd37219b3d19a34c60bc1e838b Mon Sep 17 00:00:00 2001 From: Pete Gadomski Date: Tue, 17 Oct 2023 16:56:19 -0600 Subject: [PATCH] feat: add Item.intersects --- stac/CHANGELOG.md | 8 ++++++++ stac/src/item.rs | 26 ++++++++++++++------------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/stac/CHANGELOG.md b/stac/CHANGELOG.md index f0864107..095bbce1 100644 --- a/stac/CHANGELOG.md +++ b/stac/CHANGELOG.md @@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] +### Added + +- `Item.intersects` ([#202](https://github.com/stac-utils/stac-rs/pull/202)) + +### Removed + +- `Item.intersects_bbox` ([#202](https://github.com/stac-utils/stac-rs/pull/202)) + ## [0.5.1] - 2023-09-14 ### Added diff --git a/stac/src/item.rs b/stac/src/item.rs index d86a60d1..a3ff5f21 100644 --- a/stac/src/item.rs +++ b/stac/src/item.rs @@ -209,29 +209,31 @@ impl Item { Ok(()) } - /// Returns true if this item's geometry intersects the provided bounding box. - /// - /// TODO support three dimensional bounding boxes. + /// Returns true if this item's geometry intersects the provided geojson geometry. /// /// # Examples /// /// ``` /// use stac::Item; /// use geojson::{Geometry, Value}; + /// use geo::{Rect, coord}; /// /// let mut item = Item::new("an-id"); /// item.set_geometry(Some(Geometry::new(Value::Point(vec![-105.1, 41.1])))); - /// let bbox = stac::geo::bbox(&vec![-106.0, 41.0, -105.0, 42.0]).unwrap(); - /// assert!(item.intersects_bbox(bbox).unwrap()); + /// let intersects = Rect::new( + /// coord! { x: -106.0, y: 40.0 }, + /// coord! { x: -105.0, y: 42.0 }, + /// ); + /// assert!(item.intersects(&intersects).unwrap()); /// ``` #[cfg(feature = "geo")] - pub fn intersects_bbox(&self, bbox: geo::Rect) -> Result { - // TODO support three dimensional - use geo::Intersects; - + pub fn intersects(&self, intersects: &T) -> Result + where + T: geo::Intersects, + { if let Some(geometry) = self.geometry.clone() { let geometry: geo::Geometry = geometry.try_into()?; - Ok(geometry.intersects(&bbox)) + Ok(intersects.intersects(&geometry)) } else { Ok(false) } @@ -441,7 +443,7 @@ mod tests { #[test] #[cfg(feature = "geo")] - fn insersects_bbox() { + fn insersects() { use geojson::Geometry; let mut item = Item::new("an-id"); item.set_geometry(Some(Geometry::new(geojson::Value::Point(vec![ @@ -449,7 +451,7 @@ mod tests { ])))) .unwrap(); assert!(item - .intersects_bbox(crate::geo::bbox(&vec![-106.0, 41.0, -105.0, 42.0]).unwrap()) + .intersects(&crate::geo::bbox(&vec![-106.0, 41.0, -105.0, 42.0]).unwrap()) .unwrap()); }