Created
October 2, 2014 15:30
-
-
Save aaronlidman/934483ec16a2c47ef0a5 to your computer and use it in GitHub Desktop.
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
// 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(process.argv[2], function() { | |
readFile(process.argv[2]); | |
}); | |
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