[OpenLayers-Users] cluster strategy filtering

Tim Schaub tschaub at opengeo.org
Wed Nov 5 17:19:36 EST 2008


Hey-

Didrik Pinte wrote:
> On Wed, 2008-11-05 at 11:25 +0100, Didrik Pinte wrote:
>> Hi,
>>
>> How is it possible to a Filter.Comparison on clustered layer based on
>> the feature attributes ? Looking through the code, it seems impossible
>> because the cluster strategy hiddes the feature attributes by the
>> cluster attribute (the count property). Would there be an easy and clean
>> solution to this problem ?
>>
>> Didrik
> 
> Here is a not really elegant solution.
> 
> When a cluster counts more than one feature, the only attribute
> interesting is count. When the cluster counts only one attribute, we
> could be interested by the feature attributes. Thus, when the cluster is
> created, I just copy the feature attributes to the cluster. There is
> only one line of code added (extending cluster.attributes using
> feature.attributes)

My suggestion to a similar inquiry [1] was to add a threshold property 
to the cluster strategy.  If a cluster contains fewer features than the 
threshold, the original features are added to the layer instead of a 
cluster.

You can find a ticket with a patch for this here:
http://trac.openlayers.org/ticket/1815

Note that you can also listen for "featureadded" on the layer and 
manipulate the cluster to your heart's content.

layer.events.on({
     "featureadded": function(event) {
         var feature = event.feature;
         if(feature.cluster) {
	    feature.attributes.foo = "I'm a cluster";
         }
     }
});

Tim

[1] http://n2.nabble.com/Polygon-Cluster-td1382713.html

> 
> OpenLayers/Strategy/Cluster.js - line 250
> --------------------------------------------------------------------------------
>     createCluster: function(feature) {
>         var center = feature.geometry.getBounds().getCenterLonLat();
>         var cluster = new OpenLayers.Feature.Vector(
>             new OpenLayers.Geometry.Point(center.lon, center.lat),
>             {count: 1}
>         );
>         OpenLayers.Util.extend(cluster.attributes, feature.attributes);
>         cluster.cluster = [feature];
>         return cluster;
>     },
> --------------------------------------------------------------------------------
> 
> The only problem could be when the feature already has a count
> attribute ...
> 
> Didrik
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users


-- 
Tim Schaub
OpenGeo - http://opengeo.org
Expert service straight from the developers.



More information about the Users mailing list