This repository was archived by the owner on Jun 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathtrip.js
More file actions
333 lines (305 loc) · 15 KB
/
trip.js
File metadata and controls
333 lines (305 loc) · 15 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
var OSRM = require('../');
var test = require('tape');
var berlin_path = require('./osrm-data-path').data_path;
test('trip: trip in Berlin', function(assert) {
assert.plan(2);
var osrm = new OSRM(berlin_path);
osrm.trip({coordinates: [[13.36761474609375,52.51663871100423],[13.374481201171875,52.506191342034576]]}, function(err, trip) {
assert.ifError(err);
for (t = 0; t < trip.trips.length; t++) {
assert.ok(trip.trips[t].geometry);
}
});
});
test('trip: trip with many locations in Berlin', function(assert) {
assert.plan(4);
var osrm = new OSRM(berlin_path);
var opts = {coordinates: [[13.36761474609375,52.51663871100423],[13.374481201171875,52.506191342034576],[13.404693603515625,52.50535544522142],[13.388900756835938,52.50159371284434],[13.386840820312498,52.518727886767266],[13.4088134765625,52.528754547664185],[13.41156005859375,52.51705655410405],[13.420486450195312,52.512042174642346],[13.413619995117188,52.50368360390624],[13.36212158203125,52.504101570196205],[13.35113525390625,52.52248815280757],[13.36761474609375,52.53460237630516],[13.383407592773438,52.53710835019913],[13.392333984375,52.536690697815736],[13.42529296875,52.532931647583325],[13.399200439453125,52.52415927884915],[13.390960693359375,52.51956352925745],[13.375167846679688,52.533349335723294],[13.37860107421875,52.520399155853454],[13.355255126953125,52.52081696319122],[13.385467529296875,52.5143405029259],[13.398857116699219,52.513086884218325],[13.399200439453125,52.50744515744915],[13.409500122070312,52.49783165855699],[13.424949645996094,52.500339730516934],[13.440055847167969,52.50786308797268],[13.428382873535156,52.511624283857785],[13.437652587890625,52.50451953251202],[13.443145751953125,52.5199813445422],[13.431129455566406,52.52520370034151],[13.418426513671875,52.52896341209634],[13.429069519042969,52.517474393230245],[13.418083190917969,52.528127948407935],[13.405036926269531,52.52833681581998],[13.384437561035156,52.53084314728766],[13.374481201171875,52.53084314728766],[13.3978271484375,52.532305107923925],[13.418769836425781,52.526039219655445],[13.441085815429688,52.51642978796417],[13.448638916015625,52.51601193890388],[13.44623565673828,52.50535544522142],[13.430442810058594,52.502638670794546],[13.358688354492188,52.520190250694526],[13.358001708984375,52.531887409851336],[13.367271423339842,52.528545682238736],[13.387870788574219,52.52958999943304],[13.406410217285156,52.53961418106945],[13.399543762207031,52.50556442091497],[13.374824523925781,52.50389258754797],[13.386154174804688,52.51099744023003],[13.40229034423828,52.49657756892365]]
};
osrm.trip(opts, function(err, trip) {
assert.ifError(err);
for (t = 0; t < trip.trips.length; t++) {
assert.ok(trip.trips[t].geometry);
}
assert.equal(opts.coordinates.length, trip.waypoints.length);
var indexMap = trip.waypoints.map(function(wp, i) {
return [i, wp.waypoint_index];
});
assert.ok(!indexMap.every(function(tuple) { return tuple[0] === tuple[1]; }));
});
});
test('trip: throws with too few or invalid args', function(assert) {
assert.plan(2);
var osrm = new OSRM(berlin_path);
assert.throws(function() { osrm.trip({coordinates: [[13.43864,52.51993],[13.415852,52.513191]]}) },
/Two arguments required/);
assert.throws(function() { osrm.trip(null, function(err, trip) {}) },
/First arg must be an object/);
});
test('trip: throws with bad params', function(assert) {
assert.plan(14);
var osrm = new OSRM(berlin_path);
assert.throws(function () { osrm.trip({coordinates: []}, function(err) {}) });
assert.throws(function() { osrm.trip({}, function(err, trip) {}) },
/Must provide a coordinates property/);
assert.throws(function() { osrm.trip({
coordinates: null
}, function(err, trip) {}) },
/Coordinates must be an array of \(lon\/lat\) pairs/);
assert.throws(function() { osrm.trip({
coordinates: [13.438640, 52.519930]
}, function(err, trip) {}) },
/Coordinates must be an array of \(lon\/lat\) pairs/);
assert.throws(function() { osrm.trip({
coordinates: [[13.438640], [52.519930]]
}, function(err, trip) {}) },
/Coordinates must be an array of \(lon\/lat\) pairs/);
assert.throws(function() { osrm.trip({
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
hints: null
}, function(err, trip) {}) },
/Hints must be an array of strings\/null/);
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
printInstructions: false,
hints: [13.438640, 52.519930]
};
assert.throws(function() { osrm.trip(options, function(err, trip) {}); },
/Hint must be null or string/);
options.hints = [null];
assert.throws(function() { osrm.trip(options, function(err, trip) {}); },
/Hints array must have the same length as coordinates array/);
delete options.hints;
options.geometries = 'false';
assert.throws(function() { osrm.trip(options, function(err, trip) {}); },
/'geometries' param must be one of \[polyline, polyline6, geojson\]/);
delete options.geometries;
options.source = false;
assert.throws(function() { osrm.trip(options, function(err, trip) {}); },
/Source must be a string: \[any, first\]/);
options.source = 'false';
assert.throws(function() { osrm.trip(options, function(err, trip) {}); },
/'source' param must be one of \[any, first\]/);
delete options.source;
options.destination = true;
assert.throws(function() { osrm.trip(options, function(err, trip) {}); },
/Destination must be a string: \[any, last\]/);
options.destination = 'true';
assert.throws(function() { osrm.trip(options, function(err, trip) {}); },
/'destination' param must be one of \[any, last\]/);
options.roundtrip = 'any';
assert.throws(function() { osrm.trip(options, function(err, trip) {}); },
/'roundtrip' param must be a boolean/);
});
test('trip: routes Berlin using shared memory', function(assert) {
assert.plan(2);
var osrm = new OSRM();
osrm.trip({coordinates: [[13.43864,52.51993],[13.415852,52.513191]]}, function(err, trip) {
assert.ifError(err);
for (t = 0; t < trip.trips.length; t++) {
assert.ok(trip.trips[t].geometry);
}
});
});
test('trip: routes Berlin with hints', function(assert) {
assert.plan(5);
var osrm = new OSRM(berlin_path);
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
steps: false
};
osrm.trip(options, function(err, first) {
assert.ifError(err);
for (t = 0; t < first.trips.length; t++) {
assert.ok(first.trips[t].geometry);
}
var hints = first.waypoints.map(function(wp) { return wp.hint; });
assert.ok(hints.every(function(h) { return typeof h === 'string'; }));
options.hints = hints;
osrm.trip(options, function(err, second) {
assert.ifError(err);
assert.deepEqual(first, second);
});
});
});
test('trip: trip through Berlin with geometry compression', function(assert) {
assert.plan(2);
var osrm = new OSRM(berlin_path);
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]]
};
osrm.trip(options, function(err, trip) {
assert.ifError(err);
for (t = 0; t < trip.trips.length; t++) {
assert.equal('string', typeof trip.trips[t].geometry);
}
});
});
test('trip: trip through Berlin without geometry compression', function(assert) {
assert.plan(2);
var osrm = new OSRM(berlin_path);
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
geometries: 'geojson'
};
osrm.trip(options, function(err, trip) {
assert.ifError(err);
for (t = 0; t < trip.trips.length; t++) {
assert.ok(Array.isArray(trip.trips[t].geometry.coordinates));
}
});
});
test('trip: trip through Berlin with speed annotations options', function(assert) {
assert.plan(12);
var osrm = new OSRM(berlin_path);
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
steps: true,
annotations: ['speed'],
overview: 'false'
};
osrm.trip(options, function(err, trip) {
assert.ifError(err);
assert.equal(trip.trips.length, 1);
for (t = 0; t < trip.trips.length; t++) {
assert.ok(trip.trips[t]);
assert.ok(trip.trips[t].legs.every(function(l) { return l.steps.length > 0; }), 'every leg has steps')
assert.ok(trip.trips[t].legs.every(function(l) { return l.annotation; }), 'every leg has annotations')
assert.ok(trip.trips[t].legs.every(function(l) { return l.annotation.speed; }), 'every leg has annotations for speed')
assert.notOk(trip.trips[t].legs.every(function(l) { return l.annotation.weight; }), 'has no annotations for weight')
assert.notOk(trip.trips[t].legs.every(function(l) { return l.annotation.datasources; }), 'has no annotations for datasources')
assert.notOk(trip.trips[t].legs.every(function(l) { return l.annotation.duration; }), 'has no annotations for duration')
assert.notOk(trip.trips[t].legs.every(function(l) { return l.annotation.distance; }), 'has no annotations for distance')
assert.notOk(trip.trips[t].legs.every(function(l) { return l.annotation.nodes; }), 'has no annotations for nodes')
assert.notOk(trip.trips[t].geometry);
}
});
});
test('trip: trip through Berlin with several (duration, distance, nodes) annotations options', function(assert) {
assert.plan(12);
var osrm = new OSRM(berlin_path);
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
steps: true,
annotations: ['duration', 'distance', 'nodes'],
overview: 'false'
};
osrm.trip(options, function(err, trip) {
assert.ifError(err);
assert.equal(trip.trips.length, 1);
for (t = 0; t < trip.trips.length; t++) {
assert.ok(trip.trips[t]);
assert.ok(trip.trips[t].legs.every(function(l) { return l.steps.length > 0; }), 'every leg has steps')
assert.ok(trip.trips[t].legs.every(function(l) { return l.annotation; }), 'every leg has annotations')
assert.ok(trip.trips[t].legs.every(function(l) { return l.annotation.duration; }), 'every leg has annotations for duration')
assert.ok(trip.trips[t].legs.every(function(l) { return l.annotation.distance; }), 'every leg has annotations for distance')
assert.ok(trip.trips[t].legs.every(function(l) { return l.annotation.nodes; }), 'every leg has annotations for nodes')
assert.notOk(trip.trips[t].legs.every(function(l) { return l.annotation.weight; }), 'has no annotations for weight')
assert.notOk(trip.trips[t].legs.every(function(l) { return l.annotation.datasources; }), 'has no annotations for datasources')
assert.notOk(trip.trips[t].legs.every(function(l) { return l.annotation.speed; }), 'has no annotations for speed')
assert.notOk(trip.trips[t].geometry);
}
});
});
test('trip: trip through Berlin with options', function(assert) {
assert.plan(6);
var osrm = new OSRM(berlin_path);
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
steps: true,
annotations: true,
overview: 'false'
};
osrm.trip(options, function(err, trip) {
assert.ifError(err);
assert.equal(trip.trips.length, 1);
for (t = 0; t < trip.trips.length; t++) {
assert.ok(trip.trips[t]);
assert.ok(trip.trips[t].legs.every(function(l) { return l.steps.length > 0; }), 'every leg has steps')
assert.ok(trip.trips[t].legs.every(function(l) { return l.annotation; }), 'every leg has annotations')
assert.notOk(trip.trips[t].geometry);
}
});
});
test('trip: routes Berlin with null hints', function(assert) {
assert.plan(1);
var osrm = new OSRM(berlin_path);
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
printInstructions: false,
hints: [null, null]
};
osrm.trip(options, function(err, second) {
assert.ifError(err);
});
});
test('trip: service combinations that are not implemented', function(assert) {
assert.plan(3);
var osrm = new OSRM(berlin_path);
// fixed start, non-roundtrip
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
source: 'first',
roundtrip: false
};
osrm.trip(options, function(err, second) {
assert.equal('NotImplemented', err.message);
});
// fixed start, fixed end, non-roundtrip
options.source = 'any';
options.destination = 'any';
osrm.trip(options, function(err, second) {
assert.equal('NotImplemented', err.message);
});
// fixed end, non-roundtrip
delete options.source;
options.destination = 'last';
osrm.trip(options, function(err, second) {
assert.equal('NotImplemented', err.message);
});
});
test('trip: fixed start and end combinations', function(assert) {
var osrm = new OSRM(berlin_path);
var options = {
coordinates: [[13.36761474609375,52.51663871100423],[13.374481201171875,52.506191342034576]],
source: 'first',
destination: 'last',
roundtrip: false,
geometries: 'geojson'
};
// fixed start and end, non-roundtrip
osrm.trip(options, function(err, fseTrip) {
assert.ifError(err);
assert.equal(206.8, fseTrip.trips[0].duration);
assert.equal(1, fseTrip.trips.length);
var coordinates = fseTrip.trips[0].geometry.coordinates;
assert.equal(15, coordinates.length);
assert.notEqual(JSON.stringify(coordinates[0]), JSON.stringify(coordinates[coordinates.length - 1]));
});
// variations of roundtrip
var roundtripChecks = function(options) {
osrm.trip(options, function(err, trip) {
assert.ifError(err);
assert.equal(1, trip.trips.length);
assert.equal(422, Math.round(trip.trips[0].duration));
var coordinates = trip.trips[0].geometry.coordinates;
assert.equal(29, coordinates.length);
assert.equal(JSON.stringify(coordinates[0]), JSON.stringify(coordinates[coordinates.length - 1]));
});
}
// roundtrip, source and destination not specified
roundtripChecks({coordinates: options.coordinates, geometries: options.geometries});
// roundtrip, fixed destination
options.roundtrip = true;
delete options.source;
roundtripChecks(options);
//roundtrip, fixed source
delete options.destination;
options.source = 'first';
roundtripChecks(options);
// roundtrip, non-fixed source, non-fixed destination
options.source = 'any';
options.destination = 'any';
roundtripChecks(options);
assert.end();
});