Enum geojson::GeoJson

source ·
pub enum GeoJson {
    Geometry(Geometry),
    Feature(Feature),
    FeatureCollection(FeatureCollection),
}
Expand description

GeoJSON Objects

use std::convert::TryInto;
use geojson::{Feature, GeoJson, Geometry, Value};
use serde_json::json;
let json_value = json!({
    "type": "Feature",
    "geometry": {
        "type": "Point",
        "coordinates": [102.0, 0.5]
    },
    "properties": null,
});
let feature: Feature = json_value.try_into().unwrap();

// Easily convert a feature to a GeoJson
let geojson: GeoJson = feature.into();
// and back again
let feature2: Feature = geojson.try_into().unwrap();

GeoJSON Format Specification § 3

Variants§

§

Geometry(Geometry)

§

Feature(Feature)

§

FeatureCollection(FeatureCollection)

Implementations§

source§

impl GeoJson

source

pub fn from_json_object(object: JsonObject) -> Result<Self>

source

pub fn from_json_value(value: JsonValue) -> Result<Self>

Converts a JSON Value into a GeoJson object.

Example
use std::convert::TryInto;
use geojson::{Feature, GeoJson, Geometry, Value};
use serde_json::json;

let json_value = json!({
    "type": "Feature",
    "geometry": {
        "type": "Point",
        "coordinates": [102.0, 0.5]
    },
    "properties": null,
});

assert!(json_value.is_object());

let geojson: GeoJson = json_value.try_into().unwrap();

assert_eq!(
    geojson,
    GeoJson::Feature(Feature {
        bbox: None,
        geometry: Some(Geometry::new(Value::Point(vec![102.0, 0.5]))),
        id: None,
        properties: None,
        foreign_members: None,
    })
);
source

pub fn to_json_value(self) -> JsonValue

Convenience method to convert to a JSON Value. Uses From.

use std::convert::TryFrom;
use geojson::GeoJson;
use serde_json::json;

let geojson = GeoJson::try_from( json!({
       "type": "Feature",
       "geometry": {
           "type": "Point",
           "coordinates": [102.0, 0.5]
       },
       "properties": {},
    })).unwrap();

let json_value = geojson.to_json_value();
assert_eq!(json_value,
    json!({
       "type": "Feature",
       "geometry": {
           "type": "Point",
           "coordinates": [102.0, 0.5]
       },
       "properties": {},
    })
   );
source

pub fn from_reader<R>(rdr: R) -> Result<Self>where R: Read,

Trait Implementations§

source§

impl Clone for GeoJson

source§

fn clone(&self) -> GeoJson

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for GeoJson

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for GeoJson

source§

fn deserialize<D>(deserializer: D) -> Result<GeoJson, D::Error>where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for GeoJson

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a> From<&'a GeoJson> for JsonObject

source§

fn from(geojson: &'a GeoJson) -> JsonObject

Converts to this type from the input type.
source§

impl From<Feature> for GeoJson

source§

fn from(feature: Feature) -> Self

Converts to this type from the input type.
source§

impl From<FeatureCollection> for GeoJson

source§

fn from(feature_collection: FeatureCollection) -> GeoJson

Converts to this type from the input type.
source§

impl<G: Into<Geometry>> From<G> for GeoJson

source§

fn from(geometry: G) -> Self

Converts to this type from the input type.
source§

impl From<GeoJson> for JsonValue

source§

fn from(geojson: GeoJson) -> JsonValue

Converts to this type from the input type.
source§

impl<G: Into<Geometry>> FromIterator<G> for GeoJson

source§

fn from_iter<I: IntoIterator<Item = G>>(iter: I) -> Self

Creates a value from an iterator. Read more
source§

impl FromStr for GeoJson

Example

 use geojson::GeoJson;
 use std::str::FromStr;

 let geojson_str = r#"{
   "type": "FeatureCollection",
   "features": [
     {
       "type": "Feature",
       "properties": {},
       "geometry": {
         "type": "Point",
         "coordinates": [
           -0.13583511114120483,
           51.5218870403801
         ]
       }
     }
   ]
 }
 "#;
 let geo_json = GeoJson::from_str(&geojson_str).unwrap();
 if let GeoJson::FeatureCollection(collection) = geo_json {
     assert_eq!(1, collection.features.len());
 } else {
     panic!("expected feature collection");
 }
§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self>

Parses a string s to return a value of this type. Read more
source§

impl PartialEq<GeoJson> for GeoJson

source§

fn eq(&self, other: &GeoJson) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for GeoJson

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl TryFrom<GeoJson> for Feature

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: GeoJson) -> Result<Self>

Performs the conversion.
source§

impl TryFrom<GeoJson> for FeatureCollection

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: GeoJson) -> Result<Self>

Performs the conversion.
source§

impl TryFrom<GeoJson> for Geometry

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: GeoJson) -> Result<Self>

Performs the conversion.
source§

impl<T> TryFrom<GeoJson> for Geometry<T>where T: CoordFloat,

Available on crate feature geo-types only.
§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(val: GeoJson) -> Result<Geometry<T>>

Performs the conversion.
source§

impl TryFrom<Map<String, Value>> for GeoJson

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(object: JsonObject) -> Result<Self>

Performs the conversion.
source§

impl TryFrom<Value> for GeoJson

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: JsonValue) -> Result<Self>

Performs the conversion.
source§

impl StructuralPartialEq for GeoJson

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,