logo
Expand description

Apply a function to all Coordinates of a Geometry.

Advanced Example: Fallible Geometry coordinate conversion using PROJ

// activate the [use-proj] feature in cargo.toml in order to access proj functions
use approx::assert_relative_eq;
use geo::{Coordinate, Point};
use geo::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);

Traits

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

Map a function over all the coordinates in an object in place

TryMapCoordsDeprecated

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