Skip to content
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

Support reading and writing m and zm #16

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 116 additions & 24 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,77 @@ use num_enum::{IntoPrimitive, TryFromPrimitive};

use crate::error::{WKBError, WKBResult};

/// Supported WKB dimensions
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum WKBDimension {
Xy,
Xyz,
Xym,
Xyzm,
}

impl WKBDimension {
fn as_u32_offset(&self) -> u32 {
match self {
Self::Xy => 0,
Self::Xyz => 1000,
Self::Xym => 2000,
Self::Xyzm => 3000,
}
}
}

impl TryFrom<geo_traits::Dimensions> for WKBDimension {
type Error = WKBError;

fn try_from(value: geo_traits::Dimensions) -> Result<Self, Self::Error> {
use geo_traits::Dimensions::*;

let result = match value {
Xy | Unknown(2) => Self::Xy,
Xyz | Unknown(3) => Self::Xyz,
Xym => Self::Xym,
Xyzm | Unknown(4) => Self::Xyzm,
// TODO: switch to tryfrom
Unknown(n_dim) => {
return Err(WKBError::General(format!(
"Unsupported number of dimensions: {}",
n_dim
)))
}
};
Ok(result)
}
}

impl From<WKBDimension> for geo_traits::Dimensions {
fn from(value: WKBDimension) -> Self {
match value {
WKBDimension::Xy => Self::Xy,
WKBDimension::Xyz => Self::Xyz,
WKBDimension::Xym => Self::Xym,
WKBDimension::Xyzm => Self::Xyzm,
}
}
}

/// The various WKB types supported by this crate
#[derive(Clone, Copy, Debug, PartialEq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum WKBType {
/// A WKB Point
Point = 1,
Point(WKBDimension),
/// A WKB LineString
LineString = 2,
LineString(WKBDimension),
/// A WKB Polygon
Polygon = 3,
Polygon(WKBDimension),
/// A WKB MultiPoint
MultiPoint = 4,
MultiPoint(WKBDimension),
/// A WKB MultiLineString
MultiLineString = 5,
MultiLineString(WKBDimension),
/// A WKB MultiPolygon
MultiPolygon = 6,
MultiPolygon(WKBDimension),
/// A WKB GeometryCollection
GeometryCollection = 7,
/// A WKB PointZ
PointZ = 1001,
/// A WKB LineStringZ
LineStringZ = 1002,
/// A WKB PolygonZ
PolygonZ = 1003,
/// A WKB MultiPointZ
MultiPointZ = 1004,
/// A WKB MultiLineStringZ
MultiLineStringZ = 1005,
/// A WKB MultiPolygonZ
MultiPolygonZ = 1006,
/// A WKB GeometryCollectionZ
GeometryCollectionZ = 1007,
GeometryCollection(WKBDimension),
}

impl WKBType {
Expand All @@ -49,7 +88,60 @@ impl WKBType {
1 => reader.read_u32::<LittleEndian>().unwrap(),
other => panic!("Unexpected byte order: {}", other),
};
Self::try_from_primitive(geometry_type).map_err(|err| WKBError::General(err.to_string()))
Self::try_from_u32(geometry_type)
}

pub fn try_from_u32(value: u32) -> WKBResult<Self> {
// Values 1, 2, 3 are 2D,
// 1001, 1002, 1003 are XYZ,
// 2001 etc are XYM,
// 3001 etc are XYZM
let dim = match value / 1000 {
0 => WKBDimension::Xy,
1 => WKBDimension::Xyz,
2 => WKBDimension::Xym,
3 => WKBDimension::Xyz,
_ => {
return Err(WKBError::General(format!(
"WKB type value out of range. Got: {}",
value
)))
}
};
let typ = match value % 1000 {
1 => WKBType::Point(dim),
2 => WKBType::LineString(dim),
3 => WKBType::Polygon(dim),
4 => WKBType::MultiPoint(dim),
5 => WKBType::MultiLineString(dim),
6 => WKBType::MultiPolygon(dim),
7 => WKBType::GeometryCollection(dim),
_ => {
return Err(WKBError::General(format!(
"WKB type value out of range. Got: {}",
value
)))
}
};
Ok(typ)
}

pub fn as_u32(&self) -> u32 {
match self {
Self::Point(dim) => 1 + dim.as_u32_offset(),
Self::LineString(dim) => 2 + dim.as_u32_offset(),
Self::Polygon(dim) => 3 + dim.as_u32_offset(),
Self::MultiPoint(dim) => 4 + dim.as_u32_offset(),
Self::MultiLineString(dim) => 5 + dim.as_u32_offset(),
Self::MultiPolygon(dim) => 6 + dim.as_u32_offset(),
Self::GeometryCollection(dim) => 7 + dim.as_u32_offset(),
}
}
}

impl From<WKBType> for u32 {
fn from(value: WKBType) -> Self {
value.as_u32()
}
}

