Skip to content

Instantly share code, notes, and snippets.

@aaronlidman
Created October 2, 2014 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronlidman/d5836fde59f110b23cd1 to your computer and use it in GitHub Desktop.
Save aaronlidman/d5836fde59f110b23cd1 to your computer and use it in GitHub Desktop.
// just store the coordinates of every node
var osmium = require('osmium'),
levelup = require('levelup'),
args = require('minimist')(process.argv.slice(2));
var db;
var reader;
var batch = [];
var batchSize = args.batch || 50000;
if (process.argv[2] === undefined) {
return console.log('specify a file: `node planetNodes.js [planet pbf or bz2]`');
}
createDB(args._[0], function() {
readFile(args._[0]);
});
function createDB(name, cb) {
db = levelup('./planet-nodes.ldb', cb);
}
function readFile(file) {
reader = new osmium.Reader(file, {
'node': true
});
readOSM();
}
function readOSM() {
var buffer = reader.read();
if (!buffer) return pushBatch();
while (object = buffer.next()) {
batch.push({
type: "put",
key: 'n' + object.id,
value: JSON.stringify(object.lat + ',' + object.lon)
});
}
if (batch.length > batchSize) {
pushBatch(function() {
readOSM();
});
} else {
readOSM();
}
}
function pushBatch(callback) {
db.batch(batch, function(err) {
if (err) return console.log(err);
batch = [];
return callback ? callback() : false;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment