Leaflet / Leaflet.VectorGrid

Display gridded vector data (sliced GeoJSON or protobuf vector tiles) in Leaflet 1.0.0
600 stars 194 forks source link

When tiles haven't finished loading, calling setFeatureStyle results in unstyled features #40

Closed lediur closed 7 years ago

lediur commented 7 years ago

Using latest master at 330acf2.

I want to bind some data to a Leaflet VectorGrid to make choropleths. However, I sometimes call setFeatureStyle on the vector grid layer before all the tiles have finished loading (for example, if the GeoJSON or processing time takes longer than loading data for the choropleths).

Before pull request #38, this would simply set the new features to the default style I defined when I created the VectorGrid layer.

After pull request #38, the new features become unstyled (Leaflet default style).

image

I think I've narrowed down the reason. In Leaflet.VectorGrid.js:

var styleOverride = this._overriddenStyles[id] = {}

for (var tileKey in this._vectorTiles) {
    var tile = this._vectorTiles[tileKey];
    var features = tile._features;
    var data = features[id];
    if (data) {
        styleOverride[data.layerName] = layerStyle;
// ...

Since the new tiles haven't loaded yet, this._vectorTiles may contain old tiles or be empty altogether. This means that calling setFeatureStyle with the new ids will not bind any overridden styles to this._overriddenStyles, but instead bind empty objects. When the tiles do load in, this causes the features to be unstyled.

I'm submitting a PR to hopefully address this (and one other quirk that I found). (edit: here is the PR - #42)

perliedman commented 7 years ago

Closed by merging #42.