Skip to main content

geometry_io_wkb/
lib.rs

1//! OGC Well-Known Binary (WKB) reader and writer.
2//!
3//! Follows OGC Simple Feature Access 06-103r4 §8 (Boost.Geometry ships
4//! no WKB). The parser emits a [`geometry_model::DynGeometry`]; the
5//! writer serialises concrete model geometries with [`to_wkb`] and any
6//! user-defined polygon implementing the geometry traits with
7//! [`to_wkb_polygon`].
8//!
9//! ## Serialize a user-defined polygon
10//!
11//! Application types can implement the lightweight [`geometry_trait`] traits
12//! directly; they do not need to be converted to a `geometry_model` polygon.
13//!
14//! ```
15//! use geometry_cs::Cartesian;
16//! use geometry_io_wkb::{from_wkb, to_wkb_polygon, ByteOrder};
17//! use geometry_tag::{PointTag, PolygonTag, RingTag};
18//! use geometry_trait::{Geometry, Point, Polygon, Ring};
19//!
20//! struct Coordinate(f64, f64);
21//!
22//! impl Geometry for Coordinate {
23//!     type Kind = PointTag;
24//!     type Point = Self;
25//! }
26//!
27//! impl Point for Coordinate {
28//!     type Scalar = f64;
29//!     type Cs = Cartesian;
30//!     const DIM: usize = 2;
31//!
32//!     fn get<const D: usize>(&self) -> f64 {
33//!         match D {
34//!             0 => self.0,
35//!             1 => self.1,
36//!             _ => unreachable!("a Coordinate has two dimensions"),
37//!         }
38//!     }
39//! }
40//!
41//! struct Boundary(Vec<Coordinate>);
42//!
43//! impl Geometry for Boundary {
44//!     type Kind = RingTag;
45//!     type Point = Coordinate;
46//! }
47//!
48//! impl Ring for Boundary {
49//!     fn points(&self) -> impl ExactSizeIterator<Item = &Coordinate> + Clone {
50//!         self.0.iter()
51//!     }
52//! }
53//!
54//! struct Parcel {
55//!     exterior: Boundary,
56//!     holes: Vec<Boundary>,
57//! }
58//!
59//! impl Geometry for Parcel {
60//!     type Kind = PolygonTag;
61//!     type Point = Coordinate;
62//! }
63//!
64//! impl Polygon for Parcel {
65//!     type Ring = Boundary;
66//!
67//!     fn exterior(&self) -> &Boundary {
68//!         &self.exterior
69//!     }
70//!
71//!     fn interiors(&self) -> impl ExactSizeIterator<Item = &Boundary> {
72//!         self.holes.iter()
73//!     }
74//! }
75//!
76//! let parcel = Parcel {
77//!     exterior: Boundary(vec![
78//!         Coordinate(0.0, 0.0),
79//!         Coordinate(0.0, 2.0),
80//!         Coordinate(2.0, 2.0),
81//!         Coordinate(2.0, 0.0),
82//!         Coordinate(0.0, 0.0),
83//!     ]),
84//!     holes: vec![],
85//! };
86//!
87//! let bytes = to_wkb_polygon(&parcel, ByteOrder::LittleEndian);
88//! assert_eq!(bytes[0], 0x01);
89//! assert!(from_wkb(&bytes).is_ok());
90//! ```
91
92#![cfg_attr(not(feature = "std"), no_std)]
93#![forbid(unsafe_code)]
94
95extern crate alloc;
96
97mod header;
98mod parse;
99mod write;
100
101pub use header::{ByteOrder, WkbError};
102// feature-group: I/O — Well-Known Binary
103// feature-desc: Parse and write the OGC WKB format
104pub use parse::from_wkb;
105// feature-group: I/O — Well-Known Binary
106pub use write::{WriteWkb, to_wkb, to_wkb_polygon};