From c4eff6cd656b49e3cb5fc841e868031e12a97465 Mon Sep 17 00:00:00 2001 From: Patrick Niklaus Date: Thu, 19 Apr 2018 10:08:15 +0000 Subject: [PATCH] Add option to osrm-runner to limit the distance --- scripts/osrm-runner.js | 61 +++++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/scripts/osrm-runner.js b/scripts/osrm-runner.js index ea3ce8e89..c424d8faf 100755 --- a/scripts/osrm-runner.js +++ b/scripts/osrm-runner.js @@ -36,27 +36,51 @@ const run_query = (query_options, filters, callback) => { }).end(); }; -function generate_points(polygon, number) { +function generate_points(polygon, number, coordinates_number, max_distance) { let query_points = []; while (query_points.length < number) { - var chunk = turf - .random('points', number, { bbox: turf.bbox(polygon)}) - .features - .map(x => x.geometry.coordinates) - .filter(pt => turf.inside(pt, polygon)); - query_points = query_points.concat(chunk); + let points = []; + + while(points.length < coordinates_number) { + let chunk = turf + .random('points', coordinates_number, { bbox: turf.bbox(polygon)}) + .features + .map(x => x.geometry.coordinates) + .filter(pt => turf.inside(pt, polygon)); + + if (max_distance > 0) + { + chunk.forEach(pt => { + if (points.length == 0) + { + points.push(pt); + } + else + { + let distance = turf.distance(pt, points[points.length-1], 'meters'); + if (distance < max_distance) + { + points.push(pt); + } + } + }); + } + else + { + points = points.concat(chunk); + } + } + + query_points.push(points); } - return query_points.slice(0, number); + + return query_points; } -function generate_queries(options, query_points, coordinates_number) { - let queries = []; - for (let chunk = 0; chunk < query_points.length; chunk += coordinates_number) - { - let points = query_points.slice(chunk, chunk + coordinates_number); - let query = options.path.replace(/{}/g, x => points.pop().join(',')); - queries.push(query); - } +function generate_queries(options, query_points) { + let queries = query_points.map(points => { + return options.path.replace(/{}/g, x => points.pop().join(',')); + }); return queries; } @@ -80,6 +104,7 @@ const optionsList = [ {name: 'bounding-box', alias: 'b', type: BoundingBox, defaultValue: BoundingBox('5.86442,47.2654,15.0508,55.1478'), multiple: true, description: 'queries bounding box, default "5.86442,47.2654,15.0508,55.1478"', typeLabel: '{underline west,south,east,north}'}, {name: 'max-sockets', alias: 'm', type: Number, defaultValue: 1, description: 'how many concurrent sockets the agent can have open per origin, default 1', typeLabel: '{underline number}'}, {name: 'number', alias: 'n', type: Number, defaultValue: 10, description: 'number of query points, default 10', typeLabel: '{underline number}'}, + {name: 'max-distance', alias: 'd', type: Number, defaultValue: -1, description: 'maximal distance between coordinates', typeLabel: '{underline number}'}, {name: 'queries-files', alias: 'q', type: String, description: 'CSV file with queries in the first row', typeLabel: '{underline file}'}, ]; const options = cla(optionsList); @@ -109,8 +134,8 @@ if (options.hasOwnProperty('queries-files')) { } else { const polygon = options['bounding-box'].map(x => x.poly).reduce((x,y) => turf.union(x, y)); const coordinates_number = (options.path.match(/{}/g) || []).length; - const query_points = generate_points(polygon, coordinates_number * options.number); - queries = generate_queries(options, query_points, coordinates_number); + const query_points = generate_points(polygon, options.number, coordinates_number, options['max-distance']); + queries = generate_queries(options, query_points); } queries = queries.map(q => { return {hostname: options.server.hostname, port: options.server.port, path: q}; });