Expand Down
33 changes: 13 additions & 20 deletions src/reader/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,23 @@ impl<'a> Wkb<'a> {
let byte_order = Endianness::try_from(reader.read_u8()?).unwrap();
let wkb_type = WKBType::from_buffer(buf)?;

use Dimensions::*;

let out = match wkb_type {
WKBType::Point => Wkb::Point(Point::new(buf, byte_order, 0, Xy)),
WKBType::LineString => Wkb::LineString(LineString::new(buf, byte_order, 0, Xy)),
WKBType::Polygon => Wkb::Polygon(Polygon::new(buf, byte_order, 0, Xy)),
WKBType::MultiPoint => Wkb::MultiPoint(MultiPoint::new(buf, byte_order, Xy)),
WKBType::MultiLineString => {
Wkb::MultiLineString(MultiLineString::new(buf, byte_order, Xy))
WKBType::Point(dim) => Wkb::Point(Point::new(buf, byte_order, 0, dim.into())),
WKBType::LineString(dim) => {
Wkb::LineString(LineString::new(buf, byte_order, 0, dim.into()))
}
WKBType::Polygon(dim) => Wkb::Polygon(Polygon::new(buf, byte_order, 0, dim.into())),
WKBType::MultiPoint(dim) => {
Wkb::MultiPoint(MultiPoint::new(buf, byte_order, dim.into()))
}
WKBType::MultiPolygon => Wkb::MultiPolygon(MultiPolygon::new(buf, byte_order, Xy)),
WKBType::GeometryCollection => {
Wkb::GeometryCollection(GeometryCollection::try_new(buf, byte_order, Xy)?)
WKBType::MultiLineString(dim) => {
Wkb::MultiLineString(MultiLineString::new(buf, byte_order, dim.into()))
}
WKBType::PointZ => Wkb::Point(Point::new(buf, byte_order, 0, Xyz)),
WKBType::LineStringZ => Wkb::LineString(LineString::new(buf, byte_order, 0, Xyz)),
WKBType::PolygonZ => Wkb::Polygon(Polygon::new(buf, byte_order, 0, Xyz)),
WKBType::MultiPointZ => Wkb::MultiPoint(MultiPoint::new(buf, byte_order, Xyz)),
WKBType::MultiLineStringZ => {
Wkb::MultiLineString(MultiLineString::new(buf, byte_order, Xyz))
WKBType::MultiPolygon(dim) => {
Wkb::MultiPolygon(MultiPolygon::new(buf, byte_order, dim.into()))
}
WKBType::MultiPolygonZ => Wkb::MultiPolygon(MultiPolygon::new(buf, byte_order, Xyz)),
WKBType::GeometryCollectionZ => {
Wkb::GeometryCollection(GeometryCollection::try_new(buf, byte_order, Xyz)?)
WKBType::GeometryCollection(dim) => {
Wkb::GeometryCollection(GeometryCollection::try_new(buf, byte_order, dim.into())?)
}
};
Ok(out)
Expand Down
13 changes: 2 additions & 11 deletions src/writer/geometrycollection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,8 @@ fn write_geometry_collection_content<W: Write, B: ByteOrder>(
geom: &impl GeometryCollectionTrait<T = f64>,
endianness: Endianness,
) -> WKBResult<()> {
use geo_traits::Dimensions;

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

// numGeometries
writer.write_u32::<B>(geom.num_geometries().try_into().unwrap())?;
Expand Down
13 changes: 2 additions & 11 deletions src/writer/linestring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,8 @@ fn write_line_string_content<W: Write, B: ByteOrder>(
writer: &mut W,
geom: &impl LineStringTrait<T = f64>,
) -> WKBResult<()> {
use geo_traits::Dimensions;

match geom.dim() {
Dimensions::Xy | Dimensions::Unknown(2) => {
writer.write_u32::<B>(WKBType::LineString.into()).unwrap();
}
Dimensions::Xyz | Dimensions::Unknown(3) => {
writer.write_u32::<B>(WKBType::LineStringZ.into()).unwrap();
}
_ => panic!(),
}
let wkb_type = WKBType::LineString(geom.dim().try_into()?);
writer.write_u32::<LittleEndian>(wkb_type.into())?;

// numPoints
writer
Expand Down
13 changes: 2 additions & 11 deletions src/writer/multilinestring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,8 @@ fn write_multi_line_string_content<W: Write, B: ByteOrder>(
geom: &impl MultiLineStringTrait<T = f64>,
endianness: Endianness,
) -> WKBResult<()> {
use geo_traits::Dimensions;

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

// numPoints
writer.write_u32::<B>(geom.num_line_strings().try_into().unwrap())?;
Expand Down
13 changes: 2 additions & 11 deletions src/writer/multipoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,8 @@ fn write_multi_point_content<W: Write, B: ByteOrder>(
geom: &impl MultiPointTrait<T = f64>,
endianness: Endianness,
) -> WKBResult<()> {
use geo_traits::Dimensions;

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

// numPoints
writer.write_u32::<B>(geom.num_points().try_into().unwrap())?;
Expand Down
13 changes: 2 additions & 11 deletions src/writer/multipolygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,8 @@ fn write_multi_polygon_content<W: Write, B: ByteOrder>(
geom: &impl MultiPolygonTrait<T = f64>,
endianness: Endianness,
) -> WKBResult<()> {
use geo_traits::Dimensions;

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

// numPolygons
writer.write_u32::<B>(geom.num_polygons().try_into().unwrap())?;
Expand Down
18 changes: 6 additions & 12 deletions src/writer/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,19 @@ fn write_point_content<W: Write, B: ByteOrder>(
writer: &mut W,
geom: &impl PointTrait<T = f64>,
) -> WKBResult<()> {
use geo_traits::Dimensions;

match geom.dim() {
Dimensions::Xy | Dimensions::Unknown(2) => {
writer.write_u32::<B>(WKBType::Point.into())?;
}
Dimensions::Xyz | Dimensions::Unknown(3) => {
writer.write_u32::<B>(WKBType::PointZ.into())?;
}
_ => panic!(),
}
let wkb_type = WKBType::Point(geom.dim().try_into()?);
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 {
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))?;
}
} else {
// Write POINT EMPTY as f64::NAN values
for _ in 0..geom.dim().size() {
Expand Down
13 changes: 2 additions & 11 deletions src/writer/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,8 @@ fn write_polygon_content<W: Write, B: ByteOrder>(
writer: &mut W,
geom: &impl PolygonTrait<T = f64>,
) -> WKBResult<()> {
use geo_traits::Dimensions;

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

// numRings
// TODO: support empty polygons where this will panic
Expand Down
Loading