Skip to content

Instantly share code, notes, and snippets.

@swingley swingley/make_fips_lookup.js Secret
Created Oct 15, 2019

Embed
What would you like to do?
const fs = require('fs')
const path = require('path')
// read the API response files
const DATA_DIR = 'data'
const API_RESPONSE_FILE_PREFIX = 'census_api_response'
const STATES_FILE = path.join(DATA_DIR, `${API_RESPONSE_FILE_PREFIX}_fips_states.json`)
const COUNTIES_FILE = path.join(DATA_DIR, `${API_RESPONSE_FILE_PREFIX}_fips_counties.json`)
const states = JSON.parse(fs.readFileSync(STATES_FILE))
const counties = JSON.parse(fs.readFileSync(COUNTIES_FILE))
const state_abbreviations = JSON.parse(fs.readFileSync('us_state_abbreviations.txt'))
Object.entries(state_abbreviations).forEach(([key, value]) => {
state_abbreviations[value] = key
})
// First element in the array isn't data but field headers, throw it out
states.shift()
let lookup = states.reduce((accum, [name, fips]) => {
const abbreviation = state_abbreviations[name]
if (abbreviation) {
accum[abbreviation] = {
'_name': name,
'_fips': fips
}
accum.fipsToState[fips] = abbreviation
}
return accum
}, { fipsToState: {} })
// First element in the array isn't data but field headers, throw it out
counties.shift()
counties.reduce((accum, [name, stateFips, fips]) => {
if (stateFips && fips && lookup.fipsToState[stateFips]) {
const countyFips = `${stateFips}${fips}`
const currentState = lookup.fipsToState[stateFips]
lookup[currentState][name.split(',')[0]] = countyFips
}
return lookup
}, lookup)
const FIPS_LOOKUP = path.join(DATA_DIR, 'fips_lookup_by_state.json')
fs.writeFileSync(FIPS_LOOKUP, JSON.stringify(lookup, null, 2))
// console.log(JSON.stringify(lookup, null, 2))
console.log(`wrote lookup file: ${FIPS_LOOKUP}`)
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.