Skip to content

Commit

Permalink
Merge #449
Browse files Browse the repository at this point in the history
449: Update_gdal_approx_eq r=ChristianBeilschmidt a=jdroenner



Co-authored-by: Johannes Drönner <[email protected]>
  • Loading branch information
bors[bot] and jdroenner authored Jan 20, 2022
2 parents 811eb76 + 84054c1 commit 944f91f
Show file tree
Hide file tree
Showing 15 changed files with 625 additions and 133 deletions.
2 changes: 1 addition & 1 deletion datatypes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pro = ["postgres"]
arrow = { version = "6.0", features = ["simd"] }
chrono = "0.4"
float-cmp = "0.9"
gdal = "0.11"
gdal = "0.12"
geo = "0.18"
geojson = "0.22"
image = "0.23"
Expand Down
62 changes: 42 additions & 20 deletions datatypes/src/collections/multi_line_string_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,12 @@ impl ReplaceRawArrayCoords for MultiLineStringCollection {

#[cfg(test)]
mod tests {
use float_cmp::approx_eq;

use super::*;

use crate::collections::{BuilderProvider, FeatureCollectionModifications};
use crate::primitives::TimeInterval;
use crate::primitives::{FeatureDataRef, TimeInterval};

#[test]
fn single_line() {
Expand Down Expand Up @@ -562,27 +564,47 @@ mod tests {
)
.unwrap();

let expected_collection = MultiLineStringCollection::from_slices(
&[
MultiLineString::new(vec![vec![MARBURG_EPSG_900_913, HAMBURG_EPSG_900_913]])
.unwrap(),
MultiLineString::new(vec![
vec![
COLOGNE_EPSG_900_913,
MARBURG_EPSG_900_913,
HAMBURG_EPSG_900_913,
],
vec![HAMBURG_EPSG_900_913, COLOGNE_EPSG_900_913],
])
.unwrap(),
],
&[TimeInterval::default(), TimeInterval::default()],
&[("A", FeatureData::Int(vec![1, 2]))],
)
.unwrap();
let expected = [
MultiLineString::new(vec![vec![MARBURG_EPSG_900_913, HAMBURG_EPSG_900_913]]).unwrap(),
MultiLineString::new(vec![
vec![
COLOGNE_EPSG_900_913,
MARBURG_EPSG_900_913,
HAMBURG_EPSG_900_913,
],
vec![HAMBURG_EPSG_900_913, COLOGNE_EPSG_900_913],
])
.unwrap(),
];

let proj_collection = collection.reproject(&projector).unwrap();

assert_eq!(proj_collection, expected_collection);
// Assert geometrys are approx equal
proj_collection
.geometries()
.into_iter()
.zip(expected.iter())
.for_each(|(a, e)| {
assert!(approx_eq!(
&MultiLineString,
&a.into(),
e,
epsilon = 0.00001
));
});

// Assert that feature time intervals did not move around
assert_eq!(proj_collection.time_intervals().len(), 2);
assert_eq!(
proj_collection.time_intervals(),
&[TimeInterval::default(), TimeInterval::default()]
);

// Assert that feature data did not magicaly disappear
if let FeatureDataRef::Int(numbers) = proj_collection.data("A").unwrap() {
assert_eq!(numbers.as_ref(), &[1, 2]);
} else {
unreachable!();
}
}
}
61 changes: 38 additions & 23 deletions datatypes/src/collections/multi_point_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,41 +1163,56 @@ mod tests {
],
{
let mut map = HashMap::new();
map.insert("numbers".into(), FeatureData::Float(vec![0., 1.]));
map.insert("numbers".into(), FeatureData::Int(vec![0, 1]));
map.insert(
"number_nulls".into(),
FeatureData::NullableFloat(vec![Some(0.), None]),
FeatureData::NullableInt(vec![Some(0), None]),
);
map
},
)
.unwrap();

