From 2aa426103b8d45c813dca5fe766eff1599aea112 Mon Sep 17 00:00:00 2001 From: Jilles van Gurp Date: Fri, 29 Jan 2021 10:51:16 +0100 Subject: [PATCH] short distance test & cleanup add missing fromRadians --- .../com/jillesvangurp/geo/GeoGeometry.kt | 13 +++--- .../geogeometry/GeoGeometryJvmTest.kt | 41 +++++++++++-------- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/commonMain/kotlin/com/jillesvangurp/geo/GeoGeometry.kt b/src/commonMain/kotlin/com/jillesvangurp/geo/GeoGeometry.kt index 326c2cd..3646cad 100644 --- a/src/commonMain/kotlin/com/jillesvangurp/geo/GeoGeometry.kt +++ b/src/commonMain/kotlin/com/jillesvangurp/geo/GeoGeometry.kt @@ -527,14 +527,14 @@ class GeoGeometry { return doubleArrayOf(westLon, southLat, eastLon, northLat) } - /** - * Kotlin math seems to not have this unlike Java But it is easily replicated like this - */ - fun toRadians(degrees: Double): Double { return degrees * DEGREES_TO_RADIANS } + fun fromRadians(degrees: Double): Double { + return degrees * 1/DEGREES_TO_RADIANS + } + /** * Compute the Haversine distance between the two coordinates. Haversine is * one of several distance calculation algorithms that exist. It is not very @@ -563,10 +563,7 @@ class GeoGeometry { val deltaLon = toRadians(long2 - long1) val a = - sin(deltaLat / 2) * sin(deltaLat / 2) + cos(toRadians(lat1)) * cos(toRadians(lat2)) * sin( - deltaLon / 2 - ) * sin( - deltaLon / 2 + sin(deltaLat / 2) * sin(deltaLat / 2) + cos(toRadians(lat1)) * cos(toRadians(lat2)) * sin(deltaLon / 2) * sin(deltaLon / 2 ) val c = 2 * asin(sqrt(a)) diff --git a/src/jvmTest/kotlin/com/jillesvangurp/geogeometry/GeoGeometryJvmTest.kt b/src/jvmTest/kotlin/com/jillesvangurp/geogeometry/GeoGeometryJvmTest.kt index da7b987..1241a26 100644 --- a/src/jvmTest/kotlin/com/jillesvangurp/geogeometry/GeoGeometryJvmTest.kt +++ b/src/jvmTest/kotlin/com/jillesvangurp/geogeometry/GeoGeometryJvmTest.kt @@ -37,24 +37,27 @@ import org.hamcrest.MatcherAssert import org.junit.Test import java.util.Random import kotlin.math.abs +import kotlin.math.round class GeoGeometryJvmTest { - var sydney = doubleArrayOf(151.206146, -33.872796) - var buenosaires = doubleArrayOf(-58.380449, -34.602875) - var newyork = doubleArrayOf(-74.011237, 40.721119) - var amsterdam = doubleArrayOf(4.894252, 52.372103) - var berlin = doubleArrayOf(13.385721, 52.527109) - var london = doubleArrayOf(-0.123656, 51.51283) - - var brandenBurgerGate = doubleArrayOf(13.377157, 52.516279) - var potsDammerPlatz = doubleArrayOf(13.376599, 52.509515) - var moritzPlatz = doubleArrayOf(13.410717, 52.503663) - var senefelderPlatz = doubleArrayOf(13.412949, 52.532755) - var naturkundeMuseum = doubleArrayOf(13.381921, 52.531188) - var rosenthalerPlatz = doubleArrayOf(13.401361, 52.529948) - var oranienburgerTor = doubleArrayOf(13.38707, 52.525339) - - var samplePolygon = arrayOf( + val bergstr16InvalidenBerlin = doubleArrayOf(13.393674,52.5310059) + val bergstr16Berlin = doubleArrayOf(13.3941763, 52.5298311) + val berlin = doubleArrayOf(13.385721, 52.527109) + val sydney = doubleArrayOf(151.206146, -33.872796) + val buenosaires = doubleArrayOf(-58.380449, -34.602875) + val newyork = doubleArrayOf(-74.011237, 40.721119) + val amsterdam = doubleArrayOf(4.894252, 52.372103) + val london = doubleArrayOf(-0.123656, 51.51283) + + val brandenBurgerGate = doubleArrayOf(13.377157, 52.516279) + val potsDammerPlatz = doubleArrayOf(13.376599, 52.509515) + val moritzPlatz = doubleArrayOf(13.410717, 52.503663) + val senefelderPlatz = doubleArrayOf(13.412949, 52.532755) + val naturkundeMuseum = doubleArrayOf(13.381921, 52.531188) + val rosenthalerPlatz = doubleArrayOf(13.401361, 52.529948) + val oranienburgerTor = doubleArrayOf(13.38707, 52.525339) + + val samplePolygon = arrayOf( doubleArrayOf(1.0, 1.0), doubleArrayOf(1.0, -1.0), doubleArrayOf(-1.0, -1.0), @@ -167,6 +170,12 @@ class GeoGeometryJvmTest { ) } + @Test + fun shouldCalculateShortDistance() { + val d = distance(bergstr16Berlin, bergstr16InvalidenBerlin) + round(d) shouldBe 135.0 + } + @Test fun shouldTranslateCorrectly() { val translated = translate(52.530564, 13.394964, 1000.0, 3000.0)