◆ 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
terminal
# 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
Output of the command: the network of urban influence in Pernambuco, one coloured line per regional network, as a metro map over the state outline. 100% of the volume in 228 segments · 10 places off the map · 2 labels left out.
Network of urban influence in Pernambuco (REGIC 2018, IBGE), one line per regional network. 100% of the volume in 228 segments · 10 places off the map · 2 labels left out. In the gallery, with the source and the data.

Examples with real data

Public data from Pernambuco, drawn by the command line; the command of each figure is in the gallery.

The same network with horizontal and vertical pieces alone, bevelled corners and square places.. 100% of the volume in 228 segments · 10 places off the map · 5 labels left out
The same network with horizontal and vertical pieces alone, bevelled corners and square places. In the gallery →
Recife metropolitan region: public-transport journeys per working day, one line per part of the region where the journey starts (Recife, north, west, south).. 37% of the volume in 15 segments · 63% internal to the places
Recife metropolitan region: public-transport journeys per working day, one line per part of the region where the journey starts (Recife, north, west, south). In the gallery →
The same lines with horizontal and vertical pieces alone.. 37% of the volume in 15 segments · 63% internal to the places
The same lines with horizontal and vertical pieces alone. In the gallery →
The same lines on the dark theme.. 37% of the volume in 15 segments · 63% internal to the places
The same lines on the dark theme. 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.

Open full screen · More maps in the gallery
Open full screen · More maps in the gallery

What it does

  1. 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.
  2. 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.
  3. 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.
  4. 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_json

The library. The command-line program milkflow is the crate milkflow-cli: see the Command line tab.

JavaScript

npm install milkflow

WebAssembly; Node ≥ 18 and browsers (directly or through a bundler).

Python

pip install milkflow

CPython ≥ 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/milkflow

Homebrew, 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 path

The four programs write the same file, byte for byte (checked when this site was built):

Result of the minimal example: four places and three corridors, the widest between Serra and Centro.
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).