cachingopenlayersvector-tiles

Is there a way to avoid error Unimplemented type: 4 in vectorTiles?


I use openLayers 7.4.0 for layer processing. I find that I am passing layers that I was processing in WMS as rasters to vectorTiles.

I use the following code:

var WMSTileSource = new ol.source.TileWMS({
        url: layer.url,
        params: layerParams,
        tileGrid: tileGrid,
    });

var mvtVectorSource = new ol.source.VectorTile({
        url: undefined, // Url is set to undefined to use tileUrlFunction
        params: layerParams,
        format: new ol.format.MVT(),
        tileUrlFunction: function (tileCoord, pixelRatio, projection) {
            return WMSTileSource.tileUrlFunction(tileCoord, pixelRatio, projection); // Override to use TileWMS tileUrlFunction to load with {x}{y}{z} tile coordenates
        }
    });

layer.vectorLayer = new ol.layer.VectorTile({
        source: mvtVectorSource,
        style: function (feature) {
            return setVectorLayerStyle(feature);
        },
        zIndex: layer.index,
        opacity: getLocalLayerOpacity(layer.layerId) ?? layer.opacity,
        visible: true,
        renderBuffer: 0, // Avoid render overlaping features from near tiles
        declutter: true, // Setting declutter to true prevents features to get clipped on tile edges
        preload: Infinity
    }); 

The layers worked fine until I activated the cache in the GeoServer. When I zoom into an area of the map where there are no features the tile request returns:

java.lang.NullPointerException.

I understand that it is because the request instead of returning empty features returns a null. However, once this Tile is saved in the cache it returns the empty binary file.

But the problem is that if you try to load many Tiles where the error occurs, openLayers stops responding and doesn't make any more requests.

Do you know if there is anything I can do to fix it?

Thanks for your time.


Solution

  • For a quick fix without the need for a custom loader you could subclass the format to catch errors when reading features

    class MVT_with_catch extends MVT {
      readFeatures(source, options) {
        try {
          return super.readFeatures(source, options);
        } catch (e) {
          return [];
        }
      }
    }