Skip to content

Commit

Permalink
feat: add Item.intersects
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Oct 17, 2023
1 parent 5d98af5 commit 7562610
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion stac/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ schemars = ["dep:schemars"]

[dependencies]
chrono = "0.4"
geo = { version = "0.26", optional = true }
geo = { version = "0.26", optional = true, features = ["serde"] }
geojson = { version = "0.24", optional = true }
reqwest = { version = "0.11", optional = true, features = ["json", "blocking"] }
schemars = { version = "0.8", optional = true }
Expand Down
38 changes: 34 additions & 4 deletions stac/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,36 @@ impl Item {
Ok(())
}

/// 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 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<T>(&self, intersects: &T) -> Result<bool>
where
T: geo::Intersects<geo::Geometry>,
{
if let Some(geometry) = self.geometry.clone() {
let geometry: geo::Geometry = geometry.try_into()?;
Ok(intersects.intersects(&geometry))
} else {
Ok(false)
}
}

/// Returns true if this item's geometry intersects the provided bounding box.
///
/// TODO support three dimensional bounding boxes.
Expand All @@ -222,16 +252,16 @@ impl Item {
/// 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());
/// assert!(item.intersects_bbox(&bbox).unwrap());
/// ```
#[cfg(feature = "geo")]
pub fn intersects_bbox(&self, bbox: geo::Rect) -> Result<bool> {
pub fn intersects_bbox(&self, bbox: &geo::Rect) -> Result<bool> {
// TODO support three dimensional
use geo::Intersects;

if let Some(geometry) = self.geometry.clone() {
let geometry: geo::Geometry = geometry.try_into()?;
Ok(geometry.intersects(&bbox))
Ok(geometry.intersects(bbox))
} else {
Ok(false)
}
Expand Down Expand Up @@ -449,7 +479,7 @@ mod tests {
]))))
.unwrap();
assert!(item
.intersects_bbox(crate::geo::bbox(&vec![-106.0, 41.0, -105.0, 42.0]).unwrap())
.intersects_bbox(&crate::geo::bbox(&vec![-106.0, 41.0, -105.0, 42.0]).unwrap())
.unwrap());
}

Expand Down

0 comments on commit 7562610

Please sign in to comment.