From 7ec278a537ca8c1e160c98e883c9c06122fb1c79 Mon Sep 17 00:00:00 2001 From: staudtMarius Date: Fri, 23 Feb 2024 12:47:19 +0100 Subject: [PATCH] Fixing bug in `GeoUtils.calcOrderedCoordinateDistances()`. --- CHANGELOG.md | 7 +++++++ src/main/java/edu/ie3/util/geo/GeoUtils.java | 7 ++++--- .../edu/ie3/util/geo/GeoUtilsTest.groovy | 19 +++++++++++++++++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 601a4aa4..5dfa362f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased/Snapshot] +### Changed +- `GeoUtils.calcOrderedCoordinateDistances()` now returns a manually sorted `Set` instead of a `SortedSet` [#449](https://github.com/ie3-institute/PowerSystemUtils/issues/449) + +### Fixed +- Bug where `GeoUtils.calcOrderedCoordinateDistances()` didn't return all `CoordinateDistance` [#449](https://github.com/ie3-institute/PowerSystemUtils/issues/449) + + ## [2.2.0] ### Changed diff --git a/src/main/java/edu/ie3/util/geo/GeoUtils.java b/src/main/java/edu/ie3/util/geo/GeoUtils.java index ca936db4..940cd231 100644 --- a/src/main/java/edu/ie3/util/geo/GeoUtils.java +++ b/src/main/java/edu/ie3/util/geo/GeoUtils.java @@ -120,7 +120,7 @@ private static Coordinate buildSafeCoord(Coordinate coord) { * @deprecated Use {@link #calcOrderedCoordinateDistances(Point, Collection)} instead. */ @Deprecated(since = "2.0", forRemoval = true) - public static SortedSet getCoordinateDistances( + public static Set getCoordinateDistances( Point baseCoordinate, Collection coordinates) { return calcOrderedCoordinateDistances(baseCoordinate, coordinates); } @@ -133,11 +133,12 @@ public static SortedSet getCoordinateDistances( * @param coordinates the points to calculate the distance from the base point for * @return a sorted set of distances between the base and other coordinates */ - public static SortedSet calcOrderedCoordinateDistances( + public static Set calcOrderedCoordinateDistances( Point baseCoordinate, Collection coordinates) { return coordinates.stream() .map(coordinate -> new CoordinateDistance(baseCoordinate, coordinate)) - .collect(Collectors.toCollection(TreeSet::new)); + .sorted(Comparator.comparing(CoordinateDistance::getDistance)) + .collect(Collectors.toCollection(LinkedHashSet::new)); } /** diff --git a/src/test/groovy/edu/ie3/util/geo/GeoUtilsTest.groovy b/src/test/groovy/edu/ie3/util/geo/GeoUtilsTest.groovy index badade3d..bed4a6bc 100644 --- a/src/test/groovy/edu/ie3/util/geo/GeoUtilsTest.groovy +++ b/src/test/groovy/edu/ie3/util/geo/GeoUtilsTest.groovy @@ -131,14 +131,29 @@ class GeoUtilsTest extends Specification { GeoUtils.buildPoint(49d, 7.1d) ] def coordinateDistances = [ + new CoordinateDistance(basePoint, points[3]), new CoordinateDistance(basePoint, points[0]), new CoordinateDistance(basePoint, points[1]), - new CoordinateDistance(basePoint, points[3]), new CoordinateDistance(basePoint, points[2]) ] expect: - GeoUtils.calcOrderedCoordinateDistances(basePoint, points) == new TreeSet(coordinateDistances) + GeoUtils.calcOrderedCoordinateDistances(basePoint, points) == new HashSet(coordinateDistances) + } + + def "GeoUtils should return all CoordinateDistances correctly"() { + given: + def basePoint = GeoUtils.buildPoint(50.5, 7d) + def points = [ + GeoUtils.buildPoint(50d, 7d), + GeoUtils.buildPoint(51d, 7d) + ] + + when: + def actual = GeoUtils.calcOrderedCoordinateDistances(basePoint, points) + + then: + actual.size() == 2 } def "GeoUtils should calculate haversine distance between two points correctly"() {