geolib.elevation.js
4.01 KB
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
/*! geolib.elevation 2.0.21 by Manuel Bieh
*
* Elevation Addon for Geolib.js
*
* @author Manuel Bieh
* @url http://www.manuelbieh.com/
* @version 2.0.21
* @license MIT
*/
;(function(global, geolib, undefined) {
var elevation = {
/*global google:true geolib:true require:true module:true elevationResult:true */
/**
* @param Array Collection of coords [{latitude: 51.510, longitude: 7.1321}, {latitude: 49.1238, longitude: "8° 30' W"}, ...]
* @return Array [{lat:#lat, lng:#lng, elev:#elev},....]}
*/
getElevation: function() {
if (typeof global.navigator !== 'undefined') {
this.getElevationClient.apply(this, arguments);
} else {
this.getElevationServer.apply(this, arguments);
}
},
/* Optional elevation addon requires Googlemaps API JS */
getElevationClient: function(coords, cb) {
if (!global.google) {
throw new Error("Google maps api not loaded");
}
if (coords.length === 0) {
return cb(null, null);
}
if (coords.length === 1) {
return cb(new Error("getElevation requires at least 2 points."));
}
var path = [];
for(var i = 0; i < coords.length; i++) {
path.push(new google.maps.LatLng(
this.latitude(coords[i]),
this.longitude(coords[i])
));
}
var positionalRequest = {
'path': path,
'samples': path.length
};
var elevationService = new google.maps.ElevationService();
var geolib = this;
elevationService.getElevationAlongPath(positionalRequest, function (results, status) {
geolib.elevationHandler(results, status, coords, cb);
});
},
getElevationServer: function(coords, cb) {
if (coords.length === 0) {
return cb(null, null);
}
if (coords.length === 1) {
return cb(new Error("getElevation requires at least 2 points."));
}
var gm = require('googlemaps');
var path = [];
for(var i = 0; i < coords.length; i++) {
path.push(
this.latitude(coords[i]) + ',' + this.longitude(coords[i])
);
}
var geolib = this;
gm.elevationFromPath(path.join('|'), path.length, function(err, results) {
geolib.elevationHandler(results.results, results.status, coords, cb);
});
},
elevationHandler: function(results, status, coords, cb) {
var latsLngsElevs = [];
if (status == "OK" ) {
for (var i = 0; i < results.length; i++) {
latsLngsElevs.push({
"lat": this.latitude(coords[i]),
"lng": this.longitude(coords[i]),
"elev":results[i].elevation
});
}
cb(null, latsLngsElevs);
} else {
cb(new Error("Could not get elevation using Google's API"), elevationResult.status);
}
},
/**
* @param Array [{lat:#lat, lng:#lng, elev:#elev},....]}
* @return Number % grade
*/
getGrade: function(coords) {
var rise = Math.abs(
this.elevation(coords[coords.length-1]) - this.elevation(coords[0])
);
var run = this.getPathLength(coords);
return Math.floor((rise/run)*100);
},
/**
* @param Array [{lat:#lat, lng:#lng, elev:#elev},....]}
* @return Object {gain:#gain, loss:#loss}
*/
getTotalElevationGainAndLoss: function(coords) {
var gain = 0;
var loss = 0;
for(var i = 0; i < coords.length - 1; i++) {
var deltaElev = this.elevation(coords[i]) - this.elevation(coords[i + 1]);
if (deltaElev > 0) {
loss += deltaElev;
} else {
gain += Math.abs(deltaElev);
}
}
return {
"gain": gain,
"loss": loss
};
}
};
// Node module
if (typeof module !== 'undefined' &&
typeof module.exports !== 'undefined') {
geolib = require('geolib');
geolib.extend(elevation);
// AMD module
} else if (typeof define === "function" && define.amd) {
define(["geolib"], function (geolib) {
geolib.extend(elevation);
return geolib;
});
// we're in a browser
} else {
geolib.extend(elevation);
}
}(this, this.geolib));