-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #157 from pelias/joxit/wof_extract_sqlite
Extract data via WOF SQLite database
- Loading branch information
Showing
6 changed files
with
73 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const whosonfirst = require('pelias-whosonfirst'); | ||
const config = require('pelias-config').generate().imports.whosonfirst; | ||
const SQLiteStream = whosonfirst.SQLiteStream; | ||
const through = require('through2'); | ||
const Placeholder = require('../Placeholder'); | ||
const combinedStream = require('combined-stream'); | ||
|
||
const SQLITE_REGEX = /whosonfirst-data-[a-z0-9-]+\.db$/; | ||
|
||
const WOF_DIR = process.env.WOF_DIR || '/data/whosonfirst-data/sqlite'; | ||
|
||
const layers = fs.readFileSync(path.join(__dirname, 'placetype.filter'), 'utf-8') | ||
.replace(/^.*\(/, '') // Removes all characters before the first parenthesis | ||
.match(/[a-z]+/g); // Get the layer list | ||
|
||
const jq_filter = fs.readFileSync(path.join(__dirname, 'jq.filter'), 'utf-8') | ||
.match(/test\("(.*)"\)/g) // Get all tests | ||
.map(s => s.replace(/^[^"]+"/, '').replace(/"[^"]+$/, '')) // Get only regex part | ||
.map(s => new RegExp(s)); // Transform it into JS RegExp | ||
|
||
const output = () => { | ||
if (process.argv.length > 2 && process.argv[2] === 'build') { | ||
const ph = new Placeholder(); | ||
ph.load({ reset: true }); | ||
return through.obj((row, _, next) => { | ||
ph.insertWofRecord(row, next); | ||
}, done => { | ||
console.error('populate fts...'); | ||
ph.populate(); | ||
console.error('optimize...'); | ||
ph.optimize(); | ||
console.error('close...'); | ||
ph.close(); | ||
done(); | ||
}); | ||
} else { | ||
return through.obj((row, _, next) => { | ||
console.log(JSON.stringify(row)); | ||
next(); | ||
}); | ||
} | ||
}; | ||
|
||
const sqliteStream = combinedStream.create(); | ||
fs.readdirSync(WOF_DIR) | ||
.filter(file => SQLITE_REGEX.test(file)) | ||
.map(file => path.join(WOF_DIR, file)) | ||
.forEach(dbPath => { | ||
sqliteStream.append(next => { | ||
next(new SQLiteStream( | ||
dbPath, | ||
config.importPlace ? | ||
SQLiteStream.findGeoJSONByPlacetypeAndWOFId(layers, config.importPlace) : | ||
SQLiteStream.findGeoJSONByPlacetype(layers) | ||
)); | ||
}); | ||
}); | ||
|
||
sqliteStream | ||
.pipe(whosonfirst.toJSONStream()) | ||
.pipe(through.obj((row, _, next) => { | ||
Object.keys(row.properties) | ||
.filter(key => !jq_filter.some(regex => regex.test(key))) | ||
.forEach(key => delete row.properties[key]); | ||
next(null, row.properties); | ||
})) | ||
.pipe(output()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters