Skip to content

Commit

Permalink
Write all dimensions of coord (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron authored Nov 17, 2024
1 parent 24a861f commit 0abebb1
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
18 changes: 18 additions & 0 deletions src/writer/coord.rs
Original file line number Diff line number Diff line change
@@ -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<W: Write, B: ByteOrder>(
writer: &mut W,
coord: &impl CoordTrait<T = f64>,
) -> WKBResult<()> {
for i in 0..coord.dim().size() {
writer.write_f64::<B>(coord.nth_unchecked(i))?;
}

Ok(())
}
10 changes: 3 additions & 7 deletions src/writer/linestring.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -42,12 +43,7 @@ fn write_line_string_content<W: Write, B: ByteOrder>(
.unwrap();

for coord in geom.coords() {
writer.write_f64::<B>(coord.x()).unwrap();
writer.write_f64::<B>(coord.y()).unwrap();

if geom.dim().size() == 3 {
writer.write_f64::<B>(coord.nth_unchecked(2)).unwrap();
}
write_coord::<W, B>(writer, &coord)?;
}

Ok(())
Expand Down
1 change: 1 addition & 0 deletions src/writer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod coord;
mod geometry;
mod geometrycollection;
mod linestring;
Expand Down
13 changes: 3 additions & 10 deletions src/writer/point.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -38,15 +39,7 @@ fn write_point_content<W: Write, B: ByteOrder>(
writer.write_u32::<LittleEndian>(wkb_type.into())?;

if let Some(coord) = geom.coord() {
writer.write_f64::<B>(coord.x())?;
writer.write_f64::<B>(coord.y())?;

if coord.dim().size() >= 3 {
writer.write_f64::<B>(coord.nth_unchecked(2))?;
}
if coord.dim().size() >= 4 {
writer.write_f64::<B>(coord.nth_unchecked(3))?;
}
write_coord::<W, B>(writer, &coord)?;
} else {
// Write POINT EMPTY as f64::NAN values
for _ in 0..geom.dim().size() {
Expand Down
15 changes: 4 additions & 11 deletions src/writer/polygon.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -57,23 +58,15 @@ fn write_polygon_content<W: Write, B: ByteOrder>(
writer.write_u32::<B>(ext_ring.num_coords().try_into().unwrap())?;

for coord in ext_ring.coords() {
writer.write_f64::<B>(coord.x())?;
writer.write_f64::<B>(coord.y())?;
if geom.dim().size() == 3 {
writer.write_f64::<B>(coord.nth_unchecked(2))?;
}
write_coord::<W, B>(writer, &coord)?;
}
}

for int_ring in geom.interiors() {
writer.write_u32::<B>(int_ring.num_coords().try_into().unwrap())?;

for coord in int_ring.coords() {
writer.write_f64::<B>(coord.x())?;
writer.write_f64::<B>(coord.y())?;
if geom.dim().size() == 3 {
writer.write_f64::<B>(coord.nth_unchecked(2))?;
}
write_coord::<W, B>(writer, &coord)?;
}
}

Expand Down

0 comments on commit 0abebb1

Please sign in to comment.