2016-09-26 16:25:54 -04:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-04 15:11:05 -05:00
|
|
|
var util = require('util');
|
|
|
|
var path = require('path');
|
|
|
|
var fs = require('fs');
|
|
|
|
var d3 = require('d3-queue');
|
2016-09-19 17:13:44 -04:00
|
|
|
var OSM = require('../lib/osm');
|
2016-03-04 15:11:05 -05:00
|
|
|
|
|
|
|
module.exports = function () {
|
|
|
|
this.Given(/^the profile "([^"]*)"$/, (profile, callback) => {
|
2017-09-18 10:18:07 -04:00
|
|
|
this.profile = this.OSRM_PROFILE || profile;
|
2016-09-19 17:13:44 -04:00
|
|
|
this.profileFile = path.join(this.PROFILES_PATH, this.profile + '.lua');
|
|
|
|
callback();
|
2016-03-04 15:11:05 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
this.Given(/^the extract extra arguments "(.*?)"$/, (args, callback) => {
|
2016-09-19 17:13:44 -04:00
|
|
|
this.extractArgs = this.expandOptions(args);
|
|
|
|
callback();
|
2016-03-04 15:11:05 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
this.Given(/^the contract extra arguments "(.*?)"$/, (args, callback) => {
|
2016-09-19 17:13:44 -04:00
|
|
|
this.contractArgs = this.expandOptions(args);
|
|
|
|
callback();
|
2016-03-04 15:11:05 -05:00
|
|
|
});
|
|
|
|
|
2017-03-11 03:26:12 -05:00
|
|
|
this.Given(/^the partition extra arguments "(.*?)"$/, (args, callback) => {
|
|
|
|
this.partitionArgs = this.expandOptions(args);
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.Given(/^the customize extra arguments "(.*?)"$/, (args, callback) => {
|
|
|
|
this.customizeArgs = this.expandOptions(args);
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
|
2016-06-22 16:25:22 -04:00
|
|
|
this.Given(/^a grid size of ([0-9.]+) meters$/, (meters, callback) => {
|
2016-03-04 15:11:05 -05:00
|
|
|
this.setGridSize(meters);
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
|
2017-05-11 06:13:52 -04:00
|
|
|
this.Given(/^the origin ([-+]?[0-9]*\.?[0-9]+),([-+]?[0-9]*\.?[0-9]+)$/, (lon, lat, callback) => {
|
2016-03-04 15:11:05 -05:00
|
|
|
this.setOrigin([parseFloat(lon), parseFloat(lat)]);
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.Given(/^the shortcuts$/, (table, callback) => {
|
2016-09-26 16:25:54 -04:00
|
|
|
let q = d3.queue();
|
2016-03-04 15:11:05 -05:00
|
|
|
|
2016-09-26 16:25:54 -04:00
|
|
|
let addShortcut = (row, cb) => {
|
2016-03-04 15:11:05 -05:00
|
|
|
this.shortcutsHash[row.key] = row.value;
|
|
|
|
cb();
|
|
|
|
};
|
|
|
|
|
|
|
|
table.hashes().forEach((row) => {
|
|
|
|
q.defer(addShortcut, row);
|
|
|
|
});
|
|
|
|
|
|
|
|
q.awaitAll(callback);
|
|
|
|
});
|
|
|
|
|
2016-09-30 03:33:08 -04:00
|
|
|
this.Given(/^the node map$/, (docstring, callback) => {
|
|
|
|
var q = d3.queue();
|
|
|
|
|
|
|
|
var addNode = (name, ri, ci, cb) => {
|
|
|
|
var lonLat = this.tableCoordToLonLat(ci, ri);
|
|
|
|
if (name.match(/[a-z]/) ) {
|
|
|
|
if (this.nameNodeHash[name]) throw new Error(util.format('*** duplicate node %s', name));
|
|
|
|
this.addOSMNode(name, lonLat[0], lonLat[1], null);
|
|
|
|
} else if (name.match(/[0-9]/) ) {
|
2017-04-28 10:25:41 -04:00
|
|
|
if (this.locationHash[name]) throw new Error(util.format('*** duplicate node %s', name));
|
2016-09-30 03:33:08 -04:00
|
|
|
this.addLocation(name, lonLat[0], lonLat[1], null);
|
2016-03-04 15:11:05 -05:00
|
|
|
}
|
2016-09-30 03:33:08 -04:00
|
|
|
cb();
|
2016-03-04 15:11:05 -05:00
|
|
|
};
|
|
|
|
|
2016-09-30 03:33:08 -04:00
|
|
|
docstring.split(/\n/).forEach( (row,ri) => {
|
|
|
|
row.split('').forEach( (cell,ci) => {
|
|
|
|
if( cell.match(/[a-z0-9]/) ) {
|
|
|
|
q.defer(addNode, cell, ri, ci*0.5);
|
|
|
|
}
|
2016-03-04 15:11:05 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
q.awaitAll(callback);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.Given(/^the node locations$/, (table, callback) => {
|
2016-09-26 16:25:54 -04:00
|
|
|
let q = d3.queue();
|
2016-03-04 15:11:05 -05:00
|
|
|
|
2016-09-26 16:25:54 -04:00
|
|
|
let addNodeLocations = (row, cb) => {
|
|
|
|
let name = row.node;
|
2017-04-28 10:25:41 -04:00
|
|
|
if (this.findNodeByName(name)) throw new Error(util.format('*** duplicate node %s', name));
|
2016-03-04 15:11:05 -05:00
|
|
|
|
|
|
|
if (name.match(/[a-z]/)) {
|
2016-09-26 16:25:54 -04:00
|
|
|
let id = row.id && parseInt(row.id);
|
2016-03-04 15:11:05 -05:00
|
|
|
this.addOSMNode(name, row.lon, row.lat, id);
|
|
|
|
} else {
|
|
|
|
this.addLocation(name, row.lon, row.lat);
|
|
|
|
}
|
|
|
|
|
|
|
|
cb();
|
|
|
|
};
|
|
|
|
|
|
|
|
table.hashes().forEach((row) => q.defer(addNodeLocations, row));
|
|
|
|
|
|
|
|
q.awaitAll(callback);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.Given(/^the nodes$/, (table, callback) => {
|
2016-09-26 16:25:54 -04:00
|
|
|
let q = d3.queue();
|
2016-03-04 15:11:05 -05:00
|
|
|
|
2016-09-26 16:25:54 -04:00
|
|
|
let addNode = (row, cb) => {
|
|
|
|
let name = row.node,
|
2016-03-04 15:11:05 -05:00
|
|
|
node = this.findNodeByName(name);
|
|
|
|
delete row.node;
|
2016-11-02 10:43:52 -04:00
|
|
|
if (!node) throw new Error(util.format('*** unknown node %s', name));
|
2016-09-26 16:25:54 -04:00
|
|
|
for (let key in row) {
|
2016-09-30 03:33:08 -04:00
|
|
|
if (key=='id') {
|
|
|
|
node.setID( row[key] );
|
|
|
|
} else {
|
|
|
|
node.addTag(key, row[key]);
|
|
|
|
}
|
2016-03-04 15:11:05 -05:00
|
|
|
}
|
|
|
|
cb();
|
|
|
|
};
|
|
|
|
|
|
|
|
table.hashes().forEach((row) => q.defer(addNode, row));
|
|
|
|
|
|
|
|
q.awaitAll(callback);
|
|
|
|
});
|
|
|
|
|
2017-08-15 07:57:44 -04:00
|
|
|
this.Given(/^the ways( with locations)?$/, (add_locations, table, callback) => {
|
2016-03-04 15:11:05 -05:00
|
|
|
if (this.osm_str) throw new Error('*** Map data already defined - did you pass an input file in this scenario?');
|
|
|
|
|
2016-09-26 16:25:54 -04:00
|
|
|
let q = d3.queue();
|
2016-03-04 15:11:05 -05:00
|
|
|
|
2016-09-26 16:25:54 -04:00
|
|
|
let addWay = (row, cb) => {
|
2017-08-15 07:57:44 -04:00
|
|
|
let way = new OSM.Way(this.makeOSMId(), this.OSM_USER, this.OSM_TIMESTAMP, this.OSM_UID, !!add_locations);
|
2016-03-04 15:11:05 -05:00
|
|
|
|
2016-09-26 16:25:54 -04:00
|
|
|
let nodes = row.nodes;
|
2016-03-04 15:11:05 -05:00
|
|
|
if (this.nameWayHash.nodes) throw new Error(util.format('*** duplicate way %s', nodes));
|
|
|
|
|
2016-09-26 16:25:54 -04:00
|
|
|
for (let i=0; i<nodes.length; i++) {
|
|
|
|
let c = nodes[i];
|
2016-03-04 15:11:05 -05:00
|
|
|
if (!c.match(/[a-z]/)) throw new Error(util.format('*** ways can only use names a-z (%s)', c));
|
2016-09-26 16:25:54 -04:00
|
|
|
let node = this.findNodeByName(c);
|
2016-03-04 15:11:05 -05:00
|
|
|
if (!node) throw new Error(util.format('*** unknown node %s', c));
|
|
|
|
way.addNode(node);
|
|
|
|
}
|
|
|
|
|
2016-09-26 16:25:54 -04:00
|
|
|
let tags = {
|
2016-03-04 15:11:05 -05:00
|
|
|
highway: 'primary'
|
|
|
|
};
|
|
|
|
|
2016-09-26 16:25:54 -04:00
|
|
|
for (let key in row) {
|
2016-03-04 15:11:05 -05:00
|
|
|
tags[key] = row[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
delete tags.nodes;
|
|
|
|
|
|
|
|
if (row.highway === '(nil)') delete tags.highway;
|
|
|
|
|
|
|
|
if (row.name === undefined)
|
|
|
|
tags.name = nodes;
|
|
|
|
else if (row.name === '""' || row.name === "''") // eslint-disable-line quotes
|
|
|
|
tags.name = '';
|
|
|
|
else if (row.name === '' || row.name === '(nil)')
|
|
|
|
delete tags.name;
|
|
|
|
else
|
|
|
|
tags.name = row.name;
|
|
|
|
|
|
|
|
way.setTags(tags);
|
|
|
|
this.OSMDB.addWay(way);
|
|
|
|
this.nameWayHash[nodes] = way;
|
|
|
|
cb();
|
|
|
|
};
|
|
|
|
|
|
|
|
table.hashes().forEach((row) => q.defer(addWay, row));
|
|
|
|
|
|
|
|
q.awaitAll(callback);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.Given(/^the relations$/, (table, callback) => {
|
|
|
|
if (this.osm_str) throw new Error('*** Map data already defined - did you pass an input file in this scenario?');
|
|
|
|
|
2016-09-26 16:25:54 -04:00
|
|
|
let q = d3.queue();
|
2016-03-04 15:11:05 -05:00
|
|
|
|
2016-09-26 16:25:54 -04:00
|
|
|
let addRelation = (row, cb) => {
|
|
|
|
let relation = new OSM.Relation(this.makeOSMId(), this.OSM_USER, this.OSM_TIMESTAMP, this.OSM_UID);
|
2016-03-04 15:11:05 -05:00
|
|
|
|
2017-10-04 02:47:17 -04:00
|
|
|
|
|
|
|
var name = null;
|
2016-09-26 16:25:54 -04:00
|
|
|
for (let key in row) {
|
2017-10-04 02:47:17 -04:00
|
|
|
let isNode = key.match(/^node:?(.*)/),
|
|
|
|
isWay = key.match(/^way:?(.*)/),
|
|
|
|
isRelation = key.match(/^relation:?(.*)/),
|
2016-03-04 15:11:05 -05:00
|
|
|
isColonSeparated = key.match(/^(.*):(.*)/);
|
|
|
|
if (isNode) {
|
|
|
|
row[key].split(',').map(function(v) { return v.trim(); }).forEach((nodeName) => {
|
2017-04-28 10:25:41 -04:00
|
|
|
if (nodeName.length !== 1) throw new Error(util.format('*** invalid relation node member "%s"', nodeName));
|
2016-09-26 16:25:54 -04:00
|
|
|
let node = this.findNodeByName(nodeName);
|
2017-04-28 10:25:41 -04:00
|
|
|
if (!node) throw new Error(util.format('*** unknown relation node member "%s"', nodeName));
|
2016-03-04 15:11:05 -05:00
|
|
|
relation.addMember('node', node.id, isNode[1]);
|
|
|
|
});
|
|
|
|
} else if (isWay) {
|
|
|
|
row[key].split(',').map(function(v) { return v.trim(); }).forEach((wayName) => {
|
2016-09-26 16:25:54 -04:00
|
|
|
let way = this.findWayByName(wayName);
|
2017-04-28 10:25:41 -04:00
|
|
|
if (!way) throw new Error(util.format('*** unknown relation way member "%s"', wayName));
|
2016-03-04 15:11:05 -05:00
|
|
|
relation.addMember('way', way.id, isWay[1]);
|
|
|
|
});
|
2017-10-04 02:47:17 -04:00
|
|
|
} else if (isRelation) {
|
|
|
|
row[key].split(',').map(function(v) { return v.trim(); }).forEach((relName) => {
|
|
|
|
let otherrelation = this.findRelationByName(relName);
|
|
|
|
if (!otherrelation) throw new Error(util.format('*** unknown relation relation member "%s"', relName));
|
|
|
|
relation.addMember('relation', otherrelation.id, isRelation[1]);
|
|
|
|
});
|
2016-03-04 15:11:05 -05:00
|
|
|
} else if (isColonSeparated && isColonSeparated[1] !== 'restriction') {
|
2017-04-28 10:25:41 -04:00
|
|
|
throw new Error(util.format('*** unknown relation member type "%s:%s", must be either "node" or "way"', isColonSeparated[1], isColonSeparated[2]));
|
2016-03-04 15:11:05 -05:00
|
|
|
} else {
|
|
|
|
relation.addTag(key, row[key]);
|
2017-10-04 02:47:17 -04:00
|
|
|
if (key.match(/name/)) name = row[key];
|
2016-03-04 15:11:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
relation.uid = this.OSM_UID;
|
|
|
|
|
2017-10-04 02:47:17 -04:00
|
|
|
|
|
|
|
if (name) {
|
|
|
|
this.nameRelationHash[name] = relation;
|
|
|
|
}
|
|
|
|
|
2016-03-04 15:11:05 -05:00
|
|
|
this.OSMDB.addRelation(relation);
|
|
|
|
|
|
|
|
cb();
|
|
|
|
};
|
|
|
|
|
|
|
|
table.hashes().forEach((row) => q.defer(addRelation, row));
|
|
|
|
|
|
|
|
q.awaitAll(callback);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.Given(/^the input file ([^"]*)$/, (file, callback) => {
|
|
|
|
if (path.extname(file) !== '.osm') throw new Error('*** Input file must be in .osm format');
|
|
|
|
fs.readFile(file, 'utf8', (err, data) => {
|
|
|
|
if (!err) this.osm_str = data.toString();
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.Given(/^the raster source$/, (data, callback) => {
|
2016-09-19 17:13:44 -04:00
|
|
|
// TODO: Don't overwrite if it exists
|
|
|
|
fs.writeFile(this.rasterCacheFile, data, callback);
|
|
|
|
// we need this to pass it to the profiles
|
|
|
|
this.environment = Object.assign({OSRM_RASTER_SOURCE: this.rasterCacheFile}, this.environment);
|
2016-03-04 15:11:05 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
this.Given(/^the speed file$/, (data, callback) => {
|
2016-09-19 17:13:44 -04:00
|
|
|
// TODO: Don't overwrite if it exists
|
|
|
|
fs.writeFile(this.speedsCacheFile, data, callback);
|
2016-03-04 15:11:05 -05:00
|
|
|
});
|
|
|
|
|
2016-04-29 03:48:13 -04:00
|
|
|
this.Given(/^the turn penalty file$/, (data, callback) => {
|
2016-09-19 17:13:44 -04:00
|
|
|
// TODO: Don't overwrite if it exists
|
|
|
|
fs.writeFile(this.penaltiesCacheFile, data, callback);
|
2016-04-29 03:48:13 -04:00
|
|
|
});
|
|
|
|
|
2017-05-18 08:27:28 -04:00
|
|
|
this.Given(/^the profile file(?: "([^"]*)" initialized with)?$/, (profile, data, callback) => {
|
2016-12-26 13:00:08 -05:00
|
|
|
const lua_profiles_path = this.PROFILES_PATH.split(path.sep).join('/');
|
|
|
|
let text = 'package.path = "' + lua_profiles_path + '/?.lua;" .. package.path\n';
|
2016-09-26 16:25:54 -04:00
|
|
|
if (profile == null) {
|
|
|
|
text += data + '\n';
|
|
|
|
} else {
|
2017-05-18 08:27:28 -04:00
|
|
|
text += 'local functions = require("' + profile + '")\n';
|
|
|
|
text += 'functions.setup_parent = functions.setup\n';
|
|
|
|
text += 'functions.setup = function()\n';
|
|
|
|
text += 'local profile = functions.setup_parent()\n';
|
|
|
|
text += data + '\n';
|
|
|
|
text += 'return profile\n';
|
|
|
|
text += 'end\n';
|
|
|
|
text += 'return functions\n';
|
2016-09-26 16:25:54 -04:00
|
|
|
}
|
|
|
|
this.profileFile = this.profileCacheFile;
|
|
|
|
// TODO: Don't overwrite if it exists
|
|
|
|
fs.writeFile(this.profileCacheFile, text, callback);
|
|
|
|
});
|
|
|
|
|
2016-03-04 15:11:05 -05:00
|
|
|
this.Given(/^the data has been saved to disk$/, (callback) => {
|
2016-09-29 18:11:04 -04:00
|
|
|
this.writeAndLinkOSM(callback);
|
2016-03-04 15:11:05 -05:00
|
|
|
});
|
|
|
|
|
2017-03-14 09:45:44 -04:00
|
|
|
this.Given(/^the data has been (extract|contract|partition|customiz)ed$/, (step, callback) => {
|
2016-09-19 17:13:44 -04:00
|
|
|
this.reprocess(callback);
|
2016-03-04 15:11:05 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
this.Given(/^osrm\-routed is stopped$/, (callback) => {
|
2016-09-19 17:13:44 -04:00
|
|
|
this.OSRMLoader.shutdown(callback);
|
2016-03-04 15:11:05 -05:00
|
|
|
});
|
|
|
|
|
2016-09-19 17:13:44 -04:00
|
|
|
this.Given(/^data is loaded directly/, (callback) => {
|
|
|
|
this.osrmLoader.setLoadMethod('directly');
|
|
|
|
callback();
|
2016-03-04 15:11:05 -05:00
|
|
|
});
|
|
|
|
|
2016-09-19 17:13:44 -04:00
|
|
|
this.Given(/^data is loaded with datastore$/, (callback) => {
|
|
|
|
this.osrmLoader.setLoadMethod('datastore');
|
|
|
|
callback();
|
2016-03-04 15:11:05 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
this.Given(/^the HTTP method "([^"]*)"$/, (method, callback) => {
|
|
|
|
this.httpMethod = method;
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
};
|