Public
Edited
Aug 1, 2023
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
Insert cell
Insert cell
Insert cell
mean_sending_distance = d3.rollup(
points,
(v) => d3.mean(v, (d) => d.distance),
(d) => d.sending_school_id
)
Insert cell
receiving_school_ids = Array.from(
new Set(receiving_zoned.map((d) => d.ATTSCH_ID))
)
Insert cell
receiving_school_counts = Object.fromEntries(
receiving_school_ids.map((d) => [d, 0])
)
Insert cell
location_lookup = new Map(
receiving_locations.features.map((d) => [
d.properties.school_id,
d.geometry.coordinates
])
)
Insert cell
Insert cell
points = {
let all_points = [];
for (const feature of sending.features) {
const sending_points = dot_density(
{ type: "FeatureCollection", features: [feature] },
receiving_school_ids,
students_per_point,
random_points,
5,
index
).map((d) => ({
x: d.x,
y: d.y,
school_id: d.field,
sending_school_id: feature.properties.school_id,
distance: hav.distance(d.x, d.y, ...location_lookup.get(d.field))
}));
all_points = all_points.concat(sending_points);
}
return all_points;
}
Insert cell
index = {
const dex = new KDBush(random_points.length);

for (const [x, y] of random_points) {
dex.add(x, y);
}
dex.finish();
return dex;
}
Insert cell
random_points = points_under_18.features[0].geometry.coordinates
Insert cell
points_under_18 = FileAttachment("points_under_18@2.geojson").json()
Insert cell
sending = {
const sending = structuredClone(boundary);
for (const zoned_school of sending.features) {
const recipients = sending_lookup.get(zoned_school.properties.school_id);
zoned_school.properties = {
...zoned_school.properties,
...receiving_school_counts,
...Object.fromEntries(recipients)
};
}
return sending;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
students_per_point = 5
Insert cell
KDBush = require("kdbush")
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