diff --git a/externs/topojson.js b/externs/topojson.js index 39a2d512d2..80309c7807 100644 --- a/externs/topojson.js +++ b/externs/topojson.js @@ -26,7 +26,7 @@ TopoJSONTopology.prototype.transform; /** - * @type {Object.} + * @type {Object.} */ TopoJSONTopology.prototype.objects; @@ -76,6 +76,19 @@ TopoJSONGeometry.prototype.id; +/** + * @constructor + */ +var TopoJSONGeometryCollection = function() {}; + + +/** + * @type {Array.} + */ +TopoJSONGeometryCollection.prototype.geometries; + + + /** * @constructor * @extends {TopoJSONGeometry} diff --git a/src/ol/parser/topojson.js b/src/ol/parser/topojson.js index 88cfa7925c..f709ff5ba4 100644 --- a/src/ol/parser/topojson.js +++ b/src/ol/parser/topojson.js @@ -144,6 +144,31 @@ ol.parser.TopoJSON.prototype.readFeatureFromGeometry_ = function(object, arcs, }; +/** + * Create features from a TopoJSON GeometryCollection object. + * + * @param {TopoJSONGeometryCollection} collection TopoJSON GeometryCollection + * object. + * @param {Array.} arcs Array of arcs. + * @param {Array.} scale Scale for each dimension. + * @param {Array.} translate Translation for each dimension. + * @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options. + * @return {Array.} Array of features. + * @private + */ +ol.parser.TopoJSON.prototype.readFeaturesFromGeometryCollection_ = function( + collection, arcs, scale, translate, opt_options) { + var geometries = collection.geometries; + var num = geometries.length; + var features = new Array(num); + for (var i = 0; i < num; ++i) { + features[i] = this.readFeatureFromGeometry_(geometries[i], arcs, scale, + translate, opt_options); + } + return features; +}; + + /** * @param {TopoJSONTopology} topology TopoJSON object. * @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options. @@ -160,8 +185,15 @@ ol.parser.TopoJSON.prototype.readFeaturesFromTopology_ = function( var objects = topology.objects; var features = []; for (var key in objects) { - features.push(this.readFeatureFromGeometry_(objects[key], arcs, scale, - translate, opt_options)); + if (objects[key].type === 'GeometryCollection') { + features.push.apply(features, this.readFeaturesFromGeometryCollection_( + /** @type {TopoJSONGeometryCollection} */ (objects[key]), + arcs, scale, translate, opt_options)); + } else { + features.push(this.readFeatureFromGeometry_( + /** @type {TopoJSONGeometry} */ (objects[key]), + arcs, scale, translate, opt_options)); + } } return features; };