Skip to main content

Crate ogc_gml

Crate ogc_gml 

Source
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 elementgeo-types equivalent
PointPoint
LineStringLineString
PolygonPolygon
MultiPointMultiPoint
MultiCurveMultiLineString
MultiSurfaceMultiPolygon

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, y in the order they appear in the document; the crate never reinterprets them based on srsName. GML for urn:ogc:def:crs:EPSG::4326 is conventionally latitude-first, so the first ordinate becomes x. Swap it yourself if you need lon/lat.
  • No reprojection. srsName is carried through as an opaque string.
  • 3D input is flattened. With srsDimension="3" the third ordinate is parsed and then discarded, since geo-types is strictly 2D.
  • First geometry wins. parse_gml returns the first supported geometry element it meets, which lets you point it straight at a WFS featureMember payload.

§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.

Functions§

parse_gml
Parses the first supported GML geometry element found in xml.
write_gml
Serializes a geo-types geometry to a GML 3.2 fragment using the gml: prefix.