forked from chrisveness/geodesy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
latlon-ellipsoidal.js
269 lines (228 loc) · 11.5 KB
/
latlon-ellipsoidal.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
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
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Geodesy tools for an ellipsoidal earth model (c) Chris Veness 2005-2015 / MIT Licence */
/* */
/* Includes methods for converting lat/lon coordinates between different coordinate systems. */
/* - www.movable-type.co.uk/scripts/latlong-convert-coords.html */
/* */
/* Usage: to eg convert WGS 84 coordinate to OSGB coordinate: */
/* - var wgs84 = new LatLon(latWGS84, lonWGS84, LatLon.datum.WGS84); */
/* - var osgb = wgs84.convertDatum(LatLon.datum.OSGB36); */
/* - var latOSGB = osgb.lat, lonOSGB = osgb.lon; */
/* */
/* q.v. Ordnance Survey 'A guide to coordinate systems in Great Britain' Section 6 */
/* - www.ordnancesurvey.co.uk/docs/support/guide-coordinate-systems-great-britain.pdf */
/* */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
'use strict';
if (typeof module!='undefined' && module.exports) var Vector3d = require('./vector3d.js'); // CommonJS (Node)
if (typeof module!='undefined' && module.exports) var Dms = require('./dms.js'); // CommonJS (Node)
/**
* Creates lat/lon (polar) point with latitude & longitude values, on a specified datum.
*
* @classdesc Library of geodesy functions for operations on an ellipsoidal earth model.
* @requires Dms from 'dms.js'
*
* @constructor
* @param {number} lat - Geodetic latitude in degrees.
* @param {number} lon - Longitude in degrees.
* @param {LatLon.datum} [datum=WGS84] - Datum this point is defined within.
*
* @example
* var p1 = new LatLon(51.4778, -0.0016, LatLon.datum.WGS84);
*/
function LatLon(lat, lon, datum) {
// allow instantiation without 'new'
if (!(this instanceof LatLon)) return new LatLon(lat, lon, datum);
if (datum === undefined) datum = LatLon.datum.WGS84;
this.lat = Number(lat);
this.lon = Number(lon);
this.datum = datum;
}
/**
* Ellipsoid parameters; major axis (a), minor axis (b), and flattening (f) for each ellipsoid.
*/
LatLon.ellipsoid = {
WGS84: { a: 6378137, b: 6356752.31425, f: 1/298.257223563 },
GRS80: { a: 6378137, b: 6356752.31414, f: 1/298.257222101 },
Airy1830: { a: 6377563.396, b: 6356256.909, f: 1/299.3249646 },
AiryModified: { a: 6377340.189, b: 6356034.448, f: 1/299.3249646 },
Intl1924: { a: 6378388, b: 6356911.946, f: 1/297 },
Bessel1841: { a: 6377397.155, b: 6356078.963, f: 1/299.152815351 }
};
/**
* Datums; with associated *ellipsoid* and Helmert *transform* parameters to convert from WGS 84
* into given datum.
*
* More are available from earth-info.nga.mil/GandG/coordsys/datums/NATO_DT.pdf,
* www.fieldenmaps.info/cconv/web/cconv_params.js
*/
LatLon.datum = {
WGS84: {
ellipsoid: LatLon.ellipsoid.WGS84,
transform: { tx: 0.0, ty: 0.0, tz: 0.0, // m
rx: 0.0, ry: 0.0, rz: 0.0, // sec
s: 0.0 } // ppm
},
NAD83: { // (2009); functionally ≡ WGS84 - www.uvm.edu/giv/resources/WGS84_NAD83.pdf
ellipsoid: LatLon.ellipsoid.GRS80,
transform: { tx: 1.004, ty: -1.910, tz: -0.515, // m
rx: 0.0267, ry: 0.00034, rz: 0.011, // sec
s: -0.0015 } // ppm
}, // note: if you *really* need to convert WGS84<->NAD83, you need more knowledge than this!
OSGB36: { // www.ordnancesurvey.co.uk/docs/support/guide-coordinate-systems-great-britain.pdf
ellipsoid: LatLon.ellipsoid.Airy1830,
transform: { tx: -446.448, ty: 125.157, tz: -542.060, // m
rx: -0.1502, ry: -0.2470, rz: -0.8421, // sec
s: 20.4894 } // ppm
},
ED50: { // og.decc.gov.uk/en/olgs/cms/pons_and_cop/pons/pon4/pon4.aspx
ellipsoid: LatLon.ellipsoid.Intl1924,
transform: { tx: 89.5, ty: 93.8, tz: 123.1, // m
rx: 0.0, ry: 0.0, rz: 0.156, // sec
s: -1.2 } // ppm
},
Irl1975: { // osi.ie/OSI/media/OSI/Content/Publications/transformations_booklet.pdf
ellipsoid: LatLon.ellipsoid.AiryModified,
transform: { tx: -482.530, ty: 130.596, tz: -564.557, // m
rx: -1.042, ry: -0.214, rz: -0.631, // sec
s: -8.150 } // ppm
}, // note: many sources have opposite sign to rotations - to be checked!
TokyoJapan: { // www.geocachingtoolbox.com?page=datumEllipsoidDetails
ellipsoid: LatLon.ellipsoid.Bessel1841,
transform: { tx: 148, ty: -507, tz: -685, // m
rx: 0, ry: 0, rz: 0, // sec
s: 0 } // ppm
}
};
/**
* Converts ‘this’ lat/lon coordinate to new coordinate system.
*
* @param {LatLon.datum} toDatum - Datum this coordinate is to be converted to.
* @returns {LatLon} This point converted to new datum.
*
* @example
* var pWGS84 = new LatLon(51.4778, -0.0016, LatLon.datum.WGS84);
* var pOSGB = pWGS84.convertDatum(LatLon.datum.OSGB36); // pOSGB.toString(): 51.4773°N, 000.0000°E
*/
LatLon.prototype.convertDatum = function(toDatum) {
var oldLatLon = this;
var transform;
if (oldLatLon.datum == LatLon.datum.WGS84) {
// converting from WGS 84
transform = toDatum.transform;
}
if (toDatum == LatLon.datum.WGS84) {
// converting to WGS 84; use inverse transform (don't overwrite original!)
transform = {};
for (var param in oldLatLon.datum.transform) {
if (oldLatLon.datum.transform.hasOwnProperty(param)) {
transform[param] = -oldLatLon.datum.transform[param];
}
}
}
if (transform === undefined) {
// neither this.datum nor toDatum are WGS84: convert this to WGS84 first
oldLatLon = this.convertDatum(LatLon.datum.WGS84);
transform = toDatum.transform;
}
var cartesian = oldLatLon.toCartesian(); // convert polar to cartesian...
cartesian = cartesian.applyTransform(transform); // ...apply transform...
var newLatLon = cartesian.toLatLonE(toDatum); // ...and convert cartesian to polar
return newLatLon;
};
/**
* Converts ‘this’ point from (geodetic) latitude/longitude coordinates to (geocentric) cartesian
* (x/y/z) coordinates.
*
* @returns {Vector3d} Vector pointing to lat/lon point, with x, y, z in metres from earth centre.
*/
LatLon.prototype.toCartesian = function() {
var φ = this.lat.toRadians(), λ = this.lon.toRadians();
var h = 0; // height above ellipsoid - not currently used
var a = this.datum.ellipsoid.a, b = this.datum.ellipsoid.b;
var sinφ = Math.sin(φ), cosφ = Math.cos(φ);
var sinλ = Math.sin(λ), cosλ = Math.cos(λ);
var eSq = (a*a - b*b) / (a*a);
var ν = a / Math.sqrt(1 - eSq*sinφ*sinφ);
var x = (ν+h) * cosφ * cosλ;
var y = (ν+h) * cosφ * sinλ;
var z = ((1-eSq)*ν + h) * sinφ;
var point = new Vector3d(x, y, z);
return point;
};
/**
* Converts ‘this’ (geocentric) cartesian (x/y/z) point to (ellipsoidal geodetic) latitude/longitude
* coordinates on specified datum.
*
* Uses Bowring’s (1985) formulation for μm precision.
*
* @param {LatLon.datum.transform} datum - Datum to use when converting point.
*/
Vector3d.prototype.toLatLonE = function(datum) {
var x = this.x, y = this.y, z = this.z;
var a = datum.ellipsoid.a, b = datum.ellipsoid.b;
var e2 = (a*a-b*b) / (a*a); // 1st eccentricity squared
var ε2 = (a*a-b*b) / (b*b); // 2nd eccentricity squared
var p = Math.sqrt(x*x + y*y); // distance from minor axis
var R = Math.sqrt(p*p + z*z); // polar radius
// parametric latitude (Bowring eqn 17, replacing tanβ = z·a / p·b)
var tanβ = (b*z)/(a*p) * (1+ε2*b/R);
var sinβ = tanβ / Math.sqrt(1+tanβ*tanβ);
var cosβ = sinβ / tanβ;
// geodetic latitude (Bowring eqn 18)
var φ = Math.atan2(z + ε2*b*sinβ*sinβ*sinβ,
p - e2*a*cosβ*cosβ*cosβ);
// longitude
var λ = Math.atan2(y, x);
// height above ellipsoid (Bowring eqn 7) [not currently used]
var sinφ = Math.sin(φ), cosφ = Math.cos(φ);
var ν = a*Math.sqrt(1-e2*sinφ*sinφ); // length of the normal terminated by the minor axis
var h = p*cosφ + z*sinφ - (a*a/ν);
var point = new LatLon(φ.toDegrees(), λ.toDegrees(), datum);
return point;
};
/**
* Applies Helmert transform to ‘this’ point using transform parameters t.
*
* @private
* @param {LatLon.datum.transform} t - Transform to apply to this point.
*/
Vector3d.prototype.applyTransform = function(t) {
var x1 = this.x, y1 = this.y, z1 = this.z;
var tx = t.tx, ty = t.ty, tz = t.tz;
var rx = (t.rx/3600).toRadians(); // normalise seconds to radians
var ry = (t.ry/3600).toRadians(); // normalise seconds to radians
var rz = (t.rz/3600).toRadians(); // normalise seconds to radians
var s1 = t.s/1e6 + 1; // normalise ppm to (s+1)
// apply transform
var x2 = tx + x1*s1 - y1*rz + z1*ry;
var y2 = ty + x1*rz + y1*s1 - z1*rx;
var z2 = tz - x1*ry + y1*rx + z1*s1;
var point = new Vector3d(x2, y2, z2);
return point;
};
/**
* Returns a string representation of ‘this’ point, formatted as degrees, degrees+minutes, or
* degrees+minutes+seconds.
*
* @param {string} [format=dms] - Format point as 'd', 'dm', 'dms'.
* @param {number} [dp=0|2|4] - Number of decimal places to use - default 0 for dms, 2 for dm, 4 for d.
* @returns {string} Comma-separated latitude/longitude.
*/
LatLon.prototype.toString = function(format, dp) {
return Dms.toLat(this.lat, format, dp) + ', ' + Dms.toLon(this.lon, format, dp);
};
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/** Extend Number object with method to convert numeric degrees to radians */
if (Number.prototype.toRadians === undefined) {
Number.prototype.toRadians = function() { return this * Math.PI / 180; };
}
/** Extend Number object with method to convert radians to numeric (signed) degrees */
if (Number.prototype.toDegrees === undefined) {
Number.prototype.toDegrees = function() { return this * 180 / Math.PI; };
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
if (typeof module != 'undefined' && module.exports) module.exports = LatLon; // CommonJS (Node)
if (typeof module != 'undefined' && module.exports) module.exports.Vector3d = Vector3d; // CommonJs (Node)
if (typeof define == 'function' && define.amd) define([], function() { return LatLon; }); // AMD
if (typeof define == 'function' && define.amd) define([], function() { return Vector3d; }); // AMD??