Published
Edited
Nov 4, 2021
13 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
paths = {
const paths = [];
const P = shortest_junctions(graph, tree);

for (const code of P.costs.keys()) {
const cost = P.costs.get(code),
junction = P.junctions.get(code),
path = junction.slice();
path.reverse();
while (tree.predecessor[path[0]] > -1)
path.unshift(tree.predecessor[path[0]]);
path.reverse();
while (tree.predecessor[path[0]] > -1)
path.unshift(tree.predecessor[path[0]]);
paths.push({ cost, junction, path });
}

return paths;
}
Insert cell
Insert cell
Insert cell
src = "https://gist.githubusercontent.com/Fil/450db51a15fdc8bd34a12cb8a9961976/raw/680961da57d2b11a0e3e6993e5f79bd035729831"
Insert cell
nodes = d3.csv(`${src}/node_list.csv`, d3.autoType)
Insert cell
edges = d3.csv(`${src}/edge_list.csv`, d3.autoType)
Insert cell
Insert cell
nodesIndex = new Map(nodes.map((d, i) => [d.osmid, i]))
Insert cell
function createGraph(nodesIndex, edges) {
const graph = { sources: [], targets: [], costs: [] };
edges.forEach(({ u, v, length }) => {
const i = nodesIndex.get(u),
j = nodesIndex.get(v),
cost = length;
graph.sources.push(i);
graph.targets.push(j);
graph.costs.push(cost);

graph.sources.push(j);
graph.targets.push(i);
graph.costs.push(cost);
});

return graph;
}
Insert cell
graph = createGraph(nodesIndex, edges)
Insert cell
import { shortest_tree, shortest_junctions } from "@fil/dijkstra"
Insert cell
tree = shortest_tree({ graph, origins })
Insert cell
Insert cell
pts = nodes.map(d => projection([d.x, d.y]))
Insert cell
projection = d3.geoMercator().fitExtent([[10, 10], [width - 10, height - 10]], {
type: "MultiPoint",
coordinates: nodes.map(d => [d.x, d.y])
})
Insert cell
Insert cell
color = (replay, d3.scaleOrdinal(d3.schemeCategory10))
Insert cell
Insert cell
d3 = require("d3@5")
Insert cell
height = width * 0.9
Insert cell
import { slider } from "@jashkenas/inputs"
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