Published
Edited
Aug 23, 2022
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function calculateIntersectionArea(target) {
// Filter features by only those that intersect the target feature
const intersects = indicator.features.filter((feature) =>
turf.booleanIntersects(target, feature)
);
return {
...target.properties,
intersects: intersects
.map((i) => {
// Calculate the intersection polygon
const intersect = turf.intersect(i, target);
let intersectArea = 0;
let ratio = 0;
if (intersect) {
// Take the area of the intersection polygon
intersectArea = turf.area(intersect);
// Calculate the area weight as the percent of tract that intersects the target
ratio = intersectArea / turf.area(i);
}
return {
...i.properties,
intersectArea,
ratio
};
})
.filter((i) => i.intersectArea)
};
}
Insert cell
areaRatios = places.features.slice(0, 5).map(calculateIntersectionArea)
Insert cell
Insert cell
function calculateValue(feature) {
const numerator = feature.intersects.reduce(
(acc, { numerator, ratio }) => acc + numerator * ratio,
0
);
const denominator = feature.intersects.reduce(
(acc, { denominator, ratio }) => acc + denominator * ratio,
0
);
return {
name: feature.NAME,
geoid: feature.GEOID,
numerator,
denominator,
value: numerator / denominator
};
}
Insert cell
areaValues = areaRatios.map(calculateValue)
Insert cell
Insert cell
Insert cell
Insert cell
indicators = (
await fetch(
`https://api.healthyplacesindex.org/api/indicators/hpi?key=${API_KEY}`
)
).json()
Insert cell
function loadIndicator() {
const year = d3.max(selectedIndicator.dates);
return fetch(
`https://api.healthyplacesindex.org/api/hpi?geography=tracts&year=${year}&indicator=${selectedIndicator.varname}&format=geojson&key=${API_KEY}`
).then((indicatorResponse) => indicatorResponse.json());
}
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more