Skip to content

Instantly share code, notes, and snippets.

@aaronlidman
Created October 10, 2014 22:15
Show Gist options
  • Save aaronlidman/7a8fba630c63e9150d04 to your computer and use it in GitHub Desktop.
Save aaronlidman/7a8fba630c63e9150d04 to your computer and use it in GitHub Desktop.
// cache osm nodes and ways in leveldb
var osmium = require('osmium');
levelup = require('levelup');
if (process.argv[2] === undefined) {
return console.log('specify a file: `node osmium.js [file]');
}
var db = levelup('./' + (process.argv[3] || 'test.ldb'));
var nodes = levelup('./nodes.ldb');
var ways = [];
var reader = new osmium.Reader(process.argv[2], {
'node': true,
'way': true
});
function nextBuffer() {
var buffer = reader.read();
if (!buffer) return pushBatch(batch);
while (object = buffer.next()) {
console.log(object);
var type = ('nodes_count' in object) ? 'w' : 'n';
var details;
var tags = object.tags();
details.version = object.version;
details.user = object.user;
details.changeset = object.changeset;
if (tags.length) details.tags = tags;
if (type === 'n') {
details.lat = object.lat;
details.lon = object.lon;
nodes.push({
type: 'put',
key: 'n' + object.id,
value: JSON.stringify(details)
});
}
if (type === 'w') {
ways.push({
type: 'put',
key: type + object.id,
value: JSON.stringify(details)
});
}
}
pushBatch(batch, nextBuffer);
}
function pushBatch(array, callback) {
db.batch(array, function(err) {
if (err) return console.log(err);
batch = [];
return callback ? callback() : false;
});
}
nextBuffer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment