Expand description
Parse and write OGC GML 3.2 geometry
elements to and from geo-types geometries.
GML is the XML interchange format behind WFS servers and much
government / INSPIRE open data. This crate is sans-IO: strings in,
Geometry<f64> out, and back. There are no file
handles, sockets or async runtime involved, so it drops equally well into a
WFS client, an ETL job, or a WASM build.
§Quick start
use geo_types::Geometry;
use ogc_gml::{parse_gml, write_gml};
let xml = r#"<gml:Point xmlns:gml="http://www.opengis.net/gml/3.2"
srsName="urn:ogc:def:crs:EPSG::4326">
<gml:pos>51.5 -0.12</gml:pos>
</gml:Point>"#;
let parsed = parse_gml(xml)?;
assert_eq!(parsed.srs_name.as_deref(), Some("urn:ogc:def:crs:EPSG::4326"));
match &parsed.geometry {
Geometry::Point(p) => assert_eq!((p.x(), p.y()), (51.5, -0.12)),
_ => panic!("expected point"),
}
// ...and back out again.
let roundtrip = write_gml(&parsed.geometry, parsed.srs_name.as_deref());
assert!(roundtrip.contains("<gml:pos>51.5 -0.12</gml:pos>"));§Supported geometries
| GML element | geo-types equivalent |
|---|---|
Point | Point |
LineString | LineString |
Polygon | Polygon |
MultiPoint | MultiPoint |
MultiCurve | MultiLineString |
MultiSurface | MultiPolygon |
Coordinates are read from either pos or posList. Namespace prefixes are
ignored, so <gml:Point>, <Point> and <ns0:Point> all parse. Polygon
interior rings (holes) survive both directions.
Arcs, curves with non-linear interpolation, Envelope and
GeometryCollection are out of scope for 0.1.
§Behaviour notes
- Axis order is taken verbatim. Ordinates land in
x,yin the order they appear in the document; the crate never reinterprets them based onsrsName. GML forurn:ogc:def:crs:EPSG::4326is conventionally latitude-first, so the first ordinate becomesx. Swap it yourself if you need lon/lat. - No reprojection.
srsNameis carried through as an opaque string. - 3D input is flattened. With
srsDimension="3"the third ordinate is parsed and then discarded, sincegeo-typesis strictly 2D. - First geometry wins.
parse_gmlreturns the first supported geometry element it meets, which lets you point it straight at a WFSfeatureMemberpayload.
§Errors
Everything fallible returns GmlError, which implements
std::error::Error and so composes with anyhow, thiserror and the
? operator:
use ogc_gml::{parse_gml, GmlError};
assert!(matches!(parse_gml("<featureMember/>"), Err(GmlError::NoGeometry)));
assert!(matches!(
parse_gml("<Point><pos>north east</pos></Point>"),
Err(GmlError::BadNumber(_)),
));Structs§
- GmlGeometry
- A parsed GML geometry plus the CRS metadata that accompanied it.
Enums§
- GmlError
- Errors produced while parsing GML.