Created
September 26, 2014 17:06
-
-
Save aaronlidman/fd755d1154e609f52437 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
var osmium = require('osmium'); | |
levelup = require('levelup'); | |
var db = levelup('./' + (process.argv[3] || 'test.ldb')); | |
var count = 0; | |
var batch = []; | |
var batchSize = 100000; | |
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()) { | |
var type = ('nodes_count' in object) ? 'w' : 'n'; | |
batch.push({ | |
type: 'put', | |
key: type + object.id, | |
value: JSON.stringify(object) | |
}); | |
} | |
if (batch.length > batchSize) { | |
pushBatch(batch, nextBuffer); | |
} else { | |
nextBuffer(); | |
} | |
count++; | |
} | |
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