Getfeatureinfo Image
Runnable OpenLayers example: Getfeatureinfo Image. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:影像图层查询像元值
要点:点击坐标反算图像行列号,从同一数据函数取回像元值
依赖:OpenLayers 10.3.1 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>影像图层查询像元值</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@10.3.1/ol.css">
<style>
html,body{height:100%;margin:0}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#16181d}
.bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
display:flex;gap:16px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar b{color:#16181d;font-weight:600}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<span>行列号:<b id="rc">—</b></span>
<span>像元值:<b id="val">—</b></span>
<span>范围 116.20–116.62°E / 39.75–40.05°N,栅格 128×128</span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
'use strict';
if (!window.ol) {
document.getElementById('map').textContent = 'OpenLayers 加载失败,请检查 cdn.jsdelivr.net 是否可访问';
return;
}
var N = 128;
var bbox = [116.20, 39.75, 116.62, 40.05];
var extent = ol.proj.transformExtent(bbox, 'EPSG:4326', 'EPSG:3857');
/* 数据函数:可见影像和查询走同一个实现,保证点击取值与画面一致 */
function valueAt(row, col) {
if (row < 0 || col < 0 || row >= N || col >= N) return null;
var u = col / N, v = 1 - row / N;
var a = Math.sin(u * 7.4) * Math.cos(v * 6.1);
var b = Math.sin((u + v) * 3.6);
var t = 0.5 + 0.35 * a + 0.15 * b;
return Math.round(2400 + t * 1600); /* 换算成米级高程 */
}
function colorFor(v) {
var t = Math.max(0, Math.min(1, (v - 2400) / 1600));
var r = Math.round(30 + t * 200);
var g = Math.round(90 + t * 120);
var b = Math.round(190 - t * 150);
return [r, g, b];
}
var url = (function () {
var c = document.createElement('canvas');
c.width = c.height = N;
var ctx = c.getContext('2d');
var img = ctx.createImageData(N, N);
for (var row = 0; row < N; row++) {
for (var col = 0; col < N; col++) {
var rgb = colorFor(valueAt(row, col));
var i = (row * N + col) * 4;
img.data[i] = rgb[0];
img.data[i + 1] = rgb[1];
img.data[i + 2] = rgb[2];
img.data[i + 3] = 255;
}
}
ctx.putImageData(img, 0, 0);
return c.toDataURL('image/png');
})();
var layer = new ol.layer.Image({
opacity: 0.9,
source: new ol.source.ImageStatic({
url: url,
imageExtent: extent,
projection: 'EPSG:3857'
})
});
var map = new ol.Map({
target: 'map',
layers: [new ol.layer.Tile({ source: new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'}) }), layer],
view: new ol.View({ center: ol.proj.fromLonLat([116.40, 39.90]), zoom: 10 }),
controls: ol.control.defaults.defaults({ rotate: false })
});
/* 点击:先转回经纬度,再按影像范围换算成分辨率格网的行列号 */
map.on('singleclick', function (evt) {
var lonlat = ol.proj.toLonLat(evt.coordinate);
var fx = (lonlat[0] - bbox[0]) / (bbox[2] - bbox[0]);
var fy = (bbox[3] - lonlat[1]) / (bbox[3] - bbox[1]);
if (fx < 0 || fx > 1 || fy < 0 || fy > 1) {
document.getElementById('rc').textContent = '范围外';
document.getElementById('val').textContent = '—';
return;
}
var col = Math.min(N - 1, Math.floor(fx * N));
var row = Math.min(N - 1, Math.floor(fy * N));
document.getElementById('rc').textContent = row + ' , ' + col;
document.getElementById('val').textContent = valueAt(row, col) + ' m';
});
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.