Skip to content

Commit

Permalink
Permit geometry field on feature objects to be null.
Browse files Browse the repository at this point in the history
Fixes #42.
  • Loading branch information
frewsxcv committed May 10, 2016
1 parent a077ac9 commit c4b32cd
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changes

## 0.3.0

* [Permit `geometry` field on `feature` objects to be `null`](https://github.com/georust/rust-geojson/issues/42)
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

name = "geojson"
description = "Library for serializing the GeoJSON vector GIS file format"
version = "0.2.1"
version = "0.3.0"
authors = ["Corey Farwell <coreyf@rwell.org>",
"Blake Grotewold <hello@grotewold.me>"]
license = "MIT/Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ properties.insert(
let geojson = GeoJson::Feature(Feature {
crs: None,
bbox: None,
geometry: geometry,
geometry: Some(geometry),
id: None,
properties: Some(properties),
});
Expand Down
27 changes: 23 additions & 4 deletions src/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use ::{Bbox, Crs, Error, FromObject, Geometry, util};
pub struct Feature {
pub bbox: Option<Bbox>,
pub crs: Option<Crs>,
pub geometry: Geometry,
pub geometry: Option<Geometry>,
pub id: Option<JsonValue>,
pub properties: Option<JsonObject>,
}
Expand Down Expand Up @@ -105,7 +105,7 @@ impl Deserialize for Feature {

#[cfg(test)]
mod tests {
use ::{Feature, Geometry, Value, GeoJson};
use ::{Error, Feature, Geometry, Value, GeoJson};

fn feature_json_str() -> &'static str {
"{\"geometry\":{\"coordinates\":[1.1,2.1],\"type\":\"Point\"},\"properties\":{},\"type\":\"Feature\"}"
Expand All @@ -124,11 +124,11 @@ mod tests {

fn feature() -> Feature {
::Feature {
geometry: Geometry {
geometry: Some(Geometry {
value: Value::Point(vec![1.1, 2.1]),
crs: None,
bbox: None,
},
}),
properties: properties(),
crs: None,
bbox: None,
Expand Down Expand Up @@ -173,4 +173,23 @@ mod tests {
};
assert_eq!(decoded_feature, feature);
}

#[test]
fn feature_json_null_geometry() {
let geojson_str = r#"{
"geometry": null,
"properties":{},
"type":"Feature"
}"#;
assert!(geojson_str.parse::<GeoJson>().is_ok());
}

#[test]
fn feature_json_invalid_geometry() {
let geojson_str = r#"{"geometry":3.14,"properties":{},"type":"Feature"}"#;
match geojson_str.parse::<GeoJson>().unwrap_err() {
Error::FeatureInvalidGeometryValue => (),
_ => unreachable!(),
}
}
}
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
//! let geojson = GeoJson::Feature(Feature {
//! crs: None,
//! bbox: None,
//! geometry: geometry,
//! geometry: Some(geometry),
//! id: None,
//! properties: Some(properties),
//! });
Expand Down Expand Up @@ -171,6 +171,7 @@ pub enum Error {
GeometryUnknownType,
MalformedJson,
PropertiesExpectedObjectOrNull,
FeatureInvalidGeometryValue,

// FIXME: make these types more specific
ExpectedStringValue,
Expand Down Expand Up @@ -209,6 +210,10 @@ impl std::fmt::Display for Error {
// FIXME: inform what type we actually found
write!(f, "Encountered neither object type nor null type for \
'properties' object."),
Error::FeatureInvalidGeometryValue =>
// FIXME: inform what type we actually found
write!(f, "Encountered neither object type nor null type for \
'geometry' field on 'feature' object."),
Error::ExpectedStringValue =>
write!(f, "Expected a string value."),
Error::ExpectedProperty =>
Expand Down Expand Up @@ -236,6 +241,8 @@ impl std::error::Error for Error {
Error::MalformedJson => "malformed JSON",
Error::PropertiesExpectedObjectOrNull =>
"neither object type nor null type for properties' object.",
Error::FeatureInvalidGeometryValue =>
"neither object type nor null type for 'geometry' field on 'feature' object.",
Error::ExpectedStringValue => "expected a string value",
Error::ExpectedProperty => "expected a GeoJSON 'property'",
Error::ExpectedF64Value => "expected a floating-point value",
Expand Down
13 changes: 10 additions & 3 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,16 @@ pub fn get_id(object: &JsonObject) -> Result<Option<JsonValue>, Error> {
}

/// Used by Feature
pub fn get_geometry(object: &JsonObject) -> Result<Geometry, Error> {
let geometry = expect_object!(expect_property!(object, "geometry", "Missing 'geometry' field"));
return Geometry::from_object(geometry);
pub fn get_geometry(object: &JsonObject) -> Result<Option<Geometry>, Error> {
let geometry = expect_property!(object, "geometry", "Missing 'geometry' field");
match *geometry {
JsonValue::Object(ref x) => {
let geometry_object = try!(Geometry::from_object(x));
Ok(Some(geometry_object))
},
JsonValue::Null => Ok(None),
_ => Err(Error::FeatureInvalidGeometryValue),
}
}

/// Used by FeatureCollection
Expand Down

0 comments on commit c4b32cd

Please sign in to comment.