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
Featurewith typedGeometry andProperties. - Feature
Collection - A GeoJSON
FeatureCollectionof typed features. - Geometry
Collection - A GeoJSON GeometryCollection (RFC 7946 §3.1.8): a list of geometries.
- Line
String - A GeoJSON geometry object (RFC 7946 §3.1), matching the native
@types/geojsoninterface of the same name. - Multi
Line String - A GeoJSON geometry object (RFC 7946 §3.1), matching the native
@types/geojsoninterface of the same name. - Multi
Point - A GeoJSON geometry object (RFC 7946 §3.1), matching the native
@types/geojsoninterface of the same name. - Multi
Polygon - A GeoJSON geometry object (RFC 7946 §3.1), matching the native
@types/geojsoninterface of the same name. - Point
- A GeoJSON geometry object (RFC 7946 §3.1), matching the native
@types/geojsoninterface of the same name. - Polygon
- A GeoJSON geometry object (RFC 7946 §3.1), matching the native
@types/geojsoninterface of the same name.
Enums§
- Bbox
- A GeoJSON bounding box (RFC 7946 §5): a flat array of
2*nnumbers, 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’sGeometry. Untagged at the serde layer; each member’s own"type"literal disambiguates, so it round-trips losslessly and exports to the TS union. - Geometry
Collection Type - The
"GeometryCollection"value of aGeometryCollection’stypemember. - Id
- A GeoJSON Feature
id— a string or a number (RFC 7946 §3.2). - Line
String Type - The
"type"member of a single geometry kind: a string literal, which is what makes the type assignable to the native geometry. - Multi
Line String Type - The
"type"member of a single geometry kind: a string literal, which is what makes the type assignable to the native geometry. - Multi
Point Type - The
"type"member of a single geometry kind: a string literal, which is what makes the type assignable to the native geometry. - Multi
Polygon Type - The
"type"member of a single geometry kind: a string literal, which is what makes the type assignable to the native geometry. - Point
Type - The
"type"member of a single geometry kind: a string literal, which is what makes the type assignable to the native geometry. - Polygon
Type - 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::Typescollection, ready to hand to a language exporter (e.g.specta_typescript).
Type Aliases§
- Foreign
Members - 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 standardgeojsoncrate 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’sPosition = number[](a fixed[f64; N]would export to a TS tuple, which is not mutually assignable withnumber[]). - Properties
- The default, untyped
propertiesof aFeature: a JSON object ornull(RFC 7946 §3.2). Mirrors@types/geojson’sGeoJsonProperties = { [name: string]: any } | null.