let pc_expected = MultiPointCollection::from_data(
MultiPoint::many(vec![
vec![MARBURG_EPSG_900_913, COLOGNE_EPSG_900_913],
vec![HAMBURG_EPSG_900_913],
])
.unwrap(),
vec![
TimeInterval::new_unchecked(0, 1),
TimeInterval::new_unchecked(1, 2),
],
{
let mut map = HashMap::new();
map.insert("numbers".into(), FeatureData::Float(vec![0., 1.]));
map.insert(
"number_nulls".into(),
FeatureData::NullableFloat(vec![Some(0.), None]),
);
map
},
)
let expected_points = MultiPoint::many(vec![
vec![MARBURG_EPSG_900_913, COLOGNE_EPSG_900_913],
vec![HAMBURG_EPSG_900_913],
])
.unwrap();

let proj_pc = pc.reproject(&projector).unwrap();

assert_eq!(proj_pc, pc_expected);
// Assert geometrys are approx equal
proj_pc
.geometries()
.into_iter()
.zip(expected_points.iter())
.for_each(|(a, e)| {
assert!(approx_eq!(&MultiPoint, &a.into(), e, epsilon = 0.000_001));
});

// Assert that feature time intervals did not move around
assert_eq!(proj_pc.time_intervals().len(), 2);
assert_eq!(
proj_pc.time_intervals(),
&[
TimeInterval::new_unchecked(0, 1),
TimeInterval::new_unchecked(1, 2),
]
);

// Assert that feature data did not magicaly disappear
if let FeatureDataRef::Int(numbers) = proj_pc.data("numbers").unwrap() {
assert_eq!(numbers.as_ref(), &[0, 1]);
} else {
unreachable!();
}

if let FeatureDataRef::Int(numbers) = proj_pc.data("number_nulls").unwrap() {
assert_eq!(numbers.as_ref()[1], 0);
assert_eq!(numbers.nulls(), vec![false, true]);
} else {
unreachable!();
}
}

