Skip to content
Closed
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ network = ["reqwest", "proj-sys/network"]

[dev-dependencies]
approx = "0.3"
insta = "1.12.0"

[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
303 changes: 303 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};
use geo_types;

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

impl Proj {
///
/// Convert a [`geo_types::LineString`], using [`Self::convert_array()`] internally
///
pub fn convert_line_string<'a, T>(
&self,
line_string: &'a mut geo_types::LineString<T>,
) -> Result<&'a mut geo_types::LineString<T>, ProjError>
where
T: crate::proj::CoordinateType,
{
self.convert_array(&mut line_string.0)?;
Ok(line_string)
}

///
/// Convert a [`geo_types::Line`]'s points using [`Self::convert()`]
///
pub fn convert_line<T>(
&self,
line: &geo_types::Line<T>,
) -> Result<geo_types::Line<T>, ProjError>
where
T: crate::proj::CoordinateType,
{
Ok(geo_types::Line::new(
self.convert(line.start)?,
self.convert(line.end)?,
))
}

///
/// Convert a [`geo_types::Rect`]'s point using [`Self::convert()`]
///
pub fn convert_rect<T>(
&self,
rect: &geo_types::Rect<T>,
) -> Result<geo_types::Rect<T>, ProjError>
where
T: crate::proj::CoordinateType,
{
Ok(geo_types::Rect::new(
self.convert(rect.min())?,
self.convert(rect.max())?,
))
}

///
/// Convert a [`geo_types::Triangle`]'s point using [`Self::convert()`]
///
pub fn convert_triangle<T>(
&self,
triangle: &geo_types::Triangle<T>,
) -> Result<geo_types::Triangle<T>, ProjError>
where
T: crate::proj::CoordinateType,
{
Ok(geo_types::Triangle(
self.convert(triangle.0)?,
self.convert(triangle.1)?,
self.convert(triangle.2)?,
))
}

///
/// Convert a [`geo_types::Polygon`] exterior and interior using [`Self::convert_line_string()`]
///
pub fn convert_polygon<T>(
&self,
polygon: geo_types::Polygon<T>,
) -> Result<geo_types::Polygon<T>, ProjError>
where
T: crate::proj::CoordinateType,
{
let (mut exterior, mut interiors) = polygon.into_inner();
self.convert_line_string(&mut exterior)?;
for mut interior in interiors.iter_mut() {
self.convert_line_string(&mut interior)?;
}
Ok(geo_types::Polygon::new(exterior, interiors))
}

///
/// Convert a [`geo_types::MultiPolygon`] using [`Self::convert_polygon()`]
///
pub fn convert_multi_polygon<T>(
&self,
multi_polygon: geo_types::MultiPolygon<T>,
) -> Result<geo_types::MultiPolygon<T>, ProjError>
where
T: crate::proj::CoordinateType,
{
Ok(geo_types::MultiPolygon(
multi_polygon
.into_iter()
.map(|p| self.convert_polygon(p))
.collect::<Result<Vec<_>, _>>()?,
))
}

///
/// Convert [`geo_types::MultiPoint`] using [`Self::convert_array()`]
///
pub fn convert_multi_point<'a, T>(
&self,
multi_point: &'a mut geo_types::MultiPoint<T>,
) -> Result<&'a mut geo_types::MultiPoint<T>, ProjError>
where
T: crate::proj::CoordinateType,
{
self.convert_array(&mut multi_point.0)?;
Ok(multi_point)
}

///
/// Convert a [`geo_types::MultiLineString`] using [`Self::convert_line_string()`]
///
pub fn convert_multi_line_string<'a, T>(
&self,
multi_line_string: &'a mut geo_types::MultiLineString<T>,
) -> Result<&'a mut geo_types::MultiLineString<T>, ProjError>
where
T: crate::proj::CoordinateType,
{
for ls in multi_line_string.into_iter() {
self.convert_line_string(ls)?;
}
Ok(multi_line_string)
}

