Skip to main content

Crate geometry_io_wkt

Crate geometry_io_wkt 

Source
Expand description

OGC Well-Known Text (WKT) reader and writer.

Mirrors boost/geometry/io/wkt/{read,write,wkt}.hpp. The parser emits a geometry_model::DynGeometry because WKT is heterogeneous by construction (a GEOMETRYCOLLECTION mixes kinds); the writer accepts concrete model geometries with to_wkt and user-defined polygons implementing the geometry traits with to_wkt_polygon.

Reference: OGC Simple Feature Access Part 1 (SFA-1) §7 for the WKT grammar.

§Serialize a user-defined polygon

Application types can implement the lightweight geometry_trait traits directly; they do not need to be converted to a geometry_model polygon.

use geometry_cs::Cartesian;
use geometry_io_wkt::to_wkt_polygon;
use geometry_tag::{PointTag, PolygonTag, RingTag};
use geometry_trait::{Geometry, Point, Polygon, Ring};

struct Coordinate(f64, f64);

impl Geometry for Coordinate {
    type Kind = PointTag;
    type Point = Self;
}

impl Point for Coordinate {
    type Scalar = f64;
    type Cs = Cartesian;
    const DIM: usize = 2;

    fn get<const D: usize>(&self) -> f64 {
        match D {
            0 => self.0,
            1 => self.1,
            _ => unreachable!("a Coordinate has two dimensions"),
        }
    }
}

struct Boundary(Vec<Coordinate>);

impl Geometry for Boundary {
    type Kind = RingTag;
    type Point = Coordinate;
}

impl Ring for Boundary {
    fn points(&self) -> impl ExactSizeIterator<Item = &Coordinate> + Clone {
        self.0.iter()
    }
}

struct Parcel {
    exterior: Boundary,
    holes: Vec<Boundary>,
}

impl Geometry for Parcel {
    type Kind = PolygonTag;
    type Point = Coordinate;
}

impl Polygon for Parcel {
    type Ring = Boundary;

    fn exterior(&self) -> &Boundary {
        &self.exterior
    }

    fn interiors(&self) -> impl ExactSizeIterator<Item = &Boundary> {
        self.holes.iter()
    }
}

let parcel = Parcel {
    exterior: Boundary(vec![
        Coordinate(0.0, 0.0),
        Coordinate(0.0, 2.0),
        Coordinate(2.0, 2.0),
        Coordinate(2.0, 0.0),
        Coordinate(0.0, 0.0),
    ]),
    holes: vec![],
};

assert_eq!(
    to_wkt_polygon(&parcel),
    "POLYGON((0 0,0 2,2 2,2 0,0 0))"
);

Enums§

Token
One lexeme of a WKT string.
WktError
Everything that can go wrong reading WKT.

Functions§

from_wkt
Parse a WKT string into a runtime-tagged DynGeometry.
parse_linestring
Parse a WKT LINESTRING into a concrete geometry_model::Linestring.
parse_multi_linestring
Parse a WKT MULTILINESTRING into a concrete geometry_model::MultiLinestring.
parse_multi_point
Parse a WKT MULTIPOINT into a concrete geometry_model::MultiPoint.
parse_multi_polygon
Parse a WKT MULTIPOLYGON into a concrete geometry_model::MultiPolygon.
parse_point
Parse a WKT POINT into a concrete geometry_model::Point.
parse_polygon
Parse a WKT POLYGON into a concrete geometry_model::Polygon.
to_wkt
Serialise a geometry to a canonical WKT String.
to_wkt_polygon
Serialise any polygon implementing PolygonTrait to canonical WKT.
write_wkt
Serialise a geometry into any core::fmt::Write sink.