Merge pull request #12405 from ahocevar/vectortile-getsourcetiles

VectorTile getSourceTiles behaviour with loading source tiles
This commit is contained in:
Andreas Hocevar
2021-06-21 22:04:27 +02:00
committed by GitHub
3 changed files with 94 additions and 11 deletions

View File

@@ -18,7 +18,7 @@ import {
createXYZ,
extentFromProjection,
} from '../tilegrid.js';
import {fromKey, getKeyZXY} from '../tilecoord.js';
import {fromKey, getCacheKeyForTileKey, getKeyZXY} from '../tilecoord.js';
import {isEmpty} from '../obj.js';
import {loadFeaturesXhr} from '../featureloader.js';
import {toSize} from '../size.js';
@@ -237,8 +237,19 @@ class VectorTile extends UrlTile {
* @param {!Object<string, boolean>} usedTiles Used tiles.
*/
expireCache(projection, usedTiles) {
const tileCache = this.getTileCacheForProjection(projection);
const usedSourceTiles = Object.keys(usedTiles).reduce((acc, key) => {
const cacheKey = getCacheKeyForTileKey(key);
if (tileCache.containsKey(cacheKey)) {
const sourceTiles = tileCache.get(cacheKey).sourceTiles;
for (let i = 0, ii = sourceTiles.length; i < ii; ++i) {
acc[sourceTiles[i].getKey()] = true;
}
}
return acc;
}, {});
super.expireCache(projection, usedTiles);
this.sourceTileCache.expireCache({});
this.sourceTileCache.expireCache(usedSourceTiles);
}
/**
@@ -281,15 +292,7 @@ class VectorTile extends UrlTile {
);
tile.sourceTiles.push(sourceTile);
const sourceTileState = sourceTile.getState();
if (sourceTileState === TileState.IDLE) {
sourceTile.extent = sourceTileGrid.getTileCoordExtent(
sourceTileCoord
);
sourceTile.projection = projection;
sourceTile.resolution = sourceTileGrid.getResolution(
sourceTileCoord[0]
);
this.sourceTileCache.set(tileUrl, sourceTile);
if (sourceTileState < TileState.LOADED) {
const listenChange = (event) => {
this.handleTileChange(event);
const state = sourceTile.getState();
@@ -318,6 +321,16 @@ class VectorTile extends UrlTile {
};
sourceTile.addEventListener(EventType.CHANGE, listenChange);
tile.loadingSourceTiles++;
}
if (sourceTileState === TileState.IDLE) {
sourceTile.extent = sourceTileGrid.getTileCoordExtent(
sourceTileCoord
);
sourceTile.projection = projection;
sourceTile.resolution = sourceTileGrid.getResolution(
sourceTileCoord[0]
);
this.sourceTileCache.set(tileUrl, sourceTile);
sourceTile.load();
}
});
@@ -458,6 +471,9 @@ class VectorTile extends UrlTile {
*/
updateCacheSize(tileCount, projection) {
super.updateCacheSize(tileCount * 2, projection);
this.sourceTileCache.highWaterMark = this.getTileCacheForProjection(
projection
).highWaterMark;
}
}

View File

@@ -46,6 +46,19 @@ export function getKey(tileCoord) {
return getKeyZXY(tileCoord[0], tileCoord[1], tileCoord[2]);
}
/**
* Get the tile cache key for a tile key obtained through `tile.getKey()`.
* @param {string} tileKey The tile key.
* @return {string} The cache key.
*/
export function getCacheKeyForTileKey(tileKey) {
const [z, x, y] = tileKey
.substring(tileKey.lastIndexOf('/') + 1, tileKey.length)
.split(',')
.map(Number);
return getKeyZXY(z, x, y);
}
/**
* Get a tile coord given a key.
* @param {string} key The tile coord key.