Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
frewsxcv committed Jul 24, 2020
1 parent c316806 commit eacfaa4
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 219 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -39,7 +39,7 @@ use geojson::{Feature, GeoJson, Geometry, Value};
use serde_json::{Map, to_value};

let geometry = Geometry::new(
Value::Point(vec![-120.66029,35.2812])
ValueBase::Point(vec![-120.66029,35.2812])
);

let mut properties = Map::new();
Expand Down
190 changes: 95 additions & 95 deletions src/conversion/from_geo_types.rs
Expand Up @@ -6,19 +6,19 @@ use num_traits::Float;
use std::convert::From;

#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))]
impl<'a, T> From<&'a geo_types::Point<T>> for geometry::Value
impl<'a, T, P: Position> From<&'a geo_types::Point<T>> for geometry::ValueBase<P>
where
T: Float,
{
fn from(point: &geo_types::Point<T>) -> Self {
let coords = create_point_type(point);

geometry::Value::Point(coords)
geometry::ValueBase::Point(coords)
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))]
impl<'a, T> From<&'a geo_types::MultiPoint<T>> for geometry::Value
impl<'a, T, P: Position> From<&'a geo_types::MultiPoint<T>> for geometry::ValueBase<P>
where
T: Float,
{
Expand All @@ -29,90 +29,90 @@ where
.map(|point| create_point_type(point))
.collect();

geometry::Value::MultiPoint(coords)
geometry::ValueBase::MultiPoint(coords)
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))]
impl<'a, T> From<&'a geo_types::LineString<T>> for geometry::Value
impl<'a, T, P: Position> From<&'a geo_types::LineString<T>> for geometry::ValueBase<P>
where
T: Float,
{
fn from(line_string: &geo_types::LineString<T>) -> Self {
let coords = create_line_string_type(line_string);

geometry::Value::LineString(coords)
geometry::ValueBase::LineString(coords)
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))]
impl<'a, T> From<&'a geo_types::MultiLineString<T>> for geometry::Value
impl<'a, T, P: Position> From<&'a geo_types::MultiLineString<T>> for geometry::ValueBase<P>
where
T: Float,
{
fn from(multi_line_string: &geo_types::MultiLineString<T>) -> Self {
let coords = create_multi_line_string_type(multi_line_string);

geometry::Value::MultiLineString(coords)
geometry::ValueBase::MultiLineString(coords)
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))]
impl<'a, T> From<&'a geo_types::Polygon<T>> for geometry::Value
impl<'a, T, P: Position> From<&'a geo_types::Polygon<T>> for geometry::ValueBase<P>
where
T: Float,
{
fn from(polygon: &geo_types::Polygon<T>) -> Self {
let coords = create_polygon_type(polygon);

geometry::Value::Polygon(coords)
geometry::ValueBase::Polygon(coords)
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))]
impl<'a, T> From<&'a geo_types::MultiPolygon<T>> for geometry::Value
impl<'a, T, P: Position> From<&'a geo_types::MultiPolygon<T>> for geometry::ValueBase<P>
where
T: Float,
{
fn from(multi_polygon: &geo_types::MultiPolygon<T>) -> Self {
let coords = create_multi_polygon_type(multi_polygon);

geometry::Value::MultiPolygon(coords)
geometry::ValueBase::MultiPolygon(coords)
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))]
impl<'a, T> From<&'a geo_types::GeometryCollection<T>> for geometry::Value
impl<'a, T, P: Position> From<&'a geo_types::GeometryCollection<T>> for geometry::ValueBase<P>
where
T: Float,
{
fn from(geometry_collection: &geo_types::GeometryCollection<T>) -> Self {
let coords = geometry_collection
.0
.iter()
.map(|geometry| geometry::Geometry::new(geometry::Value::from(geometry)))
.map(|geometry| geometry::GeometryBase::new(geometry::ValueBase::from(geometry)))
.collect();

geometry::Value::GeometryCollection(coords)
geometry::ValueBase::GeometryCollection(coords)
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))]
impl<'a, T> From<&'a geo_types::Geometry<T>> for geometry::Value
impl<'a, T, P: Position> From<&'a geo_types::Geometry<T>> for geometry::ValueBase<P>
where
T: Float,
{
fn from(geometry: &'a geo_types::Geometry<T>) -> Self {
match *geometry {
geo_types::Geometry::Point(ref point) => geometry::Value::from(point),
geo_types::Geometry::MultiPoint(ref multi_point) => geometry::Value::from(multi_point),
geo_types::Geometry::LineString(ref line_string) => geometry::Value::from(line_string),
geo_types::Geometry::Point(ref point) => geometry::ValueBase::from(point),
geo_types::Geometry::MultiPoint(ref multi_point) => geometry::ValueBase::from(multi_point),
geo_types::Geometry::LineString(ref line_string) => geometry::ValueBase::from(line_string),
geo_types::Geometry::MultiLineString(ref multi_line_string) => {
geometry::Value::from(multi_line_string)
geometry::ValueBase::from(multi_line_string)
}
geo_types::Geometry::Polygon(ref polygon) => geometry::Value::from(polygon),
geo_types::Geometry::Polygon(ref polygon) => geometry::ValueBase::from(polygon),
geo_types::Geometry::MultiPolygon(ref multi_polygon) => {
geometry::Value::from(multi_polygon)
geometry::ValueBase::from(multi_polygon)
}
_ => panic!("GeometryCollection not allowed"),
}
Expand Down Expand Up @@ -185,7 +185,7 @@ where

#[cfg(test)]
mod tests {
use crate::{Geometry, Value};
use crate::{GeometryBase, ValueBase};
use geo_types;
use geo_types::{
GeometryCollection, LineString, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon,
Expand All @@ -195,22 +195,22 @@ mod tests {
fn geo_point_conversion_test() {
// Test with f32 coordinates
let geo_point = Point::new(40.02f32, 116.34f32);
let geojson_point = Value::from(&geo_point);
let geojson_point = ValueBase::<(f64, f64)>::from(&geo_point);

if let Value::Point(c) = geojson_point {
assert_almost_eq!(geo_point.x(), c[0] as f32, 1e-6);
assert_almost_eq!(geo_point.y(), c[1] as f32, 1e-6);
if let ValueBase::Point(c) = geojson_point {
assert_almost_eq!(geo_point.x(), c.0 as f32, 1e-6);
assert_almost_eq!(geo_point.y(), c.1 as f32, 1e-6);
} else {
panic!("Not valid geometry {:?}", geojson_point);
}

// Test with f64 coordinates.
let geo_point = Point::new(40.02f64, 116.34f64);
let geojson_point = Value::from(&geo_point);
let geojson_point = ValueBase::<(f64, f64)>::from(&geo_point);

if let Value::Point(c) = geojson_point {
assert_almost_eq!(geo_point.x(), c[0], 1e-6);
assert_almost_eq!(geo_point.y(), c[1], 1e-6);
if let ValueBase::Point(c) = geojson_point {
assert_almost_eq!(geo_point.x(), c.0, 1e-6);
assert_almost_eq!(geo_point.y(), c.1, 1e-6);
} else {
panic!("Not valid geometry {:?}", geojson_point);
}
Expand All @@ -222,13 +222,13 @@ mod tests {
let p2 = Point::new(13.02f64, 24.34f64);

let geo_multi_point = MultiPoint(vec![p1, p2]);
let geojson_multi_point = Value::from(&geo_multi_point);
let geojson_multi_point = ValueBase::<(f64, f64)>::from(&geo_multi_point);

if let Value::MultiPoint(c) = geojson_multi_point {
assert_almost_eq!(p1.x(), c[0][0], 1e-6);
assert_almost_eq!(p1.y(), c[0][1], 1e-6);
assert_almost_eq!(p2.x(), c[1][0], 1e-6);
assert_almost_eq!(p2.y(), c[1][1], 1e-6);
if let ValueBase::MultiPoint(c) = geojson_multi_point {
assert_almost_eq!(p1.x(), c[0].0, 1e-6);
assert_almost_eq!(p1.y(), c[0].1, 1e-6);
assert_almost_eq!(p2.x(), c[1].0, 1e-6);
assert_almost_eq!(p2.y(), c[1].1, 1e-6);
} else {
panic!("Not valid geometry {:?}", geojson_multi_point);
}
Expand All @@ -240,13 +240,13 @@ mod tests {
let p2 = Point::new(13.02f64, 24.34f64);

let geo_line_string = LineString::from(vec![p1, p2]);
let geojson_line_point = Value::from(&geo_line_string);
let geojson_line_point = ValueBase::<(f64, f64)>::from(&geo_line_string);

if let Value::LineString(c) = geojson_line_point {
assert_almost_eq!(p1.x(), c[0][0], 1e-6);
assert_almost_eq!(p1.y(), c[0][1], 1e-6);
assert_almost_eq!(p2.x(), c[1][0], 1e-6);
assert_almost_eq!(p2.y(), c[1][1], 1e-6);
if let ValueBase::LineString(c) = geojson_line_point {
assert_almost_eq!(p1.x(), c[0].0, 1e-6);
assert_almost_eq!(p1.y(), c[0].1, 1e-6);
assert_almost_eq!(p2.x(), c[1].0, 1e-6);
assert_almost_eq!(p2.y(), c[1].1, 1e-6);
} else {
panic!("Not valid geometry {:?}", geojson_line_point);
}
Expand All @@ -263,17 +263,17 @@ mod tests {
let geo_line_string2 = LineString::from(vec![p3, p4]);

let geo_multi_line_string = MultiLineString(vec![geo_line_string1, geo_line_string2]);
let geojson_multi_line_point = Value::from(&geo_multi_line_string);

if let Value::MultiLineString(c) = geojson_multi_line_point {
assert_almost_eq!(p1.x(), c[0][0][0], 1e-6);
assert_almost_eq!(p1.y(), c[0][0][1], 1e-6);
assert_almost_eq!(p2.x(), c[0][1][0], 1e-6);
assert_almost_eq!(p2.y(), c[0][1][1], 1e-6);
assert_almost_eq!(p3.x(), c[1][0][0], 1e-6);
assert_almost_eq!(p3.y(), c[1][0][1], 1e-6);
assert_almost_eq!(p4.x(), c[1][1][0], 1e-6);
assert_almost_eq!(p4.y(), c[1][1][1], 1e-6);
let geojson_multi_line_point = ValueBase::<(f64, f64)>::from(&geo_multi_line_string);

if let ValueBase::MultiLineString(c) = geojson_multi_line_point {
assert_almost_eq!(p1.x(), c[0][0].0, 1e-6);
assert_almost_eq!(p1.y(), c[0][0].1, 1e-6);
assert_almost_eq!(p2.x(), c[0][1].0, 1e-6);
assert_almost_eq!(p2.y(), c[0][1].1, 1e-6);
assert_almost_eq!(p3.x(), c[1][0].0, 1e-6);
assert_almost_eq!(p3.y(), c[1][0].1, 1e-6);
assert_almost_eq!(p4.x(), c[1][1].0, 1e-6);
assert_almost_eq!(p4.y(), c[1][1].1, 1e-6);
} else {
panic!("Not valid geometry {:?}", geojson_multi_line_point);
}
Expand All @@ -292,21 +292,21 @@ mod tests {
let geo_line_string2 = LineString::from(vec![p4, p5, p6, p4]);

let geo_polygon = Polygon::new(geo_line_string1, vec![geo_line_string2]);
let geojson_polygon = Value::from(&geo_polygon);

if let Value::Polygon(c) = geojson_polygon {
assert_almost_eq!(p1.x(), c[0][0][0], 1e-6);
assert_almost_eq!(p1.y(), c[0][0][1], 1e-6);
assert_almost_eq!(p2.x(), c[0][1][0], 1e-6);
assert_almost_eq!(p2.y(), c[0][1][1], 1e-6);
assert_almost_eq!(p3.x(), c[0][2][0], 1e-6);
assert_almost_eq!(p3.y(), c[0][2][1], 1e-6);
assert_almost_eq!(p4.x(), c[1][0][0], 1e-6);
assert_almost_eq!(p4.y(), c[1][0][1], 1e-6);
assert_almost_eq!(p5.x(), c[1][1][0], 1e-6);
assert_almost_eq!(p5.y(), c[1][1][1], 1e-6);
assert_almost_eq!(p6.x(), c[1][2][0], 1e-6);
assert_almost_eq!(p6.y(), c[1][2][1], 1e-6);
let geojson_polygon = ValueBase::<(f64, f64)>::from(&geo_polygon);

if let ValueBase::Polygon(c) = geojson_polygon {
assert_almost_eq!(p1.x(), c[0][0].0, 1e-6);
assert_almost_eq!(p1.y(), c[0][0].1, 1e-6);
assert_almost_eq!(p2.x(), c[0][1].0, 1e-6);
assert_almost_eq!(p2.y(), c[0][1].1, 1e-6);
assert_almost_eq!(p3.x(), c[0][2].0, 1e-6);
assert_almost_eq!(p3.y(), c[0][2].1, 1e-6);
assert_almost_eq!(p4.x(), c[1][0].0, 1e-6);
assert_almost_eq!(p4.y(), c[1][0].1, 1e-6);
assert_almost_eq!(p5.x(), c[1][1].0, 1e-6);
assert_almost_eq!(p5.y(), c[1][1].1, 1e-6);
assert_almost_eq!(p6.x(), c[1][2].0, 1e-6);
assert_almost_eq!(p6.y(), c[1][2].1, 1e-6);
} else {
panic!("Not valid geometry {:?}", geojson_polygon);
}
Expand All @@ -327,21 +327,21 @@ mod tests {
let geo_polygon1 = Polygon::new(geo_line_string1, vec![]);
let geo_polygon2 = Polygon::new(geo_line_string2, vec![]);
let geo_multi_polygon = MultiPolygon(vec![geo_polygon1, geo_polygon2]);
let geojson_multi_polygon = Value::from(&geo_multi_polygon);

if let Value::MultiPolygon(c) = geojson_multi_polygon {
assert_almost_eq!(p1.x(), c[0][0][0][0], 1e-6);
assert_almost_eq!(p1.y(), c[0][0][0][1], 1e-6);
assert_almost_eq!(p2.x(), c[0][0][1][0], 1e-6);
assert_almost_eq!(p2.y(), c[0][0][1][1], 1e-6);
assert_almost_eq!(p3.x(), c[0][0][2][0], 1e-6);
assert_almost_eq!(p3.y(), c[0][0][2][1], 1e-6);
assert_almost_eq!(p4.x(), c[1][0][0][0], 1e-6);
assert_almost_eq!(p4.y(), c[1][0][0][1], 1e-6);
assert_almost_eq!(p5.x(), c[1][0][1][0], 1e-6);
assert_almost_eq!(p5.y(), c[1][0][1][1], 1e-6);
assert_almost_eq!(p6.x(), c[1][0][2][0], 1e-6);
assert_almost_eq!(p6.y(), c[1][0][2][1], 1e-6);
let geojson_multi_polygon = ValueBase::<(f64, f64)>::from(&geo_multi_polygon);

if let ValueBase::MultiPolygon(c) = geojson_multi_polygon {
assert_almost_eq!(p1.x(), c[0][0][0].0, 1e-6);
assert_almost_eq!(p1.y(), c[0][0][0].1, 1e-6);
assert_almost_eq!(p2.x(), c[0][0][1].0, 1e-6);
assert_almost_eq!(p2.y(), c[0][0][1].1, 1e-6);
assert_almost_eq!(p3.x(), c[0][0][2].0, 1e-6);
assert_almost_eq!(p3.y(), c[0][0][2].1, 1e-6);
assert_almost_eq!(p4.x(), c[1][0][0].0, 1e-6);
assert_almost_eq!(p4.y(), c[1][0][0].1, 1e-6);
assert_almost_eq!(p5.x(), c[1][0][1].0, 1e-6);
assert_almost_eq!(p5.y(), c[1][0][1].1, 1e-6);
assert_almost_eq!(p6.x(), c[1][0][2].0, 1e-6);
assert_almost_eq!(p6.y(), c[1][0][2].1, 1e-6);
} else {
panic!("Not valid geometry {:?}", geojson_multi_polygon);
}
Expand Down Expand Up @@ -369,17 +369,17 @@ mod tests {
geo_types::Geometry::MultiPolygon(geo_multi_polygon),
]);

let geojson_geometry_collection = Value::from(&geo_geometry_collection);

if let Value::GeometryCollection(geometries) = geojson_geometry_collection {
let geometry_type = |geometry: &Geometry| match geometry.value {
Value::Point(..) => "Point",
Value::MultiPoint(..) => "MultiPoint",
Value::LineString(..) => "LineString",
Value::MultiLineString(..) => "MultiLineString",
Value::Polygon(..) => "Polygon",
Value::MultiPolygon(..) => "MultiPolygon",
Value::GeometryCollection(..) => "GeometryCollection",
let geojson_geometry_collection = ValueBase::<(f64, f64)>::from(&geo_geometry_collection);

if let ValueBase::GeometryCollection(geometries) = geojson_geometry_collection {
let geometry_type = |geometry: &GeometryBase<(f64, f64)>| match geometry.value {
ValueBase::Point(..) => "Point",
ValueBase::MultiPoint(..) => "MultiPoint",
ValueBase::LineString(..) => "LineString",
ValueBase::MultiLineString(..) => "MultiLineString",
ValueBase::Polygon(..) => "Polygon",
ValueBase::MultiPolygon(..) => "MultiPolygon",
ValueBase::GeometryCollection(..) => "GeometryCollection",
};

assert_eq!(3, geometries.len());
Expand Down

0 comments on commit eacfaa4

Please sign in to comment.