-
Notifications
You must be signed in to change notification settings - Fork 1
/
latlon.js
398 lines (318 loc) · 15.1 KB
/
latlon.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
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
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Latitude/longitude spherical geodesy formulae & scripts (c) Chris Veness 2002-2014 */
/* - www.movable-type.co.uk/scripts/latlong.html */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* jshint node:true *//* global define */
'use strict';
if (typeof module!='undefined' && module.exports) var Geo = require('./geo'); // CommonJS (Node.js)
/**
* Creates a LatLon point on the earth's surface at the specified latitude / longitude.
*
* @classdesc Tools for geodetic calculations
* @requires Geo
*
* @constructor
* @param {number} lat - Latitude in degrees.
* @param {number} lon - Longitude in degrees.
* @param {number} [height=0] - Height above mean-sea-level in kilometres.
* @param {number} [radius=6371] - (Mean) radius of earth in kilometres.
*
* @example
* var p1 = new LatLon(52.205, 0.119);
*/
function LatLon(lat, lon, height, radius) {
// allow instantiation without 'new'
if (!(this instanceof LatLon)) return new LatLon(lat, lon, height, radius);
if (typeof height == 'undefined') height = 0;
if (typeof radius == 'undefined') radius = 6371;
radius = Math.min(Math.max(radius, 6353), 6384);
this.lat = Number(lat);
this.lon = Number(lon);
this.height = Number(height);
this.radius = Number(radius);
}
/**
* Returns the distance from 'this' point to destination point (using haversine formula).
*
* @param {LatLon} point - Latitude/longitude of destination point.
* @returns {number} Distance between this point and destination point, in km (on sphere of 'this' radius).
*
* @example
* var p1 = new LatLon(52.205, 0.119), p2 = new LatLon(48.857, 2.351);
* var d = p1.distanceTo(p2); // d.toPrecision(4): 404.3
*/
LatLon.prototype.distanceTo = function(point) {
var R = this.radius;
var φ1 = this.lat.toRadians(), λ1 = this.lon.toRadians();
var φ2 = point.lat.toRadians(), λ2 = point.lon.toRadians();
var Δφ = φ2 - φ1;
var Δλ = λ2 - λ1;
var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ/2) * Math.sin(Δλ/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d;
};
/**
* Returns the (initial) bearing from 'this' point to destination point.
*
* @param {LatLon} point - Latitude/longitude of destination point.
* @returns {number} Initial bearing in degrees from north.
*
* @example
* var p1 = new LatLon(52.205, 0.119), p2 = new LatLon(48.857, 2.351);
* var b1 = p1.bearingTo(p2); // b1.toFixed(1): 156.2
*/
LatLon.prototype.bearingTo = function(point) {
var φ1 = this.lat.toRadians(), φ2 = point.lat.toRadians();
var Δλ = (point.lon-this.lon).toRadians();
// see http://mathforum.org/library/drmath/view/55417.html
var y = Math.sin(Δλ) * Math.cos(φ2);
var x = Math.cos(φ1)*Math.sin(φ2) -
Math.sin(φ1)*Math.cos(φ2)*Math.cos(Δλ);
var θ = Math.atan2(y, x);
return (θ.toDegrees()+360) % 360;
};
/**
* Returns final bearing arriving at destination destination point from 'this' point; the final bearing
* will differ from the initial bearing by varying degrees according to distance and latitude.
*
* @param {LatLon} point - Latitude/longitude of destination point.
* @returns {number} Final bearing in degrees from north.
*
* @example
* var p1 = new LatLon(52.205, 0.119), p2 = new LatLon(48.857, 2.351);
* var b2 = p1.finalBearingTo(p2); // p2.toFixed(1): 157.9
*/
LatLon.prototype.finalBearingTo = function(point) {
// get initial bearing from destination point to this point & reverse it by adding 180°
return ( point.bearingTo(this)+180 ) % 360;
};
/**
* Returns the midpoint between 'this' point and the supplied point.
*
* @param {LatLon} point - Latitude/longitude of destination point.
* @returns {LatLon} Midpoint between this point and the supplied point.
*
* @example
* var p1 = new LatLon(52.205, 0.119), p2 = new LatLon(48.857, 2.351);
* var pMid = p1.midpointTo(p2); // pMid.toString(): 50.5363°N, 001.2746°E
*/
LatLon.prototype.midpointTo = function(point) {
// see http://mathforum.org/library/drmath/view/51822.html for derivation
var φ1 = this.lat.toRadians(), λ1 = this.lon.toRadians();
var φ2 = point.lat.toRadians();
var Δλ = (point.lon-this.lon).toRadians();
var Bx = Math.cos(φ2) * Math.cos(Δλ);
var By = Math.cos(φ2) * Math.sin(Δλ);
var φ3 = Math.atan2(Math.sin(φ1)+Math.sin(φ2),
Math.sqrt( (Math.cos(φ1)+Bx)*(Math.cos(φ1)+Bx) + By*By) );
var λ3 = λ1 + Math.atan2(By, Math.cos(φ1) + Bx);
λ3 = (λ3+3*Math.PI) % (2*Math.PI) - Math.PI; // normalise to -180..+180º
return new LatLon(φ3.toDegrees(), λ3.toDegrees());
};
/**
* Returns the destination point from 'this' point having travelled the given distance on the
* given initial bearing (bearing normally varies around path followed).
*
* @param {number} brng - Initial bearing in degrees.
* @param {number} dist - Distance in km (on sphere of 'this' radius).
* @returns {LatLon} Destination point.
*
* @example
* var p1 = new LatLon(51.4778, -0.0015);
* var p2 = p1.destinationPoint(300.7, 7.794); // p2.toString(): 51.5135°N, 000.0983°W
*/
LatLon.prototype.destinationPoint = function(brng, dist) {
// see http://williams.best.vwh.net/avform.htm#LL
var θ = Number(brng).toRadians();
var δ = Number(dist) / this.radius; // angular distance in radians
var φ1 = this.lat.toRadians();
var λ1 = this.lon.toRadians();
var φ2 = Math.asin( Math.sin(φ1)*Math.cos(δ) +
Math.cos(φ1)*Math.sin(δ)*Math.cos(θ) );
var λ2 = λ1 + Math.atan2(Math.sin(θ)*Math.sin(δ)*Math.cos(φ1),
Math.cos(δ)-Math.sin(φ1)*Math.sin(φ2));
λ2 = (λ2+3*Math.PI) % (2*Math.PI) - Math.PI; // normalise to -180..+180º
return new LatLon(φ2.toDegrees(), λ2.toDegrees());
};
/**
* Returns the point of intersection of two paths defined by point and bearing.
*
* @param {LatLon} p1 - First point.
* @param {number} brng1 - Initial bearing from first point.
* @param {LatLon} p2 - Second point.
* @param {number} brng2 - Initial bearing from second point.
* @returns {LatLon} Destination point (null if no unique intersection defined).
*
* @example
* var p1 = LatLon(51.8853, 0.2545), brng1 = 108.547;
* var p2 = LatLon(49.0034, 2.5735), brng2 = 32.435;
* var pInt = LatLon.intersection(p1, brng1, p2, brng2); // pInt.toString(): 50.9076°N, 004.5084°E
*/
LatLon.intersection = function(p1, brng1, p2, brng2) {
// see http://williams.best.vwh.net/avform.htm#Intersection
var φ1 = p1.lat.toRadians(), λ1 = p1.lon.toRadians();
var φ2 = p2.lat.toRadians(), λ2 = p2.lon.toRadians();
var θ13 = Number(brng1).toRadians(), θ23 = Number(brng2).toRadians();
var Δφ = φ2-φ1, Δλ = λ2-λ1;
var δ12 = 2*Math.asin( Math.sqrt( Math.sin(Δφ/2)*Math.sin(Δφ/2) +
Math.cos(φ1)*Math.cos(φ2)*Math.sin(Δλ/2)*Math.sin(Δλ/2) ) );
if (δ12 == 0) return null;
// initial/final bearings between points
var θ1 = Math.acos( ( Math.sin(φ2) - Math.sin(φ1)*Math.cos(δ12) ) /
( Math.sin(δ12)*Math.cos(φ1) ) );
if (isNaN(θ1)) θ1 = 0; // protect against rounding
var θ2 = Math.acos( ( Math.sin(φ1) - Math.sin(φ2)*Math.cos(δ12) ) /
( Math.sin(δ12)*Math.cos(φ2) ) );
var θ12, θ21;
if (Math.sin(λ2-λ1) > 0) {
θ12 = θ1;
θ21 = 2*Math.PI - θ2;
} else {
θ12 = 2*Math.PI - θ1;
θ21 = θ2;
}
var α1 = (θ13 - θ12 + Math.PI) % (2*Math.PI) - Math.PI; // angle 2-1-3
var α2 = (θ21 - θ23 + Math.PI) % (2*Math.PI) - Math.PI; // angle 1-2-3
if (Math.sin(α1)==0 && Math.sin(α2)==0) return null; // infinite intersections
if (Math.sin(α1)*Math.sin(α2) < 0) return null; // ambiguous intersection
//α1 = Math.abs(α1);
//α2 = Math.abs(α2);
// ... Ed Williams takes abs of α1/α2, but seems to break calculation?
var α3 = Math.acos( -Math.cos(α1)*Math.cos(α2) +
Math.sin(α1)*Math.sin(α2)*Math.cos(δ12) );
var δ13 = Math.atan2( Math.sin(δ12)*Math.sin(α1)*Math.sin(α2),
Math.cos(α2)+Math.cos(α1)*Math.cos(α3) );
var φ3 = Math.asin( Math.sin(φ1)*Math.cos(δ13) +
Math.cos(φ1)*Math.sin(δ13)*Math.cos(θ13) );
var Δλ13 = Math.atan2( Math.sin(θ13)*Math.sin(δ13)*Math.cos(φ1),
Math.cos(δ13)-Math.sin(φ1)*Math.sin(φ3) );
var λ3 = λ1 + Δλ13;
λ3 = (λ3+3*Math.PI) % (2*Math.PI) - Math.PI; // normalise to -180..+180º
return new LatLon(φ3.toDegrees(), λ3.toDegrees());
};
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/**
* Returns the distance travelling from 'this' point to destination point along a rhumb line.
*
* @param {LatLon} point - Latitude/longitude of destination point.
* @returns {number} Distance in km between this point and destination point (on sphere of 'this' radius).
*
* @example
* var p1 = new LatLon(51.127, 1.338), p2 = new LatLon(50.964, 1.853);
* var d = p1.distanceTo(p2); // d.toPrecision(4): 40.31
*/
LatLon.prototype.rhumbDistanceTo = function(point) {
// see http://williams.best.vwh.net/avform.htm#Rhumb
var R = this.radius;
var φ1 = this.lat.toRadians(), φ2 = point.lat.toRadians();
var Δφ = φ2 - φ1;
var Δλ = Math.abs(point.lon-this.lon).toRadians();
// if dLon over 180° take shorter rhumb line across the anti-meridian:
if (Math.abs(Δλ) > Math.PI) Δλ = Δλ>0 ? -(2*Math.PI-Δλ) : (2*Math.PI+Δλ);
// on Mercator projection, longitude distances shrink by latitude; q is the 'stretch factor'
// q becomes ill-conditioned along E-W line (0/0); use empirical tolerance to avoid it
var Δψ = Math.log(Math.tan(φ2/2+Math.PI/4)/Math.tan(φ1/2+Math.PI/4));
var q = Math.abs(Δψ) > 10e-12 ? Δφ/Δψ : Math.cos(φ1);
// distance is pythagoras on 'stretched' Mercator projection
var δ = Math.sqrt(Δφ*Δφ + q*q*Δλ*Δλ); // angular distance in radians
var dist = δ * R;
return dist;
};
/**
* Returns the bearing from 'this' point to destination point along a rhumb line.
*
* @param {LatLon} point - Latitude/longitude of destination point.
* @returns {number} Bearing in degrees from north.
*
* @example
* var p1 = new LatLon(51.127, 1.338), p2 = new LatLon(50.964, 1.853);
* var d = p1.rhumbBearingTo(p2); // d.toFixed(1): 116.7
*/
LatLon.prototype.rhumbBearingTo = function(point) {
var φ1 = this.lat.toRadians(), φ2 = point.lat.toRadians();
var Δλ = (point.lon-this.lon).toRadians();
// if dLon over 180° take shorter rhumb line across the anti-meridian:
if (Math.abs(Δλ) > Math.PI) Δλ = Δλ>0 ? -(2*Math.PI-Δλ) : (2*Math.PI+Δλ);
var Δψ = Math.log(Math.tan(φ2/2+Math.PI/4)/Math.tan(φ1/2+Math.PI/4));
var θ = Math.atan2(Δλ, Δψ);
return (θ.toDegrees()+360) % 360;
};
/**
* Returns the destination point having travelled along a rhumb line from 'this' point the given
* distance on the given bearing.
*
* @param {number} brng - Bearing in degrees from north.
* @param {number} dist - Distance in km (on sphere of 'this' radius).
* @returns {LatLon} Destination point.
*
* @example
* var p1 = new LatLon(51.127, 1.338);
* var p2 = p1.rhumbDestinationPoint(116.7, 40.31); // p2.toString(): 50.9641°N, 001.8531°E
*/
LatLon.prototype.rhumbDestinationPoint = function(brng, dist) {
var δ = Number(dist) / this.radius; // angular distance in radians
var φ1 = this.lat.toRadians(), λ1 = this.lon.toRadians();
var θ = Number(brng).toRadians();
var Δφ = δ * Math.cos(θ);
var φ2 = φ1 + Δφ;
// check for some daft bugger going past the pole, normalise latitude if so
if (Math.abs(φ2) > Math.PI/2) φ2 = φ2>0 ? Math.PI-φ2 : -Math.PI-φ2;
var Δψ = Math.log(Math.tan(φ2/2+Math.PI/4)/Math.tan(φ1/2+Math.PI/4));
var q = Math.abs(Δψ) > 10e-12 ? Δφ / Δψ : Math.cos(φ1); // E-W course becomes ill-conditioned with 0/0
var Δλ = δ*Math.sin(θ)/q;
var λ2 = λ1 + Δλ;
λ2 = (λ2 + 3*Math.PI) % (2*Math.PI) - Math.PI; // normalise to -180..+180º
return new LatLon(φ2.toDegrees(), λ2.toDegrees());
};
/**
* Returns the loxodromic midpoint (along a rhumb line) between 'this' point and second point.
*
* @param {LatLon} point - Latitude/longitude of second point.
* @returns {LatLon} Midpoint between this point and second point.
*
* @example
* var p1 = new LatLon(51.127, 1.338), p2 = new LatLon(50.964, 1.853);
* var p2 = p1.rhumbMidpointTo(p2); // p2.toString(): 51.0455°N, 001.5957°E
*/
LatLon.prototype.rhumbMidpointTo = function(point) {
// http://mathforum.org/kb/message.jspa?messageID=148837
var φ1 = this.lat.toRadians(), λ1 = this.lon.toRadians();
var φ2 = point.lat.toRadians(), λ2 = point.lon.toRadians();
if (Math.abs(λ2-λ1) > Math.PI) λ1 += 2*Math.PI; // crossing anti-meridian
var φ3 = (φ1+φ2)/2;
var f1 = Math.tan(Math.PI/4 + φ1/2);
var f2 = Math.tan(Math.PI/4 + φ2/2);
var f3 = Math.tan(Math.PI/4 + φ3/2);
var λ3 = ( (λ2-λ1)*Math.log(f3) + λ1*Math.log(f2) - λ2*Math.log(f1) ) / Math.log(f2/f1);
if (!isFinite(λ3)) λ3 = (λ1+λ2)/2; // parallel of latitude
λ3 = (λ3 + 3*Math.PI) % (2*Math.PI) - Math.PI; // normalise to -180..+180º
return new LatLon(φ3.toDegrees(), λ3.toDegrees());
};
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/**
* 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) {
if (typeof format == 'undefined') format = 'dms';
return Geo.toLat(this.lat, format, dp) + ', ' + Geo.toLon(this.lon, format, dp);
};
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/** Extend Number object with method to convert numeric degrees to radians */
if (typeof 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 (typeof Number.prototype.toDegrees == 'undefined') {
Number.prototype.toDegrees = function() { return this * 180 / Math.PI; };
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
if (typeof module != 'undefined' && module.exports) module.exports = LatLon; // CommonJS
if (typeof define == 'function' && define.amd) define(['Geo'], function() { return LatLon; }); // AMD