-
Notifications
You must be signed in to change notification settings - Fork 0
/
oblique.go
147 lines (126 loc) · 4 KB
/
oblique.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package flatsphere
import (
"math"
)
type ObliqueAspect struct {
poleLat float64
poleLon float64
poleTheta float64
sinPoleLat float64 // cached precompute of sin(poleLat)
cosPoleLat float64 // cached precompute of cos(poleLat)
}
func NewObliqueAspect(poleLat float64, poleLon float64, poleTheta float64) ObliqueAspect {
return ObliqueAspect{
poleLat: poleLat,
poleLon: poleLon,
poleTheta: poleTheta,
sinPoleLat: math.Sin(poleLat),
cosPoleLat: math.Cos(poleLat),
}
}
// Applies the pole shift and rotation of the Oblique projection transform to the given
// input latitude and longitude points, so that the returned latitude/longitude are able
// to be used for the non-transformed 'original' projection.
func (o ObliqueAspect) TransformFromOblique(latitude float64, longitude float64) (float64, float64) {
var newLat, newLon float64
poleRelCos := math.Cos(o.poleLon - longitude)
// relative latitude
if o.poleLat == math.Pi/2 {
newLat = latitude
} else {
preAsin := o.sinPoleLat*math.Sin(latitude) + o.cosPoleLat*math.Cos(latitude)*poleRelCos
if preAsin > 1 && preAsin < 1+1e-9 {
preAsin = 1
}
newLat = math.Asin(preAsin)
}
// relative longitude
if o.poleLat == math.Pi/2 {
newLon = longitude - o.poleLon
} else if o.poleLat == -math.Pi/2 {
newLon = o.poleLon - longitude - math.Pi
} else {
numer := o.cosPoleLat*math.Sin(latitude) - o.sinPoleLat*math.Cos(latitude)*poleRelCos
denom := math.Cos(newLat)
newLon = math.Acos(numer/denom) - math.Pi
if math.IsNaN(newLon) {
if (poleRelCos >= 0 && latitude < o.poleLat) || (poleRelCos < 0 && latitude < -o.poleLat) {
newLon = 0
} else {
newLon = -math.Pi
}
} else if math.Sin(longitude-o.poleLon) > 0 {
newLon = -newLon
}
}
// apply rotate
newLon = newLon - o.poleTheta
// constrain and kill roundoff error
if math.Abs(newLon) > math.Pi {
newLon = coerceAngle(newLon)
}
if newLon >= math.Pi-1e-7 {
newLon = -math.Pi
}
return newLat, newLon
}
func coerceAngle(angle float64) float64 {
x := angle + math.Pi
y := 2 * math.Pi
floorMod := x - math.Floor(x/y)*y
return floorMod - math.Pi
}
// Given a latitude/longitude in the non-transformed 'original' projection space, applies
// the pole shift and rotation of the Oblique projection so that the returned latitude/longitude
// are in the Oblique projection space.
func (o ObliqueAspect) TransformToOblique(latitude float64, longitude float64) (float64, float64) {
rotateLon := longitude + o.poleTheta
preAsin := o.sinPoleLat*math.Sin(latitude) - o.cosPoleLat*math.Cos(latitude)*math.Cos(rotateLon)
if preAsin > 1 && preAsin < 1+1e-9 {
preAsin = 1
}
if preAsin < -1 && preAsin > -1-1e-9 {
preAsin = -1
}
newLat := math.Asin(preAsin)
var newLon float64
inner := math.Sin(latitude)/o.cosPoleLat/math.Cos(newLat) - math.Tan(o.poleLat)*math.Tan(newLat)
if o.poleLat == math.Pi/2 {
newLon = rotateLon + o.poleLon
} else if o.poleLat == -math.Pi/2 {
newLon = -rotateLon + o.poleLon + math.Pi
} else if math.Abs(inner) > 1 {
if (rotateLon == 0 && latitude < -o.poleLat) || (rotateLon != 0 && latitude < o.poleLat) {
newLon = o.poleLon + math.Pi
} else {
newLon = o.poleLon
}
} else if math.Sin(rotateLon) > 0 {
newLon = o.poleLon + math.Acos(inner)
} else {
newLon = o.poleLon - math.Acos(inner)
}
if math.Abs(newLon) > math.Pi {
newLon = coerceAngle(newLon)
}
return newLat, newLon
}
type ObliqueProjection struct {
orig Projection
ObliqueAspect
}
func NewObliqueProjection(original Projection, poleLat float64, poleLon float64, poleTheta float64) ObliqueProjection {
return ObliqueProjection{
original,
NewObliqueAspect(poleLat, poleLon, poleTheta),
}
}
func (o ObliqueProjection) Project(latitude float64, longitude float64) (float64, float64) {
return o.orig.Project(o.TransformFromOblique(latitude, longitude))
}
func (o ObliqueProjection) Inverse(x float64, y float64) (float64, float64) {
return o.TransformToOblique(o.orig.Inverse(x, y))
}
func (o ObliqueProjection) PlanarBounds() Bounds {
return o.orig.PlanarBounds()
}