2017-04-28 00:17:30 +00:00
|
|
|
const highland = require("highland");
|
2017-04-27 12:42:42 +00:00
|
|
|
const polyline = require("@mapbox/polyline");
|
|
|
|
const request = require("request-promise").defaults({
|
|
|
|
gzip: true,
|
|
|
|
headers: {
|
|
|
|
'User-Agent': process.env.fmUserAgent
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const API_URL = "https://elevation.mapzen.com/height";
|
|
|
|
const API_KEY = "mapzen-LWPWRB1";
|
2017-04-28 00:52:29 +00:00
|
|
|
const LIMIT = 500;
|
2017-04-28 00:17:30 +00:00
|
|
|
const PER_SECOND_LIMIT = 2;
|
2017-04-27 12:42:42 +00:00
|
|
|
|
2017-04-28 00:17:30 +00:00
|
|
|
const throttle = highland();
|
|
|
|
throttle.ratelimit(PER_SECOND_LIMIT, 1000).each((func) => {
|
|
|
|
func();
|
|
|
|
});
|
2017-04-27 12:42:42 +00:00
|
|
|
|
|
|
|
const elevation = module.exports = {
|
|
|
|
|
2017-04-28 00:17:30 +00:00
|
|
|
_getThrottledSlot() {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
throttle.write(resolve);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-04-27 12:42:42 +00:00
|
|
|
getElevationForPoint(point) {
|
|
|
|
return elevation.getElevationForPoints([point]).then((points) => (points[0]));
|
|
|
|
},
|
|
|
|
|
|
|
|
getElevationForPoints(points) {
|
|
|
|
if(points.length == 0)
|
2017-04-28 00:17:30 +00:00
|
|
|
return Promise.resolve([ ]);
|
|
|
|
|
|
|
|
let ret = Promise.resolve([ ]);
|
|
|
|
for(let i=0; i<points.length; i+=LIMIT) {
|
|
|
|
ret = ret.then((heights) => {
|
|
|
|
return elevation._getThrottledSlot().then(() => (heights));
|
|
|
|
}).then((heights) => {
|
|
|
|
let json = {
|
|
|
|
encoded_polyline: polyline.encode(points.slice(i, i+LIMIT).map((point) => ([point.lat, point.lon])), 6),
|
|
|
|
range: false
|
|
|
|
};
|
2017-04-27 12:42:42 +00:00
|
|
|
|
2017-04-28 00:17:30 +00:00
|
|
|
return request.get({
|
|
|
|
url: `${API_URL}?json=${encodeURI(JSON.stringify(json))}&api_key=${API_KEY}`,
|
|
|
|
json: true
|
|
|
|
}).then((res) => (heights.concat(res.height)));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
},
|
|
|
|
|
|
|
|
getAscentDescent(elevations) {
|
|
|
|
let ret = {
|
|
|
|
ascent: 0,
|
|
|
|
descent: 0
|
2017-04-27 12:42:42 +00:00
|
|
|
};
|
|
|
|
|
2017-04-28 00:17:30 +00:00
|
|
|
for(let i=1; i<elevations.length; i++) {
|
|
|
|
if(elevations[i] > elevations[i-1])
|
|
|
|
ret.ascent += elevations[i] - elevations[i-1];
|
|
|
|
else
|
|
|
|
ret.descent += elevations[i-1] - elevations[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2017-04-27 12:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|