Leaflet.VectorGrid
Leaflet.VectorGrid copied to clipboard
howto: global style update
I'm trying to update styles for an entire pbfLayer without reloading the tiles. Use-case is that a tile layer has many attributes, and I may style by one, and then switch to another...
I'm initially setting the style with this function (which works):
function getStyle(layermeta) {
var max = Math.min(layermeta.avg + (layermeta.std * 2), layermeta.max);
var min = Math.max(layermeta.avg - (layermeta.std * 2), layermeta.min);
return {
statistics: function(properties) {
return {
weight: 0,
fillColor: getColor(properties, layermeta.field, max, min),
fillOpacity: 1,
fill: true
}
}
};
}
I'd like to be able to pbfLayer.updateStyle({newstyle}) and define the style with a different layermeta.field which contains different values for the same polygon...
It seems like this should alow me to do this, as it is included in the polygon layer, but calling pbfLayer.updateStyle() is not found.
@IvanSanchez any chance you could get some eyes on this? If it is possible, and I just mis-read the source - great, but if it isn't (yet) supported, I'd really appreciate some pointers on how you think I should approach the function, so I can try to develop it myself. Cheers!
This is currently not possible, at least not without some hacking.
The problem is that by default VectorGrid will not save the loaded features, so there's simply no way to find the feature properties and recalculate the styles.
However, if the getFeatureId option is specified, features will be saved, and stored in each tile (renderer). We could make a setVectorTileLayerStyles (or maybe just setStyles) that updates the vectorTileLayerStyles option, and if the features are available, also loops over loaded tiles and features and updates them.
The method linked by @abenrob above (updateStyle) is a part of the feature layers, not the VectorGrid layer itself. That means, it can only be used under the circumstances I describe above.
See issue #43 in which I demonstrate that one may override the options.vectorTileLayerStyles.layername with a new object or function, then redraw() the layer. This works just beautifully.
The only improvement I would want to see, is a soft redraw which would reapply styles without reloading tiles.