Published
Edited
Aug 23, 2022
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
let intersects = indicator.features.filter((feature) =>
turf.booleanIntersects(target, feature)
);
let totalWeight = 0;
intersects = intersects
.map((i) => {
// Calculate the intersection polygon
const intersect = turf.intersect(i, target);
let intersectArea = 0;
let weight = 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
weight = (intersectArea / turf.area(i)) * i.properties.population;
totalWeight += weight;
}
return {
...i.properties,
intersectArea,
weight
};
})
.filter((i) => i.intersectArea);
return {
...target.properties,
totalWeight,
intersects
};
}
Insert cell
areaRatios = school.features.slice(0, 20).map(calculateIntersectionArea)
Insert cell
Insert cell
function calculateValue(feature) {
const value = feature.intersects.reduce(
(acc, { value, weight }) => acc + value * weight,
0
);
return {
name: feature.NAME,
geoid: feature.GEOID,
value: value / feature.totalWeight
};
}
Insert cell
areaValues = areaRatios.map(calculateValue)
Insert cell
Insert cell
Insert cell
Insert cell
d3.max(selectedIndicator.dates)
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