///
/// Convert a [`geo_types::Geometry`]
///
pub fn convert_geometry<T>(
&self,
geometry: geo_types::Geometry<T>,
) -> Result<geo_types::Geometry<T>, ProjError>
where
T: crate::proj::CoordinateType,
{
match geometry {
geo_types::Geometry::Point(p) => Ok(self.convert(p)?.into()),
geo_types::Geometry::Line(mut line) => {
let _ = self.convert_line(&mut line)?;
Ok(line.into())
}
geo_types::Geometry::LineString(mut ls) => {
self.convert_line_string(&mut ls)?;
Ok(ls.into())
}
geo_types::Geometry::Polygon(p) => Ok(self.convert_polygon(p)?.into()),
geo_types::Geometry::MultiPoint(mut multi_point) => {
self.convert_multi_point(&mut multi_point)?;
Ok(multi_point.into())
}
geo_types::Geometry::MultiLineString(mut multi_line_string) => {
self.convert_multi_line_string(&mut multi_line_string)?;
Ok(multi_line_string.into())
}
geo_types::Geometry::MultiPolygon(multi_polygon) => {
Ok(self.convert_multi_polygon(multi_polygon)?.into())
}
geo_types::Geometry::GeometryCollection(geometry_collection) => {
Ok(geo_types::Geometry::GeometryCollection(
self.convert_geometry_collection(geometry_collection)?,
))
}
geo_types::Geometry::Rect(rect) => Ok(self.convert_rect(&rect)?.into()),
geo_types::Geometry::Triangle(triangle) => Ok(self.convert_triangle(&triangle)?.into()),
}
}

///
/// Convert a [`geo_types::GeometryCollection`]
///
pub fn convert_geometry_collection<T>(
&self,
geometry_collection: geo_types::GeometryCollection<T>,
) -> Result<geo_types::GeometryCollection<T>, ProjError>
where
T: crate::proj::CoordinateType,
{
Ok(geo_types::GeometryCollection(
geometry_collection
.into_iter()
.map(|g| self.convert_geometry(g))
.collect::<Result<Vec<_>, _>>()?,
))
}
}

#[cfg(test)]
mod test {
use super::*;
use geo_types::{
line_string, point, polygon, GeometryCollection, LineString, MultiPolygon, Polygon, Rect,
Triangle,
};

fn proj() -> Proj {
let from = "+proj=lcc +lat_1=49 +lat_2=44 +lat_0=46.5 +lon_0=3 +x_0=700000 +y_0=6600000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs";
let to = "proj=longlat +datum=WGS84 +no_defs +type=crs";
Proj::new_known_crs(&from, &to, None).unwrap()
}

fn triangle() -> Triangle<f64> {
Triangle(
point!(x: 493903.77, y: 6771154.06).into(),
point!(x: 648773.74, y: 6863725.64).into(),
point!(x: 740697.71, y: 6745951.11).into(),
)
}

fn rect() -> Rect<f64> {
Rect::new(
point!(x: -378305.81, y: 6093283.21),
point!(x: 1212610.74, y: 7186901.68),
)
}

fn multi_polygon() -> MultiPolygon<f64> {
MultiPolygon(vec![
polygon(),
rect().to_polygon(),
triangle().to_polygon(),
])
}

fn line_string() -> LineString<f64> {
line_string![
(x: 617466.55, y: 6471839.66),
(x: 724549.17, y: 6557378.31),
(x: 806203.24, y: 6497115.20),
]
}

fn polygon() -> Polygon<f64> {
polygon!(
exterior: [
(x: 459684.42, y: 6902803.84),
(x: 1008457.04, y: 6842618.16),
(x: 1066542.47, y: 6313538.74),
(x: 349688.67, y: 6268474.77),
],
interiors: [
[
(x: 617466.55, y: 6471839.66),
(x: 724549.17, y: 6557378.31),
(x: 806203.24, y: 6497115.20),
],
],
)
}

#[test]
fn test_convert_line_string() {
let mut input = line_string();
let output = proj().convert_line_string(&mut input).unwrap();
insta::assert_debug_snapshot!(output);
}

#[test]
fn test_convert_polygon() {
let output = proj().convert_polygon(polygon()).unwrap();
insta::assert_debug_snapshot!(output);
}

#[test]
fn test_convert_rect() {
let output = proj().convert_rect(&rect()).unwrap();
insta::assert_debug_snapshot!(output);
}

#[test]
fn test_convert_triangle() {
let output = proj().convert_triangle(&triangle()).unwrap();
insta::assert_debug_snapshot!(output);
}

#[test]
fn test_convert_multi_polygon() {
let output = proj().convert_multi_polygon(multi_polygon()).unwrap();
insta::assert_debug_snapshot!(output);
}

#[test]
fn test_convert_geometry_collection() {
let multi_geometry = GeometryCollection(vec![
triangle().into(),
line_string().into(),
rect().into(),
polygon().into(),
multi_polygon().into(),
]);

let output = proj().convert_geometry_collection(multi_geometry).unwrap();
insta::assert_debug_snapshot!(output);
}
}
Loading