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

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