#[test]
Expand Down
73 changes: 45 additions & 28 deletions datatypes/src/collections/multi_polygon_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,12 +403,13 @@ impl From<Vec<geo::MultiPolygon<f64>>> for MultiPolygonCollection {

#[cfg(test)]
mod tests {
use float_cmp::approx_eq;
use geo::polygon;

use super::*;

use crate::collections::{BuilderProvider, FeatureCollectionModifications};
use crate::primitives::TimeInterval;
use crate::primitives::{FeatureDataRef, TimeInterval};

#[test]
fn single_polygons() {
Expand Down Expand Up @@ -823,7 +824,7 @@ mod tests {
}

#[test]
fn reproject_multi_lines_epsg4326_epsg900913_collection() {
fn reproject_multi_polygons_epsg4326_epsg900913_collection() {
use crate::operations::reproject::Reproject;
use crate::operations::reproject::{CoordinateProjection, CoordinateProjector};
use crate::primitives::FeatureData;
Expand Down Expand Up @@ -868,39 +869,55 @@ mod tests {
)
.unwrap();

let expected_collection = MultiPolygonCollection::from_slices(
&[
MultiPolygon::new(vec![
vec![vec![
HAMBURG_EPSG_900_913,
MARBURG_EPSG_900_913,
COLOGNE_EPSG_900_913,
HAMBURG_EPSG_900_913,
]],
vec![vec![
COLOGNE_EPSG_900_913,
HAMBURG_EPSG_900_913,
MARBURG_EPSG_900_913,
COLOGNE_EPSG_900_913,
]],
])
.unwrap(),
MultiPolygon::new(vec![vec![vec![
let expected = [
MultiPolygon::new(vec![
vec![vec![
HAMBURG_EPSG_900_913,
MARBURG_EPSG_900_913,
COLOGNE_EPSG_900_913,
HAMBURG_EPSG_900_913,
]],
vec![vec![
COLOGNE_EPSG_900_913,
HAMBURG_EPSG_900_913,
MARBURG_EPSG_900_913,
]]])
.unwrap(),
],
&[TimeInterval::default(), TimeInterval::default()],
&[("A", FeatureData::Int(vec![1, 2]))],
)
.unwrap();
COLOGNE_EPSG_900_913,
]],
])
.unwrap(),
MultiPolygon::new(vec![vec![vec![
MARBURG_EPSG_900_913,
COLOGNE_EPSG_900_913,
HAMBURG_EPSG_900_913,
MARBURG_EPSG_900_913,
]]])
.unwrap(),
];

let proj_collection = collection.reproject(&projector).unwrap();

assert_eq!(proj_collection, expected_collection);
// Assert geometrys are approx equal
proj_collection
.geometries()
.into_iter()
.zip(expected.iter())
.for_each(|(a, e)| {
assert!(approx_eq!(&MultiPolygon, &a.into(), e, epsilon = 0.00001));
});

// Assert that feature time intervals did not move around
assert_eq!(proj_collection.time_intervals().len(), 2);
assert_eq!(
proj_collection.time_intervals(),
&[TimeInterval::default(), TimeInterval::default()]
);

// Assert that feature data did not magicaly disappear
if let FeatureDataRef::Int(numbers) = proj_collection.data("A").unwrap() {
assert_eq!(numbers.as_ref(), &[1, 2]);
} else {
unreachable!();
}
}

#[test]
Expand Down
14 changes: 12 additions & 2 deletions datatypes/src/operations/reproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,12 @@ mod tests {
(20_037_508.342_789_244, 20_048_966.104_014_594).into(),
);

assert_eq!(projected, expected);
assert!(approx_eq!(
BoundingBox2D,
projected,
expected,
epsilon = 0.000_001
));
}

#[test]
Expand All @@ -678,7 +683,12 @@ mod tests {
(20_037_508.342_789_244, 20_048_966.104_014_594).into(),
);

assert_eq!(projected, expected);
assert!(approx_eq!(
BoundingBox2D,
projected,
expected,
epsilon = 0.000_001
));
}

#[test]
Expand Down
17 changes: 17 additions & 0 deletions datatypes/src/primitives/bounding_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::convert::TryFrom;
use super::{AxisAlignedRectangle, Coordinate2D, SpatialBounded};
use crate::error;
use crate::util::Result;
use float_cmp::ApproxEq;
#[cfg(feature = "postgres")]
use postgres_types::{FromSql, ToSql};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -549,6 +550,22 @@ impl TryFrom<BoundingBox2D> for gdal::vector::Geometry {
}
}

impl ApproxEq for BoundingBox2D {
type Margin = float_cmp::F64Margin;

fn approx_eq<M>(self, other: Self, margin: M) -> bool
where
M: Into<Self::Margin>,
{
let m = margin.into();
self.upper_right_coordinate
.approx_eq(other.upper_right_coordinate, m)
&& self
.lower_left_coordinate
.approx_eq(other.lower_left_coordinate, m)
}
}

#[cfg(test)]
mod tests {

Expand Down
13 changes: 13 additions & 0 deletions datatypes/src/primitives/coordinate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::util::arrow::ArrowTyped;
use arrow::array::{ArrayBuilder, BooleanArray, Float64Builder};
use arrow::datatypes::{DataType, Field};
use arrow::error::ArrowError;
use float_cmp::ApproxEq;
use ocl::OclPrm;
#[cfg(feature = "postgres")]
use postgres_types::{FromSql, ToSql};
Expand Down Expand Up @@ -304,6 +305,18 @@ impl Div<f64> for Coordinate2D {
}
}

impl ApproxEq for Coordinate2D {
type Margin = float_cmp::F64Margin;

fn approx_eq<M>(self, other: Self, margin: M) -> bool
where
M: Into<Self::Margin>,
{
let m = margin.into();
self.x.approx_eq(other.x, m) && self.y.approx_eq(other.y, m)
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
11 changes: 11 additions & 0 deletions datatypes/src/primitives/line.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use float_cmp::{ApproxEq, F64Margin};

use super::{BoundingBox2D, Coordinate2D, SpatialBounded};

#[derive(Debug, Clone, Copy, PartialEq)]
Expand Down Expand Up @@ -48,6 +50,15 @@ impl SpatialBounded for Line {
}
}

impl ApproxEq for Line {
type Margin = F64Margin;

fn approx_eq<M: Into<Self::Margin>>(self, other: Self, margin: M) -> bool {
let m = margin.into();
self.start.approx_eq(other.start, m) && self.end.approx_eq(other.end, m)
}
}

#[cfg(test)]
mod tests {
use float_cmp::approx_eq;
Expand Down
Loading

0 comments on commit 944f91f

Please sign in to comment.