/
data.js
288 lines (231 loc) · 9.99 KB
/
data.js
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
'use strict';
var util = require('util');
var path = require('path');
var fs = require('fs');
var d3 = require('d3-queue');
var OSM = require('../lib/osm');
module.exports = function () {
this.Given(/^the profile "([^"]*)"$/, (profile, callback) => {
this.profile = profile;
this.profileFile = path.join(this.PROFILES_PATH, this.profile + '.lua');
callback();
});
this.Given(/^the extract extra arguments "(.*?)"$/, (args, callback) => {
this.extractArgs = this.expandOptions(args);
callback();
});
this.Given(/^the contract extra arguments "(.*?)"$/, (args, callback) => {
this.contractArgs = this.expandOptions(args);
callback();
});
this.Given(/^a grid size of ([0-9.]+) meters$/, (meters, callback) => {
this.setGridSize(meters);
callback();
});
this.Given(/^the origin ([-+]?[0-9]*\.?[0-9]+),([-+]?[0-9]*\.?[0-9]+)$/, (lat, lon, callback) => {
this.setOrigin([parseFloat(lon), parseFloat(lat)]);
callback();
});
this.Given(/^the shortcuts$/, (table, callback) => {
let q = d3.queue();
let addShortcut = (row, cb) => {
this.shortcutsHash[row.key] = row.value;
cb();
};
table.hashes().forEach((row) => {
q.defer(addShortcut, row);
});
q.awaitAll(callback);
});
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]/) ) {
if (this.locationHash[name]) throw new Error(util.format('*** duplicate node %s'), name);
this.addLocation(name, lonLat[0], lonLat[1], null);
}
cb();
};
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);
}
});
});
q.awaitAll(callback);
});
this.Given(/^the node locations$/, (table, callback) => {
let q = d3.queue();
let addNodeLocations = (row, cb) => {
let name = row.node;
if (this.findNodeByName(name)) throw new Error(util.format('*** duplicate node %s'), name);
if (name.match(/[a-z]/)) {
let id = row.id && parseInt(row.id);
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) => {
let q = d3.queue();
let addNode = (row, cb) => {
let name = row.node,
node = this.findNodeByName(name);
delete row.node;
if (!node) throw new Error(util.format('*** unknown node %s', name));
for (let key in row) {
if (key=='id') {
node.setID( row[key] );
} else {
node.addTag(key, row[key]);
}
}
cb();
};
table.hashes().forEach((row) => q.defer(addNode, row));
q.awaitAll(callback);
});
this.Given(/^the ways$/, (table, callback) => {
if (this.osm_str) throw new Error('*** Map data already defined - did you pass an input file in this scenario?');
let q = d3.queue();
let addWay = (row, cb) => {
let way = new OSM.Way(this.makeOSMId(), this.OSM_USER, this.OSM_TIMESTAMP, this.OSM_UID);
let nodes = row.nodes;
if (this.nameWayHash.nodes) throw new Error(util.format('*** duplicate way %s', nodes));
for (let i=0; i<nodes.length; i++) {
let c = nodes[i];
if (!c.match(/[a-z]/)) throw new Error(util.format('*** ways can only use names a-z (%s)', c));
let node = this.findNodeByName(c);
if (!node) throw new Error(util.format('*** unknown node %s', c));
way.addNode(node);
}
let tags = {
highway: 'primary'
};
for (let key in row) {
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?');
let q = d3.queue();
let addRelation = (row, cb) => {
let relation = new OSM.Relation(this.makeOSMId(), this.OSM_USER, this.OSM_TIMESTAMP, this.OSM_UID);
for (let key in row) {
let isNode = key.match(/^node:(.*)/),
isWay = key.match(/^way:(.*)/),
isColonSeparated = key.match(/^(.*):(.*)/);
if (isNode) {
row[key].split(',').map(function(v) { return v.trim(); }).forEach((nodeName) => {
if (nodeName.length !== 1) throw new Error(util.format('*** invalid relation node member "%s"'), nodeName);
let node = this.findNodeByName(nodeName);
if (!node) throw new Error(util.format('*** unknown relation node member "%s"'), nodeName);
relation.addMember('node', node.id, isNode[1]);
});
} else if (isWay) {
row[key].split(',').map(function(v) { return v.trim(); }).forEach((wayName) => {
let way = this.findWayByName(wayName);
if (!way) throw new Error(util.format('*** unknown relation way member "%s"'), wayName);
relation.addMember('way', way.id, isWay[1]);
});
} else if (isColonSeparated && isColonSeparated[1] !== 'restriction') {
throw new Error(util.format('*** unknown relation member type "%s:%s", must be either "node" or "way"'), isColonSeparated[1], isColonSeparated[2]);
} else {
relation.addTag(key, row[key]);
}
}
relation.uid = this.OSM_UID;
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) => {
// 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);
});
this.Given(/^the speed file$/, (data, callback) => {
// TODO: Don't overwrite if it exists
fs.writeFile(this.speedsCacheFile, data, callback);
});
this.Given(/^the turn penalty file$/, (data, callback) => {
// TODO: Don't overwrite if it exists
fs.writeFile(this.penaltiesCacheFile, data, callback);
});
this.Given(/^the profile file(?: "([^"]*)" extended with)?$/, (profile, data, callback) => {
const lua_profiles_path = this.PROFILES_PATH.split(path.sep).join('/');
let text = 'package.path = "' + lua_profiles_path + '/?.lua;" .. package.path\n';
if (profile == null) {
text += data + '\n';
} else {
text += 'local f = assert(io.open("' + lua_profiles_path + '/' + profile + '.lua", "r"))\n';
text += 'local s = f:read("*all") .. [[\n' + data + '\n]]\n';
text += 'f:close()\n';
text += 'local m = assert(loadstring and loadstring(s) or load(s))\n';
text += 'm()\n';
}
this.profileFile = this.profileCacheFile;
// TODO: Don't overwrite if it exists
fs.writeFile(this.profileCacheFile, text, callback);
});
this.Given(/^the data has been saved to disk$/, (callback) => {
this.writeAndLinkOSM(callback);
});
this.Given(/^the data has been extracted$/, (callback) => {
this.reprocess(callback);
});
this.Given(/^the data has been contracted$/, (callback) => {
this.reprocess(callback);
});
this.Given(/^osrm\-routed is stopped$/, (callback) => {
this.OSRMLoader.shutdown(callback);
});
this.Given(/^data is loaded directly/, (callback) => {
this.osrmLoader.setLoadMethod('directly');
callback();
});
this.Given(/^data is loaded with datastore$/, (callback) => {
this.osrmLoader.setLoadMethod('datastore');
callback();
});
this.Given(/^the HTTP method "([^"]*)"$/, (method, callback) => {
this.httpMethod = method;
callback();
});
};