Skip to content

Instantly share code, notes, and snippets.

@jamestrimble
Forked from billautomata/reduce.js
Last active August 29, 2015 14:17
Show Gist options
  • Save jamestrimble/c34e55fbf087acaf75f6 to your computer and use it in GitHub Desktop.
Save jamestrimble/c34e55fbf087acaf75f6 to your computer and use it in GitHub Desktop.
// usage
// node reduce.js worldmap.geojson 3
var fs = require('fs')
var json_string = fs.readFileSync(process.argv[2])
var precision = parseInt(process.argv[3])
console.log('original length: ' + json_string.length)
var obj = JSON.parse(json_string)
var n_found = 0
iterate(obj)
function iterate(o) {
for (var key in o) {
if (typeof o[key] === 'object') {
iterate(o[key])
} else {
if (typeof o[key] === 'number') {
n_found += 1
o[key] = +o[key].toFixed(precision)
}
}
}
}
console.log('numbers found: ', n_found)
console.log('reduced length: ' + JSON.stringify(obj, null, 0).length)
var pct = 1.0 - (JSON.stringify(obj, null, 0).length / json_string.length)
fs.writeFileSync(process.argv[2] + '.reduced.json', JSON.stringify(obj, null, 0))
console.log((pct * 100.0).toFixed(2) + '% reduction')
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment