-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft: Implement to WKT for Geometries #788
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,20 +107,20 @@ pub fn point_to_wkt<W: Write>( | |
let mut buffer = ryu::Buffer::new(); | ||
writer.write_str(buffer.format(x))?; | ||
|
||
writer.write_str(" ")?; | ||
writer.write_char(' ')?; | ||
|
||
// y | ||
let mut buffer = ryu::Buffer::new(); | ||
writer.write_str(buffer.format(y))?; | ||
|
||
// z .. n | ||
for nth in 2..point.dim() { | ||
writer.write_str(" ")?; | ||
writer.write_char(' ')?; | ||
let mut buffer = ryu::Buffer::new(); | ||
writer.write_str(buffer.format(point.nth_unchecked(nth)))?; | ||
} | ||
|
||
writer.write_str(")")?; | ||
writer.write_char(')')?; | ||
|
||
Ok(()) | ||
} | ||
|
@@ -168,11 +168,11 @@ pub fn polygon_to_wkt<W: Write>( | |
}; | ||
|
||
for interior in polygon.interiors() { | ||
writer.write_str(",")?; | ||
writer.write_char(',')?; | ||
add_coords(writer, interior.coords())?; | ||
} | ||
|
||
writer.write_str("))")?; | ||
writer.write_char(')')?; | ||
|
||
Ok(()) | ||
} | ||
|
@@ -195,11 +195,11 @@ pub fn multi_point_to_wkt<W: Write>( | |
add_point(writer, first)?; | ||
|
||
for point in points { | ||
writer.write_str(",")?; | ||
writer.write_char(',')?; | ||
add_point(writer, point)?; | ||
} | ||
|
||
writer.write_str(")")?; | ||
writer.write_char(')')?; | ||
} else { | ||
writer.write_str(" EMPTY")?; | ||
} | ||
|
@@ -224,11 +224,11 @@ pub fn multi_linestring_to_wkt<W: Write>( | |
add_coords(writer, linestring.coords())?; | ||
|
||
for linestring in lines { | ||
writer.write_str(",")?; | ||
writer.write_char(',')?; | ||
add_coords(writer, linestring.coords())?; | ||
} | ||
|
||
writer.write_str(")")?; | ||
writer.write_char(')')?; | ||
} else { | ||
writer.write_str(" EMPTY")?; | ||
} | ||
|
@@ -253,7 +253,7 @@ pub fn multi_polygon_to_wkt<W: Write>( | |
|
||
add_coords(writer, polygon.exterior().unwrap().coords())?; | ||
for interior in polygon.interiors() { | ||
writer.write_str(",")?; | ||
writer.write_char(',')?; | ||
add_coords(writer, interior.coords())?; | ||
} | ||
|
||
|
@@ -262,7 +262,7 @@ pub fn multi_polygon_to_wkt<W: Write>( | |
|
||
add_coords(writer, polygon.exterior().unwrap().coords())?; | ||
for interior in polygon.interiors() { | ||
writer.write_str(",")?; | ||
writer.write_char(',')?; | ||
add_coords(writer, interior.coords())?; | ||
} | ||
} | ||
|
@@ -293,11 +293,11 @@ pub fn geometry_collection_to_wkt<W: Write>( | |
geometry_to_wkt(&first, writer)?; | ||
|
||
for geom in geometries { | ||
writer.write_str(",")?; | ||
writer.write_char(',')?; | ||
geometry_to_wkt(&geom, writer)?; | ||
} | ||
|
||
writer.write_str(")")?; | ||
writer.write_char(')')?; | ||
} else { | ||
writer.write_str(" EMPTY")?; | ||
} | ||
|
@@ -331,15 +331,15 @@ fn add_coord<W: Write, C: CoordTrait<T = f64>>(writer: &mut W, coord: C) -> Resu | |
let mut buffer = ryu::Buffer::new(); | ||
writer.write_str(buffer.format(coord.x()))?; | ||
|
||
writer.write_str(" ")?; | ||
writer.write_char(' ')?; | ||
|
||
// y | ||
let mut buffer = ryu::Buffer::new(); | ||
writer.write_str(buffer.format(coord.y()))?; | ||
|
||
// z .. n | ||
for nth in 2..coord.dim() { | ||
writer.write_str(" ")?; | ||
writer.write_char(' ')?; | ||
let mut buffer = ryu::Buffer::new(); | ||
writer.write_str(buffer.format(coord.nth_unchecked(nth)))?; | ||
} | ||
|
@@ -348,26 +348,26 @@ fn add_coord<W: Write, C: CoordTrait<T = f64>>(writer: &mut W, coord: C) -> Resu | |
} | ||
|
||
fn add_point<W: Write, P: PointTrait<T = f64>>(writer: &mut W, point: P) -> Result<(), Error> { | ||
writer.write_str("(")?; | ||
writer.write_char('(')?; | ||
|
||
// x | ||
let mut buffer = ryu::Buffer::new(); | ||
writer.write_str(buffer.format(point.x()))?; | ||
|
||
writer.write_str(" ")?; | ||
writer.write_char(' ')?; | ||
|
||
// y | ||
let mut buffer = ryu::Buffer::new(); | ||
writer.write_str(buffer.format(point.y()))?; | ||
|
||
// z .. n | ||
for nth in 2..point.dim() { | ||
writer.write_str(" ")?; | ||
writer.write_char(' ')?; | ||
let mut buffer = ryu::Buffer::new(); | ||
writer.write_str(buffer.format(point.nth_unchecked(nth)))?; | ||
} | ||
|
||
writer.write_str(")")?; | ||
writer.write_char(')')?; | ||
|
||
Ok(()) | ||
} | ||
|
@@ -376,17 +376,130 @@ fn add_coords<W: Write, C: CoordTrait<T = f64>>( | |
writer: &mut W, | ||
mut coords: impl Iterator<Item = C>, | ||
) -> Result<(), Error> { | ||
writer.write_str("(")?; | ||
writer.write_char('(')?; | ||
|
||
let first = coords.next().unwrap(); | ||
add_coord(writer, first)?; | ||
|
||
for coord in coords { | ||
writer.write_str(",")?; | ||
writer.write_char(',')?; | ||
add_coord(writer, coord)?; | ||
} | ||
|
||
writer.write_str(")")?; | ||
writer.write_char(')')?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use arrow_buffer::OffsetBuffer; | ||
|
||
use super::*; | ||
use crate::{ | ||
array::{CoordBuffer, InterleavedCoordBuffer}, | ||
scalar::{ | ||
OwnedLineString, OwnedMultiLineString, OwnedMultiPoint, OwnedMultiPolygon, OwnedPoint, | ||
OwnedPolygon, | ||
}, | ||
}; | ||
|
||
#[test] | ||
fn point() { | ||
let coords = InterleavedCoordBuffer::<2>::new(vec![1., 2.].into()); | ||
let point = OwnedPoint::new(CoordBuffer::Interleaved(coords), 0); | ||
|
||
let mut wkt = String::new(); | ||
point_to_wkt(&point, &mut wkt).unwrap(); | ||
|
||
assert_eq!(&wkt, "POINT (1.0 2.0)"); | ||
} | ||
|
||
#[test] | ||
fn linestring() { | ||
let coords = InterleavedCoordBuffer::<2>::new(vec![1., 2., 3., 4., 5., 6.].into()); | ||
let linestring = OwnedLineString::new( | ||
CoordBuffer::Interleaved(coords), | ||
OffsetBuffer::<i32>::new(vec![0, 3].into()), | ||
0, | ||
); | ||
|
||
let mut wkt = String::new(); | ||
linestring_to_wkt(&linestring, &mut wkt).unwrap(); | ||
|
||
assert_eq!(&wkt, "LINESTRING (1.0 2.0,3.0 4.0,5.0 6.0)"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's enough to assert that |
||
} | ||
|
||
#[test] | ||
fn polygon() { | ||
let coords = InterleavedCoordBuffer::<2>::new(vec![0., 0., 4., 0., 2., 4., 0., 0.].into()); | ||
let polygon = OwnedPolygon::new( | ||
CoordBuffer::Interleaved(coords), | ||
OffsetBuffer::<i32>::new(vec![0, 1].into()), | ||
OffsetBuffer::<i32>::new(vec![0, 4].into()), | ||
0, | ||
); | ||
Comment on lines
+435
to
+441
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto for all of these. It's way too easy to get these offsets wrong to write them by hand. I'd prefer to improve the test data in a single location ( |
||
|
||
let mut wkt = String::new(); | ||
polygon_to_wkt(&polygon, &mut wkt).unwrap(); | ||
|
||
assert_eq!(&wkt, "POLYGON ((0.0 0.0,4.0 0.0,2.0 4.0,0.0 0.0))"); | ||
} | ||
|
||
#[test] | ||
fn multi_point() { | ||
let coords = InterleavedCoordBuffer::<2>::new(vec![0., 0., 4., 0., 2., 4.].into()); | ||
let multi_point = OwnedMultiPoint::new( | ||
CoordBuffer::Interleaved(coords), | ||
OffsetBuffer::<i32>::new(vec![0, 3].into()), | ||
0, | ||
); | ||
|
||
let mut wkt = String::new(); | ||
multi_point_to_wkt(&multi_point, &mut wkt).unwrap(); | ||
|
||
assert_eq!(&wkt, "MULTIPOINT ((0.0 0.0),(4.0 0.0),(2.0 4.0))"); | ||
} | ||
|
||
#[test] | ||
fn multi_linestring() { | ||
let coords = | ||
InterleavedCoordBuffer::<2>::new(vec![1., 2., 3., 4., 5., 6., 7., 8., 9., 0.].into()); | ||
let multi_linestring = OwnedMultiLineString::new( | ||
CoordBuffer::Interleaved(coords), | ||
OffsetBuffer::<i32>::new(vec![0, 2].into()), | ||
OffsetBuffer::<i32>::new(vec![0, 3, 5].into()), | ||
0, | ||
); | ||
|
||
let mut wkt = String::new(); | ||
multi_linestring_to_wkt(&multi_linestring, &mut wkt).unwrap(); | ||
|
||
assert_eq!( | ||
&wkt, | ||
"MULTILINESTRING ((1.0 2.0,3.0 4.0,5.0 6.0),(7.0 8.0,9.0 0.0))" | ||
); | ||
} | ||
|
||
#[test] | ||
fn multi_polygon() { | ||
let coords = InterleavedCoordBuffer::<2>::new( | ||
vec![ | ||
0., 0., 4., 0., 2., 4., 0., 0., 4., 4., 8., 4., 8., 8., 4., 8., 4., 4., | ||
] | ||
.into(), | ||
); | ||
let multi_polygon = OwnedMultiPolygon::new( | ||
CoordBuffer::Interleaved(coords), | ||
OffsetBuffer::<i32>::new(vec![0, 2].into()), | ||
OffsetBuffer::<i32>::new(vec![0, 1, 2].into()), | ||
OffsetBuffer::<i32>::new(vec![0, 4, 9].into()), | ||
0, | ||
); | ||
|
||
let mut wkt = String::new(); | ||
multi_polygon_to_wkt(&multi_polygon, &mut wkt).unwrap(); | ||
|
||
assert_eq!(&wkt, "MULTIPOLYGON (((0.0 0.0,4.0 0.0,2.0 4.0,0.0 0.0)),((4.0 4.0,8.0 4.0,8.0 8.0,4.0 8.0,4.0 4.0)))"); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be easier to use
crate::test::point::point_array
and then access the first point.