ghybs / Leaflet.TileLayer.Fallback

Replaces missing Tiles by scaled lower zoom Tiles
Apache License 2.0
36 stars 18 forks source link

Fix out-of-range tile indices for tms #14

Open m314 opened 5 years ago

m314 commented 5 years ago

Closes #13.

ghybs commented 5 years ago

Hi,

Thank you so much for this PR! :+1: Sorry for the delay in reviewing it...

Thinking further about it, I do not know how computation intensive are the calls to map.getPixelWorldBounds and this._pxBoundsToTileRange, but it looks like a waste calling them for every tile with getTileUrl, whereas in Leaflet GridLayer they are called only once per view update.

As you pinpointed it from the beginning, we actually need to call them when the zoom changes, i.e. in the case of this plugin, when it tries requesting tiles from a lower zoom to replace a missing tile.

Therefore it seems to me that we would basically need instead to compute them once for each zoom, and possibly cache their result in a dictionary {[zoom]: maxY}. I suspect the result is dependent only on the zoom value (to be checked).

ecsdavidt commented 4 years ago

I missed this issue/pr, the options.tms bug threw me for a loop for a bit, and I came up with my own approach, though it wouldn't surprise me if it's naïve in ways I don't understand. If the tile range is fixed by zoom level, then the calculation is simple:

    if (this.options.tms) {
        var zDifference = this._getZoomForUrl() - coords.z;
        tmsY = ((this._globalTileRange.max.y + 1) / Math.pow(2, zDifference)) - 1 - coords.y;
    }