use std::error::Error;
use geo_types::Geometry;
use geos::Geom;
use crate::geo_types::ToGeos;
pub trait BooleanOp
where Self: Sized{
fn difference(&self, other: &Self) -> Result<Self, Box<dyn Error>>;
fn union(&self, other: &Self) -> Result<Self, Box<dyn Error>>;
fn intersection(&self, other: &Self) -> Result<Self, Box<dyn Error>>;
fn unary_union(&self) -> Result<Self, Box<dyn Error>>;
}
impl BooleanOp for Geometry<f64> {
fn difference(&self, other: &Self) -> Result<Self, Box<dyn Error>> {
let geos_self = self.to_geos()?;
let geos_other = other.to_geos()?;
Ok(Geometry::try_from(geos_self.difference(&geos_other)?)?)
}
fn union(&self, other: &Self) -> Result<Self, Box<dyn Error>> {
let geos_self = self.to_geos()?;
let geos_other = other.to_geos()?;
Ok(Geometry::try_from(geos_self.union(&geos_other)?)?)
}
fn intersection(&self, other: &Self) -> Result<Self, Box<dyn Error>> {
let geos_self = self.to_geos()?;
let geos_other = other.to_geos()?;
Ok(Geometry::try_from(geos_self.intersection(&geos_other)?)?)
}
fn unary_union(&self) -> Result<Self, Box<dyn Error>> {
let geos_self = self.to_geos()?;
Ok(Geometry::try_from(geos_self.unary_union()?)?)
}
}