open-wether / Sources /App /Domains /LambertAzimuthalEqualAreaProjection.swift
soiz1's picture
Migrated from GitHub
6ee917b verified
import Foundation
/// See https://mathworld.wolfram.com/LambertAzimuthalEqual-AreaProjection.html
struct LambertAzimuthalEqualAreaProjection: Projectable {
let0: Float
let1: Float
let R: Float
/*
位0 central longitude
蠒1 standard parallal
radius of earth
*/
init(位0 位0_dec: Float, 蠒1 蠒1_dec: Float, radius: Float = 6371229) {
0 = 位0_dec.degreesToRadians
1 = 蠒1_dec.degreesToRadians
R = radius
}
func forward(latitude: Float, longitude: Float) -> (x: Float, y: Float) {
let= longitude.degreesToRadians
let= latitude.degreesToRadians
let k = sqrtf(2/(1 + sinf(蠒1) * sinf(蠒) + cosf(蠒1) * cosf(蠒) * cosf(位 -0)))
let x = R * k * cosf(蠒) * sinf(位 -0)
let y = R * k * (cosf(蠒1) * sinf(蠒) - sinf(蠒1) * cos(蠒) * cosf(位 -0))
return (x, y)
}
func inverse(x: Float, y: Float) -> (latitude: Float, longitude: Float) {
let x = x / R
let y = y / R
let p = sqrtf(x*x + y*y)
let c = 2 * asinf(0.5 * p)
let= asinf(cosf(c) * sinf(蠒1) + (y * sinf(c) * cosf(蠒1))/p)
let=0 + atanf((x * sinf(c) / (p * cosf(蠒1) * cosf(c) - y * sinf(蠒1) * sinf(c))))
return (蠒.radiansToDegrees, 位.radiansToDegrees)
}
}