2017-02-14 108 views
0

基于ol3和mapbox全球地形的海面高度,我们做了一个类似的设置,将高程值放入图块中,并使用ol.source.raster是否有可能从鼠标上的ol.source.raster获取像素值

  var elevation = new ol.source.TileImage({ 
       url: penetrationUrls[this.designator.toLowerCase()], 
       projection: newProj,// "EPSG:27700", 
       crossOrigin: 'anonymous', 
       tileGrid: tilegrid 
      }); 
      var raster = new ol.source.Raster({ 
       sources: [elevation], 
       operation: penetrates 
      }); 

现在 -

1)是否有在具有鼠标时要查询的像素值,以显示工具提示海拔任何聪明的办法? 2)是否有一个聪明的方式来重用已经加载的瓷砖,如果想要查询一个线串或什么东西的高度?

回答

1

我们不渲染图层,下面的代码就是我最终使用的。跳过正在操作高程源的栅格图层。

如果对此进行改进,我会在tile缓存中添加一个LRU缓存,也许它可能会挂接到ols缓存中。

  var elevation = new ol.source.TileImage({ 
       url: options.template, 
       projection: elevationGridProjection, 
       crossOrigin: 'anonymous', 
       tileGrid: tilegrid 
      }); 

      let tiles: { [key: string]: HTMLImageElement } = {}; 
      elevation.on("tileloadend", (e) => { 
       let coord = e.tile.getTileCoord(); 
       tiles[coord.join('-')] = e.tile.getImage(); 

      }); 

      this.map.on('pointermove', (evt) => { 
       // When user was dragging map, then coordinates didn't change and there's 
       // no need to continue 
       if (evt.dragging) { 
        return; 
       } 



       let coordinate = ol.proj.transform(evt.coordinate, this.map.getView().getProjection(), elevationGridProjection); 

       let tileCoord = tilegrid.getTileCoordForCoordAndResolution(coordinate, this.map.getView().getResolution()); 
       let key = tileCoord.join('-'); 
       if (key in tiles) { 

        let origin = tilegrid.getOrigin(tileCoord[0]); 
        let res = tilegrid.getResolution(tileCoord[0]); 
        let tileSize = tilegrid.getTileSize(tileCoord[0]); 
        let w = Math.floor(((coordinate[0] - origin[0])/res) % (tileSize[0] | tileSize as number)); 
        let h = Math.floor(((origin[1] - coordinate[1])/res) % (tileSize[1] | tileSize as number)); 

        var canvas = document.createElement("canvas"); 
        canvas.width = tiles[key].width; 
        canvas.height = tiles[key].height; 

        // Copy the image contents to the canvas 
        var ctx = canvas.getContext("2d"); 
        ctx.drawImage(tiles[key], 0, 0); 

        let img = ctx.getImageData(0, 0, canvas.width, canvas.height); 
        let imgData = img.data; 
        let index = (w + h * 256) * 4; 
        let pixel = [imgData[index + 0], imgData[index + 1], imgData[index + 2], imgData[index + 3]]; 
        let height = (-10000 + ((pixel[0] * 256 * 256 + pixel[1] * 256 + pixel[2]) * 0.01)) 

        console.log(`HEIGHT: ${height}, ${w},${h},${img.width}, ${img.height},${img.data.length} ,${index}, [${pixel.join(',')}]`); 

       } 

      }); 
0

如果您也从栅格源作为层渲染的内容,还有一个更简单的方式来获得的像素数据 - 使用Map#forEachLayerAtPixel。这样的事情:

map.on('pointermove', function(evt) { 
    map.forEachLayerAtPixel(evt.pixel, function(layer, pixel) { 
    let height = (-10000 + ((pixel[0] * 256 * 256 + pixel[1] * 256 + pixel[2]) * 0.01)); 
    console.log(height); 
    }, undefined, function(layer) { 
    return layer.getSource() == raster; 
    }); 
}); 
+0

谢谢:)不知道像素。我们可以将高程图层添加到地图并隐藏它。我更新了我的答案,提供了更详细的解决方案,允许更多的自由,但可以使用它来代替。 –

+0

当您不使用该源渲染图层时,此方法不起作用 - 并且该图层也可能不会隐藏。 – ahocevar

+0

这也是我的理解你的答案 - 我最终使用我发布的答案作为示例。考虑把它打包成可重用的东西。 –

相关问题