Skip to content

Commit

Permalink
wkt adapted for 3d/m change in geo
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Mar 22, 2022
1 parent dbd80f2 commit e21865f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ keywords = ["geo", "geospatial", "wkt"]
edition = "2021"

[dependencies]
geo-types = { version = "0.7.3", optional = true }
geo-types = { git = "https://github.com/nyurik/geo.git", branch = "dim-no-wkt", optional = true }
num-traits = "0.2"
serde = { version = "1.0", default-features = false, optional = true }
thiserror = "1.0.23"
Expand Down
51 changes: 27 additions & 24 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ where
.map(geo_types::Coordinate::from)
.collect();

geo_types::LineString(coords)
geo_types::LineString::new(coords)
}
}

Expand All @@ -173,7 +173,7 @@ where
.map(geo_types::LineString::from)
.collect();

geo_types::MultiLineString(geo_line_strings)
geo_types::MultiLineString::new(geo_line_strings)
}
}

Expand All @@ -195,7 +195,7 @@ where
let mut iter = polygon.0.into_iter().map(geo_types::LineString::from);
match iter.next() {
Some(interior) => geo_types::Polygon::new(interior, iter.collect()),
None => geo_types::Polygon::new(geo_types::LineString(vec![]), vec![]),
None => geo_types::Polygon::new(geo_types::LineString::new(vec![]), vec![]),
}
}
}
Expand Down Expand Up @@ -224,7 +224,7 @@ where
.map(geo_types::Point::try_from)
.collect::<Result<Vec<_>, _>>()?;

Ok(geo_types::MultiPoint(points))
Ok(geo_types::MultiPoint::new(points))
}
}

Expand All @@ -249,7 +249,7 @@ where
.map(geo_types::Polygon::from)
.collect();

geo_types::MultiPolygon(geo_polygons)
geo_types::MultiPolygon::new(geo_polygons)
}
}

Expand Down Expand Up @@ -278,7 +278,7 @@ where
.map(Geometry::try_into)
.collect::<Result<_, _>>()?;

Ok(geo_types::GeometryCollection(geo_geometries))
Ok(geo_types::GeometryCollection::new_from(geo_geometries))
}
}

