Add option to osrm-runner to limit the distance

This commit is contained in:
Patrick Niklaus 2018-04-19 10:08:15 +00:00 committed by Michael Krasnyk
parent b2aeb47630
commit c4eff6cd65

View File

@ -36,27 +36,51 @@ const run_query = (query_options, filters, callback) => {
}).end(); }).end();
}; };
function generate_points(polygon, number) { function generate_points(polygon, number, coordinates_number, max_distance) {
let query_points = []; let query_points = [];
while (query_points.length < number) { while (query_points.length < number) {
var chunk = turf let points = [];
.random('points', number, { bbox: turf.bbox(polygon)})
while(points.length < coordinates_number) {
let chunk = turf
.random('points', coordinates_number, { bbox: turf.bbox(polygon)})
.features .features
.map(x => x.geometry.coordinates) .map(x => x.geometry.coordinates)
.filter(pt => turf.inside(pt, polygon)); .filter(pt => turf.inside(pt, polygon));
query_points = query_points.concat(chunk);
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);
} }
return query_points.slice(0, number);
} }
function generate_queries(options, query_points, coordinates_number) { query_points.push(points);
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);
} }
return query_points;
}
function generate_queries(options, query_points) {
let queries = query_points.map(points => {
return options.path.replace(/{}/g, x => points.pop().join(','));
});
return queries; 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: '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: '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: '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}'}, {name: 'queries-files', alias: 'q', type: String, description: 'CSV file with queries in the first row', typeLabel: '{underline file}'},
]; ];
const options = cla(optionsList); const options = cla(optionsList);
@ -109,8 +134,8 @@ if (options.hasOwnProperty('queries-files')) {
} else { } else {
const polygon = options['bounding-box'].map(x => x.poly).reduce((x,y) => turf.union(x, y)); 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 coordinates_number = (options.path.match(/{}/g) || []).length;
const query_points = generate_points(polygon, coordinates_number * options.number); const query_points = generate_points(polygon, options.number, coordinates_number, options['max-distance']);
queries = generate_queries(options, query_points, coordinates_number); queries = generate_queries(options, query_points);
} }
queries = queries.map(q => { return {hostname: options.server.hostname, port: options.server.port, path: q}; }); queries = queries.map(q => { return {hostname: options.server.hostname, port: options.server.port, path: q}; });