Skip to content

Commit

Permalink
Fixing bug in GeoUtils.calcOrderedCoordinateDistances().
Browse files Browse the repository at this point in the history
  • Loading branch information
staudtMarius committed Feb 23, 2024
1 parent 0069a5e commit 7ec278a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/edu/ie3/util/geo/GeoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<CoordinateDistance> getCoordinateDistances(
public static Set<CoordinateDistance> getCoordinateDistances(

Check notice on line 123 in src/main/java/edu/ie3/util/geo/GeoUtils.java

View check run for this annotation

SonarQubeGithubPRChecks / utils Sonarqube Results

src/main/java/edu/ie3/util/geo/GeoUtils.java#L123

Do not forget to remove this deprecated code someday.
Point baseCoordinate, Collection<Point> coordinates) {
return calcOrderedCoordinateDistances(baseCoordinate, coordinates);
}
Expand All @@ -133,11 +133,12 @@ public static SortedSet<CoordinateDistance> 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<CoordinateDistance> calcOrderedCoordinateDistances(
public static Set<CoordinateDistance> calcOrderedCoordinateDistances(
Point baseCoordinate, Collection<Point> 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));
}

/**
Expand Down
19 changes: 17 additions & 2 deletions src/test/groovy/edu/ie3/util/geo/GeoUtilsTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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"() {
Expand Down

0 comments on commit 7ec278a

Please sign in to comment.