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-05-23 22:29:25 +00:00
|
|
|
const MIN_TIME_BETWEEN_REQUESTS = 600;
|
2017-04-27 12:42:42 +00:00
|
|
|
|
2017-04-28 00:17:30 +00:00
|
|
|
const throttle = highland();
|
2017-05-23 22:29:25 +00:00
|
|
|
throttle.ratelimit(1, MIN_TIME_BETWEEN_REQUESTS).each((func) => {
|
2017-04-28 00:17:30 +00:00
|
|
|
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) {
|
2017-08-23 09:44:10 +00:00
|
|
|
return Promise.resolve(points.map(() => (null)));
|
|
|
|
|
|
|
|
/*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)));
|
|
|
|
});
|
|
|
|
}
|
2017-08-23 09:44:10 +00:00
|
|
|
return ret;*/
|
2017-04-28 00:17:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getAscentDescent(elevations) {
|
2017-08-23 09:44:10 +00:00
|
|
|
if(!elevations.some((ele) => (ele != null))) {
|
|
|
|
return {
|
|
|
|
ascent: null,
|
|
|
|
descent: null
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-04-28 00:17:30 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
};
|