Skip to content

Commit e1913b5

Browse files
Merge pull request #284 from ie3-institute/sp/#283-haversine-speedup
Remove earth radius conversion from calcHaversine
2 parents c41ca86 + ce1afa0 commit e1913b5

File tree

2 files changed

+4
-7
lines changed

2 files changed

+4
-7
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,11 @@ public static SortedSet<CoordinateDistance> calcOrderedCoordinateDistances(
154154
* @param lngA longitude of coordinate a
155155
* @param latB latitude of coordinate b
156156
* @param lngB longitude of coordinate b
157-
* @return The distance between both coordinates in {@link
158-
* edu.ie3.util.quantities.PowerSystemUnits#KILOMETRE}
157+
* @return The distance between both coordinates as a {@link Length}
159158
*/
160159
public static ComparableQuantity<Length> calcHaversine(
161160
double latA, double lngA, double latB, double lngB) {
162161

163-
// average radius of the earth in km
164-
ComparableQuantity<Length> r = EARTH_RADIUS.to(KILOMETRE);
165162
ComparableQuantity<Angle> dLat = Quantities.getQuantity(toRadians(latB - latA), RADIAN);
166163
ComparableQuantity<Angle> dLon = Quantities.getQuantity(toRadians(lngB - lngA), RADIAN);
167164
double a =
@@ -171,7 +168,7 @@ public static ComparableQuantity<Length> calcHaversine(
171168
* sin(dLon.getValue().doubleValue() / 2)
172169
* sin(dLon.getValue().doubleValue() / 2);
173170
double c = 2 * atan2(sqrt(a), sqrt(1 - a));
174-
return r.multiply(c);
171+
return EARTH_RADIUS.multiply(c);
175172
}
176173

177174
/**
@@ -207,7 +204,7 @@ public static ComparableQuantity<Length> totalLengthOfLineString(LineString line
207204
* @return the length of the linestring as a quantity
208205
*/
209206
public static ComparableQuantity<Length> calcHaversine(LineString lineString) {
210-
ComparableQuantity<Length> y = Quantities.getQuantity(0, KILOMETRE);
207+
ComparableQuantity<Length> y = Quantities.getQuantity(0, METRE);
211208
for (int i = 0; i < lineString.getNumPoints() - 1; i++) {
212209
y = y.add(calcHaversine(lineString.getCoordinateN(i), lineString.getCoordinateN(i + 1)));
213210
}

src/test/groovy/edu/ie3/util/geo/GeoUtilsTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class GeoUtilsTest extends Specification {
171171
ComparableQuantity<Length> y = GeoUtils.calcHaversine(lineString)
172172

173173
then:
174-
QuantityUtil.isEquivalentAbs(y, Quantities.getQuantity(3463.37, PowerSystemUnits.KILOMETRE), 10)
174+
QuantityUtil.isEquivalentAbs(y, Quantities.getQuantity(3463370, METRE), 10000)
175175
// Value from Google Maps, error range of +-10 km
176176
}
177177

0 commit comments

Comments
 (0)