Skip to content

Commit

Permalink
Merge #227
Browse files Browse the repository at this point in the history
227: Add a conversion from Vec<Feature> to GeoJson r=michaelkirk a=dabreegster

- [X] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md).
- [X] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.
---

As discussed in Discord, this is more convenient than `let gj = geojson::GeoJson::from(features.into_iter().collect::<geojson::FeatureCollection>());` or `let gj:GeoJson = FeatureCollection::from_iter(vec_of_geojson_features.into_iter()).into();` or similar

Co-authored-by: Dustin Carlino <dabreegster@gmail.com>
  • Loading branch information
bors[bot] and dabreegster authored Jul 11, 2023
2 parents e4db586 + 7c10b0e commit bfda106
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* Added conversion from `Vec<Feature>` to `GeoJson`.

## 0.24.1

* Modified conversion from JSON to reject zero- and one-dimensional positions.
Expand Down
41 changes: 40 additions & 1 deletion src/geojson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ impl From<FeatureCollection> for GeoJson {
}
}

impl From<Vec<Feature>> for GeoJson {
fn from(features: Vec<Feature>) -> GeoJson {
GeoJson::from(features.into_iter().collect::<FeatureCollection>())
}
}

impl TryFrom<GeoJson> for Geometry {
type Error = Error;
fn try_from(value: GeoJson) -> Result<Self> {
Expand Down Expand Up @@ -392,7 +398,7 @@ impl fmt::Display for FeatureCollection {

#[cfg(test)]
mod tests {
use crate::{Error, Feature, GeoJson, Geometry, Value};
use crate::{Error, Feature, FeatureCollection, GeoJson, Geometry, Value};
use serde_json::json;
use std::convert::TryInto;
use std::str::FromStr;
Expand Down Expand Up @@ -451,6 +457,39 @@ mod tests {
);
}

#[test]
fn test_geojson_from_features() {
let features: Vec<Feature> = vec![
Value::Point(vec![0., 0., 0.]).into(),
Value::Point(vec![1., 1., 1.]).into(),
];

let geojson: GeoJson = features.into();
assert_eq!(
geojson,
GeoJson::FeatureCollection(FeatureCollection {
features: vec![
Feature {
bbox: None,
geometry: Some(Geometry::new(Value::Point(vec![0., 0., 0.]))),
id: None,
properties: None,
foreign_members: None,
},
Feature {
bbox: None,
geometry: Some(Geometry::new(Value::Point(vec![1., 1., 1.]))),
id: None,
properties: None,
foreign_members: None,
},
],
bbox: None,
foreign_members: None,
})
);
}

#[test]
fn test_missing_properties_key() {
let json_value = json!({
Expand Down

0 comments on commit bfda106

Please sign in to comment.