-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathellipsoid.go
65 lines (58 loc) · 1.44 KB
/
ellipsoid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package osgb
import (
"math"
)
type ellipsoid struct {
semiMajorAxis float64
semiMinorAxis float64
}
var (
airyEllipsoid = &ellipsoid{
semiMajorAxis: 6377563.396,
semiMinorAxis: 6356256.909,
}
grs80Ellipsoid = &ellipsoid{
semiMajorAxis: 6378137.000,
semiMinorAxis: 6356752.3141,
}
)
func (el *ellipsoid) eccentricity() float64 {
aSq := el.semiMajorAxis * el.semiMajorAxis
bSq := el.semiMinorAxis * el.semiMinorAxis
return (aSq - bSq) / aSq
}
func (el *ellipsoid) geographicToCartesian(c *geographicCoord) *cartesianCoord {
eSq := el.eccentricity()
v := el.semiMajorAxis / math.Sqrt(1.0-eSq*math.Sin(c.lat)*math.Sin(c.lat))
x := (v + c.height) * math.Cos(c.lat) * math.Cos(c.lon)
y := (v + c.height) * math.Cos(c.lat) * math.Sin(c.lon)
z := ((1-eSq)*v + c.height) * math.Sin(c.lat)
return &cartesianCoord{
x: x,
y: y,
z: z,
}
}
func (el *ellipsoid) cartesianToGeographic(c *cartesianCoord) *geographicCoord {
lon := math.Atan(c.y / c.x)
p := math.Sqrt(c.x*c.x + c.y*c.y)
eSq := el.eccentricity()
lat := math.Atan(c.z / (p * (1 - eSq)))
var v float64
// Iteratively reach new latitude value
for {
v = el.semiMajorAxis / math.Sqrt(1.0-eSq*math.Sin(lat)*math.Sin(lat))
newLat := math.Atan((c.z + eSq*v*math.Sin(lat)) / p)
const epsilon = 0.00000000001
if math.Abs(newLat-lat) < epsilon {
break
}
lat = newLat
}
height := p/math.Cos(lat) - v
return &geographicCoord{
lat: lat,
lon: lon,
height: height,
}
}