Intercity Migration Arcs
260 OD pairs among 22 provincial capitals, line width by passenger volume and arc height by distance, blue at the origin and orange at the destination, with a button switching the encoding and a checkbox filtering inter-region flows.
Business context
Population and mobility researchers, urban planners and transport authorities use OD arcs to read the strength and direction of interprovincial movement, and to inform capacity allocation, hub siting and policy evaluation. The data comes from mobile signaling, rail and air ticketing or third-party migration indices, aggregated into city pairs by day or month; national datasets commonly hold hundreds to a few thousand ODs, with each pair ranging from hundreds to tens of thousands of passengers.
Raw ODs have usually been through home-region inference and denoising already. Before deck.gl they need their city centroid coordinates unified, self-loops and very short pairs removed, and passenger volumes compressed with a log or square-root scale so a single dominant flow does not blow out line width. ArcLayer is chosen because it encodes direction, volume and distance at once; the gradient from origin to destination makes flow direction immediate, with higher information density than an undirected segment.
Arc height is a visual quantity, not a physical one: the getHeight value is not kilometers, so comparing two maps with different height multipliers distorts the sense of distance. Another trap is treating passengers as people. An OD table typically counts each direction separately, so summing both double-counts; confirm whether the measure is bidirectional before reading totals.
How it works
The 22 provincial capitals are tagged by seven regions and 260 ODs are generated from a seeded random source, with passenger volume laid on a 2.3-power curve into 500–90,000. ArcLayer uses widthUnits pixels and widthMinPixels 1; getWidth returns 1+sqrt(pax)/70 for roughly 1.3–5.3 pixels, and getHeight returns 0.12+dist/4000, a dimensionless arc multiplier where 0 is flat, so longer routes arch higher without changing geometry under zoom.
Coloring by volume raises opacity with sqrt(pax), while coloring by distance walks hue from deep to light blue and dark to bright orange. The inter-region box drops same-region ODs by province tag, typically leaving about half of the 260, so short same-region links such as Beijing–Tianjin or Shanghai–Nanjing no longer bury the long routes. Filtering builds a new array only, object references are reused, and the layer id stays migration-arc.
| Configuration | What it does |
|---|---|
| widthUnits | pixels, so line width does not change with zoom |
| getWidth | 1+sqrt(pax)/70 maps passengers to 1.3–5.3 px |
| getHeight | 0.12+dist/4000, a dimensionless arc-height multiplier |
| getSourceColor | Origin blue computed from the selected encoding |
Note. ArcLayer getHeight is a dimensionless arc multiplier, not a height in meters; to exaggerate long routes raise the denominator rather than passing kilometers.
Source
<!--
示例:城市迁徙飞线
要点:ArcLayer 按人次定线宽、按距离定弧高,按钮切换着色口径,勾选框按大区过滤 OD
依赖:deck.gl 9.0 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>城市迁徙飞线</title>
<style>
html,body{height:100%;margin:0;background:#0d1117}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#e6e8ec}
.bar{position:relative;z-index:5;flex:0 0 auto;padding:8px 12px;background:#161b22;border-bottom:1px solid #252c36;
display:flex;gap:10px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
.bar b{color:#58a6ff;font-weight:600}
.bar label{display:flex;align-items:center;gap:5px;cursor:pointer}
.bar input[type=checkbox]{accent-color:#4f9ef8}
.bar button{font:inherit;font-size:12px;padding:3px 10px;border-radius:5px;cursor:pointer;
border:1px solid #303845;background:#1d2531;color:#c8d2e0}
.bar button:hover{background:#253040}
.bar button.on{background:#2c4a7a;border-color:#4f8ad6;color:#dfe9ff}
.bar .sep{width:1px;height:18px;background:#2a313c;margin:0 4px}
#cv{flex:1 1 auto;min-height:0;width:100%;display:block}
</style>
</head>
<body>
<div class="bar">
<span>OD <b id="n">—</b></span>
<span class="sep"></span>
<button data-mode="pax" class="on">按人次着色</button>
<button data-mode="dist">按距离着色</button>
<span class="sep"></span>
<label><input id="cross" type="checkbox"> 只显示跨区域</label>
<span class="sep"></span>
<span>widthUnits pixels · widthMinPixels 1</span>
</div>
<canvas id="cv"></canvas>
<script src="https://cdn.jsdelivr.net/npm/deck.gl@9.0.35/dist.min.js"></script>
<script>
(function () {
'use strict';
if (typeof deck === 'undefined') {
document.body.innerHTML =
'<div style="padding:24px;color:#ff8c74">deck.gl 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
function rng(seed) {
var a = seed >>> 0;
return function () {
a = (a + 0x6D2B79F5) >>> 0;
var t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
var cities = [
{ name: '北京', pos: [116.41, 39.90], region: '华北' },
{ name: '天津', pos: [117.20, 39.13], region: '华北' },
{ name: '石家庄', pos: [114.51, 38.04], region: '华北' },
{ name: '太原', pos: [112.55, 37.87], region: '华北' },
{ name: '沈阳', pos: [123.43, 41.80], region: '东北' },
{ name: '长春', pos: [125.32, 43.90], region: '东北' },
{ name: '哈尔滨', pos: [126.64, 45.76], region: '东北' },
{ name: '上海', pos: [121.47, 31.23], region: '华东' },
{ name: '南京', pos: [118.80, 32.06], region: '华东' },
{ name: '杭州', pos: [120.15, 30.27], region: '华东' },
{ name: '合肥', pos: [117.23, 31.82], region: '华东' },
{ name: '福州', pos: [119.30, 26.08], region: '华东' },
{ name: '郑州', pos: [113.62, 34.75], region: '华中' },
{ name: '武汉', pos: [114.31, 30.59], region: '华中' },
{ name: '长沙', pos: [112.94, 28.23], region: '华中' },
{ name: '广州', pos: [113.26, 23.13], region: '华南' },
{ name: '南宁', pos: [108.37, 22.82], region: '华南' },
{ name: '海口', pos: [110.20, 20.04], region: '华南' },
{ name: '成都', pos: [104.07, 30.67], region: '西南' },
{ name: '昆明', pos: [102.83, 24.88], region: '西南' },
{ name: '西安', pos: [108.94, 34.34], region: '西北' },
{ name: '兰州', pos: [103.83, 36.06], region: '西北' }
];
function distKm(a, b) {
var rad = Math.PI / 180;
var dLat = (b[1] - a[1]) * rad;
var dLng = (b[0] - a[0]) * rad;
var s = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(a[1] * rad) * Math.cos(b[1] * rad) *
Math.sin(dLng / 2) * Math.sin(dLng / 2);
return 6371 * 2 * Math.atan2(Math.sqrt(s), Math.sqrt(1 - s));
}
var rand = rng(20261124);
var arcs = [];
var seen = {};
var guard = 0;
while (arcs.length < 260 && guard < 6000) {
guard++;
var ai = Math.floor(rand() * cities.length);
var bi;
if (rand() < 0.5) {
var pool = [];
for (var ci = 0; ci < cities.length; ci++) {
if (cities[ci].region === cities[ai].region && ci !== ai) pool.push(ci);
}
bi = pool[Math.floor(rand() * pool.length)];
} else {
bi = Math.floor(rand() * cities.length);
}
if (ai === bi) continue;
var key = ai + '-' + bi;
if (seen[key]) continue;
seen[key] = 1;
var pax = Math.round(500 + Math.pow(rand(), 2.3) * 89500);
arcs.push({
a: cities[ai],
b: cities[bi],
pax: pax,
dist: distKm(cities[ai].pos, cities[bi].pos)
});
}
var maxDist = 1;
for (var d = 0; d < arcs.length; d++) {
if (arcs[d].dist > maxDist) maxDist = arcs[d].dist;
}
function lerp(a, b, t) {
return [
Math.round(a[0] + (b[0] - a[0]) * t),
Math.round(a[1] + (b[1] - a[1]) * t),
Math.round(a[2] + (b[2] - a[2]) * t)
];
}
var mode = 'pax';
var crossOnly = false;
function colorPair(d) {
var t = mode === 'pax' ? Math.sqrt(d.pax / 90000) : d.dist / maxDist;
if (t > 1) t = 1;
var src = mode === 'pax'
? lerp([25, 70, 160], [90, 175, 255], t)
: lerp([40, 60, 140], [80, 200, 255], t);
var dst = mode === 'pax'
? lerp([200, 70, 20], [255, 190, 90], t)
: lerp([200, 50, 40], [255, 200, 80], t);
var alpha = mode === 'pax' ? Math.round(50 + 190 * t) : 200;
src.push(alpha);
dst.push(alpha);
return [src, dst];
}
function visible() {
if (!crossOnly) return arcs;
var out = [];
for (var k = 0; k < arcs.length; k++) {
if (arcs[k].a.region !== arcs[k].b.region) out.push(arcs[k]);
}
return out;
}
function buildLayer() {
return new deck.ArcLayer({
id: 'migration-arc',
data: visible(),
getSourcePosition: function (d) { return d.a.pos; },
getTargetPosition: function (d) { return d.b.pos; },
getSourceColor: function (d) { return colorPair(d)[0]; },
getTargetColor: function (d) { return colorPair(d)[1]; },
getWidth: function (d) { return 1 + Math.sqrt(d.pax) / 70; },
widthUnits: 'pixels',
widthMinPixels: 1,
getHeight: function (d) { return 0.12 + d.dist / 4000; },
pickable: true
});
}
var deckgl = new deck.Deck({
canvas: 'cv',
width: '100%',
height: '100%',
initialViewState: {
longitude: 109.5,
latitude: 34.0,
zoom: 4,
pitch: 35,
bearing: 0,
maxPitch: 65
},
controller: true,
layers: [buildLayer()],
getTooltip: function (info) {
if (!info.object) return null;
return info.object.a.name + ' → ' + info.object.b.name +
' · ' + info.object.pax.toLocaleString() + ' 人次 · ' +
Math.round(info.object.dist) + ' km';
}
});
function refreshCount() {
document.getElementById('n').textContent = visible().length;
}
var buttons = document.querySelectorAll('[data-mode]');
for (var b = 0; b < buttons.length; b++) {
buttons[b].addEventListener('click', function (e) {
mode = e.currentTarget.getAttribute('data-mode');
for (var k = 0; k < buttons.length; k++) {
buttons[k].classList.toggle('on', buttons[k] === e.currentTarget);
}
deckgl.setProps({ layers: [buildLayer()] });
});
}
document.getElementById('cross').addEventListener('change', function (e) {
crossOnly = e.currentTarget.checked;
refreshCount();
deckgl.setProps({ layers: [buildLayer()] });
});
refreshCount();
window.__deck = deckgl;
})();
</script>
</body>
</html>