-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
60 lines (41 loc) · 1.35 KB
/
index.js
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
// https://gist.github.com/avar/585850
var a = 6378137.0;
var f = 1/298.257222101;
var lat1 = 64.25;
var lat2 = 65.75;
var latc = 65.00;
var lonc = 19.00;
var eps = 0.00000000001;
var fx = function(p) {
return a * Math.cos(p/rho)/Math.sqrt(1 - Math.pow(e * Math.sin(p/rho), 2));
}
var f1 = function(p) {
return Math.log((1 - p)/(1 + p));
}
var f2 = function(p) {
return f1(p) - e * f1(e * p);
}
var f3 = function(p) {
return pol1*Math.exp((f2(Math.sin(p/rho)) - f2sin1)*sint/2);
}
var rho = 45/Math.atan2(1.0,1.0);
var e = Math.sqrt(f * (2 - f));
var dum = f2(Math.sin(lat1/rho)) - f2(Math.sin(lat2/rho));;
var sint = 2 * (Math.log(fx(lat1)) - Math.log(fx(lat2))) / dum;
var f2sin1 = f2(Math.sin(lat1/rho));
var pol1 = fx(lat1)/sint;
var polc = f3(latc) + 500000.0;
var peq = a * Math.cos(latc/rho)/(sint*Math.exp(sint*Math.log((45-latc/2)/rho)));
module.exports = function(x, y) {
var pol = Math.sqrt(Math.pow(x-500000, 2) + Math.pow(polc-y, 2));
var lat = 90 - 2 * rho * Math.atan(Math.exp(Math.log(pol / peq) / sint));
var lon = 0;
var fact = rho * Math.cos(lat / rho) / sint / pol;
var delta = 1.0;
while (Math.abs(delta) > eps) {
delta = (f3(lat) - pol) * fact;
lat += delta;
}
var lon = -(lonc + rho * Math.atan((500000 - x) / (polc - y)) / sint);
return { latitude: lat, longitude: lon };
}