Skip to content

Commit b6f5fea

Browse files
authored
Merge pull request #495 from HSLdevcom/modify-hsl-preprocessing
AB#235 - Add documentation for OSM preprocessing and change instructions file to bash script
2 parents 5dcc723 + 939123a commit b6f5fea

5 files changed

Lines changed: 46 additions & 41 deletions

File tree

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ It is possible to change the behaviour of the data builder by defining environme
6363
- `USE_PREBUILT_STREET_GRAPH` to use the prebuilt street graph to finish a complete graph build
6464
- All other values default to `NO_SPLIT_BUILD` which indicates that the build is run as normal
6565
- (Optional) `USE_SEEDED_OSM` skips OSM updating and uses existing seed version
66+
- (Optional) `SKIP_OSM_PREPROCESSING` skips OSM preprocessing even if an instruction file is defined
67+
- (Optional) `SKIP_OTP_TESTS` skips OTP tests
6668

6769
### Data processing steps
6870

@@ -175,3 +177,16 @@ It is possible to change the behaviour of the data builder by defining environme
175177
Contains tools, such as the OneBusAway gtfs filter, for gtfs manipulation.
176178
It uses the [opentransitsoftwarefoundation/onebusaway-gtfs-transformer-cli](https://registry.hub.docker.com/r/opentransitsoftwarefoundation/onebusaway-gtfs-transformer-cli) as the base image.
177179
These tools are packaged inside a docker container and are used during the data build process.
180+
181+
#### OSM preprocessing
182+
183+
OSM preprocessing is done if a bash script is defined for a specific config and a specific OSM file.
184+
See [hsl.sh](hsl/osm-preprocessing/hsl.sh) for an example.
185+
186+
When creating OSM preprocessing instructions you should:
187+
1. Name the bash file as follows: `<osm_id_of_osm_file>.sh`. Valid file names can be e.g. `hsl.sh` or `southFinland.sh`.
188+
2. Place the file in the `osm-preprocessing` directory of the config you want to use.
189+
3. Make sure that the name of the output file is the same as the input file e.g. `hsl.pbf`.
190+
4. Make sure that you do not reuse input and output filenames in commands.
191+
For example, do not use: `osmfilter hsl.o5m -o=hsl.o5m ...` you can instead use `osmfilter hsl.o5m -o=hsl2.o5m ...`.
192+
5. Test the script by running it locally and verifying that the output makes sense.

gulpfile.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,26 @@ gulp.task('osm:download', async cb => {
6262
cb();
6363
});
6464

65+
gulp.task('osm:copyPreprocessingFiles', () =>
66+
gulp
67+
.src(`${config.router.id}/osm-preprocessing/*.sh`, noBuf)
68+
.pipe(gulp.dest(`${config.dataDir}/${config.router.id}/osm-preprocessing`)),
69+
);
70+
6571
gulp.task(
6672
'osm:update',
6773
gulp.series(
74+
'osm:copyPreprocessingFiles',
6875
'osm:download',
6976
() =>
7077
gulp
7178
.src(`${osmDlDir}/*`, noBuf)
7279
.pipe(validateBlobSize())
73-
.pipe(runOSMPreprocessing(`${config.router.id}/osm-preprocessing`))
80+
.pipe(
81+
runOSMPreprocessing(
82+
`${config.dataDir}/${config.router.id}/osm-preprocessing`,
83+
),
84+
)
7485
.pipe(testOTPFile())
7586
.pipe(gulp.dest(osmDir)),
7687
() => del(tmpDir),

hsl/osm-preprocessing/hsl.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
set -x
4+
5+
osmconvert hsl.pbf -o=hsl.o5m
6+
osmfilter hsl.o5m -o=hsl2.o5m --modify-tags="ref=H0071 to ref=no \
7+
ref=H0091 to ref=no \
8+
ref=H0072 to ref=no \
9+
ref=H0079 to ref=no \
10+
ref=H0082 to ref=no \
11+
ref=H0083 to ref=no \
12+
ref=H0084 to ref=no"
13+
osmconvert hsl2.o5m -o=hsl.pbf

hsl/osm-preprocessing/hsl.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

task/OSMPreprocessing.js

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,12 @@
11
const fs = require('fs');
2-
const { once } = require('events');
3-
const readline = require('readline');
42
const fse = require('fs-extra');
53
const exec = require('child_process').exec;
64
const through = require('through2');
75
const { dataDir, constants, dataToolImage } = require('../config');
86
const { postSlackMessage, createDir } = require('../util');
97

10-
async function readPreprocessingInstructions(preprocessingInstructionsFile) {
11-
const preprocessingInstructions = [];
12-
const errorLines = [];
13-
const rl = readline.createInterface({
14-
input: fs.createReadStream(preprocessingInstructionsFile),
15-
});
16-
rl.on('line', line => {
17-
if (/^(osmconvert|osmfilter|osmupdate).*$/.test(line)) {
18-
preprocessingInstructions.push(line);
19-
} else if (line === '') {
20-
process.stdout.write(
21-
'Skipping empty line in ' + preprocessingInstructionsFile + '\n',
22-
);
23-
} else {
24-
errorLines.push(line);
25-
}
26-
});
27-
await once(rl, 'close');
28-
if (errorLines.length > 0) {
29-
throw Error(
30-
`${errorLines.join(', ')} are not valid preprocessing instructions!\n`,
31-
);
32-
}
33-
return `-c '${preprocessingInstructions.join(' && ')}'`;
34-
}
35-
368
/**
37-
* Runs the instructions listed in the OSM preprocessing file.
38-
* Only the osmfilter, osmconvert, and osmupdate commands can be used.
9+
* Runs the instructions listed in the OSM preprocessing bash script.
3910
*/
4011
function preprocessWithFile(
4112
osmFile,
@@ -47,7 +18,7 @@ function preprocessWithFile(
4718
const lastLog = [];
4819

4920
return new Promise((resolve, reject) => {
50-
const preprocessingInstructionsFile = `${osmPreprocessingDir}/${osmId}.txt`;
21+
const preprocessingInstructionsFile = `${osmPreprocessingDir}/${osmId}.sh`;
5122

5223
if (!fs.existsSync(osmFile)) {
5324
reject(new Error(`${osmFile} does not exist!\n`));
@@ -73,15 +44,13 @@ function preprocessWithFile(
7344
const r = fs.createReadStream(osmFile);
7445
r.on('end', async () => {
7546
try {
76-
const concatenatedInstructions =
77-
await readPreprocessingInstructions(
78-
preprocessingInstructionsFile,
79-
);
8047
process.stdout.write(
81-
'Running command: ' + concatenatedInstructions + '\n',
48+
'Running commands from file: ' +
49+
preprocessingInstructionsFile +
50+
'\n',
8251
);
8352
const preprocessingCommand = exec(
84-
`docker run -v ${folder}:/tmp/osm-preprocessing:rw -w /tmp/osm-preprocessing --rm --entrypoint /bin/bash ${dataToolImage} ${concatenatedInstructions}`,
53+
`docker run -v ${folder}:/tmp/osm-preprocessing:rw -v ${preprocessingInstructionsFile}:/tmp/preprocessing.sh:ro -w /tmp/osm-preprocessing --rm --entrypoint /bin/bash ${dataToolImage} /tmp/preprocessing.sh`,
8554
{ maxBuffer: constants.BUFFER_SIZE },
8655
);
8756
preprocessingCommand.on('exit', function (c) {

0 commit comments

Comments
 (0)