Published
Edited
May 10, 2020
1 fork
Importers
2 stars
Insert cell
Insert cell
// Import me!
usTopoJsonWithMetros
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
usTopoJson = FileAttachment("counties-albers-10m.json").json()
Insert cell
Insert cell
countiesByCBSA = {
const text = await FileAttachment("list1_Sep_2018.csv").text();
return d3.csvParse(text, row => ({
countyName: row["County/County Equivalent"],
fipsStateCode: row["FIPS State Code"],
fipsCountyOnlyCode: row["FIPS County Code"],
fipsCountyCode: row["FIPS State Code"] + row["FIPS County Code"],
areaName: row["CBSA Title"],
areaType: row["Metropolitan/Micropolitan Statistical Area"],
areaCode: row["CBSA Code"]
}));
}
Insert cell
Insert cell
cbsaToCounties = countiesByCBSA.reduce((accumulator, county) => {
const key = county.areaName;
const countyObj = {
name: county.countyName,
fipsCode: county.fipsCountyCode
};

if (!accumulator[key]) {
const countiesMap = new Map();
countiesMap.set(countyObj.fipsCode, countyObj);
accumulator[key] = {
id: county.areaCode,
name: county.areaName,
type: county.areaType,
counties: countiesMap,
countiesFipsArray: [county.fipsCountyCode]
};
} else {
accumulator[key].counties.set(countyObj.fipsCode, countyObj);
accumulator[key].countiesFipsArray.push(county.fipsCountyCode);
}

return accumulator;
}, {})
Insert cell
cbsaToArrayOfCountyFipsCodes = {
return new Map(
Object.values(cbsaToCounties).map(metro => [
metro.name,
metro.countiesFipsArray
])
);
}
Insert cell
Insert cell
Insert cell
countyFipsToGeometry = new Map(
usTopoJson.objects.counties.geometries.map(countyGeo => [
countyGeo.id,
countyGeo
])
)
Insert cell
Insert cell
metroGeometries = {
return new Map(
Object.values(cbsaToCounties)
// our usTopoJson does not include Puerto Rico, so remove these
.filter(metro => !metro.name.includes(', PR'))
// todo: solve micropolitan issue
.filter(metro => metro.type.includes('Metro'))
.map(metro => {
const countyGeomArray = [];
metro.countiesFipsArray.forEach(fipsCode => {
const countyGeom = countyFipsToGeometry.get(fipsCode);
countyGeomArray.push(countyGeom);
});
const mergedGeometry = topojson.mergeArcs(usTopoJson, countyGeomArray);
mergedGeometry.id = metro.name;
mergedGeometry.properties = {
name: metro.name
};

// Uncomment to change map key to id intead of metro name
// return [metro.id, mergedGeometry];
return [metro.name, mergedGeometry];
})
);
}
Insert cell
Insert cell
usTopoJsonWithMetros = {
const newTopoJson = JSON.parse(JSON.stringify(usTopoJson));
newTopoJson.objects.metros = {
type: "GeometryCollection",
geometries: Array.from(metroGeometries.values())
};
return newTopoJson;
}
Insert cell
Insert cell
d3 = require("d3@5")
Insert cell
topojson = require("topojson-client@3")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more