diff --git a/externs/olx.js b/externs/olx.js index a47e678e71..b0841651ba 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -1981,9 +1981,9 @@ olx.format.MVTOptions; * {@link ol.Feature} to get full editing and geometry support at the cost of * decreased rendering performance. The default is {@link ol.render.Feature}, * which is optimized for rendering and hit detection. - * @type {undefined|function((ol.geom.Geometry|Object.)=)| + * @type {undefined|function((ol.geom.Geometry|Object.)=)| * function(ol.geom.GeometryType,Array., - * (Array.|Array.>),Object.)} + * (Array.|Array.>),Object.,number)} * @api */ olx.format.MVTOptions.prototype.featureClass; diff --git a/src/ol/format/mvt.js b/src/ol/format/mvt.js index 9745c59bf4..599f65f895 100644 --- a/src/ol/format/mvt.js +++ b/src/ol/format/mvt.js @@ -44,9 +44,9 @@ ol.format.MVT = function(opt_options) { /** * @private - * @type {function((ol.geom.Geometry|Object.)=)| + * @type {function((ol.geom.Geometry|Object.)=)| * function(ol.geom.GeometryType,Array., - * (Array.|Array.>),Object.)} + * (Array.|Array.>),Object.,number)} */ this.featureClass_ = options.featureClass ? options.featureClass : ol.render.Feature; @@ -137,8 +137,9 @@ ol.format.MVT.prototype.readRenderFeature_ = function(rawFeature, layer) { var values = rawFeature.properties; values[this.layerName_] = layer; + var id = rawFeature.id; - return new this.featureClass_(geometryType, flatCoordinates, ends, values); + return new this.featureClass_(geometryType, flatCoordinates, ends, values, id); }; diff --git a/src/ol/render/feature.js b/src/ol/render/feature.js index d056ab7f57..497e801cc6 100644 --- a/src/ol/render/feature.js +++ b/src/ol/render/feature.js @@ -16,14 +16,21 @@ goog.require('ol.geom.GeometryType'); * to be right-handed for polygons. * @param {Array.|Array.>} ends Ends or Endss. * @param {Object.} properties Properties. + * @param {number|string|undefined} id Feature id. */ -ol.render.Feature = function(type, flatCoordinates, ends, properties) { +ol.render.Feature = function(type, flatCoordinates, ends, properties, id) { /** * @private * @type {ol.Extent|undefined} */ this.extent_; + /** + * @private + * @type {number|string|undefined} + */ + this.id_ = id; + /** * @private * @type {ol.geom.GeometryType} @@ -85,6 +92,16 @@ ol.render.Feature.prototype.getExtent = function() { return this.extent_; }; +/** + * Get the feature identifier. This is a stable identifier for the feature and + * is set when reading data from a remote source. + * @return {number|string|undefined} Id. + * @api + */ +ol.render.Feature.prototype.getId = function() { + return this.id_; +}; + /** * @return {Array.} Flat coordinates. diff --git a/test/spec/ol/format/mvt.test.js b/test/spec/ol/format/mvt.test.js index ded61e1efe..1c441c250a 100644 --- a/test/spec/ol/format/mvt.test.js +++ b/test/spec/ol/format/mvt.test.js @@ -73,12 +73,19 @@ where('ArrayBuffer.isView').describe('ol.format.MVT', function() { }); it('parses id property', function() { + // ol.Feature var format = new ol.format.MVT({ featureClass: ol.Feature, layers: ['building'] }); var features = format.readFeatures(data); expect(features[0].getId()).to.be(2); + // ol.render.Feature + format = new ol.format.MVT({ + layers: ['building'] + }); + features = format.readFeatures(data); + expect(features[0].getId()).to.be(2); }); }); diff --git a/test/spec/ol/render/feature.test.js b/test/spec/ol/render/feature.test.js index 496989b24b..09df09e3ab 100644 --- a/test/spec/ol/render/feature.test.js +++ b/test/spec/ol/render/feature.test.js @@ -14,7 +14,7 @@ describe('ol.render.Feature', function() { describe('Constructor', function() { it('creates an instance', function() { renderFeature = - new ol.render.Feature(type, flatCoordinates, ends, properties); + new ol.render.Feature(type, flatCoordinates, ends, properties, 'foo'); expect(renderFeature).to.be.a(ol.render.Feature); }); }); @@ -57,6 +57,12 @@ describe('ol.render.Feature', function() { }); }); + describe('#getId()', function() { + it('returns the feature id', function() { + expect(renderFeature.getId()).to.be('foo'); + }); + }); + describe('#getProperties()', function() { it('returns the properties it was created with', function() { expect(renderFeature.getProperties()).to.equal(properties);