forked from kscience/kmath
Web mercator conversion
This commit is contained in:
parent
081304f110
commit
027ab64989
@ -9,24 +9,55 @@ import space.kscience.kmath.operations.Algebra
|
||||
import kotlin.math.*
|
||||
|
||||
public class GeodeticCoordinates private constructor(public val latitude: Double, public val longitude: Double) {
|
||||
init {
|
||||
require(longitude in (-PI)..(PI)) { "Longitude $longitude is not in (-PI)..(PI)" }
|
||||
require(latitude in (-PI / 2)..(PI / 2)) { "Latitude $latitude is not in (-PI/2)..(PI/2)" }
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other == null || this::class != other::class) return false
|
||||
|
||||
other as GeodeticCoordinates
|
||||
|
||||
if (latitude != other.latitude) return false
|
||||
if (longitude != other.longitude) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
public companion object {
|
||||
public fun ofRadians(longitude: Double, latitude: Double): GeodeticCoordinates =
|
||||
GeodeticCoordinates(latitude, longitude)
|
||||
override fun hashCode(): Int {
|
||||
var result = latitude.hashCode()
|
||||
result = 31 * result + longitude.hashCode()
|
||||
return result
|
||||
}
|
||||
|
||||
public fun ofDegrees(longitude: Double, latitude: Double): GeodeticCoordinates =
|
||||
GeodeticCoordinates(latitude * PI / 180, longitude * PI / 180)
|
||||
override fun toString(): String {
|
||||
return "GeodeticCoordinates(latitude=$latitude, longitude=$longitude)"
|
||||
}
|
||||
|
||||
|
||||
public companion object {
|
||||
public fun ofRadians(latitude: Double, longitude: Double): GeodeticCoordinates {
|
||||
require(longitude in (-PI)..(PI)) { "Longitude $longitude is not in (-PI)..(PI)" }
|
||||
return GeodeticCoordinates(latitude, longitude.rem(PI / 2))
|
||||
}
|
||||
|
||||
public fun ofDegrees(latitude: Double, longitude: Double): GeodeticCoordinates {
|
||||
require(latitude in (-90.0)..(90.0)) { "Latitude $latitude is not in -90..90" }
|
||||
return GeodeticCoordinates(latitude * PI / 180, (longitude.rem(180) * PI / 180))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public data class WebMercatorCoordinates(val zoom: Double, val x: Double, val y: Double)
|
||||
|
||||
public object WebMercatorAlgebra : Algebra<WebMercatorCoordinates> {
|
||||
fun WebMercatorCoordinates.toGeodetic(): GeodeticCoordinates = TODO()
|
||||
|
||||
private fun scaleFactor(zoom: Double) = 256.0 / 2 / PI * 2.0.pow(zoom)
|
||||
|
||||
public fun WebMercatorCoordinates.toGeodetic(): GeodeticCoordinates {
|
||||
val scaleFactor = scaleFactor(zoom)
|
||||
val longitude = x / scaleFactor - PI
|
||||
val latitude = (atan(exp(PI - y / scaleFactor)) - PI / 4) * 2
|
||||
return GeodeticCoordinates.ofRadians(latitude, longitude)
|
||||
}
|
||||
|
||||
/**
|
||||
* https://en.wikipedia.org/wiki/Web_Mercator_projection#Formulas
|
||||
@ -34,7 +65,7 @@ public object WebMercatorAlgebra : Algebra<WebMercatorCoordinates> {
|
||||
public fun GeodeticCoordinates.toMercator(zoom: Double): WebMercatorCoordinates {
|
||||
require(abs(latitude) <= 2 * atan(E.pow(PI)) - PI / 2) { "Latitude exceeds the maximum latitude for mercator coordinates" }
|
||||
|
||||
val scaleFactor = 256.0 / 2 / PI * 2.0.pow(zoom)
|
||||
val scaleFactor = scaleFactor(zoom)
|
||||
return WebMercatorCoordinates(
|
||||
zoom = zoom,
|
||||
x = scaleFactor * (longitude + PI),
|
||||
@ -57,7 +88,7 @@ public object WebMercatorAlgebra : Algebra<WebMercatorCoordinates> {
|
||||
)
|
||||
|
||||
val computedZoom = zoom ?: floor(log2(PI / max(abs(xOffsetUnscaled), abs(yOffsetUnscaled))))
|
||||
val scaleFactor = 256.0 / 2 / PI * 2.0.pow(computedZoom)
|
||||
val scaleFactor = scaleFactor(computedZoom)
|
||||
return WebMercatorCoordinates(
|
||||
computedZoom,
|
||||
x = scaleFactor * xOffsetUnscaled,
|
||||
|
@ -6,12 +6,28 @@
|
||||
package space.kscience.kmath.geometry
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class MercatorTest {
|
||||
|
||||
@Test
|
||||
fun mercatorOffset(){
|
||||
fun boundaries() {
|
||||
assertEquals(GeodeticCoordinates.ofDegrees(45.0, 20.0), GeodeticCoordinates.ofDegrees(45.0, 200.0))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun webMercatorConversion() = with(WebMercatorAlgebra) {
|
||||
val mskCoordinates = GeodeticCoordinates.ofDegrees(55.7558, 37.6173)
|
||||
val m = mskCoordinates.toMercator(2.0)
|
||||
val r = m.toGeodetic()
|
||||
|
||||
assertEquals(mskCoordinates.longitude, r.longitude,1e-4)
|
||||
assertEquals(mskCoordinates.latitude, r.latitude,1e-4)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun webMercatorOffset() {
|
||||
val mskCoordinates = GeodeticCoordinates.ofDegrees(55.7558, 37.6173)
|
||||
val spbCoordinates = GeodeticCoordinates.ofDegrees(59.9311, 30.3609)
|
||||
|
||||
@ -19,6 +35,6 @@ class MercatorTest {
|
||||
|
||||
assertTrue { offset.x in -127.0..128.0 }
|
||||
assertTrue { offset.y in -127.0..128.0 }
|
||||
assertTrue { offset.zoom > 1.0 }
|
||||
assertTrue { offset.zoom > 0.0 }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user