Based on the sea height eample from ol3 and mapbox global terrain we made a similar setup putting elevation values into tiles and setting up with 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
});
now -
1) is there any smart way to when having mouse over to query the pixel value to show tooltip for the elevation?
2) is there a smart way to reuse already loaded tiles if wanting toquery the heights following a linestring or something?
We do not render the layer and the following code was what I ended up using. Skipped the raster layers that was manipulating the elevation source.
If improving on this, i would add a LRU cache on the tile cache, maybe its possible to hook into ols tile cache.
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(',')}]`);
}
});
If you also render the contents from the raster source as layer, there is an easier way to get pixel data - using Map#forEachLayerAtPixel
. Something like this:
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;
});
});