用ClickHouse FORMAT PNG渲染FAA停飞当天全美空域
直接阅读译文DataHot 速览
ClickHouse 官方博客展示了用 SQL 直接渲染动画的实践:查询每像素返回一行,借助内置 FORMAT PNG 输出生成动画,无需绘图库或图像处理脚本。作者用 ADS-B 航班数据可视化 11 月 13 日 FAA 停飞事件,并给出逐小时统计:14:00 ET 约 1.5 万架飞机在飞,到 20:00 ET 巡航高度飞机不足早期水平的十分之一。颜色编码同时表示高度与地速,机场周边爬升下降呈黄橙色。
为什么值得关注:它示范了数据库内计算与渲染一体化的思路,把可视化下推到 SQL 引擎,对实时分析与报表渲染链路的成本与架构取舍有参考价值。
译文
AI 逐段翻译动画由 ClickHouse 本身渲染。没有绘图库,也没有图像处理脚本。一个 SQL 查询为每个像素返回一行,内置的 PNG 输出格式把这些行变成动画 PNG。地图、标题、时钟和底部的时间轴都是同一个结果集的行。连字母也是 SQL:一种 3×5 点阵字体存储在一个 map 字面量中。
数据展示了什么
在动画中,每架飞机呈现为一段短 streak:它在五分钟内飞过的距离。颜色遵循 adsb.exposed 的默认调色板:
- 红色随高度渐变。
- 绿色在前 5,000 英尺内增长,因此机场周围的爬升和下降呈现黄色和橙色。
- 蓝色显示地速,因此高空巡航航班是蓝色的。
这个早晨看起来和任何周五一样。网络从东海岸向西填充,到美东时间 13:00–14:00,画面内每小时有近 15,000 架飞机在空中。大约美东时间 15:40,位置数量开始下降,并在下午剩余时间里持续下降。高空 streak 首先变稀疏,机场一个接一个变暗。到美东时间 20:00,只剩下零星的低空航班和画面边缘的一些过境航班。
画面内每小时的数字(按 ICAO 地址统计的唯一飞机,以及其中在 18,000 英尺以上被看到的):
| 小时(美东时间) | 位置数 | 飞机数 | FL180 以上 |
|---|---|---|---|
| 12:00 | 4,038,963 | 14,787 | 5,850 |
| 13:00 | 4,151,849 | 14,885 | 6,046 |
| 14:00 | 4,217,014 | 14,957 | 5,995 |
| 15:00 | 3,991,427 | 13,958 | 5,376 |
| 16:00 | 2,556,066 | 10,080 | 3,196 |
| 17:00 | 1,465,549 | 6,304 | 1,658 |
| 18:00 | 949,405 | 4,084 | 907 |
| 19:00 | 543,053 | 2,592 | 632 |
| 20:00 | 418,486 | 2,131 | 559 |
| 21:00 | 566,155 | 2,641 | 1,059 |
| 22:00 | 819,072 | 3,132 | 1,731 |
| 23:00 | 796,677 | 3,128 | 1,929 |
在美东时间 20:00,巡航高度上的飞机数量不到下午早些时候水平的十分之一。作为对比,前一晚最安静的小时(美东时间 03:00)仍有 818 架。美东时间 21:00 之后,高空交通开始回升。
这是两个 10 分钟窗口的静态图像,美东时间 14:00(上)和 20:00(下)。它由公共 adsb.exposed 服务器上的单个查询渲染而成(见下文):
你可以交互式探索同样的日子:adsb.exposed,11 月 11–16 日。
数据
adsb.exposed 将来自 adsb.lol、airplanes.live 和 ADSB Exchange 的每一条位置报告存储在一个 ClickHouse 表 planes_mercator 中。它有 3,800 亿行,每一行包含时间、坐标、高度、速度和飞机。坐标也以 Web Mercator 投影存储,预先计算为两个 UInt32 列:
1mercator_x UInt32 MATERIALIZED 4294967295* ((lon +180) /360),2mercator_y UInt32 MATERIALIZED 4294967295* ((1/2) - ((log(tan(((lat +90) /360) * pi())) /2) / pi())),整个世界是一个 2³² × 2³² 的正方形。表按 mortonEncode(mercator_x, mercator_y) 排序,这是一条 Z 序曲线。一个地图瓦片,或地图上的任意矩形,都是 mercator_x 和 mercator_y 的一个范围,而主键使这样的范围读取成本很低。将坐标转换为像素只需要一次位移:mercator_x >> 19 将世界划分为 2¹³ = 8192 个像素,这是网站上的缩放级别 5。在这个尺度下,从西经 128° 到西经 64°、从北纬 22° 到北纬 51° 的矩形是 1456 × 840 像素。
第 1 步:在服务器上聚合
对于每一帧和每一个像素,服务器统计位置数并平均高度和速度(pixels.sql):
1WITH2 toUInt32(4294967295* ((-128+180) /360)) AS x0,3 toUInt32(4294967295* ((-64+180) /360)) AS x1,4 toUInt32(4294967295* (1/2-log(tan((51+90) /360* pi())) /2/ pi())) AS y0,5 toUInt32(4294967295* (1/2-log(tan((22+90) /360* pi())) /2/ pi())) AS y1,6 toDateTime('2026-11-13 05:00:00', 'UTC') AS t0,7 t0 + {h:UInt32} *3600AS hour_begin8SELECT9 toUInt16(intDiv(toUnixTimestamp(time) - toUnixTimestamp(t0), 300)) AS frame,10 toUInt16(bitShiftRight(mercator_x - x0, 19)) AS px,11 toUInt16(bitShiftRight(mercator_y - y0, 19)) AS py,12 toUInt32(count()) AS c,13 toFloat32(avg(altitude)) AS alt,14 toFloat32(avg(ground_speed)) AS speed15FROM planes_mercator16WHEREtime>= hour_begin ANDtime< hour_begin +360017AND mercator_x >= x0 AND mercator_x < x118AND mercator_y >= y0 AND mercator_y < y119GROUPBY frame, px, py;一天是美东时间的一天:美东时间午夜是 UTC 05:00。该查询读取 4250 万个位置并返回 900 万行,每个在 288 帧中的某一帧里至少看到一架飞机的像素对应一行。平均每帧在 120 万个像素中约有 31,000 个点亮像素。
adsb.exposed 的公共端点将结果限制在约一百万行。因此查询把小时作为参数 {h:UInt32},并运行 24 次。clickhouse-local 在一条语句中完成这一点:url 表函数将 URL 中的 {0..23} 展开为 24 个 URL,这里通配符放在查询参数本身中:
1URL="https://kvzqttvc2n.eu-west-1.aws.clickhouse-staging.com/?user=website&default_format=RowBinary¶m_h={0..23}&query=$(python3 -c 'import sys, urllib.parse; print(urllib.parse.quote(sys.stdin.read(), safe=""))' < pixels.sql)"23clickhouse local --query "4 SELECT * FROM url('$URL', RowBinary, 'frame UInt16, px UInt16, py UInt16, c UInt32, alt Float32, speed Float32')5 INTO OUTFILE 'pixels.native' TRUNCATE FORMAT Native6 SETTINGS max_threads = 6"注意 default_format=RowBinary。url 函数告诉客户端要解析什么格式,但不告诉服务器要发送什么格式。网站 的默认格式是 TSV,而将 TSV 按 RowBinary 解析会失败,并给出令人困惑的“Cannot read all data”错误。
结果是一个 160 MB 的预聚合像素文件,之后每一步都在本地运行。
第 2 步:用 FORMAT PNG
渲染r、g、b 表示 RGB,r、g、b、a 表示 RGBA,或 v 表示灰度。如果有整数 x 和 y 列,它们会把每一行放到画布上。否则各行按扫描线顺序填充像素。如果有 t 列,结果会变成动画 PNG,每个 t 值对应一帧。一帧持续到下一个 t 值,设置 output_format_image_time_multiplier_seconds 和 output_format_image_time_divisor_seconds 设定 t 的单位。这里是 2/25 秒,因此每个 5 分钟帧播放 80 毫秒,一整天用时 23 秒。
当两行落在同一像素上时,后一行获胜(画家算法)。因此图像按层构建,用 ORDER BY layer 排序:
- 幽灵。 全天交通量,按像素在对数尺度上求和,并在每一帧中以深灰色绘制。它在天空变空时保持国家轮廓可见。这些是 405,000 个像素,用
ARRAY JOIN range(frames)复制到 288 帧中。 - 飞机。 当前帧的飞机,按高度和速度着色。
- 时间轴。 每一帧中的位置数,作为地图下方的条形图,当前帧高亮显示,过去比未来用更浅的灰色。
- 文本。 每个字符串拆分成字符,每个字符拆分成其字形的 15 个位,每个点亮的位拆分成一个
scale × scale的像素方块。四个ARRAY JOIN一个接一个,产生笛卡尔积。时钟就是普通的formatDateTime,位于America/New_York和UTC时区。
完整查询(render.sql):
1-- Renders the animation from the per-frame, per-pixel aggregates in `pixels.native`.2-- Every row of the result is one pixel of one frame: (t, x, y, r, g, b).3-- Rows are painted in the order they arrive, so the layers are sorted: background first, text last.45WITH61456AS width, -- map width in pixels7840AS map_height, -- the timeline goes below the map8288AS frames, -- one frame per 5 minutes910 pixels AS11 (12SELECT*FROM file('pixels.native')13WHERE px < width AND py < map_height AND frame < frames14 ),1516-- The faint trace of the whole day, repeated in every frame, so that the outline of the US17-- is still there when the sky is empty.18 ghost AS19 (20SELECT px, py, toUInt8(pow(log1p(total) / log1p(max(total) OVER ()), 1.5) *55) AS v21FROM (SELECT px, py, sum(c) AS total FROM pixels GROUPBY px, py)22 QUALIFY v >=423 ),2425-- The number of positions in every frame, for the timeline.26 activity AS27 (28SELECT frame, sum(c) /max(sum(c)) OVER () AS share29FROM pixels30GROUPBY frame31 ),3233-- A 3x5 bitmap font: five rows of three bits each.34 map(35'0', '111101101101111', '1', '010110010010111', '2', '111001111100111', '3', '111001111001111',36'4', '101101111001001', '5', '111100111001111', '6', '111100111101111', '7', '111001001001001',37'8', '111101111101111', '9', '111101111001111', ':', '000010000010000', '.', '000000000000010',38'-', '000000111000000', ' ', '000000000000000',39'A', '010101111101101', 'B', '110101110101110', 'C', '111100100100111', 'D', '110101101101110',40'E', '111100110100111', 'F', '111100110100100', 'G', '111100101101111', 'I', '111010010010111',41'N', '110101101101101', 'O', '010101101101010', 'P', '111101111100100', 'R', '110101110101101',42'S', '111100111001111', 'T', '111010010010010', 'U', '101101101101111', 'V', '101101101101010',43'X', '101101010101101', 'Y', '101101010010010') AS font,4445-- All the text, one row per string: the frame, the position of the top-left corner, the scale, the color.46 texts AS47 (48SELECT number AS frame, 20AS x0, 16AS y0, 4AS scale,49'FAA OUTAGE - FRIDAY NOV 13 2026'AS text, 255AS r, 255AS g, 255AS b50FROM numbers(frames)51UNIONALL52SELECT number, 20, 44, 4,53 formatDateTime(toDateTime('2026-11-13 05:00:00', 'UTC') + number *300, '%H:%i ET', 'America/New_York')54||' '|| formatDateTime(toDateTime('2026-11-13 05:00:00', 'UTC') + number *300, '%H:%i UTC', 'UTC'),55255, 210, 11056FROM numbers(frames)57UNIONALL58SELECT number, width -20-12*8, map_height -22, 2, 'ADSB.EXPOSED', 150, 150, 15059FROM numbers(frames)60UNIONALL61-- The hour labels under the timeline: 00:00, 03:00, ..., 24:00.62SELECT frame, toInt32(30+ (width -60) *hour/24-2.5*8), map_height +72, 2,63 leftPad(toString(hour), 2, '0') ||':00', 150, 150, 15064FROM (SELECT number *3AShourFROM numbers(9)) ARRAYJOINrange(frames) AS frame65 ),6667 text_pixels AS68 (69SELECT frame,70 x0 + ((char_num -1) *4+ bit %3) * scale + dx AS x, -- glyphs are 3 pixels wide, plus 1 of spacing71 y0 + intDiv(bit, 3) * scale + dy AS y,72 r, g, b73FROM texts74ARRAYJOIN arrayEnumerate(splitByString('', text)) AS char_num75ARRAYJOINrange(15) AS bit76ARRAYJOINrange(scale) AS dx77ARRAYJOINrange(scale) AS dy78WHEREsubstring(font[substring(text, char_num, 1)], bit +1, 1) ='1'79 ),8081-- The timeline: a bar for every frame, highlighted for the current one.82 timeline AS83 (84SELECT85currentAS frame,86 toInt32(floor(30+ (width -60) * bar / frames)) AS xa,87 toInt32(floor(30+ (width -60) * (bar +1) / frames)) -1AS xb,88 toInt32(48* share) AS h,89 multiIf(bar =current, (255, 200, 80), bar <current, (110, 110, 110), (55, 55, 55)) AS color90FROM (SELECT frame AS bar, share FROM activity) ARRAYJOINrange(frames) AScurrent91 )9293SELECT t, toInt32(x) AS x, toInt32(y) AS y, toUInt8(round(r)) AS r, toUInt8(round(g)) AS g, toUInt8(round(b)) AS b94FROM95(96-- Layer 0: the ghost of the whole day.97SELECT0AS layer, frame AS t, px AS x, py AS y, v AS r, v AS g, v AS b98FROM ghost ARRAYJOINrange(frames) AS frame99100UNIONALL101102-- Layer 1: the aircraft, colored with the same formula as on adsb.exposed: red fades with the altitude,103-- green grows over the first 5000 feet (so the climbs and descents near airports are yellow), blue is the speed.104SELECT1, frame, px, py,105 (1+ transparency) /2* (1- least(alt, 50000) /50000) *255,106 transparency * greatest(0, least(alt, 5000)) /5000*255,107 greatest(0, least(speed, 700)) /700*255108FROM (SELECT*, pow(least(c /30, 1), 1/5) AS transparency FROM pixels)109110UNIONALL111112-- Layer 2: the timeline.113SELECT2, frame, xa + dx, map_height +66- dy, color.1, color.2, color.3114FROM timeline115ARRAYJOINrange(xb - xa +1) AS dx116ARRAYJOINrange(h +1) AS dy117118UNIONALL119120-- Layer 3: the text.121SELECT3, frame, x, y, r, g, b122FROM text_pixels123)124ORDERBY layer125126INTO OUTFILE 'faa-outage-2026-11-13.png'TRUNCATE127FORMAT PNG128SETTINGS129 output_format_image_width =1456,130 output_format_image_height =930,131-- One unit of `t` is 2/25 of a second: 80 ms per frame, the whole day in 23 seconds.132 output_format_image_time_multiplier_seconds =2,133 output_format_image_time_divisor_seconds =25;该查询产生约1.3亿个像素行:其中1.17亿行用于幽灵,900万行用于飞机,其余用于时间线和文本。它在单台机器上于31秒内写出全部288帧。
需要注意几点:
- 像素值会被钳制,浮点数会被缩放。整数通道被钳制到
[0, 255],而Float通道被钳制到[0, 1],然后再乘以255。这里的颜色运算产生的浮点数在[0, 255]范围内,因此外层的SELECT会用toUInt8(round(...))转换它们。否则,每个亮起的像素都会变成纯白色。 - 各帧相互独立。每一帧都从黑色画布开始,这就是幽灵必须在每一帧中重复出现、而不能只绘制一次的原因。作为交换,
t可以以任意顺序到达。一帧的各行只需在彼此之间按图层排序。 - 内存与流式输出。默认情况下,该格式会将每一帧保留在内存中直到查询结束:288 × 1456 × 930 × 3 字节,此处约为1.2 GB。使用
output_format_image_streaming_animation = 1时,它会在t推进时立即写出每一帧,并且只在内存中保留一帧。这需要ORDER BY t, layer。此外,此时文件头中的帧数只是一个上界:浏览器可以播放这样的文件,但某些解码器会报错。
FORMAT PNG 写出真彩色帧,288帧总计150 MB。画面大部分是黑色,因此根据少量样本帧构建的单一256色调色板造成的损失非常小。转换为该调色板后,APNG 降至25 MB。GIF 和 MP4 由同一文件通过 ffmpeg 生成:
1PALETTE="split[a][b];[a]palettegen=max_colors=256:stats_mode=full[p];[b][p]paletteuse=dither=none"2ffmpeg -i faa-outage-2026-11-13.png -vf "$PALETTE" -loop 0 faa-outage-2026-11-13.gif3ffmpeg -i faa-outage-2026-11-13.png -c:v libx264 -preset slow -crf 18 -pix_fmt yuv420p \4 -profile:v high -level 4.1 -movflags +faststart faa-outage-2026-11-13.mp4MP4 使用 yuv420p 中的 H.264 High profile,因此可以在 QuickTime、Windows 播放器、VLC、浏览器和即时通讯应用中播放。它是三者中最大的,因为数以千计的细小单像素条纹很难进行视频压缩。
直接从服务器生成的图片
adsb.exposed 的公开 网站 用户是只读的,因此无法更改图像尺寸。但默认画布为1024 × 1024,对于静态图像来说已经足够。此查询容纳上下两个面板,并保持地图的宽高比。它在服务器上运行,并在 HTTP 响应中返回 PNG(before-after.sql):
1WITH2 toUInt32(4294967295* ((-128+180) /360)) AS x0,3 toUInt32(4294967295* ((-64+180) /360)) AS x1,4 toUInt32(4294967295* (1/2-log(tan((51+90) /360* pi())) /2/ pi())) AS y0,5 toUInt32(4294967295* (1/2-log(tan((22+90) /360* pi())) /2/ pi())) AS y1,6-- Two panels of 1024 x 512, one above the other; keep the aspect ratio of the map.7512/ (y1 - y0) AS scale,8 (1024- (x1 - x0) * scale) /2AS left_margin,9time>='2026-11-13 21:00:00'AS after, -- 14:00 ET goes to the top panel, 20:00 ET to the bottom one10 toInt32(left_margin + (mercator_x - x0) * scale) AS x,11 toInt32((mercator_y - y0) * scale) + after *512AS y,12 pow(least(count() /20, 1), 1/5) AS transparency13SELECT x, y,14 toUInt8((1+ transparency) /2* (1- least(avg(altitude), 50000) /50000) *255) AS r,15 toUInt8(transparency * greatest(0, least(avg(altitude), 5000)) /5000*255) AS g,16 toUInt8(greatest(0, least(avg(ground_speed), 700)) /700*255) AS b17FROM planes_mercator18WHERE ((time>='2026-11-13 19:00:00'ANDtime<'2026-11-13 19:10:00')19OR (time>='2026-11-14 01:00:00'ANDtime<'2026-11-14 01:10:00'))20AND mercator_x >= x0 AND mercator_x < x121AND mercator_y >= y0 AND mercator_y < y122GROUPBY x, y23FORMAT PNG;1curl 'https://kvzqttvc2n.eu-west-1.aws.clickhouse-staging.com/?user=website' --data-binary @before-after.sql > before-after.png它耗时10秒,并且除 ClickHouse 之外不运行任何代码。
复现它
所有内容都在此目录中:make.sh 运行这三个步骤。你需要 clickhouse(任何支持 FORMAT PNG 的近期版本;本示例使用26.10制作)、ffmpeg,以及带有 Pillow 的 Python 用于最终的调色板转换。
数据来源:© adsb.lol (ODbL v1.0),© airplanes.live,© adsbexchange.com。
阅读记录仅保存在当前浏览器,用于「为你」推荐。管理阅读记录
这篇内容对你有用吗?
反馈只用于改善内容筛选,不等同于收藏