From 0abebb1754c2cc21645d9607a1244b7fcbd065ed Mon Sep 17 00:00:00 2001 From: Kyle Barron Date: Sun, 17 Nov 2024 17:49:53 -0500 Subject: [PATCH] Write all dimensions of coord (#30) --- src/writer/coord.rs | 18 ++++++++++++++++++ src/writer/linestring.rs | 10 +++------- src/writer/mod.rs | 1 + src/writer/point.rs | 13 +++---------- src/writer/polygon.rs | 15 ++++----------- 5 files changed, 29 insertions(+), 28 deletions(-) create mode 100644 src/writer/coord.rs diff --git a/src/writer/coord.rs b/src/writer/coord.rs new file mode 100644 index 0000000..ceb040d --- /dev/null +++ b/src/writer/coord.rs @@ -0,0 +1,18 @@ +use std::io::Write; + +use byteorder::{ByteOrder, WriteBytesExt}; +use geo_traits::CoordTrait; + +use crate::error::WKBResult; + +/// Write a coordinate to a Writer encoded as WKB +pub(crate) fn write_coord( + writer: &mut W, + coord: &impl CoordTrait, +) -> WKBResult<()> { + for i in 0..coord.dim().size() { + writer.write_f64::(coord.nth_unchecked(i))?; + } + + Ok(()) +} diff --git a/src/writer/linestring.rs b/src/writer/linestring.rs index 327fdc1..2ad49b1 100644 --- a/src/writer/linestring.rs +++ b/src/writer/linestring.rs @@ -1,8 +1,9 @@ use crate::common::WKBType; use crate::error::WKBResult; +use crate::writer::coord::write_coord; use crate::Endianness; use byteorder::{BigEndian, ByteOrder, LittleEndian, WriteBytesExt}; -use geo_traits::{CoordTrait, LineStringTrait}; +use geo_traits::LineStringTrait; use std::io::Write; /// The byte length of a LineString @@ -42,12 +43,7 @@ fn write_line_string_content( .unwrap(); for coord in geom.coords() { - writer.write_f64::(coord.x()).unwrap(); - writer.write_f64::(coord.y()).unwrap(); - - if geom.dim().size() == 3 { - writer.write_f64::(coord.nth_unchecked(2)).unwrap(); - } + write_coord::(writer, &coord)?; } Ok(()) diff --git a/src/writer/mod.rs b/src/writer/mod.rs index ef28d8f..d18de6d 100644 --- a/src/writer/mod.rs +++ b/src/writer/mod.rs @@ -1,3 +1,4 @@ +mod coord; mod geometry; mod geometrycollection; mod linestring; diff --git a/src/writer/point.rs b/src/writer/point.rs index e2cb6c1..543e525 100644 --- a/src/writer/point.rs +++ b/src/writer/point.rs @@ -1,9 +1,10 @@ use crate::common::WKBType; use crate::error::WKBResult; +use crate::writer::coord::write_coord; use crate::Endianness; use byteorder::{BigEndian, ByteOrder, LittleEndian, WriteBytesExt}; use core::f64; -use geo_traits::{CoordTrait, PointTrait}; +use geo_traits::PointTrait; use std::io::Write; /// The byte length of a Point @@ -38,15 +39,7 @@ fn write_point_content( writer.write_u32::(wkb_type.into())?; if let Some(coord) = geom.coord() { - writer.write_f64::(coord.x())?; - writer.write_f64::(coord.y())?; - - if coord.dim().size() >= 3 { - writer.write_f64::(coord.nth_unchecked(2))?; - } - if coord.dim().size() >= 4 { - writer.write_f64::(coord.nth_unchecked(3))?; - } + write_coord::(writer, &coord)?; } else { // Write POINT EMPTY as f64::NAN values for _ in 0..geom.dim().size() { diff --git a/src/writer/polygon.rs b/src/writer/polygon.rs index 3cd4ca4..6098c29 100644 --- a/src/writer/polygon.rs +++ b/src/writer/polygon.rs @@ -1,8 +1,9 @@ use crate::common::WKBType; use crate::error::WKBResult; +use crate::writer::coord::write_coord; use crate::Endianness; use byteorder::{BigEndian, ByteOrder, LittleEndian, WriteBytesExt}; -use geo_traits::{CoordTrait, LineStringTrait, PolygonTrait}; +use geo_traits::{LineStringTrait, PolygonTrait}; use std::io::Write; /// The byte length of a Polygon @@ -57,11 +58,7 @@ fn write_polygon_content( writer.write_u32::(ext_ring.num_coords().try_into().unwrap())?; for coord in ext_ring.coords() { - writer.write_f64::(coord.x())?; - writer.write_f64::(coord.y())?; - if geom.dim().size() == 3 { - writer.write_f64::(coord.nth_unchecked(2))?; - } + write_coord::(writer, &coord)?; } } @@ -69,11 +66,7 @@ fn write_polygon_content( writer.write_u32::(int_ring.num_coords().try_into().unwrap())?; for coord in int_ring.coords() { - writer.write_f64::(coord.x())?; - writer.write_f64::(coord.y())?; - if geom.dim().size() == 3 { - writer.write_f64::(coord.nth_unchecked(2))?; - } + write_coord::(writer, &coord)?; } }