Skip to main content

Crate tpt_geo_geojson

Crate tpt_geo_geojson 

Source
Expand description

§tpt-geo-geojson

docs.rs crates.io

Strict, validating GeoJSON parser with path-accurate error messages.

The existing geojson crate is good, but often lacks strict validation for malformed GIS data, which causes downstream mapping libraries to panic. This crate adds a second validation pass with precise error reporting.

§Features

  • Full GeoJSON support — Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection, Feature, FeatureCollection
  • Strict validation — coordinate arrays must have 2 or 3 elements; polygon rings must be closed with ≥ 4 positions
  • Path-accurate errorsGeoError.path tells you exactly where in the structure the problem is (e.g. features[2].geometry.coordinates[0])
  • Reader API — parse from any Read source, not just strings

§Usage

use tpt_geo_geojson::parse;

let geojson = r#"{
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "geometry": {"type": "Point", "coordinates": [125.6, 10.1]},
        "properties": {"name": "Dinagat Islands"}
    }]
}"#;

let result = parse(geojson).unwrap();

§Error handling

use tpt_geo_geojson::{parse, GeoErrorKind};

// 4-element coordinate array — invalid
let bad = r#"{"type":"Point","coordinates":[1,2,3,4]}"#;
let err = parse(bad).unwrap_err();
println!("path: {}", err.path);   // ".coordinates"
println!("error: {}", err.kind);  // "malformed coordinates: ..."

§Serialization & round-trip

Parsed values implement Serialize, so you can write valid GeoJSON back out with to_json. Feature and FeatureCollection correctly emit their "type" member, and any captured bbox or non-standard (foreign) members are preserved across a parse → serialize → parse round-trip.

use tpt_geo_geojson::parse;

let geo = parse(r#"{"type":"FeatureCollection","bbox":[0,0,10,10],"features":[]}"#).unwrap();
let json = tpt_geo_geojson::to_json(&geo).unwrap();
assert!(json.contains(r#""type":"FeatureCollection""#));
assert!(json.contains("bbox"));

// Non-standard members survive the round-trip too:
let f = parse(r#"{"type":"Feature","properties":null,"title":"hi"}"#).unwrap();
let out = tpt_geo_geojson::to_json(&f).unwrap();
assert!(out.contains(r#""title":"hi""#));

§License

Licensed under either of Apache License 2.0 or MIT at your option.

Structs§

Feature
A GeoJSON Feature.
FeatureCollection
A GeoJSON FeatureCollection.
GeoError
A GeoJSON parse or validation error with a path into the structure.
Position
A GeoJSON position: [longitude, latitude] or [longitude, latitude, altitude].

Enums§

GeoErrorKind
The kind of validation or parse error.
GeoJson
The top-level GeoJSON object.
Geometry
A GeoJSON geometry object.

Functions§

parse
Parse a GeoJSON string.
parse_reader
Parse GeoJSON from any Read source.
to_json
Serialize this GeoJSON value back to a compact JSON string.