首页 > 解决方案 > OpenLayers:查询 TileImage 源中的像素而不在地图上显示图层

问题描述

在 OpenLayers 4.6.5 中,我试图从 TileImage 源中读取像素值,而没有实际将源渲染为地图上的图层。我一直在尝试对这个问题的一些代码进行如下调整 - 请注意,在这个阶段,我只需要检查图块是否存在,还没有到实际查询图块的地步:

window['tiles'] = {};

window["L8SourceNRG"] = new ol.source.TileImage({
    url: NRGtileURL,
    crossOrigin: 'anonymous',
    transition: 0,
    preload: Infinity,
    tileGrid: L8SourceTileGrid
});

window["L8SourceNRG"].on("tileloadend", (e) => {
    console.log('tileloadend event fired');
    let coord = e.tile.getTileCoord();
    window['tiles'][coord.join('-')] = e.tile.getImage();
});

map.on('pointermove', (evt) => {
    if (evt.dragging) {
        return;
    }

    let coordinate = ol.proj.transform(evt.coordinate, map.getView().getProjection(), ol.proj.get('EPSG:3857'));

    let tileCoord = L8SourceTileGrid.getTileCoordForCoordAndResolution(coordinate, this.map.getView().getResolution());
    let key = tileCoord.join('-');
    if (key in window['tiles']) {
        console.log('key in tiles');
    }
});

单独使用上面的代码,控制台日志消息“key in tiles”永远不会出现。如果我已经将“L8SourceNRG”源加载到图层中并在地图上渲染,我只能显示它。但我不想在地图上显示它——我只想使用像素值来运行一些计算,这些计算将用于创建一个新的多边形图层。

如您所见,在定义该源时,我还尝试使用“预加载”设置,但这似乎没有任何区别——尽管我很有可能做错了。

是否可以更改我的代码以使其工作 - 或者是否有更好的方法从 TileImage 源读取像素值而不将源渲染为地图上的图层?

标签: openlayers

解决方案


推荐阅读