Crate geojson [] [src]

Examples

Both rustc_serialize (default) and serde are supported. To use serde, add the with-serde feature to your Cargo.toml:

[dependencies]
geojson={version="*", features=["with-serde"]}

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.

For rustc_serialize use rustc_serialize::json::Object:

use rustc_serialize::json::ToJson;
use std::collections::BTreeMap;

let mut properties = BTreeMap::new();
properties.insert(
    String::from("name"),
    "Firestone Grill".to_json(),
);

For serde use serde_json::Value::Object:

use serde_json::to_value;
use std::collections::BTreeMap;

let mut properties = BTreeMap::new();
properties.insert(
    String::from("name"),
    to_value("Firestone Grill"),
);

GeoJson can then be serialized by calling to_string:

use rustc_serialize::json::ToJson;
use std::collections::BTreeMap;
use geojson::{Feature, GeoJson, Geometry, Value};

let geometry = Geometry::new(
    Value::Point(vec![-120.66029,35.2812])
);

let geojson = GeoJson::Feature(Feature {
    crs: None,
    bbox: None,
    geometry: Some(geometry),
    id: None,
    properties: Some(properties),
});

let geojson_string = geojson.to_string();

Structs

Feature

Feature Objects

FeatureCollection

Feature Collection Objects

Geometry

Geometry Objects

Enums

Crs

Coordinate Reference System Objects

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