TopoJSON 是 GeoJSON 的拓扑扩展。采用共享弧段编码(多边形之间的边界仅存储一次)和坐标的整数量化——生成的文件比等效的 GeoJSON 小 60-80%。是 D3.js、Mapshaper 和 Observable 的标准输入;非常适合交互式分级统计图和 Web 地图集。
主要特点
关于 TopoJSON 格式
TopoJSON was invented by Mike Bostock, the author of D3.js, to solve a simple problem: in GeoJSON neighboring polygons store their shared border twice. TopoJSON moves all the lines into a common array of arcs that the objects then refer to. On top of that, coordinates are rounded to an integer grid and written as increments — the file gets sharply lighter.
The format is taken for web graphics and analytics: choropleths in D3, interactive atlases, boundary maps in Observable, lightweight layers for offline applications. The second motive is simplification: during generalization the shared borders move together, and no gaps appear between neighboring districts.
The output is a single document: every layer becomes a separate object inside it — roads, buildings, water and so on — and object attributes are preserved. Coordinates are in WGS84, up to 100,000 objects per layer. The topology is built by our own code, with no external dependencies.
The price of compactness is the rounding of coordinates. The grid step depends on the size of the area: for a district it is on the order of a meter, for a country hundreds of meters. For a web map that is invisible; for precise measurements take GeoJSON or GeoPackage.
打开 TopoJSON 文件的程序
以下程序可以打开、编辑和处理从 OSM2CDR 导出的 TopoJSON (Topology-Aware JSON)(.topojson)文件:
格式规格
- 开放标准
- 仅 2D
- 无损(无质量损失)
- 存储要素属性
- 输出:1 个文件
- TopoJSON (open spec). Topologically encoded GeoJSON: shared borders stored once, file size 30-80% smaller. Used by D3.js.
谁在使用 TopoJSON 地图
如何导出 TopoJSON
导出后如何打开
- D3.js — include topojson-client and unpack the layer you need: topojson.feature(topology, topology.objects.buildings), then the usual d3.geoPath.
- Mapshaper — drag the file onto mapshaper.org: objects and attributes are visible, and simplification and export to SHP or GeoJSON are right there.
- QGIS — opens .topojson as an ordinary vector layer through GDAL; handy for checking the geometry with your own eyes.
- Converting to GeoJSON — topo2geo buildings=buildings.geojson < map.topojson from the topojson-client package, or ogr2ogr.
- Leaflet and MapLibre — they do not understand the format directly: unpack it into GeoJSON on the client with the topojson-client library and feed it on as usual.
TopoJSON 格式的热门地图
导出 TopoJSON (Topology-Aware JSON) 地图
从 OpenStreetMap 下载专业的 TopoJSON 地图。即时生成,任意区域。