Expand All @@ -295,7 +295,7 @@ where
if g.0.is_some() {
geo_types::Point::try_from(g)?.into()
} else {
geo_types::MultiPoint(vec![]).into()
geo_types::MultiPoint::new(vec![]).into()
}
}
Geometry::LineString(g) => geo_types::Geometry::LineString(g.into()),
Expand Down Expand Up @@ -358,7 +358,7 @@ mod tests {
#[test]
fn convert_empty_linestring() {
let w_linestring = LineString(vec![]).as_item();
let g_linestring: geo_types::LineString<f64> = geo_types::LineString(vec![]);
let g_linestring: geo_types::LineString<f64> = geo_types::LineString::new(vec![]);
assert_eq!(
geo_types::Geometry::LineString(g_linestring),
w_linestring.try_into().unwrap()
Expand Down Expand Up @@ -393,7 +393,7 @@ mod tests {
fn convert_empty_polygon() {
let w_polygon = Polygon(vec![]).as_item();
let g_polygon: geo_types::Polygon<f64> =
geo_types::Polygon::new(geo_types::LineString(vec![]), vec![]);
geo_types::Polygon::new(geo_types::LineString::new(vec![]), vec![]);
assert_eq!(
geo_types::Geometry::Polygon(g_polygon),
w_polygon.try_into().unwrap()
Expand Down Expand Up @@ -470,7 +470,8 @@ mod tests {
#[test]
fn convert_empty_multilinestring() {
let w_multilinestring = MultiLineString(vec![]).as_item();
let g_multilinestring: geo_types::MultiLineString<f64> = geo_types::MultiLineString(vec![]);
let g_multilinestring: geo_types::MultiLineString<f64> =
geo_types::MultiLineString::new(vec![]);
assert_eq!(
geo_types::Geometry::MultiLineString(g_multilinestring),
w_multilinestring.try_into().unwrap()
Expand Down Expand Up @@ -510,10 +511,11 @@ mod tests {
]),
])
.as_item();
let g_multilinestring: geo_types::MultiLineString<f64> = geo_types::MultiLineString(vec![
vec![(10., 20.), (30., 40.)].into(),
vec![(50., 60.), (70., 80.)].into(),
]);
let g_multilinestring: geo_types::MultiLineString<f64> =
geo_types::MultiLineString::new(vec![
vec![(10., 20.), (30., 40.)].into(),
vec![(50., 60.), (70., 80.)].into(),
]);
assert_eq!(
geo_types::Geometry::MultiLineString(g_multilinestring),
w_multilinestring.try_into().unwrap()
Expand All @@ -523,7 +525,7 @@ mod tests {
#[test]
fn convert_empty_multipoint() {
let w_multipoint = MultiPoint(vec![]).as_item();
let g_multipoint: geo_types::MultiPoint<f64> = geo_types::MultiPoint(vec![]);
let g_multipoint: geo_types::MultiPoint<f64> = geo_types::MultiPoint::new(vec![]);
assert_eq!(
geo_types::Geometry::MultiPoint(g_multipoint),
w_multipoint.try_into().unwrap()
Expand Down Expand Up @@ -557,7 +559,7 @@ mod tests {
#[test]
fn convert_empty_multipolygon() {
let w_multipolygon = MultiPolygon(vec![]).as_item();
let g_multipolygon: geo_types::MultiPolygon<f64> = geo_types::MultiPolygon(vec![]);
let g_multipolygon: geo_types::MultiPolygon<f64> = geo_types::MultiPolygon::new(vec![]);
assert_eq!(
geo_types::Geometry::MultiPolygon(g_multipolygon),
w_multipolygon.try_into().unwrap()
Expand Down Expand Up @@ -650,7 +652,7 @@ mod tests {
])
.as_item();

let g_multipolygon: geo_types::MultiPolygon<f64> = geo_types::MultiPolygon(vec![
let g_multipolygon: geo_types::MultiPolygon<f64> = geo_types::MultiPolygon::new(vec![
geo_types::Polygon::new(
vec![(0., 0.), (20., 40.), (40., 0.), (0., 0.)].into(),
vec![vec![(5., 5.), (20., 30.), (30., 5.), (5., 5.)].into()],
Expand All @@ -670,7 +672,7 @@ mod tests {
fn convert_empty_geometrycollection() {
let w_geometrycollection = GeometryCollection(vec![]).as_item();
let g_geometrycollection: geo_types::GeometryCollection<f64> =
geo_types::GeometryCollection(vec![]);
geo_types::GeometryCollection::default();
assert_eq!(
geo_types::Geometry::GeometryCollection(g_geometrycollection),
w_geometrycollection.try_into().unwrap()
Expand Down Expand Up @@ -851,12 +853,13 @@ mod tests {
vec![(0., 0.), (20., 40.), (40., 0.), (0., 0.)].into(),
vec![],
);
let g_multilinestring: geo_types::MultiLineString<f64> = geo_types::MultiLineString(vec![
vec![(10., 20.), (30., 40.)].into(),
vec![(50., 60.), (70., 80.)].into(),
]);
let g_multilinestring: geo_types::MultiLineString<f64> =
geo_types::MultiLineString::new(vec![
vec![(10., 20.), (30., 40.)].into(),
vec![(50., 60.), (70., 80.)].into(),
]);
let g_multipoint: geo_types::MultiPoint<f64> = vec![(10., 20.), (30., 40.)].into();
let g_multipolygon: geo_types::MultiPolygon<f64> = geo_types::MultiPolygon(vec![
let g_multipolygon: geo_types::MultiPolygon<f64> = geo_types::MultiPolygon::new(vec![
geo_types::Polygon::new(
vec![(0., 0.), (20., 40.), (40., 0.), (0., 0.)].into(),
vec![],
Expand All @@ -868,7 +871,7 @@ mod tests {
]);

let g_geometrycollection: geo_types::GeometryCollection<f64> =
geo_types::GeometryCollection(vec![
geo_types::GeometryCollection::new_from(vec![
geo_types::Geometry::Point(g_point),
geo_types::Geometry::MultiPoint(g_multipoint),
geo_types::Geometry::LineString(g_linestring),
Expand Down
6 changes: 3 additions & 3 deletions src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ where
/// # extern crate wkt;
/// # extern crate geo_types;
/// # extern crate serde_json;
/// use geo_types::Point;
/// use geo_types::{Point, PointTZM};
///
/// #[derive(serde::Deserialize)]
/// struct MyType {
Expand All @@ -137,7 +137,7 @@ where
///
/// let json = r#"{ "geometry": "POINT (3.14 42)" }"#;
/// let my_type: MyType = serde_json::from_str(json).unwrap();
/// assert!(matches!(my_type.geometry, Some(Point(_))));
/// assert!(matches!(my_type.geometry, Some(PointTZM(_))));
///
/// let json = r#"{ "geometry": "POINT EMPTY" }"#;
/// let my_type: MyType = serde_json::from_str(json).unwrap();
Expand All @@ -156,7 +156,7 @@ where
geo_types::Geometry::try_from(wkt)
.map_err(D::Error::custom)
.and_then(|geom| {
use geo_types::Geometry::*;
use geo_types::GeometryTZM::*;
match geom {
Point(p) => Ok(Some(p)),
MultiPoint(mp) if mp.0.is_empty() => Ok(None),
Expand Down
20 changes: 7 additions & 13 deletions src/towkt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ fn g_linestring_to_w_linestring<T>(g_linestring: &geo_types::LineString<T>) -> L
where
T: CoordFloat,
{
let &geo_types::LineString(ref g_points) = g_linestring;
g_points_to_w_linestring(g_points)
g_points_to_w_linestring(&g_linestring.0)
}

fn g_points_to_w_linestring<T>(g_coords: &[geo_types::Coordinate<T>]) -> LineString<T>
Expand All @@ -84,8 +83,7 @@ where
{
let mut w_lines = vec![];
for g_line in g_lines {
let &geo_types::LineString(ref g_points) = g_line;
w_lines.push(g_points_to_w_linestring(g_points));
w_lines.push(g_points_to_w_linestring(&g_line.0));
}
w_lines
}
Expand Down Expand Up @@ -115,7 +113,7 @@ where
let mut poly_lines = vec![];

// Outer
let &geo_types::LineString(ref outer_points) = outer_line;
let outer_points = &outer_line.0;
if !outer_points.is_empty() {
poly_lines.push(g_points_to_w_linestring(outer_points));
}
Expand All @@ -131,17 +129,15 @@ fn g_mpoint_to_w_mpoint<T>(g_mpoint: &geo_types::MultiPoint<T>) -> MultiPoint<T>
where
T: CoordFloat,
{
let &geo_types::MultiPoint(ref g_points) = g_mpoint;
let w_points = g_points_to_w_points(g_points);
let w_points = g_points_to_w_points(&g_mpoint.0);
MultiPoint(w_points)
}

fn g_mline_to_w_mline<T>(g_mline: &geo_types::MultiLineString<T>) -> MultiLineString<T>
where
T: CoordFloat,
{
let &geo_types::MultiLineString(ref g_lines) = g_mline;
let w_lines = g_lines_to_w_lines(g_lines);
let w_lines = g_lines_to_w_lines(&g_mline.0);
MultiLineString(w_lines)
}

Expand All @@ -160,18 +156,16 @@ fn g_mpolygon_to_w_mpolygon<T>(g_mpolygon: &geo_types::MultiPolygon<T>) -> Multi
where
T: CoordFloat,
{
let &geo_types::MultiPolygon(ref g_polygons) = g_mpolygon;
let w_polygons = g_polygons_to_w_polygons(g_polygons);
let w_polygons = g_polygons_to_w_polygons(&g_mpolygon.0);
MultiPolygon(w_polygons)
}

fn g_geocol_to_w_geocol<T>(g_geocol: &geo_types::GeometryCollection<T>) -> GeometryCollection<T>
where
T: CoordFloat,
{
let &geo_types::GeometryCollection(ref g_geoms) = g_geocol;
let mut w_geoms = vec![];
for g_geom in g_geoms {
for g_geom in g_geocol.iter() {
let w_geom = g_geom_to_w_geom(g_geom);
w_geoms.push(w_geom);
}
Expand Down

0 comments on commit e21865f

Please sign in to comment.