Skip to content

Commit

Permalink
Fix endianness when writing (#13)
Browse files Browse the repository at this point in the history
* Fix endianness when writing

* Fix writing endianness

* fix compile
  • Loading branch information
kylebarron authored Nov 17, 2024
1 parent 7d58a23 commit 99048fe
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl WKBType {
let geometry_type = match byte_order {
0 => reader.read_u32::<BigEndian>().unwrap(),
1 => reader.read_u32::<LittleEndian>().unwrap(),
_ => panic!("Unexpected byte order."),
other => panic!("Unexpected byte order: {}", other),
};
Self::try_from_primitive(geometry_type).map_err(|err| WKBError::General(err.to_string()))
}
Expand Down
28 changes: 22 additions & 6 deletions src/writer/geometrycollection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::common::WKBType;
use crate::error::WKBResult;
use crate::writer::geometry::{geometry_wkb_size, write_geometry};
use crate::Endianness;
use byteorder::{LittleEndian, WriteBytesExt};
use byteorder::{BigEndian, ByteOrder, LittleEndian, WriteBytesExt};
use geo_traits::GeometryCollectionTrait;
use std::io::Write;

Expand All @@ -23,23 +23,39 @@ pub fn write_geometry_collection<W: Write>(
geom: &impl GeometryCollectionTrait<T = f64>,
endianness: Endianness,
) -> WKBResult<()> {
use geo_traits::Dimensions;

// Byte order
writer.write_u8(Endianness::LittleEndian.into())?;

// Content
match endianness {
Endianness::LittleEndian => {
write_geometry_collection_content::<W, LittleEndian>(writer, geom, endianness)
}
Endianness::BigEndian => {
write_geometry_collection_content::<W, BigEndian>(writer, geom, endianness)
}
}
}

fn write_geometry_collection_content<W: Write, B: ByteOrder>(
writer: &mut W,
geom: &impl GeometryCollectionTrait<T = f64>,
endianness: Endianness,
) -> WKBResult<()> {
use geo_traits::Dimensions;

match geom.dim() {
Dimensions::Xy | Dimensions::Unknown(2) => {
writer.write_u32::<LittleEndian>(WKBType::GeometryCollection.into())?;
writer.write_u32::<B>(WKBType::GeometryCollection.into())?;
}
Dimensions::Xyz | Dimensions::Unknown(3) => {
writer.write_u32::<LittleEndian>(WKBType::GeometryCollectionZ.into())?;
writer.write_u32::<B>(WKBType::GeometryCollectionZ.into())?;
}
_ => panic!(),
}

// numGeometries
writer.write_u32::<LittleEndian>(geom.num_geometries().try_into().unwrap())?;
writer.write_u32::<B>(geom.num_geometries().try_into().unwrap())?;

for inner_geom in geom.geometries() {
write_geometry(writer, &inner_geom, endianness)?;
Expand Down
28 changes: 22 additions & 6 deletions src/writer/multilinestring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::common::WKBType;
use crate::error::WKBResult;
use crate::writer::linestring::{line_string_wkb_size, write_line_string};
use crate::Endianness;
use byteorder::{LittleEndian, WriteBytesExt};
use byteorder::{BigEndian, ByteOrder, LittleEndian, WriteBytesExt};
use geo_traits::MultiLineStringTrait;
use std::io::Write;

Expand All @@ -22,23 +22,39 @@ pub fn write_multi_line_string<W: Write>(
geom: &impl MultiLineStringTrait<T = f64>,
endianness: Endianness,
) -> WKBResult<()> {
use geo_traits::Dimensions;

// Byte order
writer.write_u8(endianness.into())?;

// Content
match endianness {
Endianness::LittleEndian => {
write_multi_line_string_content::<W, LittleEndian>(writer, geom, endianness)
}
Endianness::BigEndian => {
write_multi_line_string_content::<W, BigEndian>(writer, geom, endianness)
}
}
}

fn write_multi_line_string_content<W: Write, B: ByteOrder>(
writer: &mut W,
geom: &impl MultiLineStringTrait<T = f64>,
endianness: Endianness,
) -> WKBResult<()> {
use geo_traits::Dimensions;

match geom.dim() {
Dimensions::Xy | Dimensions::Unknown(2) => {
writer.write_u32::<LittleEndian>(WKBType::MultiLineString.into())?;
writer.write_u32::<B>(WKBType::MultiLineString.into())?;
}
Dimensions::Xyz | Dimensions::Unknown(3) => {
writer.write_u32::<LittleEndian>(WKBType::MultiLineStringZ.into())?;
writer.write_u32::<B>(WKBType::MultiLineStringZ.into())?;
}
_ => panic!(),
}

// numPoints
writer.write_u32::<LittleEndian>(geom.num_line_strings().try_into().unwrap())?;
writer.write_u32::<B>(geom.num_line_strings().try_into().unwrap())?;

for line_string in geom.line_strings() {
write_line_string(writer, &line_string, endianness)?;
Expand Down
28 changes: 22 additions & 6 deletions src/writer/multipoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::common::WKBType;
use crate::error::WKBResult;
use crate::writer::point::{point_wkb_size, write_point};
use crate::Endianness;
use byteorder::{LittleEndian, WriteBytesExt};
use byteorder::{BigEndian, ByteOrder, LittleEndian, WriteBytesExt};
use geo_traits::MultiPointTrait;
use std::io::Write;

Expand All @@ -17,23 +17,39 @@ pub fn write_multi_point<W: Write>(
geom: &impl MultiPointTrait<T = f64>,
endianness: Endianness,
) -> WKBResult<()> {
use geo_traits::Dimensions;

// Byte order
writer.write_u8(endianness.into())?;

// Content
match endianness {
Endianness::LittleEndian => {
write_multi_point_content::<W, LittleEndian>(writer, geom, endianness)
}
Endianness::BigEndian => {
write_multi_point_content::<W, BigEndian>(writer, geom, endianness)
}
}
}

fn write_multi_point_content<W: Write, B: ByteOrder>(
writer: &mut W,
geom: &impl MultiPointTrait<T = f64>,
endianness: Endianness,
) -> WKBResult<()> {
use geo_traits::Dimensions;

match geom.dim() {
Dimensions::Xy | Dimensions::Unknown(2) => {
writer.write_u32::<LittleEndian>(WKBType::MultiPoint.into())?;
writer.write_u32::<B>(WKBType::MultiPoint.into())?;
}
Dimensions::Xyz | Dimensions::Unknown(3) => {
writer.write_u32::<LittleEndian>(WKBType::MultiPointZ.into())?;
writer.write_u32::<B>(WKBType::MultiPointZ.into())?;
}
_ => panic!(),
}

// numPoints
writer.write_u32::<LittleEndian>(geom.num_points().try_into().unwrap())?;
writer.write_u32::<B>(geom.num_points().try_into().unwrap())?;

for point in geom.points() {
write_point(writer, &point, endianness)?;
Expand Down
28 changes: 22 additions & 6 deletions src/writer/multipolygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::common::WKBType;
use crate::error::WKBResult;
use crate::writer::polygon::{polygon_wkb_size, write_polygon};
use crate::Endianness;
use byteorder::{LittleEndian, WriteBytesExt};
use byteorder::{BigEndian, ByteOrder, LittleEndian, WriteBytesExt};
use geo_traits::MultiPolygonTrait;
use std::io::Write;

Expand All @@ -22,23 +22,39 @@ pub fn write_multi_polygon<W: Write>(
geom: &impl MultiPolygonTrait<T = f64>,
endianness: Endianness,
) -> WKBResult<()> {
use geo_traits::Dimensions;

// Byte order
writer.write_u8(endianness.into())?;

// Content
match endianness {
Endianness::LittleEndian => {
write_multi_polygon_content::<W, LittleEndian>(writer, geom, endianness)
}
Endianness::BigEndian => {
write_multi_polygon_content::<W, BigEndian>(writer, geom, endianness)
}
}
}

fn write_multi_polygon_content<W: Write, B: ByteOrder>(
writer: &mut W,
geom: &impl MultiPolygonTrait<T = f64>,
endianness: Endianness,
) -> WKBResult<()> {
use geo_traits::Dimensions;

match geom.dim() {
Dimensions::Xy | Dimensions::Unknown(2) => {
writer.write_u32::<LittleEndian>(WKBType::MultiPolygon.into())?;
writer.write_u32::<B>(WKBType::MultiPolygon.into())?;
}
Dimensions::Xyz | Dimensions::Unknown(3) => {
writer.write_u32::<LittleEndian>(WKBType::MultiPolygonZ.into())?;
writer.write_u32::<B>(WKBType::MultiPolygonZ.into())?;
}
_ => panic!(),
}

// numPolygons
writer.write_u32::<LittleEndian>(geom.num_polygons().try_into().unwrap())?;
writer.write_u32::<B>(geom.num_polygons().try_into().unwrap())?;

for polygon in geom.polygons() {
write_polygon(writer, &polygon, endianness)?;
Expand Down

0 comments on commit 99048fe

Please sign in to comment.