Interpolating Contours 来自 a DEM
Interpolating Contours 来自 a DEM示例。 image-interpolation contours 栅格 mouse-position mapzen
本示例展示了在 WebGL 中使用栅格 DEM(数字高程模型)数据进行数据重采样的方法。 示例使用了带 gutter(边距)的 Mapzen Terrarium 瓦片,从而支持跨瓦片边界的插值。 通过加载九个相邻瓦片生成一个带缓冲区的瓦片。 数据插值通过计算像素 RGB 波段所表示的值来完成 (使用 Float32 数据瓦片以提高移动设备上的精度), 插值后的值再以 RGB 的形式写回样式,以便 ol/source/Raster 可以读取。 一个栅格数据源用于在相邻插值像素跨越等高线间距时绘制等高线。 另一个不可见的栅格数据源则让自定义的鼠标位置控件能够 基于渲染出的像素显示插值后的高程值。
DEM 数据源的最大缩放级别为 15,而视图和背景图层可以继续放大到更高的级别。 原始数据源的数据值一并列出以便对比。请注意,尤其是在陡峭的地形上, 插值后的数值和等高线变化是平滑的,而原始数据值则呈阶梯状变化。
main.js
js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import MousePosition from 'ol/control/MousePosition.js';
import {toStringHDMS} from 'ol/coordinate.js';
import ImageLayer from 'ol/layer/Image.js';
import TileLayer from 'ol/layer/WebGLTile.js';
import {fromLonLat, toLonLat} from 'ol/proj.js';
import DataTile from 'ol/source/DataTile.js';
import OSM from 'ol/source/OSM.js';
import RasterSource from 'ol/source/Raster.js';
const attribution =
'<a href="https://github.com/tilezen/joerd/blob/master/docs/attribution.md" target="_blank">Data sources and attribution</a>';
const calculateElevation = function (pixel) {
if (pixel[3]) {
return -32768 + (pixel[0] * 256 + pixel[1] + pixel[2] / 256);
}
};
// 在支持的设备上使用 Float32 插值,以在移动设备上获得最佳效果。
const calculateElevationFromData = function (pixel) {
return pixel[0];
};
const elevation = ['band', 1];
const tileSize = 256;
const gutter = 1;
const canvas = document.createElement('canvas');
canvas.width = tileSize * 3;
canvas.height = tileSize * 3;
const context = canvas.getContext('2d', {willReadFrequently: true});
const source = new DataTile({
attributions: attribution,
tileSize: tileSize,
gutter: gutter,
maxZoom: 15,
interpolate: true,
wrapX: true,
loader: (z, x, y) => {
const promises = [];
for (let i = 0; i < 3; ++i) {
for (let j = 0; j < 3; ++j) {
promises.push(
new Promise((resolve, reject) => {
const maxY = 2 ** z;
const yy = y + j - 1;
if (yy < 0 || yy >= maxY) {
return resolve();
}
const maxX = 2 ** z;
const xx = (((x + i - 1) % maxX) + maxX) % maxX;
const image = new Image();
image.crossOrigin = '';
image.addEventListener('error', () => reject());
image.addEventListener('load', () => resolve(image));
image.src = `https://s3.amazonaws.com/elevation-tiles-prod/terrarium/${z}/${xx}/${yy}.png`;
}),
);
}
}
return Promise.all(promises).then((images) => {
context.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < 3; ++i) {
for (let j = 0; j < 3; ++j) {
const image = images.shift();
if (image) {
context.drawImage(image, i * tileSize, j * tileSize);
}
}
}
const data = context.getImageData(
tileSize - gutter,
tileSize - gutter,
tileSize + 2 * gutter,
tileSize + 2 * gutter,
).data;
const pixels = data.length / 4;
const floatData = new Float32Array(data.buffer);
for (let i = 0, j = 0; i < pixels;) {
floatData[i++] = calculateElevation(data.slice(j, (j += 4)));
}
return floatData;
});
},
});
const pixelValue = ['*', ['+', elevation, 32768], 256];
const style = {
color: [
'array',
['/', ['floor', ['/', pixelValue, 256 * 256]], 255],
['/', ['floor', ['/', ['%', pixelValue, 256 * 256], 256]], 255],
['/', ['%', pixelValue, 256], 255],
1,
],
};
/// 复制图层:一个图层被两个栅格数据源共享会导致渲染问题
const elevation1 = new TileLayer({
source: source,
style: style,
});
const elevation2 = new TileLayer({
source: source,
style: style,
});
const contours = function (inputs, data) {
const elevationImage = inputs[0];
const width = elevationImage.width;
const height = elevationImage.height;
const elevationData = elevationImage.data;
const pixel = [0, 0, 0, 0];
const contourData = new Uint8ClampedArray(elevationData.length);
const interval = data.interval;
let offset, pixelY, pixelX;
for (pixelY = 0; pixelY < height; ++pixelY) {
for (pixelX = 0; pixelX < width; ++pixelX) {
offset = (pixelY * width + Math.max(pixelX - 1, 0)) * 4;
pixel[0] = elevationData[offset];
pixel[1] = elevationData[offset + 1];
pixel[2] = elevationData[offset + 2];
pixel[3] = elevationData[offset + 3];
const leftElevation = calculateElevation(pixel);
offset = (pixelY * width + Math.min(pixelX + 1, width - 1)) * 4;
pixel[0] = elevationData[offset];
pixel[1] = elevationData[offset + 1];
pixel[2] = elevationData[offset + 2];
pixel[3] = elevationData[offset + 3];
const rightElevation = calculateElevation(pixel);
offset = (Math.max(pixelY - 1, 0) * width + pixelX) * 4;
pixel[0] = elevationData[offset];
pixel[1] = elevationData[offset + 1];
pixel[2] = elevationData[offset + 2];
pixel[3] = elevationData[offset + 3];
const topElevation = calculateElevation(pixel);
offset = (Math.min(pixelY + 1, height - 1) * width + pixelX) * 4;
pixel[0] = elevationData[offset];
pixel[1] = elevationData[offset + 1];
pixel[2] = elevationData[offset + 2];
pixel[3] = elevationData[offset + 3];
const bottomElevation = calculateElevation(pixel);
offset = (pixelY * width + pixelX) * 4;
pixel[0] = elevationData[offset];
pixel[1] = elevationData[offset + 1];
pixel[2] = elevationData[offset + 2];
pixel[3] = elevationData[offset + 3];
const centerElevation = calculateElevation(pixel);
if (
leftElevation !== undefined &&
rightElevation !== undefined &&
topElevation !== undefined &&
bottomElevation !== undefined &&
centerElevation !== undefined
) {
const contour = Math.floor(centerElevation / interval);
if (
contour !== Math.floor(leftElevation / interval) ||
contour !== Math.floor(rightElevation / interval) ||
contour !== Math.floor(topElevation / interval) ||
contour !== Math.floor(bottomElevation / interval)
) {
if (
centerElevation > 0 &&
leftElevation > 0 &&
rightElevation > 0 &&
topElevation > 0 &&
bottomElevation > 0
) {
contourData[offset] = 0xe0;
contourData[offset + 1] = 0x94;
contourData[offset + 2] = 0x5e;
contourData[offset + 3] = 255;
} else {
contourData[offset] = 0x00;
contourData[offset + 1] = 0xa9;
contourData[offset + 2] = 0xca;
contourData[offset + 3] = 255;
}
}
}
}
}
return {data: contourData, width: width, height: height};
};
const contourSource = new RasterSource({
sources: [elevation1],
operationType: 'image',
operation: contours,
lib: {
calculateElevation: calculateElevation,
},
resolutions: null,
});
contourSource.on('beforeoperations', function (event) {
const data = event.data;
if (event.resolution < 5) {
data.interval = 10;
} else if (event.resolution < 25) {
data.interval = 50;
} else if (event.resolution < 50) {
data.interval = 100;
} else if (event.resolution < 250) {
data.interval = 500;
} else {
data.interval = 1000;
}
});
const contourLayer = new ImageLayer({
source: contourSource,
opacity: 0.5,
});
const elevationLayer = new ImageLayer({
source: new RasterSource({
sources: [elevation2],
operation: function (pixels) {
return pixels[0];
},
resolutions: null,
}),
opacity: 0,
});
const dataLayer = new TileLayer({
source: source,
style: {color: ['array', 0, 0, 0, 0]},
});
dataLayer.once('postrender', function (event) {
const gl = event.context;
if (!gl.getSupportedExtensions().includes('OES_texture_float_linear')) {
alert('Device does not support float interpolation');
}
});
const map = new Map({
layers: [
new TileLayer({
source: new OSM(),
}),
dataLayer,
contourLayer,
elevationLayer,
],
target: 'map',
view: new View({
center: fromLonLat([-78.8175, -1.469167]),
zoom: 17,
maxZoom: 21,
}),
});
const mousePositionControl = new MousePosition({
className: 'custom-mouse-position',
target: 'info',
wrapX: false,
coordinateFormat: function (coordinate) {
let position = 'Position ' + toStringHDMS(toLonLat(coordinate)) + '<br>';
let pixel = elevationLayer.getData(map.getPixelFromCoordinate(coordinate));
if (pixel) {
const elevation = calculateElevation(pixel);
if (elevation !== undefined) {
position += 'Elevation ' + elevation.toFixed(1) + ' meters';
}
}
position += '<br><br>';
pixel = dataLayer.getData(map.getPixelFromCoordinate(coordinate));
if (pixel) {
const elevation = calculateElevationFromData(pixel);
if (elevation !== undefined) {
position += 'Data value ' + elevation.toFixed(1) + ' meters';
}
}
position += '<br>';
return position;
},
placeholder: '<br><br><br><br>',
});
map.addControl(mousePositionControl);index.html
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>从 DEM 插值等高线</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<div id="info"></div>
<script type="module" src="main.js"></script>
</body>
</html>package.json
json
{
"name": "contour-interpolation",
"dependencies": {
"ol": "10.11.0"
},
"devDependencies": {
"vite": "^8.1.5"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}