logo
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use std::{error::Error, fmt};

use crate::{Proj, ProjError};

/// Transform a geometry using PROJ.
pub trait Transform<T> {
    type Output;

    /// Transform a Geometry by mutating it in place.
    ///
    #[cfg_attr(feature = "geo-types", doc = r##"
# Examples

Transform a geometry using a PROJ string definition:

```
use geo_types;
use proj::{Proj, Transform};
 # use approx::assert_relative_eq;

let mut point = geo_types::point!(x: -36.508f32, y: -54.2815f32);
let proj = Proj::new("+proj=axisswap +order=2,1,3,4").expect("invalid proj string");
 point.transform(&proj);

assert_relative_eq!(
    point,
    geo_types::point!(x: -54.2815f32, y: -36.508f32)
);
```
"##)]
    fn transform(&mut self, proj: &Proj) -> Result<(), ProjError>;

    /// Immutable flavor of [`Transform::transform`], which allocates a new geometry.
    ///
    #[cfg_attr(feature = "geo-types", doc = r##"
# Examples

Transform a geometry using a PROJ string definition:

```
use geo_types;
use proj::{Proj, Transform};
# use approx::assert_relative_eq;

let point = geo_types::point!(x: -36.508f32, y: -54.2815f32);
let proj = Proj::new("+proj=axisswap +order=2,1,3,4").expect("invalid proj string");

assert_relative_eq!(
    point.transformed(&proj).unwrap(),
    geo_types::point!(x: -54.2815f32, y: -36.508f32)
);

// original `point` is untouched
assert_relative_eq!(
    point,
    geo_types::point!(x: -36.508f32, y: -54.2815f32)
);
```
"##)]
    fn transformed(&self, proj: &Proj) -> Result<Self::Output, ProjError>;

    /// Transform a geometry from one CRS to another CRS by modifying it in place.
    ///
    #[cfg_attr(feature = "geo-types", doc = r##"
# Examples

```
# use approx::assert_relative_eq;
use proj::Transform;
use geo_types::{point, Point};

let mut point: Point<f32> = point!(x: -36.508f32, y: -54.2815f32);
point.transform_crs_to_crs("EPSG:4326", "EPSG:3857").unwrap();

assert_relative_eq!(point, point!(x: -4064052.0f32, y: -7223650.5f32));
```
"##)]
    fn transform_crs_to_crs(
        &mut self,
        source_crs: &str,
        target_crs: &str,
    ) -> Result<(), TransformError> {
        let proj = Proj::new_known_crs(source_crs, target_crs, None)?;
        Ok(self.transform(&proj)?)
    }

    /// Immutable flavor of [`Transform::transform_crs_to_crs`], which allocates a new geometry.
    ///
    #[cfg_attr(feature = "geo-types", doc = r##"
# Examples

```
# use approx::assert_relative_eq;
use proj::Transform;
use geo_types::{point, Point};

let mut point: Point<f32> = point!(x: -36.508f32, y: -54.2815f32);

assert_relative_eq!(
    point.transformed_crs_to_crs("EPSG:4326", "EPSG:3857").unwrap(),
    point!(x: -4064052.0f32, y: -7223650.5f32)
);
```
"##)]
    fn transformed_crs_to_crs(
        &self,
        source_crs: &str,
        target_crs: &str,
    ) -> Result<Self::Output, TransformError> {
        let proj = Proj::new_known_crs(source_crs, target_crs, None)?;
        Ok(self.transformed(&proj)?)
    }
}

#[derive(Debug)]
pub enum TransformError {
    ProjCreateError(crate::ProjCreateError),
    ProjError(crate::ProjError),
}

impl From<crate::ProjError> for TransformError {
    fn from(e: crate::ProjError) -> Self {
        TransformError::ProjError(e)
    }
}

impl From<crate::ProjCreateError> for TransformError {
    fn from(e: crate::ProjCreateError) -> Self {
        TransformError::ProjCreateError(e)
    }
}

impl fmt::Display for TransformError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TransformError::ProjCreateError(err) => err.fmt(f),
            TransformError::ProjError(err) => err.fmt(f),
        }
    }
}

impl Error for TransformError {}