问题
I'am merging code, the code relying on v0 breaks on v1.
What are the syntaxes changes between topojson.v0.min.js and topojson.v1.min.js?*
--
List of suspect syntaxes:
- V0 > V1
- .object > .feature
- .geometries > .features (in some cases or always?)
- *.coordinates > *.geometry.coordinates
- others ?
回答1:
The 1.0.0 major release (see release notes) replaced the topojson.object function with topojson.feature for better GeoJSON compatibility.
In previous versions of TopoJSON, topojson.object returned a geometry object (which may be a geometry collection), consistent with how the geometry object is represented inside the TopoJSON Topology. However, unlike GeoJSON geometries, TopoJSON geometries are more like features, and could have an id and properties; likewise, null geometries were represented as a null type.
As of version 1.0.0, topojson.feature replaces topojson.object, returning a Feature or a FeatureCollection instead, consistent with how the geometry was originally represented in the GeoJSON, prior to conversion to TopoJSON. (As in GeoJSON, null geometries are represented as features with a null geometry object.) As discussed in #37, this offers greater compatibility with the GeoJSON specification and libraries that deal with GeoJSON.
To upgrade your code, you can replace topojson.object with topojson.feature. However, code that assumed that topojson.object returned a geometry must be changed to handle the feature (or feature collection) now returned by topojson.feature. For example, prior to 1.0, if you said:
svg.selectAll("path")
.data(topojson.object(topology, topology.objects.states).geometries)
.enter().append("path")
.attr("d", path);
In 1.0 and later, the corresponding code is:
svg.selectAll("path")
.data(topojson.feature(topology, topology.objects.states).features)
.enter().append("path")
.attr("d", path);
Likewise, if you were iterating over an array of point geometries, prior to 1.0, you might have said:
topojson.object(topology, topology.objects.points).geometries.forEach(function(point) {
console.log("x, y", point.coordinates[0], point.coordinates[1]);
});
In 1.0 and later, the corresponding code is:
topojson.feature(topology, topology.objects.points).features.forEach(function(point) {
console.log("x, y", point.geometry.coordinates[0], point.geometry.coordinates[1]);
});
来源:https://stackoverflow.com/questions/17404239/topojson-list-of-differences-between-v0-and-v1