Skip to content

Commit

Permalink
Merge pull request #289 from pelias/joxit/multi-sqlite
Browse files Browse the repository at this point in the history
Supports for multiple databases
  • Loading branch information
orangejulius authored Mar 10, 2020
2 parents e158106 + 36337fd commit e07f770
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/pip/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ module.exports.create = function createPIPService(datapath, layers, localizedAdm
layers = _.intersection(defaultLayers, _.isEmpty(layers) ? defaultLayers : layers);

if (isSqlite === true) {
const filename = path.join(datapath, 'sqlite', 'whosonfirst-data-latest.db');
if (!fs.existsSync(filename)) {
return callback(`unable to locate sqlite file ${filename}`);
const folder = path.join(datapath, 'sqlite');
if (!fs.existsSync(folder)) {
return callback(`unable to locate sqlite folder`);
}
} else {
// keep track of any missing metafiles for later reporting and error conditions
Expand Down
28 changes: 22 additions & 6 deletions src/pip/readStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ const simplifyGeometry = require('./components/simplifyGeometry');
const filterOutCitylessNeighbourhoods = require('./components/filterOutCitylessNeighbourhoods');
const filterOutHierarchylessNeighbourhoods = require('./components/filterOutHierarchylessNeighbourhoods');
const filterOutPointRecords = require('./components/filterOutPointRecords');
const combinedStream = require('combined-stream');
const fs = require('fs');
const SQLiteStream = whosonfirst.SQLiteStream;

const SQLITE_REGEX = /whosonfirst-data-[a-z0-9-]+\.db$/;

function readBundleRecords(datapath, layer) {
return whosonfirst.metadataStream(datapath).create(layer)
.pipe(whosonfirst.parseMetaFiles())
Expand All @@ -17,13 +21,25 @@ function readBundleRecords(datapath, layer) {
.pipe(whosonfirst.loadJSON(datapath, false));
}

function getSqliteFilePaths(root) {
return fs.readdirSync(root)
.filter(d => SQLITE_REGEX.test(d))
.map(db => path.join(root, db));
}

function readSqliteRecords(datapath, layer) {
return new SQLiteStream(
path.join(datapath, 'sqlite', 'whosonfirst-data-latest.db'),
config.importPlace ?
SQLiteStream.findGeoJSONByPlacetypeAndWOFId(layer, config.importPlace) :
SQLiteStream.findGeoJSONByPlacetype(layer)
).pipe(whosonfirst.toJSONStream());
const sqliteStream = combinedStream.create();
getSqliteFilePaths(path.join(datapath, 'sqlite')).forEach(dbPath => {
sqliteStream.append(next => {
next(new SQLiteStream(
dbPath,
config.importPlace ?
SQLiteStream.findGeoJSONByPlacetypeAndWOFId(layer, config.importPlace) :
SQLiteStream.findGeoJSONByPlacetype(layer)
));
});
});
return sqliteStream.pipe(whosonfirst.toJSONStream());
}

/**
Expand Down

0 comments on commit e07f770

Please sign in to comment.