Ticket #1974 (new feature)

Opened 4 years ago

Last modified 4 years ago

Add a decluster function to the deactivate function of the cluster strategy

Reported by: vdeparday Owned by: tschaub
Priority: minor Milestone: 2.13 Release
Component: Strategy.Cluster Version: 2.7
Keywords: Cc:
State:

Description

I want to activate and deactivate the cluster strategy. When I deactivate the cluster strategy I have to reload the data from the server to decluster the feature. However, the features are still present in the clustered layer so that there is actually no need to reload the data. So, I think a function to decluster the features should be added when the strategy is deactivate.

Attachments

clusterOnActivate.patch Download (3.8 KB) - added by vdeparday 4 years ago.
patch for clusterOnActivate (including the threshold patch)

Change History

Changed 4 years ago by tschaub

The strategy already has a reference to the original features (strategy.features). In the deactivate sequence, if clearCache is not called, those features will still be around after the strategy is deactivated.

Instead of always doing this (load originals on deactivate), this could be made an option. An analog would be to cluster on activate.

Changed 4 years ago by vdeparday

So I added a boolean: clusterOnActivate and then I just moved the clearcache() after the unregistration of the events and added two lines of code in the deactivate function:

if (this.clusterOnActivate) {

this.layer.removeFeatures(this.clusters); this.layer.addFeatures(this.features);

} this.clearCache();

This works fine.

So I tried to do the cluster on activate and I had less luck with it. The clustering is triggered on the 'beforefeaturesadded'. So as the start I tried to fake the event with:

var event = {features: vectors.features}; vectors.events.triggerEvent("beforefeaturesadded", event);

The first activate it's fine but then the deactivate (as above) doesn't work because the cache is empty (this.features) . I've found it's because the this.layer.destroyFeatures(); on line 165 destroys it.

Would you have any hint on how to do this cluster on activate? Does my deactivate looks ok?

Changed 4 years ago by vdeparday

patch for clusterOnActivate (including the threshold patch)

Changed 4 years ago by vdeparday

So, I ended up caching the features with a .slice() instead of a simple copy so that it's not destroyed when this.layer.destroyFeatures() is called. Indeed, I think that when calling the this.cacheFeatures() directly, the features are passed by reference so they are destroyed by this.layer.destroyFeatures() which didn't happen before when triggered by the event. I'm not sure if it's the best solution?

I see one more issue. It's when you activate the layer and it's already activated the cache contains cluster so I think I need to add a condition in the if of the activation.

Note: See TracTickets for help on using tickets.