◆ Origin–destination flow maps
Simplified flow maps, in the style of a metro map
With dozens of places, one line per origin–destination pair becomes a tangle. milkflow draws a few shared corridors, with widths by volume, and says how much of the volume was left out.
- v0.1.0
- MIT OR Apache-2.0
- Rust · WebAssembly · Python · R
- SVG · HTML · TikZ
# from source, until the packages are published
$ cargo install --path crates/milk-cli
$ milkflow regic_pe_redes.json --name regic-redes --title "" \
--k 1 --frame boundary --labels 30 --pin \
--corners smooth --corner-radius 10 \
--place-shape square --max-categories 12 \
--line-colours "#1F77B4,#E66100,#7F7F7F,#9C2A6B,#3F2B96,#D45FB3,#8C5A00,#1D1E20,#17A2B8,#C9372C,#2CA02C" \
--lang en
✓ out/regic-redes-corredores.svg
✓ out/regic-redes-esquematico.svg # and 4 more files
Examples with real data
Public data from Pernambuco, drawn by the command line; the command of each figure is in the gallery.
How an origin–destination table becomes these corridors, step by step on a synthetic city, from the tangle of straight lines to the metro map: How it works.
Over a map
The corridors can also be layers of a MapLibre GL map: the network of urban influence in Pernambuco (REGIC), one line per regional network (hover over a town to see the paths of its own flows), and the journeys between the municipalities of the Recife metropolitan region, in the geographic drawing and the pinned one. These maps load MapLibre GL and the OpenFreeMap base map; the rest of the site does not use the network.
What it does
- Simplifies before drawing: merges places by group (at one level or several, from the same file) and picks, for each place, the pairs that design the corridors.
- Routes every flow through a skeleton of a few corridors (a pruned triangulation), each segment's width in volume classes or one coloured line per category.
- Keeps the books: every unit of volume falls in corridors, internal, filtered or without a path, and the sum closes. Every figure carries a line that says how much of the volume it shows and how much was left out.
- Redraws as a metro map: on an octilinear grid or with every place pinned at its point of the map, with bends at 45° or horizontal and vertical pieces alone. The same figure comes out as SVG, as an interactive page or as TikZ for LaTeX.
The limit of the thesis. A corridor is a graphical aggregate, not an observed route. Routing A→C through A–B–C does not say that anyone went through B, and different sets of pairs can give exactly the same loads. The figure answers “how much flow joins this region to that one” and “where the trunks are”; it does not answer who is linked to whom, pair by pair. The original pairs stay in the model and in the interactive page. More on the limits.
Install
Rust
cargo add milkflow serde_jsonThe library. The command-line program milkflow is the crate milkflow-cli: see the Command line tab.
JavaScript
npm install milkflowWebAssembly; Node ≥ 18 and browsers (directly or through a bundler).
Python
pip install milkflowCPython ≥ 3.9, one abi3 wheel per platform; no dependencies.
R
install.packages("milkflow_0.1.0.tar.gz", repos = NULL, type = "source")For now, from source: needs Rust (rustc and cargo) 1.85 or later. CRAN or r-universe are planned. Help pages and vignette: the R package site.
Command line
brew install strategicprojects/tap/milkflowHomebrew, on macOS and Linux. Or, with Rust 1.85 or later: cargo install milkflow-cli. Both build from source and install the program milkflow.
Minimal example
The same data and options in the four languages. Coordinates are planar (projected, metres here); longitude and latitude must be projected first.
Rust
use milkflow::model::Input;
use milkflow::pipeline::{Options, run};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let input: Input = serde_json::from_str(
r#"{
"nodes": [
{"id": "centro", "x": 0, "y": 0, "label": "Centro"},
{"id": "porto", "x": 3200, "y": -1400, "label": "Porto"},
{"id": "serra", "x": -3800, "y": 3600, "label": "Serra"},
{"id": "olaria", "x": -400, "y": -3900, "label": "Olaria"}
],
"flows": [
{"origin": "serra", "destination": "centro", "value": 410},
{"origin": "porto", "destination": "centro", "value": 260},
{"origin": "olaria", "destination": "centro", "value": 180},
{"origin": "centro", "destination": "porto", "value": 90},
{"origin": "serra", "destination": "porto", "value": 35}
]
}"#,
)?;
let out = run(&input, &Options { k: 2, ..Options::default() })?;
std::fs::write("mapa.svg", out.svg())?;
println!("{:?}", out.spec.meta.accounting);
Ok(())
}JavaScript
import { writeFileSync } from "node:fs";
import { init, flowmap } from "milkflow";
await init();
const { svg, spec } = flowmap(
{
nodes: [
{ id: "centro", x: 0, y: 0, label: "Centro" },
{ id: "porto", x: 3200, y: -1400, label: "Porto" },
{ id: "serra", x: -3800, y: 3600, label: "Serra" },
{ id: "olaria", x: -400, y: -3900, label: "Olaria" },
],
flows: [
{ origin: "serra", destination: "centro", value: 410 },
{ origin: "porto", destination: "centro", value: 260 },
{ origin: "olaria", destination: "centro", value: 180 },
{ origin: "centro", destination: "porto", value: 90 },
{ origin: "serra", destination: "porto", value: 35 },
],
},
{ k: 2 },
);
writeFileSync("mapa.svg", svg);
console.log(spec.meta.accounting);Python
import milkflow
nodes = [
{"id": "centro", "x": 0, "y": 0, "label": "Centro"},
{"id": "porto", "x": 3200, "y": -1400, "label": "Porto"},
{"id": "serra", "x": -3800, "y": 3600, "label": "Serra"},
{"id": "olaria", "x": -400, "y": -3900, "label": "Olaria"},
]
flows = [
{"origin": "serra", "destination": "centro", "value": 410},
{"origin": "porto", "destination": "centro", "value": 260},
{"origin": "olaria", "destination": "centro", "value": 180},
{"origin": "centro", "destination": "porto", "value": 90},
{"origin": "serra", "destination": "porto", "value": 35},
]
r = milkflow.flowmap(nodes, flows, k=2)
r.save("mapa.svg")
print(r.spec["meta"]["accounting"])R
library(milkflow)
nodes <- data.frame(
id = c("centro", "porto", "serra", "olaria"),
x = c(0, 3200, -3800, -400),
y = c(0, -1400, 3600, -3900),
label = c("Centro", "Porto", "Serra", "Olaria")
)
flows <- data.frame(
origin = c("serra", "porto", "olaria", "centro", "serra"),
destination = c("centro", "centro", "centro", "porto", "porto"),
value = c(410, 260, 180, 90, 35)
)
m <- flowmap(nodes, flows, k = 2)
write_svg(m, "mapa.svg")
m # the accounting: in corridors, internal, filtered, without a pathThe four programs write the same file, byte for byte (checked when this site was built):
mapa.svg of the minimal example, drawn here with style: {lang: "en"} added so that the legend and the honesty line are in English (the examples above write them in Portuguese, the default).Keep going
Next: how it works, step by step; the gallery; the reference; or try it in the browser.