-
Notifications
You must be signed in to change notification settings - Fork 85
/
GraphHopperIsochrone.js
65 lines (54 loc) · 1.92 KB
/
GraphHopperIsochrone.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
let request = require('axios');
let GHUtil = require("./GHUtil");
let ghUtil = new GHUtil();
GraphHopperIsochrone = function (args, requestDefaults) {
this.defaults = {
time_limit: 600,
distance_limit: 0,
buckets: 3,
profile: "car",
debug: false,
reverse_flow: false
};
if (requestDefaults)
Object.keys(requestDefaults).forEach(key => {
this.defaults[key] = requestDefaults[key];
});
this.key = args.key;
this.host = args.host ? args.host : "https://graphhopper.com/api/1";
this.endpoint = args.endpoint ? args.endpoint : '/isochrone';
this.timeout = args.timeout ? args.timeout : 30000;
};
GraphHopperIsochrone.prototype.getParametersAsQueryString = function (args) {
let qString = "point=" + args.point;
qString += "&time_limit=" + args.time_limit;
qString += "&distance_limit=" + args.distance_limit;
qString += "&buckets=" + args.buckets;
qString += "&profile=" + args.profile;
qString += "&reverse_flow=" + args.reverse_flow;
if (args.debug)
qString += "&debug=true";
return qString;
};
GraphHopperIsochrone.prototype.doRequest = function (reqArgs) {
Object.keys(this.defaults).forEach(key => {
if (!reqArgs[key]) reqArgs[key] = this.defaults[key];
});
let url = this.host + this.endpoint + "?" + this.getParametersAsQueryString(reqArgs) + "&key=" + this.key;
let that = this;
return new Promise(function (resolve, reject) {
request
.get(url, {timeout: that.timeout})
.then(res => {
if (res.status !== 200) {
reject(ghUtil.extractError(res, url));
} else if (res) {
resolve(res.data);
}
})
.catch(err => {
reject(ghUtil.extractError(err.response, url));
})
});
};
module.exports = GraphHopperIsochrone;