Skip to main content

Crate typed_geojson

Crate typed_geojson 

Source
Expand description

Strongly-typed GeoJSON for Rust.

The geojson crate models a Feature’s properties as an untyped Option<serde_json::Map<String, Value>>. This crate adds generics, Feature<G, P> and FeatureCollection<G, P>, typed over both the Geometry and the Properties, in the same parameter order as @types/geojson’s Feature<G, P> so the two interoperate.

G defaults to the typed Geometry union (which mirrors @types/geojson’s Geometry and exports to TypeScript via the specta feature) and P to Properties (an untyped JSON object, like native GeoJsonProperties); pick your own P for typed properties:

Per RFC 7946 a Feature’s geometry is mandatory but may be null; like native, that nullability lives in G: Feature<Geometry> is non-null, Feature<Option<Geometry>> (→ TS Geometry | null) is the nullable form. geojson::Geometry interop is available through the Feature / Geometry TryFrom bridges.

use serde::{Deserialize, Serialize};
use typed_geojson::{Feature, Geometry};

#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Station {
    id: u32,
    name: String,
    temp_c: f64,
}

let raw = r#"{
    "type": "Feature",
    "geometry": { "type": "Point", "coordinates": [-96.8, 32.8] },
    "properties": { "id": 7, "name": "DFW", "temp_c": 31.5 }
}"#;

let feature: Feature<Geometry, Station> = serde_json::from_str(raw).unwrap();
assert_eq!(feature.properties.name, "DFW");

// round-trips back to standard GeoJSON
let back: Feature<Geometry, Station> =
    serde_json::from_str(&serde_json::to_string(&feature).unwrap()).unwrap();
assert_eq!(back, feature);

It also bridges to the untyped geojson crate (whose geometry is nullable, so use Option<Geometry>):

use typed_geojson::{Feature, Geometry, Point};

// geojson stores properties as a JSON object, so `P` must serialize to one.
let f: Feature<Option<Geometry>, serde_json::Value> = Feature::new(
    Some(Geometry::Point(Point::new(vec![-96.8, 32.8]))),
    serde_json::json!({ "name": "DFW" }),
);
let untyped: geojson::Feature = f.try_into().unwrap();
assert!(untyped.geometry.is_some());

Unknown top-level members (RFC 7946 §6.1 foreign members — an extension’s "title", a vendor field) are preserved rather than dropped; see ForeignMembers. With the geo-types feature, the geometry types convert to and from geo_types so a typed geometry flows into georust’s geo algorithms:

use typed_geojson::Point;

let p: geo_types::Point<f64> = Point::new(vec![-96.8, 32.8]).try_into().unwrap();
assert_eq!(p, geo_types::Point::new(-96.8, 32.8));

Structs§

Feature
A GeoJSON Feature with typed Geometry and Properties.
FeatureCollection
A GeoJSON FeatureCollection of typed features.
GeometryCollection
A GeoJSON GeometryCollection (RFC 7946 §3.1.8): a list of geometries.
LineString
A GeoJSON geometry object (RFC 7946 §3.1), matching the native @types/geojson interface of the same name.
MultiLineString
A GeoJSON geometry object (RFC 7946 §3.1), matching the native @types/geojson interface of the same name.
MultiPoint
A GeoJSON geometry object (RFC 7946 §3.1), matching the native @types/geojson interface of the same name.
MultiPolygon
A GeoJSON geometry object (RFC 7946 §3.1), matching the native @types/geojson interface of the same name.
Point
A GeoJSON geometry object (RFC 7946 §3.1), matching the native @types/geojson interface of the same name.
Polygon
A GeoJSON geometry object (RFC 7946 §3.1), matching the native @types/geojson interface of the same name.

Enums§

Bbox
A GeoJSON bounding box (RFC 7946 §5): a flat array of 2*n numbers, either 4 (2D, [west, south, east, north]) or 6 (3D, with min/max elevation).
Geometry
The GeoJSON geometry union (RFC 7946 §3.1): mirrors @types/geojson’s Geometry. Untagged at the serde layer; each member’s own "type" literal disambiguates, so it round-trips losslessly and exports to the TS union.
GeometryCollectionType
The "GeometryCollection" value of a GeometryCollection’s type member.
Id
A GeoJSON Feature id — a string or a number (RFC 7946 §3.2).
LineStringType
The "type" member of a single geometry kind: a string literal, which is what makes the type assignable to the native geometry.
MultiLineStringType
The "type" member of a single geometry kind: a string literal, which is what makes the type assignable to the native geometry.
MultiPointType
The "type" member of a single geometry kind: a string literal, which is what makes the type assignable to the native geometry.
MultiPolygonType
The "type" member of a single geometry kind: a string literal, which is what makes the type assignable to the native geometry.
PointType
The "type" member of a single geometry kind: a string literal, which is what makes the type assignable to the native geometry.
PolygonType
The "type" member of a single geometry kind: a string literal, which is what makes the type assignable to the native geometry.

Functions§

specta_types
Register every public typed-geojson type into a specta::Types collection, ready to hand to a language exporter (e.g. specta_typescript).

Type Aliases§

ForeignMembers
A GeoJSON object’s foreign members (RFC 7946 §6.1): members that the specification does not define — an extension’s "title", a vendor field, a legacy "crs", and so on. The standard geojson crate documents that its typed path drops these on the floor; this crate keeps them, so a parse → serialize round-trip is byte-faithful and nothing a producer wrote is silently lost.
Position
A GeoJSON position (RFC 7946 §3.1.1): longitude, latitude, and an optional third element (elevation). Modeled as Vec<f64> to match @types/geojson’s Position = number[] (a fixed [f64; N] would export to a TS tuple, which is not mutually assignable with number[]).
Properties
The default, untyped properties of a Feature: a JSON object or null (RFC 7946 §3.2). Mirrors @types/geojson’s GeoJsonProperties = { [name: string]: any } | null.