Skip to content

Commit

Permalink
short distance test & cleanup
Browse files Browse the repository at this point in the history
add missing fromRadians
  • Loading branch information
jillesvangurp committed Jan 29, 2021
1 parent 0dfa118 commit 2aa4261
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
13 changes: 5 additions & 8 deletions src/commonMain/kotlin/com/jillesvangurp/geo/GeoGeometry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2aa4261

Please sign in to comment.