Crate geojson[−][src]
Examples
This crate uses serde for serialization.
To get started, add geojson to your Cargo.toml:
[dependencies]
geojson= "*"
Reading
use geojson::GeoJson; let geojson_str = r#" { "type": "Feature", "properties": { "name": "Firestone Grill" }, "geometry": { "type": "Point", "coordinates": [-120.66029,35.2812] } } "#; let geojson = geojson_str.parse::<GeoJson>().unwrap();
Writing
Writing geojson depends on the serialization framework because some structs
(like Feature) use json values for properties.
extern crate serde_json; use serde_json::{Map, to_value}; let mut properties = Map::new(); properties.insert( String::from("name"), to_value("Firestone Grill").unwrap(), );
GeoJson can then be serialized by calling to_string:
use geojson::{Feature, GeoJson, Geometry, Value}; let geometry = Geometry::new( Value::Point(vec![-120.66029,35.2812]) ); let geojson = GeoJson::Feature(Feature { bbox: None, geometry: Some(geometry), id: None, properties: Some(properties), foreign_members: None }); let geojson_string = geojson.to_string();
Parsing
GeoJSON's spec is quite simple, but it has several subtleties that must be taken into account when parsing it:
- The
geometryfield of aFeatureis anOption GeometryCollections contain otherGeometryobjects, and can nest.
Here's a minimal example which will parse valid GeoJSON without taking ownership of it.
use geojson::{GeoJson, Geometry, Value}; /// Process GeoJSON geometries fn match_geometry(geom: &Geometry) { match geom.value { Value::Polygon(_) => println!("Matched a Polygon"), Value::MultiPolygon(_) => println!("Matched a MultiPolygon"), Value::GeometryCollection(ref gc) => { println!("Matched a GeometryCollection"); // GeometryCollections contain other Geometry types, and can nest // we deal with this by recursively processing each geometry for geometry in gc { match_geometry(geometry) } } // Point, LineString, and their Multi– counterparts _ => println!("Matched some other geometry"), } } /// Process top-level GeoJSON items fn process_geojson(gj: &GeoJson) { match *gj { GeoJson::FeatureCollection(ref ctn) => for feature in &ctn.features { if let Some(ref geom) = feature.geometry { match_geometry(geom) } }, GeoJson::Feature(ref feature) => { if let Some(ref geom) = feature.geometry { match_geometry(geom) } } GeoJson::Geometry(ref geometry) => match_geometry(geometry), } } fn main() { let geojson_str = r#" { "type": "GeometryCollection", "geometries": [ {"type": "Point", "coordinates": [0,1]}, {"type": "MultiPoint", "coordinates": [[-1,0],[1,0]]}, {"type": "LineString", "coordinates": [[-1,-1],[1,-1]]}, {"type": "MultiLineString", "coordinates": [ [[-2,-2],[2,-2]], [[-3,-3],[3,-3]] ]}, {"type": "Polygon", "coordinates": [ [[-5,-5],[5,-5],[0,5],[-5,-5]], [[-4,-4],[4,-4],[0,4],[-4,-4]] ]}, { "type": "MultiPolygon", "coordinates": [[ [[-7,-7],[7,-7],[0,7],[-7,-7]], [[-6,-6],[6,-6],[0,6],[-6,-6]] ],[ [[-9,-9],[9,-9],[0,9],[-9,-9]], [[-8,-8],[8,-8],[0,8],[-8,-8]]] ]}, {"type": "GeometryCollection", "geometries": [ {"type": "Polygon", "coordinates": [ [[-5.5,-5.5],[5,-5],[0,5],[-5,-5]], [[-4,-4],[4,-4],[0,4],[-4.5,-4.5]] ]} ]} ] } "#; let geojson = geojson_str.parse::<GeoJson>().unwrap(); process_geojson(&geojson); }
Conversion to Geo objects
The try_into trait provides fallible conversions from GeoJSON Value structs to Geo types, allowing them to be measured or used in calculations. Note that this conversion consumes the GeoJSON object, so you will not be able to match by reference as in the example above. The polylabel_cmd crate contains an implementation which may be useful if you wish to perform these conversions.
Modules
| conversion |
Convert Geometries into Geo types |
Structs
| Feature |
Feature Objects |
| FeatureCollection |
Feature Collection Objects |
| Geometry |
Geometry Objects |
Enums
| Error |
Error when reading a GeoJSON object from a str or Object |
| GeoJson |
GeoJSON Objects |
| Value |
The underlying Geometry value |
Type Definitions
| Bbox |
Bounding Boxes |
| LineStringType | |
| PointType | |
| PolygonType | |
| Position |
Positions |