-
Notifications
You must be signed in to change notification settings - Fork 430
/
proj.js
181 lines (168 loc) · 6.41 KB
/
proj.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
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
/**
* Contains methods for transforming point on sphere to
* Cartesian coordinates using various projections.
* @class
*/
jvm.Proj = {
degRad: 180 / Math.PI,
radDeg: Math.PI / 180,
radius: 6381372,
sgn: function(n){
if (n > 0) {
return 1;
} else if (n < 0) {
return -1;
} else {
return n;
}
},
/**
* Converts point on sphere to the Cartesian coordinates using Miller projection
* @param {Number} lat Latitude in degrees
* @param {Number} lng Longitude in degrees
* @param {Number} c Central meridian in degrees
*/
mill: function(lat, lng, c){
return {
x: this.radius * (lng - c) * this.radDeg,
y: - this.radius * Math.log(Math.tan((45 + 0.4 * lat) * this.radDeg)) / 0.8
};
},
/**
* Inverse function of mill()
* Converts Cartesian coordinates to point on sphere using Miller projection
* @param {Number} x X of point in Cartesian system as integer
* @param {Number} y Y of point in Cartesian system as integer
* @param {Number} c Central meridian in degrees
*/
mill_inv: function(x, y, c){
return {
lat: (2.5 * Math.atan(Math.exp(0.8 * y / this.radius)) - 5 * Math.PI / 8) * this.degRad,
lng: (c * this.radDeg + x / this.radius) * this.degRad
};
},
/**
* Converts point on sphere to the Cartesian coordinates using Mercator projection
* @param {Number} lat Latitude in degrees
* @param {Number} lng Longitude in degrees
* @param {Number} c Central meridian in degrees
*/
merc: function(lat, lng, c){
return {
x: this.radius * (lng - c) * this.radDeg,
y: - this.radius * Math.log(Math.tan(Math.PI / 4 + lat * Math.PI / 360))
};
},
/**
* Inverse function of merc()
* Converts Cartesian coordinates to point on sphere using Mercator projection
* @param {Number} x X of point in Cartesian system as integer
* @param {Number} y Y of point in Cartesian system as integer
* @param {Number} c Central meridian in degrees
*/
merc_inv: function(x, y, c){
return {
lat: (2 * Math.atan(Math.exp(y / this.radius)) - Math.PI / 2) * this.degRad,
lng: (c * this.radDeg + x / this.radius) * this.degRad
};
},
/**
* Converts point on sphere to the Cartesian coordinates using Albers Equal-Area Conic
* projection
* @see <a href="http://mathworld.wolfram.com/AlbersEqual-AreaConicProjection.html">Albers Equal-Area Conic projection</a>
* @param {Number} lat Latitude in degrees
* @param {Number} lng Longitude in degrees
* @param {Number} c Central meridian in degrees
*/
aea: function(lat, lng, c){
var fi0 = 0,
lambda0 = c * this.radDeg,
fi1 = 29.5 * this.radDeg,
fi2 = 45.5 * this.radDeg,
fi = lat * this.radDeg,
lambda = lng * this.radDeg,
n = (Math.sin(fi1)+Math.sin(fi2)) / 2,
C = Math.cos(fi1)*Math.cos(fi1)+2*n*Math.sin(fi1),
theta = n*(lambda-lambda0),
ro = Math.sqrt(C-2*n*Math.sin(fi))/n,
ro0 = Math.sqrt(C-2*n*Math.sin(fi0))/n;
return {
x: ro * Math.sin(theta) * this.radius,
y: - (ro0 - ro * Math.cos(theta)) * this.radius
};
},
/**
* Converts Cartesian coordinates to the point on sphere using Albers Equal-Area Conic
* projection
* @see <a href="http://mathworld.wolfram.com/AlbersEqual-AreaConicProjection.html">Albers Equal-Area Conic projection</a>
* @param {Number} x X of point in Cartesian system as integer
* @param {Number} y Y of point in Cartesian system as integer
* @param {Number} c Central meridian in degrees
*/
aea_inv: function(xCoord, yCoord, c){
var x = xCoord / this.radius,
y = yCoord / this.radius,
fi0 = 0,
lambda0 = c * this.radDeg,
fi1 = 29.5 * this.radDeg,
fi2 = 45.5 * this.radDeg,
n = (Math.sin(fi1)+Math.sin(fi2)) / 2,
C = Math.cos(fi1)*Math.cos(fi1)+2*n*Math.sin(fi1),
ro0 = Math.sqrt(C-2*n*Math.sin(fi0))/n,
ro = Math.sqrt(x*x+(ro0-y)*(ro0-y)),
theta = Math.atan( x / (ro0 - y) );
return {
lat: (Math.asin((C - ro * ro * n * n) / (2 * n))) * this.degRad,
lng: (lambda0 + theta / n) * this.degRad
};
},
/**
* Converts point on sphere to the Cartesian coordinates using Lambert conformal
* conic projection
* @see <a href="http://mathworld.wolfram.com/LambertConformalConicProjection.html">Lambert Conformal Conic Projection</a>
* @param {Number} lat Latitude in degrees
* @param {Number} lng Longitude in degrees
* @param {Number} c Central meridian in degrees
*/
lcc: function(lat, lng, c){
var fi0 = 0,
lambda0 = c * this.radDeg,
lambda = lng * this.radDeg,
fi1 = 33 * this.radDeg,
fi2 = 45 * this.radDeg,
fi = lat * this.radDeg,
n = Math.log( Math.cos(fi1) * (1 / Math.cos(fi2)) ) / Math.log( Math.tan( Math.PI / 4 + fi2 / 2) * (1 / Math.tan( Math.PI / 4 + fi1 / 2) ) ),
F = ( Math.cos(fi1) * Math.pow( Math.tan( Math.PI / 4 + fi1 / 2 ), n ) ) / n,
ro = F * Math.pow( 1 / Math.tan( Math.PI / 4 + fi / 2 ), n ),
ro0 = F * Math.pow( 1 / Math.tan( Math.PI / 4 + fi0 / 2 ), n );
return {
x: ro * Math.sin( n * (lambda - lambda0) ) * this.radius,
y: - (ro0 - ro * Math.cos( n * (lambda - lambda0) ) ) * this.radius
};
},
/**
* Converts Cartesian coordinates to the point on sphere using Lambert conformal conic
* projection
* @see <a href="http://mathworld.wolfram.com/LambertConformalConicProjection.html">Lambert Conformal Conic Projection</a>
* @param {Number} x X of point in Cartesian system as integer
* @param {Number} y Y of point in Cartesian system as integer
* @param {Number} c Central meridian in degrees
*/
lcc_inv: function(xCoord, yCoord, c){
var x = xCoord / this.radius,
y = yCoord / this.radius,
fi0 = 0,
lambda0 = c * this.radDeg,
fi1 = 33 * this.radDeg,
fi2 = 45 * this.radDeg,
n = Math.log( Math.cos(fi1) * (1 / Math.cos(fi2)) ) / Math.log( Math.tan( Math.PI / 4 + fi2 / 2) * (1 / Math.tan( Math.PI / 4 + fi1 / 2) ) ),
F = ( Math.cos(fi1) * Math.pow( Math.tan( Math.PI / 4 + fi1 / 2 ), n ) ) / n,
ro0 = F * Math.pow( 1 / Math.tan( Math.PI / 4 + fi0 / 2 ), n ),
ro = this.sgn(n) * Math.sqrt(x*x+(ro0-y)*(ro0-y)),
theta = Math.atan( x / (ro0 - y) );
return {
lat: (2 * Math.atan(Math.pow(F/ro, 1/n)) - Math.PI / 2) * this.degRad,
lng: (lambda0 + theta / n) * this.degRad
};
}
};