Protomaps: Self-hosting map tiles has gotten cheap and easy

Protomaps: Self-hosting map tiles has gotten cheap and easy

PMTiles is a tile format which works via HTTP Range requests and can be deployed as a single file to a storage bucket.

PMTiles 是一种通过 HTTP Range 请求工作的切片格式,可以作为单个文件部署到存储桶中。

I came across Protomaps in research for my latest project, Mapboard. For most non-commercial projects, commercial APIs become prohibitively expensive if you ever go viral (Mapbox, for instance, charges $5 per 1k map loads once you’ve exhausted the free tier).

我在研究我的最新项目 Mapboard 时发现了 Protomaps。对于大多数非商业项目而言,一旦你的项目走红,商业 API 的费用就会变得极其昂贵(例如,Mapbox 在你用完免费额度后,每 1000 次地图加载收费 5 美元)。

PMTiles, part of the Protomaps ecosystem, is a tile format that ships in a single file, accessible via Range requests, and is thus extremely portable and easy to deploy. For CDNs that support caching Range requests of large files, it requires no effort besides down- and uploading it to storage. For others, like Cloudflare, you can utilize an edge function to translate between the traditional x/y/z URL path and Range requests.

作为 Protomaps 生态系统的一部分,PMTiles 是一种以单个文件形式分发的切片格式,可通过 Range 请求访问,因此具有极高的可移植性和易部署性。对于支持缓存大文件 Range 请求的 CDN,除了下载并上传到存储空间外,无需任何额外工作。对于其他 CDN(如 Cloudflare),你可以利用边缘函数在传统的 x/y/z URL 路径和 Range 请求之间进行转换。

If all you want is the world map, getting it is as simple as going to the build page and downloading the latest daily build.

如果你只需要世界地图,获取它的方法非常简单:只需前往构建页面并下载最新的每日构建版本即可。

However, more fun can be had with the CLI. For example, you can extract data for only a bounding box or specific zoom levels, to get a significantly smaller file.

然而,使用命令行界面(CLI)会更有趣。例如,你可以仅提取特定边界框或特定缩放级别的数据,从而获得体积小得多的文件。

One great CDN for deploying this is Bunny.net (affiliate link). They support HTTP Range requests, don’t collect personal data by default, are headquartered in the EU, and offer low-cost access to a global network of CDN and edge storage servers.

部署此方案的一个绝佳 CDN 是 Bunny.net(含推广链接)。他们支持 HTTP Range 请求,默认不收集个人数据,总部位于欧盟,并提供对全球 CDN 和边缘存储服务器网络的高性价比访问。

Pricing varies based on region and replication factor of the edge storage. But generally you pay $0.01/GB ($0.005/GB on the smaller Volume network) for bandwidth to Europe and North America, and $0.01/GB/month (no replication) to $0.055/GB/month (maximum replication) for storage. A high degree of replication mostly matters for latency in case of a cache miss (as the data is further replicated when it’s cached at the CDN PoPs).

定价因地区和边缘存储的复制因子而异。但通常情况下,面向欧洲和北美的带宽费用为每 GB 0.01 美元(较小的 Volume 网络为每 GB 0.005 美元),存储费用则从每月每 GB 0.01 美元(无复制)到每月每 GB 0.055 美元(最大复制)不等。高程度的复制主要影响缓存未命中时的延迟(因为数据在 CDN 节点缓存时会被进一步复制)。

In practical terms, storing the 139GB world map with replication to most continents (5x) will run you about $5/month in storage fees. Loading a map of Europe on a laptop and zooming in on a city causes about 5MB of data to be transmitted, which would be $0.00005 — further navigations and reloads will be less expensive as more of the map gets cached locally. At $0.05/1k map loads that could be 100x cheaper than Mapbox. However, the actual bandwidth of course varies (less for people that bounce immediately, possibly tens of MB for heavy users). For Mapbox you only pay a flat fee per visit.

实际上,存储 139GB 的世界地图并复制到大多数大洲(5 倍复制),每月的存储费用约为 5 美元。在笔记本电脑上加载欧洲地图并放大到某个城市大约会传输 5MB 数据,费用约为 0.00005 美元——随着更多地图数据在本地缓存,后续的导航和重新加载会更便宜。按每 1000 次地图加载 0.05 美元计算,这可能比 Mapbox 便宜 100 倍。当然,实际带宽消耗会有所不同(立即离开的用户消耗较少,重度用户可能消耗数十 MB)。而 Mapbox 则是按访问次数收取固定费用。

It’s a good idea to create a storage zone with S3 compatibility enabled, as uploading via the HTTP API timed out after 15 minutes for me.

建议创建一个启用了 S3 兼容性的存储区域,因为我通过 HTTP API 上传时在 15 分钟后超时了。

Then, with rclone installed, you can download and stream the file to the Bunny storage zone. Preferably in tmux or from a temporary VPS, if your connection isn’t great.

安装 rclone 后,你可以下载并将文件流式传输到 Bunny 存储区域。如果你的网络连接不太稳定,最好在 tmux 中或通过临时 VPS 进行操作。

Theoretically, at this point you just need to create a pull zone, select the storage zone as an origin, and you’re done. Practically, it would be sensible to change some settings.

理论上,此时你只需要创建一个拉取区域(pull zone),选择该存储区域作为源站,就大功告成了。但在实际操作中,调整一些设置会更明智。

Allowed CORS origins can be configured under Security > General > Allowed referrers, so that other websites cannot embed your tiles.

可以在“安全 > 常规 > 允许的引用来源”(Security > General > Allowed referrers)下配置允许的 CORS 来源,以防止其他网站嵌入你的切片。

You’ll also want to verify under Caching that “Optimize for large object delivery” is enabled, as this makes it possible to cache Range requests.

你还需要在“缓存”(Caching)选项下确认是否启用了“针对大对象交付进行优化”(Optimize for large object delivery),因为这使得缓存 Range 请求成为可能。

You might also want to dissuade people from downloading the whole file and leaving you with a large CDN bill. The most practical way I’ve found for this is limiting the download speed to 10kB/s after 10MB transmitted in a single request (using the “Download speed limits” and “Limit after” settings under “Network limits”). Regular usage should never reach this amount. You may further want to introduce a requests/second limit (do note that legitimate usage can also quickly reach high numbers here) or a monthly bandwidth limit for the pull zone.

你可能还想阻止他人下载整个文件,从而避免产生高额的 CDN 账单。我发现最实用的方法是在单次请求传输 10MB 后,将下载速度限制为 10kB/s(使用“网络限制”下的“下载速度限制”和“限制阈值”设置)。正常使用绝不会达到这个量级。你还可以进一步设置每秒请求数限制(请注意,合法使用也可能很快达到高数值)或为拉取区域设置每月带宽上限。

With the tiles deployed, the only thing left to do is to embed them in your website via MapLibre GL, for example. To do this, you’ll need to install the pmtiles npm package and add the protocol and a style to MapLibre as detailed in the official installation guide.

切片部署完成后,剩下的工作就是将其嵌入到你的网站中,例如通过 MapLibre GL。为此,你需要安装 pmtiles npm 包,并按照官方安装指南中的详细说明,将协议和样式添加到 MapLibre 中。

Congratulations, you now host your own cost-effective and highly available map of the world!

恭喜你,现在你拥有了属于自己的、高性价比且高可用的世界地图!