[OpenLayers-Dev] Layer.Vector: getFeatureBy and getFeaturesByAttribute

Marc Jansen jansen at terrestris.de
Thu Dec 9 12:16:41 EST 2010


Hi list,

instances of OpenLayers.Vector have a method getFeatureBy(property, 
value) that was introduced in revision 10691 [1] and lives in 2.10. This 
method returns the *first* feature that has the property set to the 
given value.

Wouldn't it make more sense to have a function getFeature*s*By(property, 
value) that returns an *array* of matching features?

AFAICT vector layers also do not provide a method to get features that 
have a certain attribute set to a given value. I propose a new method 
getFeaturesByAttribute(attrName, attrValue, strict) that would scan the 
attributes of features for matching candidates and eventually return 
those as an array (see [2] for a hardly tested implementation).

What do you think?

I have working code for both methods and would gladly share it (Although 
I would need to develop tests).

Regards and thanks for comments,

Marc


[1] 
http://trac.osgeo.org/openlayers/changeset/10691/trunk/openlayers/lib/OpenLayers/Layer/Vector.js

[2]
OpenLayers.Layer.Vector.prototype.getFeaturesByAttribute = function 
getFeaturesByAttribute(attrName, attrValue, strict) {
     var i,
         feature,
         doStrictComparison = !!(typeof strict !== 'undefined'),
         useAttrValue = !!(typeof attrValue !== 'undefined'),
         len = this.features.length,
         foundFeatures = [];
     for( i = 0; i < len; i++ ) {
         feature = this.features[i];
         if(feature && feature.attributes && typeof 
feature.attributes[attrName] !== 'undefined'){
             if (useAttrValue) {
                 if (doStrictComparison) {
                     if ( feature.attributes[attrName] === attrValue) {
                         foundFeatures.push(feature);
                     }
                 } else {
                     if ( feature.attributes[attrName] == attrValue) {
                         foundFeatures.push(feature);
                     }
                 }
             } else {
                 foundFeatures.push(feature);
             }
         }
     }
     return foundFeatures;
};
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-dev/attachments/20101209/8cb9e0fb/attachment.html


More information about the Dev mailing list