Last active
August 29, 2015 14:02
-
-
Save missinglink/47952fa0dc6e83448827 to your computer and use it in GitHub Desktop.
validate pdf documents
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
// INSTALL DEPENDENCIES | |
// $> npm install osm-read | |
// USAGE | |
// $> node validate.js auckland.osm.pbf | |
var osmread = require('osm-read'); | |
var nodeIds = {}, | |
orphanedRefIds = {}; | |
// check filename | |
if( process.argv.length < 3 ){ | |
console.error( 'invalid filename' ); | |
process.exit(1); | |
} | |
// parse pbf document | |
osmread.parse({ | |
filePath: process.argv[2], | |
endDocument: function(){ | |
var missingNodeIds = Object.keys( orphanedRefIds ); | |
if( missingNodeIds.length ){ | |
console.log( '\ntotal missing node ids: %d', missingNodeIds.length ); | |
process.exit(1); | |
} else { | |
console.log( '\nyay, no missing node ids!!' ); | |
process.exit(0); | |
} | |
}, | |
bounds: function( bounds ){ | |
incStat( 'bounds' ); | |
}, | |
node: function( node ){ | |
incStat( 'node' ); | |
nodeIds[ node.id ] = true; | |
}, | |
way: function( way ){ | |
incStat( 'way' ); | |
way.nodeRefs.forEach( function( nodeRefId ){ | |
if( !nodeIds.hasOwnProperty( nodeRefId ) ){ // referenced node not found in this document | |
orphanedRefIds[ nodeRefId ] = true; | |
// console.log( 'missing referenced node id: %s', nodeRefId ); | |
} | |
}); | |
}, | |
error: function( msg ){ | |
incStat( 'error' ); | |
console.error( 'error: ' + msg ); | |
} | |
}); | |
// live import stats | |
var stats = {}; | |
function incStat( type ){ | |
if( !stats.hasOwnProperty( type ) ){ stats[type] = 0; } | |
stats[type]++; | |
} | |
function writeStats(){ | |
process.stderr.clearLine(); // clear current text | |
process.stderr.cursorTo(0); | |
process.stderr.write( JSON.stringify( stats ) ); | |
} | |
setInterval( writeStats, 100 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment