Last active
August 29, 2015 14:17
-
-
Save billautomata/4fbd58569b248ed247f6 to your computer and use it in GitHub Desktop.
reduces the precision of floats in json files to save space
This file contains hidden or 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
// 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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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