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

Port transform trait from geo and add a mutable flavor #109

Merged
merged 4 commits into from
Feb 20, 2022
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ pkg_config = [ "proj-sys/pkg_config" ]
network = ["reqwest", "proj-sys/network"]

[dev-dependencies]
approx = "0.3"
approx = "0.4"
geo-types = { version = "0.7", features = ["approx"] }

[package.metadata.docs.rs]
features = [ "proj-sys/nobuild", "network", "geo-types" ]
Expand Down
2 changes: 1 addition & 1 deletion proj-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn build_from_source() -> Result<std::path::PathBuf, Box<dyn std::error::Error>>
config.define("BUILD_PROJSYNC", "OFF");
config.define("ENABLE_CURL", "OFF");

let enable_tiff = cfg!(feature="network");
let enable_tiff = cfg!(feature = "network");
if enable_tiff {
eprintln!("enabling tiff support");
config.define("ENABLE_TIFF", "ON");
Expand Down
342 changes: 342 additions & 0 deletions src/geo_types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use crate::{Proj, ProjError, Transform};
use geo_types::Geometry;

///```rust
/// # use approx::assert_relative_eq;
/// extern crate proj;
Expand Down Expand Up @@ -51,3 +54,342 @@ impl<T: crate::proj::CoordinateType> crate::Coord<T> for geo_types::Point<T> {
Self::new(x, y)
}
}

impl<T> Transform<T> for geo_types::Geometry<T>
where
T: crate::proj::CoordinateType,
{
type Output = Self;

fn transformed(&self, proj: &Proj) -> Result<Self, ProjError> {
let mut output = self.clone();
output.transform(proj)?;
Ok(output)
}

fn transform(&mut self, proj: &Proj) -> Result<(), ProjError> {
match self {
Geometry::Point(g) => g.transform(proj),
Geometry::Line(g) => g.transform(proj),
Geometry::LineString(g) => g.transform(proj),
Geometry::Polygon(g) => g.transform(proj),
Geometry::MultiPoint(g) => g.transform(proj),
Geometry::MultiLineString(g) => g.transform(proj),
Geometry::MultiPolygon(g) => g.transform(proj),
Geometry::GeometryCollection(g) => g.transform(proj),
Geometry::Rect(g) => g.transform(proj),
Geometry::Triangle(g) => g.transform(proj),
}
}
}

impl<T> Transform<T> for geo_types::Coordinate<T>
where
T: crate::proj::CoordinateType,
{
type Output = Self;

fn transformed(&self, proj: &Proj) -> Result<Self, ProjError> {
let mut output = *self;
output.transform(proj)?;
Ok(output)
}

fn transform(&mut self, proj: &Proj) -> Result<(), ProjError> {
*self = proj.convert(*self)?;
Ok(())
}
}

impl<T> Transform<T> for geo_types::Point<T>
where
T: crate::proj::CoordinateType,
{
type Output = Self;

fn transformed(&self, proj: &Proj) -> Result<Self, ProjError> {
let mut output = *self;
output.transform(proj)?;
Ok(output)
}

fn transform(&mut self, proj: &Proj) -> Result<(), ProjError> {
self.0.transform(proj)
}
}

impl<T> Transform<T> for geo_types::Line<T>
where
T: crate::proj::CoordinateType,
{
type Output = Self;

fn transformed(&self, proj: &Proj) -> Result<Self, ProjError> {
let mut output = *self;
output.transform(proj)?;
Ok(output)
}

fn transform(&mut self, proj: &Proj) -> Result<(), ProjError> {
self.start.transform(proj)?;
self.end.transform(proj)?;
Ok(())
}
}

impl<T> Transform<T> for geo_types::LineString<T>
where
T: crate::proj::CoordinateType,
{
type Output = Self;

fn transformed(&self, proj: &Proj) -> Result<Self, ProjError> {
let mut output = self.clone();
output.transform(proj)?;
Ok(output)
}

fn transform(&mut self, proj: &Proj) -> Result<(), ProjError> {
proj.convert_array(&mut self.0)?;
Ok(())
}
}

impl<T> Transform<T> for geo_types::Polygon<T>
where
T: crate::proj::CoordinateType,
{
type Output = Self;

fn transformed(&self, proj: &Proj) -> Result<Self, ProjError> {
let mut output = self.clone();
output.transform(proj)?;
Ok(output)
}

fn transform(&mut self, proj: &Proj) -> Result<(), ProjError> {
let mut exterior_result = Ok(());
self.exterior_mut(|exterior| {
exterior_result = exterior.transform(proj);
});
exterior_result?;

let mut interiors_result = Ok(());
self.interiors_mut(|interiors| {
interiors_result = interiors
.iter_mut()
.try_for_each(|interior| interior.transform(proj))
});
interiors_result?;

Ok(())
}
}

impl<T> Transform<T> for geo_types::MultiPoint<T>
where
T: crate::proj::CoordinateType,
{
type Output = Self;

fn transformed(&self, proj: &Proj) -> Result<Self, ProjError> {
let mut output = self.clone();
output.transform(proj)?;
Ok(output)
}

fn transform(&mut self, proj: &Proj) -> Result<(), ProjError> {
proj.convert_array(&mut self.0)?;
Ok(())
}
}

impl<T> Transform<T> for geo_types::MultiLineString<T>
where
T: crate::proj::CoordinateType,
{
type Output = Self;

fn transformed(&self, proj: &Proj) -> Result<Self, ProjError> {
let mut output = self.clone();
output.transform(proj)?;
Ok(output)
}

fn transform(&mut self, proj: &Proj) -> Result<(), ProjError> {
for line_string in &mut self.0 {
line_string.transform(proj)?;
}
Ok(())
}
}

impl<T> Transform<T> for geo_types::MultiPolygon<T>
where
T: crate::proj::CoordinateType,
{
type Output = Self;

fn transformed(&self, proj: &Proj) -> Result<Self, ProjError> {
let mut output = self.clone();
output.transform(proj)?;
Ok(output)
}

fn transform(&mut self, proj: &Proj) -> Result<(), ProjError> {
for polygon in &mut self.0 {
polygon.transform(proj)?;
}
Ok(())
}
}

impl<T> Transform<T> for geo_types::GeometryCollection<T>
where
T: crate::proj::CoordinateType,
{
type Output = Self;

fn transformed(&self, proj: &Proj) -> Result<Self, ProjError> {
let mut output = self.clone();
output.transform(proj)?;
Ok(output)
}

fn transform(&mut self, proj: &Proj) -> Result<(), ProjError> {
for geometry in &mut self.0 {
geometry.transform(proj)?;
}
Ok(())
}
}

impl<T> Transform<T> for geo_types::Rect<T>
where
T: crate::proj::CoordinateType,
{
type Output = Self;

fn transformed(&self, proj: &Proj) -> Result<Self, ProjError> {
let mut output = *self;
output.transform(proj)?;
Ok(output)
}

fn transform(&mut self, proj: &Proj) -> Result<(), ProjError> {
let a = self.min();
let b = self.max();
let new = geo_types::Rect::new(proj.convert(a)?, proj.convert(b)?);
*self = new;
Ok(())
}
}

impl<T> Transform<T> for geo_types::Triangle<T>
where
T: crate::proj::CoordinateType,
{
type Output = Self;

fn transformed(&self, proj: &Proj) -> Result<Self, ProjError> {
let mut output = *self;
output.transform(proj)?;
Ok(output)
}

fn transform(&mut self, proj: &Proj) -> Result<(), ProjError> {
self.0.transform(proj)?;
self.1.transform(proj)?;
self.2.transform(proj)?;
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use geo_types::{point, MultiPoint, Rect};

#[test]
fn test_point() {
let mut subject = point!(x: 4760096.421921f64, y: 3744293.729449f64);
subject
.transform_crs_to_crs("EPSG:2230", "EPSG:26946")
.unwrap();
let expected = point!(x: 1450880.29f64, y: 1141263.01f64);
assert_relative_eq!(subject, expected, epsilon = 0.2);
}

#[test]
fn test_rect() {
let mut subject = {
let point_a = point!(x: 4760096.421921f64, y: 3744293.729449f64);
let point_b = point!(x: 4760196.421921f64, y: 3744393.729449f64);
Rect::new(point_a, point_b)
};

subject
.transform_crs_to_crs("EPSG:2230", "EPSG:26946")
.unwrap();
let expected = {
let point_a = point!(x: 1450880.2910605022, y: 1141263.0111604782);
let point_b = point!(x: 1450910.771121464, y: 1141293.4912214363);
Rect::new(point_a, point_b)
};
assert_relative_eq!(subject, expected, epsilon = 0.2);
}

#[test]
fn test_multi_point() {
let mut subject = {
let point_a = point!(x: 4760096.421921f64, y: 3744293.729449f64);
let point_b = point!(x: 4760196.421921f64, y: 3744393.729449f64);
MultiPoint(vec![point_a, point_b])
};

subject
.transform_crs_to_crs("EPSG:2230", "EPSG:26946")
.unwrap();
let expected = {
let point_a = point!(x: 1450880.2910605022, y: 1141263.0111604782);
let point_b = point!(x: 1450910.771121464, y: 1141293.4912214363);
MultiPoint(vec![point_a, point_b])
};
assert_relative_eq!(subject, expected, epsilon = 0.2);
}

#[test]
fn test_geometry_collection() {
let mut subject = {
let multi_point = {
let point_a = point!(x: 4760096.421921f64, y: 3744293.729449f64);
let point_b = point!(x: 4760196.421921f64, y: 3744393.729449f64);
MultiPoint(vec![point_a, point_b])
};
let rect = {
let point_a = point!(x: 4760096.421921f64, y: 3744293.729449f64);
let point_b = point!(x: 4760196.421921f64, y: 3744393.729449f64);
Rect::new(point_a, point_b)
};
geo_types::GeometryCollection(vec![Geometry::from(multi_point), Geometry::from(rect)])
};

subject
.transform_crs_to_crs("EPSG:2230", "EPSG:26946")
.unwrap();
let expected = {
let multi_point = {
let point_a = point!(x: 1450880.2910605022, y: 1141263.0111604782);
let point_b = point!(x: 1450910.771121464, y: 1141293.4912214363);
MultiPoint(vec![point_a, point_b])
};
let rect = {
let point_a = point!(x: 1450880.2910605022, y: 1141263.0111604782);
let point_b = point!(x: 1450910.771121464, y: 1141293.4912214363);
Rect::new(point_a, point_b)
};
geo_types::GeometryCollection(vec![Geometry::from(multi_point), Geometry::from(rect)])
};
assert_relative_eq!(subject, expected, epsilon = 0.2);
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ mod geo_types;
extern crate approx;

mod proj;
mod transform;
pub use transform::{Transform, TransformError};

pub use crate::proj::Area;
pub use crate::proj::Coord;
Expand Down
3 changes: 1 addition & 2 deletions src/proj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use proj_sys::{
PJ_DIRECTION_PJ_INV, PJ_INFO, PJ_LPZT, PJ_XYZT,
};
use std::{
convert,
ffi,
convert, ffi,
fmt::{self, Debug},
str,
};
Expand Down
Loading