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 d1c2d57
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
8 changes: 8 additions & 0 deletions stac/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 14 additions & 12 deletions stac/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> {
// TODO support three dimensional
use geo::Intersects;

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(geometry.intersects(&bbox))
Ok(intersects.intersects(&geometry))
} else {
Ok(false)
}
Expand Down Expand Up @@ -441,15 +443,15 @@ 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![
-105.1, 41.1,
]))))
.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());
}

Expand Down

0 comments on commit d1c2d57

Please sign in to comment.