本示例演示数据插值
本示例演示使用栅格 DEM(数字高程模型)数据的数据重采样。
interpolate: false
参数的设置,用于在重投影和渲染过程中,禁止数据插值。
高程数据是根据 forEachLayerAtPixel 返回的像素值计算的。
对比之下,在启动插值的第二个地图中很明显,在接近 3107 米的地方返回了不准确的高程数据。
这是因为高程数据是由像素值计算方式导致的。
import Map from 'ol/Map.js';
import TileLayer from 'ol/layer/Tile.js';
import View from 'ol/View.js';
import XYZ from 'ol/source/XYZ.js';
const key = 'Get your own API key at https://www.maptiler.com/cloud/';
const attributions =
'<a href="https://www.maptiler.com/copyright/" target="_blank">© MapTiler</a> ' +
'<a href="https://www.openstreetmap.org/copyright" target="_blank">© OpenStreetMap contributors</a>';
const notInterpolated = new TileLayer({
source: new XYZ({
attributions: attributions,
url:
'https://api.maptiler.com/tiles/terrain-rgb/{z}/{x}/{y}.png?key=' + key,
tileSize: 512,
maxZoom: 12,
crossOrigin: '',
interpolate: false,
}),
});
const interpolated = new TileLayer({
source: new XYZ({
attributions: attributions,
url:
'https://api.maptiler.com/tiles/terrain-rgb/{z}/{x}/{y}.png?key=' + key,
tileSize: 512,
maxZoom: 12,
crossOrigin: '',
}),
});
const view = new View({
center: [6.893, 45.8295],
zoom: 16,
projection: 'EPSG:4326',
});
const map1 = new Map({
target: 'map1',
layers: [notInterpolated],
view: view,
});
const map2 = new Map({
target: 'map2',
layers: [interpolated],
view: view,
});
function getHeight(rgba) {
return -10000 + (rgba[0] * 256 * 256 + rgba[1] * 256 + rgba[2]) * 0.1;
}
const info1 = document.getElementById('info1');
const info2 = document.getElementById('info2');
const showElevations = function (evt) {
if (evt.dragging) {
return;
}
const notInterpolatedPixel = notInterpolated.getData(evt.pixel);
info1.innerText = notInterpolatedPixel
? getHeight(notInterpolatedPixel).toFixed(1)
: '-';
const interpolatedPixel = interpolated.getData(evt.pixel);
info2.innerText = interpolatedPixel
? getHeight(interpolatedPixel).toFixed(1)
: '-';
};
map1.on('pointermove', showElevations);
map2.on('pointermove', showElevations);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Interpolation</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
@media (min-width: 800px) {
.wrapper {
display: flex;
}
.half {
padding: 0 10px;
width: 50%;
float: left;
}
}
#opacity {
display: inline-block;
width: 150px;
vertical-align: text-bottom;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="half">
<h4>Not Interpolated</h4>
<div id="map1" class="map"></div>
<div>
<label>
Elevation
<span id="info1">-</span> meters
</label>
</div>
</div>
<div class="half">
<h4>Interpolated</h4>
<div id="map2" class="map"></div>
<div>
<label>
Elevation
<span id="info2">-</span> meters
</label>
</div>
</div>
</div>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="./resources/elm-pep.js"></script>
<script type="module" src="main.js"></script>
</body>
</html>
{
"name": "interpolation",
"dependencies": {
"ol": "7.3.0"
},
"devDependencies": {
"vite": "^3.2.3"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}