Add option to osrm-runner to limit the distance
This commit is contained in:
parent
b2aeb47630
commit
c4eff6cd65
@ -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)})
|
|
||||||
.features
|
while(points.length < coordinates_number) {
|
||||||
.map(x => x.geometry.coordinates)
|
let chunk = turf
|
||||||
.filter(pt => turf.inside(pt, polygon));
|
.random('points', coordinates_number, { bbox: turf.bbox(polygon)})
|
||||||
query_points = query_points.concat(chunk);
|
.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) {
|
function generate_queries(options, query_points) {
|
||||||
let queries = [];
|
let queries = query_points.map(points => {
|
||||||
for (let chunk = 0; chunk < query_points.length; chunk += coordinates_number)
|
return options.path.replace(/{}/g, x => points.pop().join(','));
|
||||||
{
|
});
|
||||||
let points = query_points.slice(chunk, chunk + coordinates_number);
|
|
||||||
let query = options.path.replace(/{}/g, x => points.pop().join(','));
|
|
||||||
queries.push(query);
|
|
||||||
}
|
|
||||||
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}; });
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user