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 pathroute.js
More file actions
471 lines (443 loc) · 19.5 KB
/
route.js
File metadata and controls
471 lines (443 loc) · 19.5 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
var OSRM = require('../');
var test = require('tape');
var berlin_path = require('./osrm-data-path').data_path;
test('route: routes Berlin', function(assert) {
assert.plan(5);
var osrm = new OSRM(berlin_path);
osrm.route({coordinates: [[13.43864,52.51993],[13.415852,52.513191]]}, function(err, route) {
assert.ifError(err);
assert.ok(route.waypoints);
assert.ok(route.routes);
assert.ok(route.routes.length);
assert.ok(route.routes[0].geometry);
});
});
test('route: throws with too few or invalid args', function(assert) {
assert.plan(3);
var osrm = new OSRM(berlin_path);
assert.throws(function() { osrm.route({coordinates: [[13.43864,52.51993],[13.415852,52.513191]]}) },
/Two arguments required/);
assert.throws(function() { osrm.route(null, function(err, route) {}) },
/First arg must be an object/);
assert.throws(function() { osrm.route({coordinates: [[13.43864,52.51993],[13.415852,52.513191]]}, true)},
/last argument must be a callback function/);
});
test('route: provides no alternatives by default, but when requested', function(assert) {
assert.plan(6);
var osrm = new OSRM(berlin_path);
var options = {coordinates: [[13.302383,52.490516], [13.418427,52.522070]]};
osrm.route(options, function(err, route) {
assert.ifError(err);
assert.ok(route.routes);
assert.equal(route.routes.length, 1);
});
options.alternatives = true;
osrm.route(options, function(err, route) {
assert.ifError(err);
assert.ok(route.routes);
assert.equal(route.routes.length, 2);
});
});
test('route: throws with bad params', function(assert) {
assert.plan(11);
var osrm = new OSRM(berlin_path);
assert.throws(function () { osrm.route({coordinates: []}, function(err) {}) });
assert.throws(function() { osrm.route({}, function(err, route) {}) },
/Must provide a coordinates property/);
assert.throws(function() { osrm.route({coordinates: null}, function(err, route) {}) },
/Coordinates must be an array of \(lon\/lat\) pairs/);
assert.throws(function() { osrm.route({coordinates: [13.438640, 52.519930]}, function(err, route) {}) },
/Coordinates must be an array of \(lon\/lat\) pairs/);
assert.throws(function() { osrm.route({coordinates: [[true, '52.519930'], [13.438640, 52.519930]]}, function(err, route) {}) },
/Each member of a coordinate pair must be a number/);
assert.throws(function() { osrm.route({coordinates: [[213.43864,252.51993],[413.415852,552.513191]]}, function(err, route) {}) },
/Lng\/Lat coordinates must be within world bounds \(-180 < lng < 180, -90 < lat < 90\)/);
assert.throws(function() { osrm.route({coordinates: [[13.438640], [52.519930]]}, function(err, route) {}) },
/Coordinates must be an array of \(lon\/lat\) pairs/);
assert.throws(function() { osrm.route({coordinates: [[13.43864,52.51993],[13.415852,52.513191]], hints: null}, function(err, route) {}) },
/Hints must be an array of strings\/null/);
assert.throws(function() { osrm.route({coordinates: [[13.43864,52.51993],[13.415852,52.513191]], steps: null}, function(err, route) {}) });
assert.throws(function() { osrm.route({coordinates: [[13.43864,52.51993],[13.415852,52.513191]], annotations: null}, function(err, route) {}) });
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
alternateRoute: false,
hints: [13.438640, 52.519930]
};
assert.throws(function() { osrm.route(options, function(err, route) {}) },
/Hint must be null or string/);
});
test('route: routes Berlin using shared memory', function(assert) {
assert.plan(2);
var osrm = new OSRM();
osrm.route({coordinates: [[13.43864,52.51993],[13.415852,52.513191]]}, function(err, route) {
assert.ifError(err);
assert.ok(Array.isArray(route.routes));
});
});
test('route: routes 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.route(options, function(err, route) {
assert.ifError(err);
assert.equal('string', typeof route.routes[0].geometry);
});
});
test('route: routes Berlin without geometry compression', function(assert) {
assert.plan(4);
var osrm = new OSRM(berlin_path);
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
geometries: 'geojson'
};
osrm.route(options, function(err, route) {
assert.ifError(err);
assert.ok(Array.isArray(route.routes));
assert.ok(Array.isArray(route.routes[0].geometry.coordinates));
assert.equal(route.routes[0].geometry.type, 'LineString');
});
});
test('Test polyline6 geometries option', function(assert) {
assert.plan(6);
var osrm = new OSRM(berlin_path);
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
continue_straight: false,
overview: 'false',
geometries: 'polyline6',
steps: true
};
osrm.route(options, function(err, first) {
assert.ifError(err);
assert.ok(first.routes);
assert.equal(first.routes.length, 1);
assert.notOk(first.routes[0].geometry);
assert.ok(first.routes[0].legs[0]);
assert.equal(typeof first.routes[0].legs[0].steps[0].geometry, 'string');
});
});
test('route: routes Berlin with speed annotations options', function(assert) {
assert.plan(17);
var osrm = new OSRM(berlin_path);
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
continue_straight: false,
overview: 'false',
geometries: 'polyline',
steps: true,
annotations: ['speed']
};
osrm.route(options, function(err, first) {
assert.ifError(err);
assert.ok(first.routes);
assert.ok(first.routes[0].legs.every(function(l) { return Array.isArray(l.steps) && l.steps.length > 0; }));
assert.equal(first.routes.length, 1);
assert.notOk(first.routes[0].geometry);
assert.ok(first.routes[0].legs[0]);
assert.ok(first.routes[0].legs.every(l => { return l.steps.length > 0; }), 'every leg has steps');
assert.ok(first.routes[0].legs.every(l => { return l.annotation;}), 'every leg has annotations');
assert.ok(first.routes[0].legs.every(l => { return l.annotation.speed;}), 'every leg has annotations for speed');
assert.notOk(first.routes[0].legs.every(l => { return l.annotation.weight; }), 'has no annotations for weight')
assert.notOk(first.routes[0].legs.every(l => { return l.annotation.datasources; }), 'has no annotations for datasources')
assert.notOk(first.routes[0].legs.every(l => { return l.annotation.duration; }), 'has no annotations for duration')
assert.notOk(first.routes[0].legs.every(l => { return l.annotation.distance; }), 'has no annotations for distance')
assert.notOk(first.routes[0].legs.every(l => { return l.annotation.nodes; }), 'has no annotations for nodes')
options.overview = 'full';
osrm.route(options, function(err, full) {
assert.ifError(err);
options.overview = 'simplified';
osrm.route(options, function(err, simplified) {
assert.ifError(err);
assert.notEqual(full.routes[0].geometry, simplified.routes[0].geometry);
});
});
});
});
test('route: routes Berlin with several (duration, distance, nodes) annotations options', function(assert) {
assert.plan(17);
var osrm = new OSRM(berlin_path);
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
continue_straight: false,
overview: 'false',
geometries: 'polyline',
steps: true,
annotations: ['duration','distance','nodes']
};
osrm.route(options, function(err, first) {
assert.ifError(err);
assert.ok(first.routes);
assert.ok(first.routes[0].legs.every(function(l) { return Array.isArray(l.steps) && l.steps.length > 0; }));
assert.equal(first.routes.length, 1);
assert.notOk(first.routes[0].geometry);
assert.ok(first.routes[0].legs[0]);
assert.ok(first.routes[0].legs.every(l => { return l.steps.length > 0; }), 'every leg has steps');
assert.ok(first.routes[0].legs.every(l => { return l.annotation;}), 'every leg has annotations');
assert.ok(first.routes[0].legs.every(l => { return l.annotation.distance;}), 'every leg has annotations for distance');
assert.ok(first.routes[0].legs.every(l => { return l.annotation.duration;}), 'every leg has annotations for durations');
assert.ok(first.routes[0].legs.every(l => { return l.annotation.nodes;}), 'every leg has annotations for nodes');
assert.notOk(first.routes[0].legs.every(l => { return l.annotation.weight; }), 'has no annotations for weight')
assert.notOk(first.routes[0].legs.every(l => { return l.annotation.datasources; }), 'has no annotations for datasources')
assert.notOk(first.routes[0].legs.every(l => { return l.annotation.speed; }), 'has no annotations for speed')
options.overview = 'full';
osrm.route(options, function(err, full) {
assert.ifError(err);
options.overview = 'simplified';
osrm.route(options, function(err, simplified) {
assert.ifError(err);
assert.notEqual(full.routes[0].geometry, simplified.routes[0].geometry);
});
});
});
});
test('route: routes Berlin with options', function(assert) {
assert.plan(11);
var osrm = new OSRM(berlin_path);
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
continue_straight: false,
overview: 'false',
geometries: 'polyline',
steps: true,
annotations: true
};
osrm.route(options, function(err, first) {
assert.ifError(err);
assert.ok(first.routes);
assert.ok(first.routes[0].legs.every(function(l) { return Array.isArray(l.steps) && l.steps.length > 0; }));
assert.equal(first.routes.length, 1);
assert.notOk(first.routes[0].geometry);
assert.ok(first.routes[0].legs[0]);
assert.ok(first.routes[0].legs.every(l => { return l.steps.length > 0; }), 'every leg has steps');
assert.ok(first.routes[0].legs.every(l => { return l.annotation;}), 'every leg has annotations');
options.overview = 'full';
osrm.route(options, function(err, full) {
assert.ifError(err);
options.overview = 'simplified';
osrm.route(options, function(err, simplified) {
assert.ifError(err);
assert.notEqual(full.routes[0].geometry, simplified.routes[0].geometry);
});
});
});
});
test('route: routes Berlin with options', function(assert) {
assert.plan(11);
var osrm = new OSRM(berlin_path);
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
continue_straight: false,
overview: 'false',
geometries: 'polyline',
steps: true,
annotations: true
};
osrm.route(options, function(err, first) {
assert.ifError(err);
assert.ok(first.routes);
assert.ok(first.routes[0].legs.every(function(l) { return Array.isArray(l.steps) && l.steps.length > 0; }));
assert.equal(first.routes.length, 1);
assert.notOk(first.routes[0].geometry);
assert.ok(first.routes[0].legs[0]);
assert.ok(first.routes[0].legs.every(l => { return l.steps.length > 0; }), 'every leg has steps');
assert.ok(first.routes[0].legs.every(l => { return l.annotation;}), 'every leg has annotations');
options.overview = 'full';
osrm.route(options, function(err, full) {
assert.ifError(err);
options.overview = 'simplified';
osrm.route(options, function(err, simplified) {
assert.ifError(err);
assert.notEqual(full.routes[0].geometry, simplified.routes[0].geometry);
});
});
});
});
test('route: invalid route options', function(assert) {
assert.plan(8);
var osrm = new OSRM(berlin_path);
assert.throws(function() { osrm.route({
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
continue_straight: []
}, function(err, route) {}); },
/must be boolean/);
assert.throws(function() { osrm.route({
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
alternatives: []
}, function(err, route) {}); },
/must be boolean/);
assert.throws(function() { osrm.route({
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
geometries: true
}, function(err, route) {}); },
/Geometries must be a string: \[polyline, polyline6, geojson\]/);
assert.throws(function() { osrm.route({
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
overview: false
}, function(err, route) {}); },
/Overview must be a string: \[simplified, full, false\]/);
assert.throws(function() { osrm.route({
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
overview: false
}, function(err, route) {}); },
/Overview must be a string: \[simplified, full, false\]/);
assert.throws(function() { osrm.route({
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
overview: 'maybe'
}, function(err, route) {}); },
/'overview' param must be one of \[simplified, full, false\]/);
assert.throws(function() { osrm.route({
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
geometries: 'maybe'
}, function(err, route) {}); },
/'geometries' param must be one of \[polyline, polyline6, geojson\]/);
assert.throws(function() { osrm.route({
coordinates: [[NaN, -NaN],[Infinity, -Infinity]]
}, function(err, route) {}); },
/Lng\/Lat coordinates must be valid numbers/);
});
test('route: integer bearing values no longer supported', function(assert) {
assert.plan(1);
var osrm = new OSRM(berlin_path);
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
bearings: [200, 250],
};
assert.throws(function() { osrm.route(options, function(err, route) {}); },
/Bearing must be an array of \[bearing, range\] or null/);
});
test('route: valid bearing values', function(assert) {
assert.plan(4);
var osrm = new OSRM(berlin_path);
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
bearings: [[200, 180], [250, 180]],
};
osrm.route(options, function(err, route) {
assert.ifError(err);
assert.ok(route.routes[0]);
});
options.bearings = [null, [360, 180]];
osrm.route(options, function(err, route) {
assert.ifError(err);
assert.ok(route.routes[0]);
});
});
test('route: invalid bearing values', function(assert) {
assert.plan(6);
var osrm = new OSRM(berlin_path);
assert.throws(function() { osrm.route({
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
bearings: [[400, 180], [-250, 180]],
}, function(err, route) {}) },
/Bearing values need to be in range 0..360, 0..180/);
assert.throws(function() { osrm.route({
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
bearings: [[200], [250, 180]],
}, function(err, route) {}) },
/Bearing must be an array of/);
assert.throws(function() { osrm.route({
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
bearings: [[400, 109], [100, 720]],
}, function(err, route) {}) },
/Bearing values need to be in range 0..360, 0..180/);
assert.throws(function() { osrm.route({
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
bearings: 400,
}, function(err, route) {}) },
/Bearings must be an array of arrays of numbers/);
assert.throws(function() { osrm.route({
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
bearings: [[100, 100]],
}, function(err, route) {}) },
/Bearings array must have the same length as coordinates array/);
assert.throws(function() { osrm.route({
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
bearings: [Infinity, Infinity],
}, function(err, route) {}) },
/Bearing must be an array of \[bearing, range\] or null/);
});
test('route: 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]]
};
osrm.route(options, function(err, first) {
assert.ifError(err);
assert.ok(first.waypoints);
var hints = first.waypoints.map(function(wp) { return wp.hint; });
assert.ok(hints.every(function(h) { return typeof h === 'string'; }));
options.hints = hints;
osrm.route(options, function(err, second) {
assert.ifError(err);
assert.deepEqual(first, second);
});
});
});
test('route: 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]],
hints: [null, null]
};
osrm.route(options, function(err, route) {
assert.ifError(err);
});
});
test('route: throws on bad hints', function(assert) {
assert.plan(2);
var osrm = new OSRM(berlin_path);
assert.throws(function() { osrm.route({
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
hints: ['', '']
}, function(err, route) {})}, /Hint cannot be an empty string/);
assert.throws(function() { osrm.route({
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
hints: [null]
}, function(err, route) {})}, /Hints array must have the same length as coordinates array/);
});
test('route: routes Berlin with valid radius values', function(assert) {
assert.plan(3);
var osrm = new OSRM(berlin_path);
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
radiuses: [100, 100]
};
osrm.route(options, function(err, route) {
assert.ifError(err);
});
options.radiuses = [null, null];
osrm.route(options, function(err, route) {
assert.ifError(err);
});
options.radiuses = [100, null];
osrm.route(options, function(err, route) {
assert.ifError(err);
});
});
test('route: throws on bad radiuses', function(assert) {
assert.plan(3);
var osrm = new OSRM(berlin_path);
var options = {
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
radiuses: [10, 10]
};
assert.throws(function() { osrm.route({
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
radiuses: 10
}, function(err, route) {}) },
/Radiuses must be an array of non-negative doubles or null/);
assert.throws(function() { osrm.route({
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
radiuses: ['magic', 'numbers']
}, function(err, route) {}) },
/Radius must be non-negative double or null/);
assert.throws(function() { osrm.route({
coordinates: [[13.43864,52.51993],[13.415852,52.513191]],
radiuses: [10]
}, function(err, route) {}) },
/Radiuses array must have the same length as coordinates array/);
});