Skip to main content

Crate geometry_io_ewkb

Crate geometry_io_ewkb 

Source
Expand description

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

Structs§

Ewkb
A geometry, the spatial-reference id its record carried, and the byte order its record declared.
Srid
A PostGIS spatial-reference identifier: the code naming the coordinate reference system a geometry’s ordinates are expressed in.

Enums§

ByteOrder
The two byte orders a WKB record may declare.
EwkbError
Everything that can go wrong reading EWKB.
WkbError
Everything that can go wrong reading WKB.

Functions§

from_ewkb
Read an EWKB record: strip the PostGIS header, parse the OGC record underneath it, and return both with the byte order the record declared.
from_ewkb_hex
Read hex-encoded EWKB — the text form of a PostGIS geometry column.
to_ewkb
Write a geometry to EWKB, with or without an SRID.
to_ewkb_hex
Write a geometry as hex-encoded EWKB, the way PostGIS prints it.
to_ewkb_polygon
Write a caller’s own polygon type to EWKB, through the geometry traits.