-
Notifications
You must be signed in to change notification settings - Fork 1
/
polarstereographic.go
436 lines (380 loc) · 13 KB
/
polarstereographic.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package coordconv
import (
"errors"
"math"
"github.com/golang/geo/s1"
"github.com/golang/geo/s2"
)
// PolarStereographic provides conversions between geodetic (latitude and
// longitude) coordinates and Polar Stereographic (easting and northing)
// coordinates.
type PolarStereographic struct {
semiMajorAxis float64
flattening float64
es float64 // Eccentricity of ellipsoid
esOverTwo float64 // es / 2.0
isSouthernHemisphere bool // Flag variable
polarTC float64
polarK90 float64
polaraMc float64 // Polar_a * mc
twoPolarA float64 // 2.0 * Polar_a
// Polar Stereographic projection Parameters
polarStandardParallel float64 // Latitude of origin in radians
polarCentralMeridian float64 // Longitude of origin in radians
polarFalseEasting float64 // False easting in meters
polarFalseNorthing float64 // False northing in meters
// Maximum variance for easting and northing values for WGS 84.
polarDeltaEasting float64
polarDeltaNorthing float64
polarScaleFactor float64
}
// NewPolarStereographic receives the ellipsoid parameters and Polar
// Stereograpic (Standard Parallel) projection parameters as inputs, and sets
// the corresponding state variables.
func NewPolarStereographic(ellipsoidSemiMajorAxis,
ellipsoidFlattening,
centralMeridian,
standardParallel,
falseEasting,
falseNorthing float64) (*PolarStereographic, error) {
p := &PolarStereographic{
es: (0.08181919084262188000),
esOverTwo: (.040909595421311),
isSouthernHemisphere: (false),
polarTC: (1.0),
polarK90: (1.0033565552493),
polaraMc: (6378137.0),
twoPolarA: (12756274.0),
polarCentralMeridian: (0.0),
polarStandardParallel: ((math.Pi * 90) / 180),
polarFalseEasting: (0.0),
polarFalseNorthing: (0.0),
polarScaleFactor: (1.0),
polarDeltaEasting: (12713601.0),
polarDeltaNorthing: (12713601.0),
}
invF := 1 / ellipsoidFlattening
if ellipsoidSemiMajorAxis <= 0.0 {
return nil, errors.New("Semi-major axis must be greater than zero")
}
if (invF < 250) ||
(invF > 350) {
return nil, errors.New("Inverse flattening must be between 250 and 350")
}
if (standardParallel < -math.Pi/2) ||
(standardParallel > math.Pi/2) {
return nil, errors.New("Origin Latitude out of range")
}
if (centralMeridian < -math.Pi) ||
(centralMeridian > 2*math.Pi) {
return nil, errors.New("Origin Longitude out of range")
}
p.semiMajorAxis = ellipsoidSemiMajorAxis
p.flattening = ellipsoidFlattening
p.twoPolarA = 2.0 * p.semiMajorAxis
if centralMeridian > math.Pi {
centralMeridian -= 2 * math.Pi
}
if standardParallel < 0 {
p.isSouthernHemisphere = true
p.polarStandardParallel = -standardParallel
p.polarCentralMeridian = -centralMeridian
} else {
p.isSouthernHemisphere = false
p.polarStandardParallel = standardParallel
p.polarCentralMeridian = centralMeridian
}
p.polarFalseEasting = falseEasting
p.polarFalseNorthing = falseNorthing
es2 := 2*p.flattening - p.flattening*p.flattening
p.es = math.Sqrt(es2)
p.esOverTwo = p.es / 2.0
if math.Abs(math.Abs(p.polarStandardParallel)-math.Pi/2) > 1.0e-10 {
sinolat := math.Sin(p.polarStandardParallel)
essin := p.es * sinolat
powEs := p.polarPow(essin)
cosolat := math.Cos(p.polarStandardParallel)
mc := cosolat / math.Sqrt(1.0-essin*essin)
p.polaraMc = p.semiMajorAxis * mc
p.polarTC = math.Tan(math.Pi/4-p.polarStandardParallel/2.0) / powEs
}
onePlusEs := 1.0 + p.es
oneMinusEs := 1.0 - p.es
p.polarK90 = math.Sqrt(math.Pow(onePlusEs, onePlusEs) * math.Pow(oneMinusEs, oneMinusEs))
slat := math.Sin(math.Abs(standardParallel))
onePlusEsSinoLat := 1.0 + p.es*slat
oneMinusEsSinoLat := 1.0 - p.es*slat
p.polarScaleFactor = ((1 + slat) / 2) *
(p.polarK90 / math.Sqrt(math.Pow(onePlusEsSinoLat, onePlusEs)*
math.Pow(oneMinusEsSinoLat, oneMinusEs)))
// Calculate Radius
tempGeodeticCoordinates := s2.LatLng{Lng: s1.Angle(centralMeridian), Lat: 0}
tempCoordinates, err := p.ConvertFromGeodetic(tempGeodeticCoordinates)
if err != nil {
return nil, err
}
p.polarDeltaNorthing = tempCoordinates.Northing
if p.polarFalseNorthing != 0 {
p.polarDeltaNorthing -= p.polarFalseNorthing
}
if p.polarDeltaNorthing < 0 {
p.polarDeltaNorthing = -p.polarDeltaNorthing
}
p.polarDeltaNorthing *= 1.01
p.polarDeltaEasting = p.polarDeltaNorthing
return p, nil
}
// NewPolarStereographicScaleFactor ellipsoid parameters and Polar Stereograpic
// (Scale Factor) projection parameters as inputs, and sets the corresponding
// state variables.
func NewPolarStereographicScaleFactor(ellipsoidSemiMajorAxis,
ellipsoidFlattening,
centralMeridian,
scaleFactor float64, hemisphere Hemisphere,
falseEasting,
falseNorthing float64) (*PolarStereographic, error) {
p := &PolarStereographic{
// coordinateType: (CoordinateType::polarStereographicScaleFactor),
es: (0.08181919084262188000),
esOverTwo: (.040909595421311),
isSouthernHemisphere: false,
polarTC: (1.0),
polarK90: (1.0033565552493),
polaraMc: (6378137.0),
twoPolarA: (12756274.0),
polarCentralMeridian: (0.0),
polarStandardParallel: ((math.Pi * 90) / 180),
polarFalseEasting: (0.0),
polarFalseNorthing: (0.0),
polarScaleFactor: (1.0),
polarDeltaEasting: (12713601.0),
polarDeltaNorthing: (12713601.0),
}
tolerance := 1.0e-15
count := 30
invF := 1 / ellipsoidFlattening
const minScaleFactor = 0.1
const maxScaleFactor = 3.0
if ellipsoidSemiMajorAxis <=
0.0 {
return nil, errors.New("Semi-major axis must be greater than zero")
}
if (invF < 250) ||
(invF > 350) {
return nil, errors.New("Inverse flattening must be between 250 and 350")
}
if (scaleFactor < minScaleFactor) || (scaleFactor > maxScaleFactor) {
return nil, errors.New("Scale factor out of range")
}
if (centralMeridian < -math.Pi) ||
(centralMeridian > 2*math.Pi) {
return nil, errors.New("Origin Longitude out of range")
}
if (hemisphere != HemisphereNorth) && (hemisphere != HemisphereSouth) {
return nil, errors.New("Hemisphere out of range")
}
p.semiMajorAxis = ellipsoidSemiMajorAxis
p.flattening = ellipsoidFlattening
p.polarScaleFactor = scaleFactor
p.polarFalseEasting = falseEasting
p.polarFalseNorthing = falseNorthing
p.twoPolarA = 2.0 * p.semiMajorAxis
es2 := 2*p.flattening - p.flattening*p.flattening
p.es = math.Sqrt(es2)
p.esOverTwo = p.es / 2.0
onePlusEs := 1.0 + p.es
oneMinusEs := 1.0 - p.es
p.polarK90 =
math.Sqrt(math.Pow(onePlusEs, onePlusEs) * math.Pow(oneMinusEs, oneMinusEs))
sk := 0.0
skPlus1 := -1 + 2*p.polarScaleFactor
for math.Abs(skPlus1-sk) > tolerance && count != 0 {
sk = skPlus1
onePlusEsSk := 1.0 + p.es*sk
oneMinusEsSk := 1.0 - p.es*sk
skPlus1 = ((2 * p.polarScaleFactor *
math.Sqrt(math.Pow(onePlusEsSk, onePlusEs)*
math.Pow(oneMinusEsSk, oneMinusEs))) /
p.polarK90) - 1
count--
}
if count == 0 {
return nil, errors.New("origin latitude error")
}
standardParallel := 0.0
if skPlus1 >= -1.0 && skPlus1 <= 1.0 {
standardParallel = math.Asin(skPlus1)
} else {
return nil, errors.New("origin latitude error")
}
if hemisphere == HemisphereSouth {
standardParallel *= -1.0
}
if centralMeridian > math.Pi {
centralMeridian -= 2 * math.Pi
}
if standardParallel < 0 {
p.isSouthernHemisphere = true
p.polarStandardParallel = -standardParallel
p.polarCentralMeridian = -centralMeridian
} else {
p.isSouthernHemisphere = false
p.polarStandardParallel = standardParallel
p.polarCentralMeridian = centralMeridian
}
sinolat := math.Sin(p.polarStandardParallel)
if math.Abs(math.Abs(p.polarStandardParallel)-math.Pi/2) > 1.0e-10 {
essin := p.es * sinolat
powEs := p.polarPow(essin)
cosolat := math.Cos(p.polarStandardParallel)
mc := cosolat / math.Sqrt(1.0-essin*essin)
p.polaraMc = p.semiMajorAxis * mc
p.polarTC = math.Tan(math.Pi/4-p.polarStandardParallel/2.0) / powEs
}
// Calculate Radius
tempGeodeticCoordinates := s2.LatLng{Lng: s1.Angle(centralMeridian), Lat: 0}
tempCoordinates, err := p.ConvertFromGeodetic(tempGeodeticCoordinates)
if err != nil {
return nil, err
}
p.polarDeltaNorthing = tempCoordinates.Northing
if p.polarFalseNorthing != 0 {
p.polarDeltaNorthing -= p.polarFalseNorthing
}
if p.polarDeltaNorthing < 0 {
p.polarDeltaNorthing = -p.polarDeltaNorthing
}
p.polarDeltaNorthing *= 1.01
p.polarDeltaEasting = p.polarDeltaNorthing
return p, nil
}
// ConvertFromGeodetic converts geodetic coordinates (latitude and longitude) to
// Polar Stereographic coordinates (easting and northing), according to the
// current ellipsoid and Polar Stereographic projection parameters.
func (p *PolarStereographic) ConvertFromGeodetic(geodeticCoordinates s2.LatLng) (MapCoords, error) {
longitude := geodeticCoordinates.Lng.Radians()
latitude := geodeticCoordinates.Lat.Radians()
if (latitude < -math.Pi/2) || (latitude > math.Pi/2) {
return MapCoords{}, errors.New("latitide out of range")
} else if (latitude < 0) && (!p.isSouthernHemisphere) {
return MapCoords{}, errors.New("latitude and Origin Latitude in different hemispheres")
} else if (latitude > 0) && (p.isSouthernHemisphere) {
return MapCoords{}, errors.New("latitude and Origin Latitude in different hemispheres")
}
if (longitude < -math.Pi) || (longitude > 2*math.Pi) {
return MapCoords{}, errors.New("longitude out of range")
}
var easting, northing float64
if math.Abs(math.Abs(latitude)-math.Pi/2) < 1.0e-10 {
easting = p.polarFalseEasting
northing = p.polarFalseNorthing
} else {
if p.isSouthernHemisphere {
longitude *= -1.0
latitude *= -1.0
}
dlam := longitude - p.polarCentralMeridian
if dlam > math.Pi {
dlam -= 2 * math.Pi
}
if dlam < -math.Pi {
dlam += 2 * math.Pi
}
slat := math.Sin(latitude)
essin := p.es * slat
powEs := p.polarPow(essin)
t := math.Tan(math.Pi/4-latitude/2.0) / powEs
var rho float64
if math.Abs(math.Abs(p.polarStandardParallel)-math.Pi/2) > 1.0e-10 {
rho = p.polaraMc * t / p.polarTC
} else {
rho = p.twoPolarA * t / p.polarK90
}
if p.isSouthernHemisphere {
easting = -(rho*math.Sin(dlam) - p.polarFalseEasting)
northing = rho*math.Cos(dlam) + p.polarFalseNorthing
} else {
easting = rho*math.Sin(dlam) + p.polarFalseEasting
northing = -rho*math.Cos(dlam) + p.polarFalseNorthing
}
}
return MapCoords{Easting: easting, Northing: northing}, nil
}
// ConvertToGeodetic converts Polar Stereographic coordinates (easting and
// northing) to geodetic coordinates (latitude and longitude) according to the
// current ellipsoid and Polar Stereographic projection Parameters.
func (p *PolarStereographic) ConvertToGeodetic(mapProjectionCoordinates MapCoords) (s2.LatLng, error) {
easting := mapProjectionCoordinates.Easting
northing := mapProjectionCoordinates.Northing
minEasting := p.polarFalseEasting - p.polarDeltaEasting
maxEasting := p.polarFalseEasting + p.polarDeltaEasting
minNorthing := p.polarFalseNorthing - p.polarDeltaNorthing
maxNorthing := p.polarFalseNorthing + p.polarDeltaNorthing
if easting > maxEasting ||
easting < minEasting {
return s2.LatLng{}, errors.New("easting out of range")
}
if northing > maxNorthing ||
northing < minNorthing {
return s2.LatLng{}, errors.New("northing out of range")
}
dy := northing - p.polarFalseNorthing
dx := easting - p.polarFalseEasting
// Radius of point with origin of false easting, false northing
rho := math.Sqrt(dx*dx + dy*dy)
deltaRadius := math.Sqrt(p.polarDeltaEasting*p.polarDeltaEasting +
p.polarDeltaNorthing*p.polarDeltaNorthing)
if rho > deltaRadius {
return s2.LatLng{}, errors.New("Point is outside of projection area")
}
var latitude, longitude float64
if (dy == 0.0) && (dx == 0.0) {
latitude = math.Pi / 2
longitude = p.polarCentralMeridian
} else {
if p.isSouthernHemisphere {
dy *= -1.0
dx *= -1.0
}
var t float64
if math.Abs(math.Abs(p.polarStandardParallel)-math.Pi/2) > 1.0e-10 {
t = rho * p.polarTC / (p.polaraMc)
} else {
t = rho * p.polarK90 / (p.twoPolarA)
}
PHI := math.Pi/2 - 2.0*math.Atan(t)
tempPHI := 0.0
for math.Abs(PHI-tempPHI) > 1.0e-10 {
tempPHI = PHI
sinPhi := math.Sin(PHI)
essin := p.es * sinPhi
powEs := p.polarPow(essin)
PHI = math.Pi/2 - 2.0*math.Atan(t*powEs)
}
latitude = PHI
longitude = p.polarCentralMeridian + math.Atan2(dx, -dy)
if longitude > math.Pi {
longitude -= 2 * math.Pi
} else if longitude < -math.Pi {
longitude += 2 * math.Pi
}
if latitude > math.Pi/2 { // force distorted values to 90, -90 degrees
latitude = math.Pi / 2
} else if latitude < -math.Pi/2 {
latitude = -math.Pi / 2
}
if longitude > math.Pi { // force distorted values to 180, -180 degrees
longitude = math.Pi
} else if longitude < -math.Pi {
longitude = -math.Pi
}
}
if p.isSouthernHemisphere {
latitude *= -1.0
longitude *= -1.0
}
return s2.LatLng{Lat: s1.Angle(latitude), Lng: s1.Angle(longitude)}, nil
}
func (p *PolarStereographic) polarPow(esSin float64) float64 {
return math.Pow((1.0-esSin)/(1.0+esSin), p.esOverTwo)
}