1.0.0 主要版本(请参阅发行说明)用 topojson.feature 替换了 topojson.object 函数,以获得更好的 GeoJSON 兼容性。
在以前版本的 TopoJSON 中,topojson.object 返回一个几何对象(可能是一个几何集合),与 TopoJSON 中几何对象的表示方式一致Topology。然而,与 GeoJSON 几何图形不同,TopoJSON 几何图形更像是特征,并且可以具有 id 和属性;同样,空几何图形被表示为空类型。
从 1.0.0 版本开始,topojson.feature替换 topojson.object,返回一个Feature或FeatureCollection,与转换为TopoJSON之前几何图形最初在GeoJSON中的表示方式一致。 (与在 GeoJSON 中一样,空几何图形表示为具有空几何对象的要素。)如中所述#37,这提供了与GeoJSON 规范以及处理 GeoJSON 的库。
要升级代码,您可以将 topojson.object 替换为 topojson.feature。然而,必须更改假设 topojson.object 返回几何图形的代码以处理该要素(或要素集合)现在由 topojson.feature 返回。例如,在 1.0 之前,如果您说:
svg.selectAll("path")
.data(topojson.object(topology, topology.objects.states).geometries)
.enter().append("path")
.attr("d", path);
在1.0及以后版本,对应的代码为:
svg.selectAll("path")
.data(topojson.feature(topology, topology.objects.states).features)
.enter().append("path")
.attr("d", path);
同样,如果您在 1.0 之前迭代点几何数组,您可能会说:
topojson.object(topology, topology.objects.points).geometries.forEach(function(point) {
console.log("x, y", point.coordinates[0], point.coordinates[1]);
});
在1.0及以后版本,对应的代码为:
topojson.feature(topology, topology.objects.points).features.forEach(function(point) {
console.log("x, y", point.geometry.coordinates[0], point.geometry.coordinates[1]);
});