Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const fs = require('fs')
const path = require('path')
const exec = require('child_process').exec
const glob = require('glob')
const blockBatches = 'filtered-census-blocks'
const out = 'filtered-merged'
const prefix = 'CAF_'
const format = '-f \'ESRI Shapefile\''
const stateToFips = JSON.parse(fs.readFileSync('state-fips.json'))
let processedStates = 0
let allStates = Object.keys(stateToFips)
let processedBatches = 0
let currentBatches = null
let currentShp = null
let checkProcessed = () => {
if ( processedStates < allStates.length ) {
// if ( processedStates < 2 ) {
processState()
} else {
console.log('\nFinished.')
}
}
let processState = () => {
let current = allStates[processedStates];
console.log(current)
// Look for a all .shp's for the current state.
glob(`${blockBatches}${path.sep}${prefix}${current}*.shp`, null, (err, matches) => {
// No matches means no shapefiles for a state/territory.
if ( matches.length === 0 ) {
processedStates += 1
checkProcessed()
return
}
currentShp = `${out}${path.sep}${current}.shp`
// Copy the first file then merge the rest into the copy.
// create: ogr2ogr -f 'ESRI Shapefile' $file $i
let create = `ogr2ogr ${format} ${currentShp} ${matches[0]}`
console.log(create)
exec(create, (error, stdout, stderr) => {
// All shapefiles but the first.
currentBatches = matches.slice(1)
if ( currentBatches.length === 0 ) {
processedStates += 1
checkProcessed()
return
}
mergeBatch(currentShp, currentBatches[processedBatches])
})
})
}
let checkBatches = () => {
if ( processedBatches < currentBatches.length ) {
mergeBatch(currentShp, currentBatches[processedBatches])
} else {
processedBatches = 0
currentBatches = null
processedStates += 1
checkProcessed()
}
}
let mergeBatch = (target, batch) => {
// merge: ogr2ogr -f 'ESRI Shapefile' -update -append $file $i
let append = `ogr2ogr ${format} -update -append ${currentShp} ${batch}`
console.log('\t', append)
exec(append, (error, stdout, stderr) => {
processedBatches += 1
checkBatches()
})
}
checkProcessed()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.