worldpop
Version:
Compute the population within a polygon from GeoJSON population data
48 lines (39 loc) • 1.2 kB
JavaScript
var fs = require('fs')
var path = require('path')
var geojson = require('geojson-stream')
var getTotal = require('../')
if (process.argv.length < 5) {
console.log('Usage: ' + process.argv[0] + ' ' + process.argv[1],
'(tileliveUri|geojson_file|mbtiles_file)',
'density_property_name ',
'test-polygon.json',
'[multiplier=10000]',
'[min_zoom=8]',
'[max_zoom=12]')
process.exit()
}
var tilesUri = process.argv[2]
var densityProp = process.argv[3]
var testPoly = JSON.parse(fs.readFileSync(process.argv[4]))
var multiplier = +(process.argv[5] || 10000)
if (testPoly.features) testPoly = testPoly.features[0]
function density (feature) {
return feature.properties[densityProp] / multiplier
}
// set up source
if (/json$/.test(tilesUri)) {
tilesUri = fs.createReadStream(tilesUri).pipe(geojson.parse())
} else if (!/^[^\/]*\:\/\//.test(tilesUri)) {
tilesUri = 'mbtiles://' + path.resolve(tilesUri)
}
console.log('Source:', tilesUri)
getTotal({
source: tilesUri,
density: density,
polygon: testPoly,
min_zoom: +(process.argv[6] || 8),
max_zoom: +(process.argv[7] || 12)
}, function (err, total) {
if (err) console.error(err)
console.log(total)
})