在 openlayers 中使用矢量图层作为栅格源的输入

Using a vector layer as an input to a raster source in openlayers

我正在尝试使用矢量特征作为 Openlayers 5 中栅格源的掩码。(我想仅在特定管辖区内对栅格数据进行计算,用户可以动态选择这些管辖区。)要执行此操作,我尝试使用带有我的特征的矢量图层作为栅格源的输入,然后测试每个像素以查看是否存在遮罩。

documentation表示如果矢量图层配置了选项renderMode: 'image',这应该是可能的。但是,当栅格图层尝试 运行 我得到 TypeError: h.getImage is not a function.

我的代码如下:

// The base raster data
var arc = new TileArcGISRest({
    params: {
        'LAYERS': "view:0"
    },
    url: myDataURL,
    crossOrigin: 'anonymous',
});

// The vector mask in GeoJSON format
var town_vector = new ol.source.Vector({
    url: 'towns.json',
    format: new ol.format.GeoJSON(),
});

// The layer based on that vector
var town_layer = new ol.layer.Vector({
    title: 'added Layer',
    source: town_vector,
    renderMode: 'image',
    style: interest_style,
});

// The raster source that attempts the analysis
var summary_raster = new RasterSource({
    sources: [arc, town_layer],
    operation: function (pixels, data) {
        var pixel = pixels[0];
        var mask_pixel = pixels[1];
        if (mask_pixel[0] == mask_value) {
            run_calculation(pixel)
        }
    },
    lib: {
        run_calculation: run_calculation,
    }
});

当我从源中删除 "town_layer" 并消除屏蔽逻辑时,它工作得很好。但是,当我将 "town_layer" 添加到光栅源时,我得到了前面提到的 TypeError: h.getImage is not a function。如何 "rasterize" 这个矢量图层并将其用作栅格源的输入?

请检查 这是指 a link

它指出 ol.source.ImageVector 已从 v4.5.0 开始弃用,建议使用 ol.layer.Vector。它对我有用。(我用过 a openlayers v6.0)请尝试使用 v6.0 它可能对你也有用。