Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Item.intersects #202

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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