add an optional argument to getAttributes so we can get a feature's attributes except for the geometry ones

This commit is contained in:
Bart van den Eijnden
2013-11-19 15:39:22 +01:00
parent 936f86568e
commit 8d03fa1197
4 changed files with 28 additions and 10 deletions

View File

@@ -68,17 +68,23 @@ goog.inherits(ol.Feature, ol.Object);
/** /**
* Gets a copy of the attributes of this feature. * Gets a copy of the attributes of this feature.
* @param {boolean=} opt_nonGeometry Don't include any geometry attributes
* (by default geometry attributes are returned).
* @return {Object.<string, *>} Attributes object. * @return {Object.<string, *>} Attributes object.
* @todo stability experimental * @todo stability experimental
*/ */
ol.Feature.prototype.getAttributes = function() { ol.Feature.prototype.getAttributes = function(opt_nonGeometry) {
var keys = this.getKeys(), var keys = this.getKeys(),
len = keys.length, len = keys.length,
attributes = {}, attributes = {},
i, key; i, key;
for (i = 0; i < len; ++ i) { for (i = 0; i < len; ++ i) {
key = keys[i]; key = keys[i];
attributes[key] = this.get(key); var value = this.get(key);
if (!goog.isDef(opt_nonGeometry) || opt_nonGeometry == false ||
(opt_nonGeometry === true && !(value instanceof ol.geom.Geometry))) {
attributes[key] = value;
}
} }
return attributes; return attributes;
}; };

View File

@@ -351,11 +351,7 @@ ol.parser.GeoJSON.prototype.encodeFeatureCollection_ = function(collection) {
*/ */
ol.parser.GeoJSON.prototype.encodeFeature_ = function(feature) { ol.parser.GeoJSON.prototype.encodeFeature_ = function(feature) {
var geometry = feature.getGeometry(), var geometry = feature.getGeometry(),
attributes = feature.getAttributes(); properties = feature.getAttributes(true);
var properties = goog.object.filter(attributes,
function(element, index, array) {
return !(element instanceof ol.geom.Geometry);
});
return /** @type {GeoJSONFeature} */({ return /** @type {GeoJSONFeature} */({
type: 'Feature', type: 'Feature',
properties: properties, properties: properties,

View File

@@ -422,11 +422,10 @@ ol.parser.ogc.GML = function(opt_options) {
this.writeNode('_geometry', feature.getGeometry(), this.featureNS, this.writeNode('_geometry', feature.getGeometry(), this.featureNS,
node); node);
} }
var attributes = feature.getAttributes(); var attributes = feature.getAttributes(true);
for (var name in attributes) { for (var name in attributes) {
var value = attributes[name]; var value = attributes[name];
if (goog.isDefAndNotNull(value) && !(value instanceof if (goog.isDefAndNotNull(value)) {
ol.geom.Geometry)) {
this.writeNode('_attribute', {name: name, value: value}, this.writeNode('_attribute', {name: name, value: value},
this.featureNS, node); this.featureNS, node);
} }

View File

@@ -78,6 +78,23 @@ describe('ol.Feature', function() {
expect(attributes.ten).to.be(10); expect(attributes.ten).to.be(10);
}); });
it('returns an object with all attributes except geometry', function() {
var point = new ol.geom.Point([15, 30]);
var feature = new ol.Feature({
foo: 'bar',
ten: 10,
loc: point
});
var attributes = feature.getAttributes(true);
var keys = goog.object.getKeys(attributes);
expect(keys.sort()).to.eql(['foo', 'ten']);
expect(attributes.foo).to.be('bar');
expect(attributes.ten).to.be(10);
});
}); });