-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathGraphHopperRouting.js
139 lines (126 loc) · 4.82 KB
/
GraphHopperRouting.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
let request = require('axios');
var GHUtil = require("./GHUtil");
let ghUtil = new GHUtil();
GraphHopperRouting = function (args, requestDefaults) {
this.defaults = {
profile: "car",
debug: false,
locale: "en",
points_encoded: true,
instructions: true,
elevation: true,
optimize: "false"
}
if (requestDefaults)
Object.keys(requestDefaults).forEach(key => {
this.defaults[key] = requestDefaults[key];
});
// required API key
this.key = args.key;
this.host = args.host ? args.host : "https://graphhopper.com/api/1";
this.endpoint = args.endpoint ? args.endpoint : '/route';
this.timeout = args.timeout ? args.timeout : 10000;
this.turn_sign_map = args.turn_sign_map ? args.turn_sign_map : {
"-6": "leave roundabout",
"-3": "turn sharp left",
"-2": "turn left",
"-1": "turn slight left",
0: "continue",
1: "turn slight right",
2: "turn right",
3: "turn sharp right",
4: "finish",
5: "reached via point",
6: "enter roundabout"
};
};
/**
* Execute the routing request using the provided args.
*/
GraphHopperRouting.prototype.doRequest = function (reqArgs) {
Object.keys(this.defaults).forEach(key => {
if (!reqArgs[key]) reqArgs[key] = this.defaults[key];
});
let url = this.host + this.endpoint + "?key=" + this.key;
let that = this;
return new Promise((resolve, reject) => {
request.post(url, reqArgs, {
timeout: that.timeout,
headers: {'Content-Type': 'application/json'}
})
.then(res => {
if (res.status !== 200) {
reject(ghUtil.extractError(res, url));
return;
}
if (res.data.paths) {
for (let i = 0; i < res.data.paths.length; i++) {
let path = res.data.paths[i];
// convert encoded polyline to geo json
if (path.points_encoded) {
let tmpArray = ghUtil.decodePath(path.points, reqArgs.elevation);
path.points = {
"type": "LineString", "coordinates": tmpArray
};
let tmpSnappedArray = ghUtil.decodePath(path.snapped_waypoints, reqArgs.elevation);
path.snapped_waypoints = {
"type": "LineString", "coordinates": tmpSnappedArray
};
}
if (path.instructions) {
for (let j = 0; j < path.instructions.length; j++) {
// Add a LngLat to every instruction
let interval = path.instructions[j].interval;
// The second parameter of slice is non inclusive, therefore we have to add +1
path.instructions[j].points = path.points.coordinates.slice(interval[0], interval[1] + 1);
}
}
}
}
resolve(res.data);
})
.catch(err => {
reject(ghUtil.extractError(err.response, url));
});
});
};
GraphHopperRouting.prototype.info = function () {
let that = this;
return new Promise((resolve, reject) => {
let url = that.host + "/info?key=" + that.key;
request.get(url, {timeout: that.timeout, headers: {'Content-Type': 'application/json'}})
.then(res => {
if (res.status !== 200) {
reject(ghUtil.extractError(res, url));
return;
}
resolve(res.data);
})
.catch(err => {
console.log(err)
reject(ghUtil.extractError(err.response, url));
});
});
};
GraphHopperRouting.prototype.i18n = function (args) {
let locale = args && args.locale ? args.locale : this.defaults.locale;
let that = this;
return new Promise((resolve, reject) => {
let url = that.host + "/i18n/" + locale + "?key=" + that.key;
request.get(url, {timeout: that.timeout, headers: {'Content-Type': 'application/json'}})
.then(res => {
if (res.status !== 200) {
reject(ghUtil.extractError(res, url));
return;
}
resolve(res.data);
})
.catch(err => {
reject(ghUtil.extractError(err.response, url));
});
});
};
GraphHopperRouting.prototype.getTurnText = function (sign) {
return this.turn_sign_map[sign];
};
module.exports = GraphHopperRouting;