1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
// Copyright 2015 The GeoRust Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use geo_types::{self, CoordFloat, GeometryCollection};
use crate::geojson::GeoJson;
use crate::geojson::GeoJson::{Feature, FeatureCollection, Geometry};
use crate::Result;
use std::convert::TryInto;
#[cfg(test)]
macro_rules! assert_almost_eq {
($x:expr, $y:expr, $epsilon:expr) => {{
use num_traits::Zero;
let a = $x.abs();
let b = $y.abs();
let delta = (a - b).abs();
if a.is_infinite() || a.is_nan() || b.is_infinite() || b.is_nan() {
panic!(
"Assertion failed: Non comparable value ({} = {}, {} = {})",
stringify!($x),
$x,
stringify!($y),
$y
);
} else if a.is_zero() || b.is_zero() {
if delta > $epsilon {
panic!(
"Assertion failed: ({} = {}, {} = {}, delta = {})",
stringify!($x),
$x,
stringify!($y),
$y,
delta / b
);
}
} else {
let normalized_delta = delta / b;
if normalized_delta > $epsilon {
panic!(
"Assertion failed: ({} = {}, {} = {}, delta = {})",
stringify!($x),
$x,
stringify!($y),
$y,
normalized_delta
);
}
}
}};
}
macro_rules! try_from_owned_value {
($to:ty) => {
#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))]
impl<T: CoordFloat> TryFrom<geometry::Value> for $to {
type Error = Error;
fn try_from(value: geometry::Value) -> Result<Self> {
(&value).try_into()
}
}
};
}
pub(crate) mod from_geo_types;
pub(crate) mod to_geo_types;
// Process top-level `GeoJSON` items, returning a geo_types::GeometryCollection or an Error
fn process_geojson<T>(gj: &GeoJson) -> Result<geo_types::GeometryCollection<T>>
where
T: CoordFloat,
{
match gj {
FeatureCollection(collection) => Ok(GeometryCollection(
collection
.features
.iter()
// Only pass on non-empty geometries
.filter_map(|feature| feature.geometry.as_ref())
.map(|geometry| geometry.clone().try_into())
.collect::<Result<_>>()?,
)),
Feature(feature) => {
if let Some(geometry) = &feature.geometry {
Ok(GeometryCollection(vec![geometry.clone().try_into()?]))
} else {
Ok(GeometryCollection(vec![]))
}
}
Geometry(geometry) => Ok(GeometryCollection(vec![geometry.clone().try_into()?])),
}
}
/// A shortcut for producing `geo_types` [GeometryCollection](../geo_types/struct.GeometryCollection.html) objects
/// from arbitrary valid GeoJSON input.
///
/// This function is primarily intended for easy processing of GeoJSON `FeatureCollection`
/// objects using the `geo` crate, and sacrifices a little performance for convenience.
/// # Example
///
/// ```
/// use geo_types::GeometryCollection;
/// use geojson::{quick_collection, GeoJson};
///
/// let geojson_str = r#"
/// {
/// "type": "FeatureCollection",
/// "features": [
/// {
/// "type": "Feature",
/// "properties": {},
/// "geometry": {
/// "type": "Point",
/// "coordinates": [
/// -0.13583511114120483,
/// 51.5218870403801
/// ]
/// }
/// }
/// ]
/// }
/// "#;
/// let geojson = geojson_str.parse::<GeoJson>().unwrap();
/// // Turn the GeoJSON string into a geo_types GeometryCollection
/// let mut collection: GeometryCollection<f64> = quick_collection(&geojson).unwrap();
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))]
pub fn quick_collection<T>(gj: &GeoJson) -> Result<geo_types::GeometryCollection<T>>
where
T: CoordFloat,
{
process_geojson(gj)
}