geometry-io-ewkb 0.0.11

PostGIS Extended Well-Known Binary (EWKB) reader and writer for the geometry model.
Documentation

geometry-io-ewkb

Part of the boost_geometry workspace — a Rust port of Boost.Geometry. Most users should depend on the facade crate, which re-exports this one; depend on this crate directly only for a slimmer build.

PostGIS Extended Well-Known Binary (EWKB) reader and writer.

EWKB is OGC Well-Known Binary with a PostGIS header dialect: four flag bits in the 32-bit type word, and an optional 32-bit spatial-reference id between the type word and the body. Everything after that header is byte-identical OGC WKB, so this crate is a header codec — it delegates every body to geometry-io-wkb rather than carrying a second parser.

Only the outermost record carries an SRID, which is what PostGIS writes. This is a strictly-2D reader: the Z, M and bounding-box flags are refused by name rather than silently dropped.

Empty polygons use zero rings, including inside collections. Other geometry structure is preserved without validation; consumers can reject short or unclosed rings and holes without an exterior.

use geometry_cs::Cartesian;
use geometry_io_ewkb::{ByteOrder, Srid, from_ewkb, to_ewkb, to_ewkb_hex};
use geometry_model::Point2D;

let p = Point2D::<f64, Cartesian>::new(1.0, 2.0);
let bytes = to_ewkb(&p, Some(Srid::new(4326)), ByteOrder::LittleEndian);

let read = from_ewkb(&bytes).unwrap();
assert_eq!(read.srid, Some(Srid::new(4326)));
assert_eq!(read.byte_order, ByteOrder::LittleEndian);

// The text form of a PostGIS `geometry` column is hex EWKB.
assert_eq!(
    to_ewkb_hex(&p, Some(Srid::new(4326)), ByteOrder::LittleEndian),
    "0101000020E6100000000000000000F03F0000000000000040",
);

Bringing your own polygon

use geometry_cs::Cartesian;
use geometry_io_ewkb::{ByteOrder, Srid, to_ewkb_polygon};
use geometry_model::{Point2D, Polygon, Ring};

type Pt = Point2D<f64, Cartesian>;
let pg = Polygon::<Pt>::new(Ring::from_vec(vec![
    Pt::new(0.0, 0.0),
    Pt::new(0.0, 1.0),
    Pt::new(1.0, 1.0),
    Pt::new(0.0, 0.0),
]));
let bytes = to_ewkb_polygon(&pg, Some(Srid::new(4326)), ByteOrder::BigEndian);
assert_eq!(bytes[0], 0x00); // big-endian

License

BSL-1.0 — see LICENSE.