forked from autonome/aframe-ar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeolib.js
1150 lines (893 loc) · 27.7 KB
/
geolib.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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! geolib 2.0.7 by Manuel Bieh
* Library to provide geo functions like distance calculation,
* conversion of decimal coordinates to sexagesimal and vice versa, etc.
* WGS 84 (World Geodetic System 1984)
*
* @author Manuel Bieh
* @url http://www.manuelbieh.com/
* @version 2.0.7
* @license MIT
**/;(function(global, undefined) {
"use strict";
function Geolib() {}
// Setting readonly defaults
var geolib = Object.create(Geolib.prototype, {
version: {
value: "2.0.7"
},
radius: {
value: 6378137
},
minLat: {
value: -90
},
maxLat: {
value: 90
},
minLon: {
value: -180
},
maxLon: {
value: 180
},
sexagesimalPattern: {
value: /^([0-9]{1,3})°\s*([0-9]{1,3}(?:\.(?:[0-9]{1,2}))?)'\s*(([0-9]{1,3}(\.([0-9]{1,2}))?)"\s*)?([NEOSW]?)$/
},
measures: {
value: Object.create(Object.prototype, {
"m" : {value: 1},
"km": {value: 0.001},
"cm": {value: 100},
"mm": {value: 1000},
"mi": {value: (1 / 1609.344)},
"sm": {value: (1 / 1852.216)},
"ft": {value: (100 / 30.48)},
"in": {value: (100 / 2.54)},
"yd": {value: (1 / 0.9144)}
})
},
prototype: {
value: Geolib.prototype
},
extend: {
value: function(methods, overwrite) {
for(var prop in methods) {
if(typeof geolib.prototype[prop] === 'undefined' || overwrite === true) {
geolib.prototype[prop] = methods[prop];
}
}
}
}
});
if (typeof(Number.prototype.toRad) === "undefined") {
Number.prototype.toRad = function() {
return this * Math.PI / 180;
};
}
if (typeof(Number.prototype.toDeg) === "undefined") {
Number.prototype.toDeg = function() {
return this * 180 / Math.PI;
};
}
// Here comes the magic
geolib.extend({
decimal: {},
sexagesimal: {},
distance: null,
getKeys: function(point) {
// GeoJSON Array [longitude, latitude(, elevation)]
if(Object.prototype.toString.call(point) == '[object Array]') {
return {
longitude: point.length >= 1 ? 0 : undefined,
latitude: point.length >= 2 ? 1 : undefined,
elevation: point.length >= 3 ? 2 : undefined
};
}
var getKey = function(possibleValues) {
var key;
possibleValues.every(function(val) {
// TODO: check if point is an object
if(typeof point != 'object') {
return true;
}
return point.hasOwnProperty(val) ? (function() { key = val; return false; }()) : true;
});
return key;
};
var longitude = getKey(['lng', 'lon', 'longitude']);
var latitude = getKey(['lat', 'latitude']);
var elevation = getKey(['alt', 'altitude', 'elevation', 'elev']);
// return undefined if not at least one valid property was found
if(typeof latitude == 'undefined' &&
typeof longitude == 'undefined' &&
typeof elevation == 'undefined') {
return undefined;
}
return {
latitude: latitude,
longitude: longitude,
elevation: elevation
};
},
// returns latitude of a given point, converted to decimal
// set raw to true to avoid conversion
getLat: function(point, raw) {
return raw === true ? point[this.getKeys(point).latitude] : this.useDecimal(point[this.getKeys(point).latitude]);
},
// Alias for getLat
latitude: function(point) {
return this.getLat.call(this, point);
},
// returns longitude of a given point, converted to decimal
// set raw to true to avoid conversion
getLon: function(point, raw) {
return raw === true ? point[this.getKeys(point).longitude] : this.useDecimal(point[this.getKeys(point).longitude]);
},
// Alias for getLon
longitude: function(point) {
return this.getLon.call(this, point);
},
getElev: function(point) {
return point[this.getKeys(point).elevation];
},
// Alias for getElev
elevation: function(point) {
return this.getElev.call(this, point);
},
coords: function(point, raw) {
var retval = {
latitude: raw === true ? point[this.getKeys(point).latitude] : this.useDecimal(point[this.getKeys(point).latitude]),
longitude: raw === true ? point[this.getKeys(point).longitude] : this.useDecimal(point[this.getKeys(point).longitude])
};
var elev = point[this.getKeys(point).elevation];
if(typeof elev !== 'undefined') {
retval['elevation'] = elev;
}
return retval;
},
// checks if a variable contains a valid latlong object
validate: function(point) {
var keys = this.getKeys(point);
if(typeof keys === 'undefined' || typeof keys.latitude === 'undefined' || keys.longitude === 'undefined') {
return false;
}
var lat = point[keys.latitude];
var lng = point[keys.longitude];
if(typeof lat === 'undefined' || !this.isDecimal(lat) && !this.isSexagesimal(lat)) {
return false;
}
if(typeof lng === 'undefined' || !this.isDecimal(lng) && !this.isSexagesimal(lng)) {
return false;
}
lat = this.useDecimal(lat);
lng = this.useDecimal(lng);
if(lat < this.minLat || lat > this.maxLat || lng < this.minLon || lng > this.maxLon) {
return false;
}
return true;
},
/**
* Calculates geodetic distance between two points specified by latitude/longitude using
* Vincenty inverse formula for ellipsoids
* Vincenty Inverse Solution of Geodesics on the Ellipsoid (c) Chris Veness 2002-2010
* (Licensed under CC BY 3.0)
*
* @param object Start position {latitude: 123, longitude: 123}
* @param object End position {latitude: 123, longitude: 123}
* @param integer Accuracy (in meters)
* @return integer Distance (in meters)
*/
getDistance: function(start, end, accuracy) {
accuracy = Math.floor(accuracy) || 1;
var s = this.coords(start);
var e = this.coords(end);
var a = 6378137, b = 6356752.314245, f = 1/298.257223563; // WGS-84 ellipsoid params
var L = (e['longitude']-s['longitude']).toRad();
var cosSigma, sigma, sinAlpha, cosSqAlpha, cos2SigmaM, sinSigma;
var U1 = Math.atan((1-f) * Math.tan(parseFloat(s['latitude']).toRad()));
var U2 = Math.atan((1-f) * Math.tan(parseFloat(e['latitude']).toRad()));
var sinU1 = Math.sin(U1), cosU1 = Math.cos(U1);
var sinU2 = Math.sin(U2), cosU2 = Math.cos(U2);
var lambda = L, lambdaP, iterLimit = 100;
do {
var sinLambda = Math.sin(lambda), cosLambda = Math.cos(lambda);
sinSigma = (
Math.sqrt(
(
cosU2 * sinLambda
) * (
cosU2 * sinLambda
) + (
cosU1 * sinU2 - sinU1 * cosU2 * cosLambda
) * (
cosU1 * sinU2 - sinU1 * cosU2 * cosLambda
)
)
);
if (sinSigma === 0) {
return geolib.distance = 0; // co-incident points
}
cosSigma = sinU1 * sinU2 + cosU1 * cosU2 * cosLambda;
sigma = Math.atan2(sinSigma, cosSigma);
sinAlpha = cosU1 * cosU2 * sinLambda / sinSigma;
cosSqAlpha = 1 - sinAlpha * sinAlpha;
cos2SigmaM = cosSigma - 2 * sinU1 * sinU2 / cosSqAlpha;
if (isNaN(cos2SigmaM)) {
cos2SigmaM = 0; // equatorial line: cosSqAlpha=0 (§6)
}
var C = (
f / 16 * cosSqAlpha * (
4 + f * (
4 - 3 * cosSqAlpha
)
)
);
lambdaP = lambda;
lambda = (
L + (
1 - C
) * f * sinAlpha * (
sigma + C * sinSigma * (
cos2SigmaM + C * cosSigma * (
-1 + 2 * cos2SigmaM * cos2SigmaM
)
)
)
);
} while (Math.abs(lambda-lambdaP) > 1e-12 && --iterLimit>0);
if (iterLimit === 0) {
return NaN; // formula failed to converge
}
var uSq = (
cosSqAlpha * (
a * a - b * b
) / (
b*b
)
);
var A = (
1 + uSq / 16384 * (
4096 + uSq * (
-768 + uSq * (
320 - 175 * uSq
)
)
)
);
var B = (
uSq / 1024 * (
256 + uSq * (
-128 + uSq * (
74-47 * uSq
)
)
)
);
var deltaSigma = (
B * sinSigma * (
cos2SigmaM + B / 4 * (
cosSigma * (
-1 + 2 * cos2SigmaM * cos2SigmaM
) -B / 6 * cos2SigmaM * (
-3 + 4 * sinSigma * sinSigma
) * (
-3 + 4 * cos2SigmaM * cos2SigmaM
)
)
)
);
var distance = b * A * (sigma - deltaSigma);
distance = distance.toFixed(3); // round to 1mm precision
//if (start.hasOwnProperty(elevation) && end.hasOwnProperty(elevation)) {
if (typeof this.elevation(start) !== 'undefined' && typeof this.elevation(end) !== 'undefined') {
var climb = Math.abs(this.elevation(start) - this.elevation(end));
distance = Math.sqrt(distance * distance + climb * climb);
}
return this.distance = Math.floor(
Math.round(distance / accuracy) * accuracy
);
/*
// note: to return initial/final bearings in addition to distance, use something like:
var fwdAz = Math.atan2(cosU2*sinLambda, cosU1*sinU2-sinU1*cosU2*cosLambda);
var revAz = Math.atan2(cosU1*sinLambda, -sinU1*cosU2+cosU1*sinU2*cosLambda);
return { distance: s, initialBearing: fwdAz.toDeg(), finalBearing: revAz.toDeg() };
*/
},
/**
* Calculates the distance between two spots.
* This method is more simple but also far more inaccurate
*
* @param object Start position {latitude: 123, longitude: 123}
* @param object End position {latitude: 123, longitude: 123}
* @param integer Accuracy (in meters)
* @return integer Distance (in meters)
*/
getDistanceSimple: function(start, end, accuracy) {
accuracy = Math.floor(accuracy) || 1;
var distance =
Math.round(
Math.acos(
Math.sin(
this.latitude(end).toRad()
) *
Math.sin(
this.latitude(start).toRad()
) +
Math.cos(
this.latitude(end).toRad()
) *
Math.cos(
this.latitude(start).toRad()
) *
Math.cos(
this.longitude(start).toRad() - this.longitude(end).toRad()
)
) * this.radius
);
return geolib.distance = Math.floor(Math.round(distance/accuracy)*accuracy);
},
/**
* Calculates the center of a collection of geo coordinates
*
* @param array Collection of coords [{latitude: 51.510, longitude: 7.1321}, {latitude: 49.1238, longitude: "8° 30' W"}, ...]
* @return object {latitude: centerLat, longitude: centerLng, distance: diagonalDistance}
*/
getCenter: function(coords) {
if (!coords.length) {
return false;
}
var max = function( array ){
return Math.max.apply( Math, array );
};
var min = function( array ){
return Math.min.apply( Math, array );
};
var latitude;
var longitude;
var splitCoords = {latitude: [], longitude: []};
for(var coord in coords) {
splitCoords.latitude.push(
this.latitude(coords[coord])
);
splitCoords.longitude.push(
this.longitude(coords[coord])
);
}
var minLat = min(splitCoords.latitude);
var minLon = min(splitCoords.longitude);
var maxLat = max(splitCoords.latitude);
var maxLon = max(splitCoords.longitude);
latitude = ((minLat + maxLat)/2).toFixed(6);
longitude = ((minLon + maxLon)/2).toFixed(6);
// distance from the deepest left to the highest right point (diagonal distance)
var distance = this.convertUnit('km', this.getDistance({latitude: minLat, longitude: minLon}, {latitude: maxLat, longitude: maxLon}));
return {
latitude: latitude,
longitude: longitude,
distance: distance
};
},
/**
* Gets the max and min, latitude, longitude, and elevation (if provided).
* @param array array with coords e.g. [{latitude: 51.5143, longitude: 7.4138}, {latitude: 123, longitude: 123}, ...]
* @return object {maxLat: maxLat,
* minLat: minLat
* maxLng: maxLng,
* minLng: minLng,
* maxElev: maxElev,
* minElev: minElev}
*/
getBounds: function(coords) {
if (!coords.length) {
return false;
}
var useElevation = this.elevation(coords[0]);
var stats = {
maxLat: -Infinity,
minLat: Infinity,
maxLng: -Infinity,
minLng: Infinity
};
if (typeof useElevation != 'undefined') {
stats.maxElev = 0;
stats.minElev = Infinity;
}
for (var i = 0, l = coords.length; i < l; ++i) {
stats.maxLat = Math.max(this.latitude(coords[i]), stats.maxLat);
stats.minLat = Math.min(this.latitude(coords[i]), stats.minLat);
stats.maxLng = Math.max(this.longitude(coords[i]), stats.maxLng);
stats.minLng = Math.min(this.longitude(coords[i]), stats.minLng);
if (useElevation) {
stats.maxElev = Math.max(this.elevation(coords[i]), stats.maxElev);
stats.minElev = Math.min(this.elevation(coords[i]), stats.minElev);
}
}
return stats;
},
/**
* Computes the bounding coordinates of all points on the surface
* of the earth less than or equal to the specified great circle
* distance.
*
* @param object Point position {latitude: 123, longitude: 123}
* @param number Distance (in meters).
* @return array Collection of two points defining the SW and NE corners.
*/
getBoundsOfDistance: function(point, distance) {
var latitude = this.latitude(point);
var longitude = this.longitude(point);
var radLat = latitude.toRad();
var radLon = longitude.toRad();
var radDist = distance / this.radius;
var minLat = radLat - radDist;
var maxLat = radLat + radDist;
var MAX_LAT_RAD = this.maxLat.toRad();
var MIN_LAT_RAD = this.minLat.toRad();
var MAX_LON_RAD = this.maxLon.toRad();
var MIN_LON_RAD = this.minLon.toRad();
var minLon;
var maxLon;
if (minLat > MIN_LAT_RAD && maxLat < MAX_LAT_RAD) {
var deltaLon = Math.asin(Math.sin(radDist) / Math.cos(radLat));
minLon = radLon - deltaLon;
if (minLon < MIN_LON_RAD) {
minLon += 2 * Math.PI;
}
maxLon = radLon + deltaLon;
if (maxLon > MAX_LON_RAD) {
maxLon -= 2 * Math.PI;
}
} else {
// A pole is within the distance.
minLat = Math.max(minLat, MIN_LAT_RAD);
maxLat = Math.min(maxLat, MAX_LAT_RAD);
minLon = MIN_LON_RAD;
maxLon = MAX_LON_RAD;
}
return [
// Southwest
{
latitude: minLat.toDeg(),
longitude: minLon.toDeg()
},
// Northeast
{
latitude: maxLat.toDeg(),
longitude: maxLon.toDeg()
}
];
},
/**
* Checks whether a point is inside of a polygon or not.
* Note that the polygon coords must be in correct order!
*
* @param object coordinate to check e.g. {latitude: 51.5023, longitude: 7.3815}
* @param array array with coords e.g. [{latitude: 51.5143, longitude: 7.4138}, {latitude: 123, longitude: 123}, ...]
* @return bool true if the coordinate is inside the given polygon
*/
isPointInside: function(latlng, coords) {
for(var c = false, i = -1, l = coords.length, j = l - 1; ++i < l; j = i) {
if(
(
(this.longitude(coords[i]) <= this.longitude(latlng) && this.longitude(latlng) < this.longitude(coords[j])) ||
(this.longitude(coords[j]) <= this.longitude(latlng) && this.longitude(latlng) < this.longitude(coords[i]))
) &&
(
this.latitude(latlng) < (this.latitude(coords[j]) - this.latitude(coords[i])) *
(this.longitude(latlng) - this.longitude(coords[i])) /
(this.longitude(coords[j]) - this.longitude(coords[i])) +
this.latitude(coords[i])
)
) {
c = !c;
}
}
return c;
},
/**
* Shortcut for geolib.isPointInside()
*/
isInside: function() {
return this.isPointInside.apply(this, arguments);
},
/**
* Checks whether a point is inside of a circle or not.
*
* @param object coordinate to check (e.g. {latitude: 51.5023, longitude: 7.3815})
* @param object coordinate of the circle's center (e.g. {latitude: 51.4812, longitude: 7.4025})
* @param integer maximum radius in meters
* @return bool true if the coordinate is within the given radius
*/
isPointInCircle: function(latlng, center, radius) {
return this.getDistance(latlng, center) < radius;
},
/**
* Shortcut for geolib.isPointInCircle()
*/
withinRadius: function() {
return this.isPointInCircle.apply(this, arguments);
},
/**
* Gets rhumb line bearing of two points. Find out about the difference between rhumb line and
* great circle bearing on Wikipedia. It's quite complicated. Rhumb line should be fine in most cases:
*
* http://en.wikipedia.org/wiki/Rhumb_line#General_and_mathematical_description
*
* Function heavily based on Doug Vanderweide's great PHP version (licensed under GPL 3.0)
* http://www.dougv.com/2009/07/13/calculating-the-bearing-and-compass-rose-direction-between-two-latitude-longitude-coordinates-in-php/
*
* @param object origin coordinate (e.g. {latitude: 51.5023, longitude: 7.3815})
* @param object destination coordinate
* @return integer calculated bearing
*/
getRhumbLineBearing: function(originLL, destLL) {
// difference of longitude coords
var diffLon = this.longitude(destLL).toRad() - this.longitude(originLL).toRad();
// difference latitude coords phi
var diffPhi = Math.log(
Math.tan(
this.latitude(destLL).toRad() / 2 + Math.PI / 4
) /
Math.tan(
this.latitude(originLL).toRad() / 2 + Math.PI / 4
)
);
// recalculate diffLon if it is greater than pi
if(Math.abs(diffLon) > Math.PI) {
if(diffLon > 0) {
diffLon = (2 * Math.PI - diffLon) * -1;
}
else {
diffLon = 2 * Math.PI + diffLon;
}
}
//return the angle, normalized
return (Math.atan2(diffLon, diffPhi).toDeg() + 360) % 360;
},
/**
* Gets great circle bearing of two points. See description of getRhumbLineBearing for more information
*
* @param object origin coordinate (e.g. {latitude: 51.5023, longitude: 7.3815})
* @param object destination coordinate
* @return integer calculated bearing
*/
getBearing: function(originLL, destLL) {
destLL['latitude'] = this.latitude(destLL);
destLL['longitude'] = this.longitude(destLL);
originLL['latitude'] = this.latitude(originLL);
originLL['longitude'] = this.longitude(originLL);
var bearing = (
(
Math.atan2(
Math.sin(
destLL['longitude'].toRad() -
originLL['longitude'].toRad()
) *
Math.cos(
destLL['latitude'].toRad()
),
Math.cos(
originLL['latitude'].toRad()
) *
Math.sin(
destLL['latitude'].toRad()
) -
Math.sin(
originLL['latitude'].toRad()
) *
Math.cos(
destLL['latitude'].toRad()
) *
Math.cos(
destLL['longitude'].toRad() - originLL['longitude'].toRad()
)
)
).toDeg() + 360
) % 360;
return bearing;
},
/**
* Gets the compass direction from an origin coordinate to a destination coordinate.
*
* @param object origin coordinate (e.g. {latitude: 51.5023, longitude: 7.3815})
* @param object destination coordinate
* @param string Bearing mode. Can be either circle or rhumbline
* @return object Returns an object with a rough (NESW) and an exact direction (NNE, NE, ENE, E, ESE, etc).
*/
getCompassDirection: function(originLL, destLL, bearingMode) {
var direction;
var bearing;
if(bearingMode == 'circle') {
// use great circle bearing
bearing = this.getBearing(originLL, destLL);
} else {
// default is rhumb line bearing
bearing = this.getRhumbLineBearing(originLL, destLL);
}
switch(Math.round(bearing/22.5)) {
case 1:
direction = {exact: "NNE", rough: "N"};
break;
case 2:
direction = {exact: "NE", rough: "N"};
break;
case 3:
direction = {exact: "ENE", rough: "E"};
break;
case 4:
direction = {exact: "E", rough: "E"};
break;
case 5:
direction = {exact: "ESE", rough: "E"};
break;
case 6:
direction = {exact: "SE", rough: "E"};
break;
case 7:
direction = {exact: "SSE", rough: "S"};
break;
case 8:
direction = {exact: "S", rough: "S"};
break;
case 9:
direction = {exact: "SSW", rough: "S"};
break;
case 10:
direction = {exact: "SW", rough: "S"};
break;
case 11:
direction = {exact: "WSW", rough: "W"};
break;
case 12:
direction = {exact: "W", rough: "W"};
break;
case 13:
direction = {exact: "WNW", rough: "W"};
break;
case 14:
direction = {exact: "NW", rough: "W"};
break;
case 15:
direction = {exact: "NNW", rough: "N"};
break;
default:
direction = {exact: "N", rough: "N"};
}
direction['bearing'] = bearing;
return direction;
},
/**
* Shortcut for getCompassDirection
*/
getDirection: function(originLL, destLL, bearingMode) {
return this.getCompassDirection.apply(this, arguments);
},
/**
* Sorts an array of coords by distance from a reference coordinate
*
* @param object reference coordinate e.g. {latitude: 51.5023, longitude: 7.3815}
* @param mixed array or object with coords [{latitude: 51.5143, longitude: 7.4138}, {latitude: 123, longitude: 123}, ...]
* @return array ordered array
*/
orderByDistance: function(latlng, coords) {
var coordsArray = [];
for(var coord in coords) {
var d = this.getDistance(latlng, coords[coord]);
coordsArray.push({
key: coord,
latitude: this.latitude(coords[coord]),
longitude: this.longitude(coords[coord]),
distance: d
});
}
return coordsArray.sort(function(a, b) { return a.distance - b.distance; });
},
/**
* Finds the nearest coordinate to a reference coordinate
*
* @param object reference coordinate e.g. {latitude: 51.5023, longitude: 7.3815}
* @param mixed array or object with coords [{latitude: 51.5143, longitude: 7.4138}, {latitude: 123, longitude: 123}, ...]
* @return array ordered array
*/
findNearest: function(latlng, coords, offset, limit) {
offset = offset || 0;
limit = limit || 1;
var ordered = this.orderByDistance(latlng, coords);
if(limit === 1) {
return ordered[offset];
} else {
return ordered.splice(offset, limit);
}
},
/**
* Calculates the length of a given path
*
* @param mixed array or object with coords [{latitude: 51.5143, longitude: 7.4138}, {latitude: 123, longitude: 123}, ...]
* @return integer length of the path (in meters)
*/
getPathLength: function(coords) {
var dist = 0;
var last;
for (var i = 0, l = coords.length; i < l; ++i) {
if(last) {
//console.log(coords[i], last, this.getDistance(coords[i], last));
dist += this.getDistance(this.coords(coords[i]), last);
}
last = this.coords(coords[i]);
}
return dist;
},
/**
* Calculates the speed between to points within a given time span.
*
* @param object coords with javascript timestamp {latitude: 51.5143, longitude: 7.4138, time: 1360231200880}
* @param object coords with javascript timestamp {latitude: 51.5502, longitude: 7.4323, time: 1360245600460}
* @param object options (currently "unit" is the only option. Default: km(h));
* @return float speed in unit per hour
*/
getSpeed: function(start, end, options) {
var unit = options && options.unit || 'km';
if(unit == 'mph') {
unit = 'mi';
} else if(unit == 'kmh') {
unit = 'km';
}
var distance = geolib.getDistance(start, end);
var time = ((end.time*1)/1000) - ((start.time*1)/1000);
var mPerHr = (distance/time)*3600;
var speed = Math.round(mPerHr * this.measures[unit] * 10000)/10000;
return speed;
},
/**
* Converts a distance from meters to km, mm, cm, mi, ft, in or yd
*
* @param string Format to be converted in
* @param float Distance in meters
* @param float Decimal places for rounding (default: 4)
* @return float Converted distance
*/
convertUnit: function(unit, distance, round) {
if(distance === 0 || typeof distance === 'undefined') {
if(this.distance === 0) {
// throw 'No distance given.';
return 0;
} else {
distance = this.distance;
}
}
unit = unit || 'm';
round = (null == round ? 4 : round);
if(typeof this.measures[unit] !== 'undefined') {
return this.round(distance * this.measures[unit], round);
} else {
throw new Error('Unknown unit for conversion.');
}
},
/**
* Checks if a value is in decimal format or, if neccessary, converts to decimal
*
* @param mixed Value(s) to be checked/converted (array of latlng objects, latlng object, sexagesimal string, float)
* @return float Input data in decimal format
*/
useDecimal: function(value) {
if(Object.prototype.toString.call(value) === '[object Array]') {
var geolib = this;
value = value.map(function(val) {
//if(!isNaN(parseFloat(val))) {
if(geolib.isDecimal(val)) {
return geolib.useDecimal(val);
} else if(typeof val == 'object') {
if(geolib.validate(val)) {
return geolib.coords(val);
} else {
for(var prop in val) {
val[prop] = geolib.useDecimal(val[prop]);
}
return val;
}
} else if(geolib.isSexagesimal(val)) {
return geolib.sexagesimal2decimal(val);
} else {
return val;
}
});
return value;