我使用openLayers 7.4.0进行图层处理。 我发现我将在 WMS 中作为栅格处理的图层传递给了 vectorTiles。
我使用以下代码:
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
});
这些图层工作正常,直到我激活地理服务器中的缓存。当我放大地图上没有要素的区域时,图块请求返回:
java.lang.NullPointerException
.
我理解这是因为请求返回的是 null,而不是返回空特征。但是,一旦将此图块保存在缓存中,它就会返回空的二进制文件。
但问题是,如果您尝试加载许多发生错误的 Tiles,openLayers 将停止响应并且不再发出任何请求。
你知道我能做些什么来解决这个问题吗?
感谢您的宝贵时间。
在 @Mike 帮助我了解
tileLoadFunction()
函数的存在之后,我能够解决添加 tileLoadFunction()
的错误,如下所示:
const mvtVectorSource = new VectorTileSource({
url: undefined,
format: new MVT(),
tileLoadFunction: function (tile: any, url: string) {
tile.setLoader(function (extent: any, resolution: any, projection: any): void {
fetch(url)
.then(function (response) {
if (response.status === 200) {
return response.arrayBuffer();
} else {
throw new Error('Tile request failed with status: ' + response.status);
}
})
.then(function(data){
const responseText = new TextDecoder('utf-8').decode(data);
if (responseText.includes('java.lang.NullPointerException')) {
console.warn('Null tile detected, ignoring tile:', url);
return;
}
const format = tile.getFormat();
const features = format.readFeatures(data, {
extent: extent,
featureProjection: projection
});
tile.setFeatures(features);
})
.catch(function (error){
console.error('Error loading tile:', error);
});
});
},
tileUrlFunction: function (tileCoord: TileCoord, pixelRatio: number, projection: Projection) {
return WMSTileSource.tileUrlFunction(tileCoord, pixelRatio, projection);
}
});
tileLoadFunction
检查图块响应是否不为空,如果为空,则忽略它。