Skip to content

Instantly share code, notes, and snippets.

@billautomata
Last active August 29, 2015 14:17
Show Gist options
  • Save billautomata/4fbd58569b248ed247f6 to your computer and use it in GitHub Desktop.
Save billautomata/4fbd58569b248ed247f6 to your computer and use it in GitHub Desktop.
reduces the precision of floats in json files to save space
// 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.
@jamestrimble
Copy link

This is useful - thanks! On line 25, you could add a + to convert the the fixed-precision numbers from strings: o[key] = +o[key].toFixed(precision)
https://gist.github.com/jamestrimble/c34e55fbf087acaf75f6

@billautomata
Copy link
Author

@jamestrimble - done! thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment