forked from georust/geo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
718: Add new `Transform` algorithm for transforming a geometry with PROJ. r=lnicola a=frewsxcv - [x] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md). - [x] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users. - I will do this if we decide we want this - [x] Add entry to table of contents in `lib.rs` Co-authored-by: Corey Farwell <coreyf@rwell.org>
- Loading branch information
Showing
5 changed files
with
133 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
use std::{convert::TryInto, error::Error, fmt}; | ||
|
||
/// Transform a geometry using PROJ. | ||
pub trait Transform<T> { | ||
type Output; | ||
|
||
/// Transform a geometry. | ||
/// | ||
/// # Examples | ||
/// | ||
/// Transform a geometry using a PROJ string definition: | ||
/// | ||
/// ``` | ||
/// use geo::{self, prelude::*}; | ||
/// | ||
/// let point = geo::point!(x: -36.508f32, y: -54.2815f32); | ||
/// | ||
/// assert_eq!( | ||
/// point.transform("+proj=axisswap +order=2,1,3,4").unwrap(), | ||
/// geo::point!(x: -54.2815f32, y: -36.508f32) | ||
/// ); | ||
/// ``` | ||
/// | ||
/// Transform a geometry from one CRS to another CRS: | ||
/// | ||
/// ``` | ||
/// use geo::{self, prelude::*}; | ||
/// | ||
/// let point = geo::point!(x: -36.508f32, y: -54.2815f32); | ||
/// | ||
/// assert_eq!( | ||
/// point.transform(("EPSG:4326", "EPSG:3857")).unwrap(), | ||
/// geo::point!(x: -4064052.0f32, y: -7223650.5f32) | ||
/// ); | ||
/// ``` | ||
fn transform( | ||
&self, | ||
proj: impl TryInto<proj::Proj, Error = proj::ProjCreateError>, | ||
) -> Result<Self::Output, TransformError>; | ||
|
||
/// Transform a geometry from one CRS to another CRS. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use geo::{self, prelude::*}; | ||
/// | ||
/// let point: geo::Point<f32> = geo::point!(x: -36.508f32, y: -54.2815f32); | ||
/// | ||
/// assert_eq!( | ||
/// point.transform_crs_to_crs("EPSG:4326", "EPSG:3857").unwrap(), | ||
/// geo::point!(x: -4064052.0f32, y: -7223650.5f32) | ||
/// ); | ||
/// ``` | ||
fn transform_crs_to_crs( | ||
&self, | ||
source_crs: &str, | ||
target_crs: &str, | ||
) -> Result<Self::Output, TransformError>; | ||
} | ||
|
||
impl<T, G> Transform<T> for G | ||
where | ||
T: crate::CoordFloat, | ||
G: crate::algorithm::map_coords::TryMapCoords<T, T, proj::ProjError>, | ||
{ | ||
type Output = G::Output; | ||
|
||
fn transform( | ||
&self, | ||
proj: impl TryInto<proj::Proj, Error = proj::ProjCreateError>, | ||
) -> Result<Self::Output, TransformError> { | ||
let transformer: proj::Proj = proj.try_into()?; | ||
let result: Result<G::Output, proj::ProjError> = | ||
self.try_map_coords(|&(x, y)| transformer.convert((x, y))); | ||
Ok(result?) | ||
} | ||
|
||
fn transform_crs_to_crs( | ||
&self, | ||
source_crs: &str, | ||
target_crs: &str, | ||
) -> Result<Self::Output, TransformError> { | ||
self.transform((source_crs, target_crs)) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum TransformError { | ||
UnknownCrs, | ||
ProjCreateError(proj::ProjCreateError), | ||
ProjError(proj::ProjError), | ||
} | ||
|
||
impl From<proj::ProjError> for TransformError { | ||
fn from(e: proj::ProjError) -> Self { | ||
TransformError::ProjError(e) | ||
} | ||
} | ||
|
||
impl From<proj::ProjCreateError> for TransformError { | ||
fn from(e: proj::ProjCreateError) -> Self { | ||
TransformError::ProjCreateError(e) | ||
} | ||
} | ||
|
||
impl fmt::Display for TransformError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
TransformError::UnknownCrs => write!(f, "Unknown CRS"), | ||
TransformError::ProjCreateError(err) => err.fmt(f), | ||
TransformError::ProjError(err) => err.fmt(f), | ||
} | ||
} | ||
} | ||
|
||
impl Error for TransformError {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters