openlayerscesiumjs

Cannot limit TileLayer with minZoom in ol-cesium


I have a map created with OpenLayers and ol-cesium.

I have created a tile layer that should only appear after a certain zoom level:

return new TileLayer({
    minZoom: minZoom,
    source: new XYZ({
        url: ...
    })
});

This works fine in 2D, but when I switch to 3D mode (Cesium), the layer shows in every zoom level.

Is there a way to get Cesium to follow the minimum zoom level for a tile layer?

Is this a limitation or bug in Cesium or in ol-cesium, or do I need to do anything differently?


Solution

  • Layer minZoom refers to view zoom level, not the tile grid zoom level. That can be demonstrated by setting a minZoom in a example such as https://openlayers.org/en/latest/examples/projection-and-scale.html - the layer may appear or disappear when changing the view to a different projection. 3d mode zooms may not relate an OpenLayers view. Instead you could restrict tile zoom using the source minZoom option (that could result in a huge number of minZoom level tiles being loaded when zoomed out) or prevent low zoom level tiles being loaded by discarding them in a tileUrlFunction:

    const defaultTileUrlFunction = source.getTileUrlFunction();
    source.setTileUrlFunction(([z, x, y]) =>
      z > minZoom ? defaultTileUrlFunction([z, x, y]) : undefined,
    );
    

    Instead of discarding with undefined you could use a transparent data url

    const canvas = document.createElement('canvas');
    canvas.width = 1;
    canvas.height = 1;
    const defaultUrl = canvas.toDataURL();
    const defaultTileUrlFunction = source.getTileUrlFunction();
    source.setTileUrlFunction(([z, x, y]) =>
      z > minZoom ? defaultTileUrlFunction([z, x, y]) : defaultUrl,
    );