logo
pub trait MapCoords<T, NT> {
    type Output;

    fn map_coords(
        &self,
        func: impl Fn(Coordinate<T>) -> Coordinate<NT> + Copy
    ) -> Self::Output
    where
        T: CoordNum,
        NT: CoordNum
; fn try_map_coords<E>(
        &self,
        func: impl Fn(Coordinate<T>) -> Result<Coordinate<NT>, E> + Copy
    ) -> Result<Self::Output, E>
    where
        T: CoordNum,
        NT: CoordNum
; }
Expand description

Map a function over all the coordinates in an object, returning a new one

Required Associated Types

Required Methods

Apply a function to all the coordinates in a geometric object, returning a new object.

Examples
use geo::MapCoords;
use geo::{Coordinate, Point};
use approx::assert_relative_eq;

let p1 = Point::new(10., 20.);
let p2 = p1.map_coords(|Coordinate { x, y }| Coordinate { x: x + 1000., y: y * 2. });

assert_relative_eq!(p2, Point::new(1010., 40.), epsilon = 1e-6);

Note that the input and output numeric types need not match.

For example, consider OpenStreetMap’s coordinate encoding scheme, which, to save space, encodes latitude/longitude as 32bit signed integers from the floating point values to six decimal places (eg. lat/lon * 1000000).


let SCALE_FACTOR: f64 = 1000000.0;
let floating_point_geom: Point<f64> = Point::new(10.15f64, 20.05f64);
let fixed_point_geom: Point<i32> = floating_point_geom.map_coords(|Coordinate { x, y }| {
    Coordinate { x: (x * SCALE_FACTOR) as i32, y: (y * SCALE_FACTOR) as i32 }
});

assert_eq!(fixed_point_geom.x(), 10150000);

If you want only to convert between numeric types (i32 -> f64) without further transformation, consider using Convert.

Map a fallible function over all the coordinates in a geometry, returning a Result

Examples
use approx::assert_relative_eq;
use geo::MapCoords;
use geo::{Coordinate, Point};

let p1 = Point::new(10., 20.);
let p2 = p1
    .try_map_coords(|Coordinate { x, y }| -> Result<_, std::convert::Infallible> {
        Ok(Coordinate { x: x + 1000., y: y * 2. })
    }).unwrap();

assert_relative_eq!(p2, Point::new(1010., 40.), epsilon = 1e-6);
Advanced Example: Geometry coordinate conversion using PROJ
use approx::assert_relative_eq;
// activate the [use-proj] feature in cargo.toml in order to access proj functions
use geo::{Coordinate, Point};
use geo::map_coords::MapCoords;
use proj::{Coord, Proj, ProjError};
// GeoJSON uses the WGS 84 coordinate system
let from = "EPSG:4326";
// The NAD83 / California zone 6 (ftUS) coordinate system
let to = "EPSG:2230";
let to_feet = Proj::new_known_crs(&from, &to, None).unwrap();
let transform = |c: Coordinate<f64>| -> Result<_, ProjError> {
    // proj can accept Point, Coordinate, Tuple, and array values, returning a Result
    let shifted = to_feet.convert(c)?;
    Ok(shifted)
};
// 👽
let usa_m = Point::new(-115.797615, 37.2647978);
let usa_ft = usa_m.try_map_coords(|coord| transform(coord)).unwrap();
assert_relative_eq!(6693625.67217475, usa_ft.x(), epsilon = 1e-6);
assert_relative_eq!(3497301.5918027186, usa_ft.y(), epsilon = 1e-6);

Implementors