我們不渲染圖層,下面的代碼就是我最終使用的。跳過正在操作高程源的柵格圖層。
如果對此進行改進,我會在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(',')}]`);
}
});
謝謝:)不知道像素。我們可以將高程圖層添加到地圖並隱藏它。我更新了我的答案,提供了更詳細的解決方案,允許更多的自由,但可以使用它來代替。 –
當您不使用該源渲染圖層時,此方法不起作用 - 並且該圖層也可能不會隱藏。 – ahocevar
這也是我的理解你的答案 - 我最終使用我發佈的答案作爲示例。考慮把它打包成可重用的東西。 –