栅格数据源
栅格数据源示例。 栅格 pixel MapTiler
本示例使用 ol/source/Raster 基于另一个数据源生成数据。栅格数据源可以接受任意数量的输入数据源(基于瓦片或图像),并对输入像素运行一系列操作流水线。最后一个操作的返回值将用作输出数据源的数据。
在本例中,使用单个影像瓦片数据源作为输入。针对每个像素,根据输入像素计算植被绿度指数(VGI)。第二个操作根据阈值对这些像素着色(高于阈值的像素为绿色,低于阈值的像素为透明)。
main.js
js
import {max} from 'd3-array';
import {scaleLinear} from 'd3-scale';
import {select} from 'd3-selection';
import 'd3-transition';
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import ImageLayer from 'ol/layer/Image.js';
import TileLayer from 'ol/layer/Tile.js';
import ImageTile from 'ol/source/ImageTile.js';
import RasterSource from 'ol/source/Raster.js';
const minVgi = 0;
const maxVgi = 0.5;
const bins = 10;
/**
* 根据输入像素计算植被绿度指数(VGI)。
* 这是一个粗略的估计,假设像素值对应反射率。
* @param {Array<number>} pixel 由 [R, G, B, A] 值组成的数组。
* @return {number} 给定像素的 VGI 值。
*/
function vgi(pixel) {
const r = pixel[0] / 255;
const g = pixel[1] / 255;
const b = pixel[2] / 255;
return (2 * g - r - b) / (2 * g + r + b);
}
/**
* 汇总直方图的值。
* @param {number} value 一个 VGI 值。
* @param {Object} counts 用于记录 VGI 计数的对象。
*/
function summarize(value, counts) {
const min = counts.min;
const max = counts.max;
const num = counts.values.length;
if (value < min) {
// 什么都不做
} else if (value >= max) {
counts.values[num - 1] += 1;
} else {
const index = Math.floor((value - min) / counts.delta);
counts.values[index] += 1;
}
}
/**
* 使用航空影像作为栅格数据源的输入数据。
*/
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 aerial = new ImageTile({
attributions: attributions,
url: 'https://api.maptiler.com/maps/satellite/{z}/{x}/{y}.jpg?key=' + key,
tileSize: 512,
maxZoom: 20,
crossOrigin: '',
});
/**
* 创建一个栅格数据源,将 VGI 值高于阈值的
* 像素着色为绿色。
*/
const raster = new RasterSource({
sources: [aerial],
/**
* 对像素数据进行计算。
* @param {Array} pixels 像素列表(每个数据源一个)。
* @param {Object} data 用户数据对象。
* @return {Array} 输出像素。
*/
operation: function (pixels, data) {
const pixel = pixels[0];
const value = vgi(pixel);
summarize(value, data.counts);
if (value >= data.threshold) {
pixel[0] = 0;
pixel[1] = 255;
pixel[2] = 0;
pixel[3] = 128;
} else {
pixel[3] = 0;
}
return pixel;
},
lib: {
vgi: vgi,
summarize: summarize,
},
});
raster.set('threshold', 0.25);
function createCounts(min, max, num) {
const values = new Array(num);
for (let i = 0; i < num; ++i) {
values[i] = 0;
}
return {
min: min,
max: max,
values: values,
delta: (max - min) / num,
};
}
raster.on('beforeoperations', function (event) {
event.data.counts = createCounts(minVgi, maxVgi, bins);
event.data.threshold = raster.get('threshold');
});
raster.on('afteroperations', function (event) {
schedulePlot(event.resolution, event.data.counts, event.data.threshold);
});
const map = new Map({
layers: [
new TileLayer({
source: aerial,
}),
new ImageLayer({
source: raster,
}),
],
target: 'map',
view: new View({
center: [-9651695, 4937351],
zoom: 13,
minZoom: 12,
maxZoom: 19,
}),
});
let timer = null;
function schedulePlot(resolution, counts, threshold) {
if (timer) {
clearTimeout(timer);
timer = null;
}
timer = setTimeout(plot.bind(null, resolution, counts, threshold), 1000 / 60);
}
const barWidth = 15;
const plotHeight = 150;
const chart = select('#plot')
.append('svg')
.attr('width', barWidth * bins)
.attr('height', plotHeight);
const chartRect = chart.node().getBoundingClientRect();
const tip = select(document.body).append('div').attr('class', 'tip');
function plot(resolution, counts, threshold) {
const yScale = scaleLinear()
.domain([0, max(counts.values)])
.range([0, plotHeight]);
const bar = chart.selectAll('rect').data(counts.values);
bar.enter().append('rect');
bar
.attr('class', function (count, index) {
const value = counts.min + index * counts.delta;
return 'bar' + (value >= threshold ? ' selected' : '');
})
.attr('width', barWidth - 2);
bar
.transition()
.attr('transform', function (value, index) {
return (
'translate(' +
index * barWidth +
', ' +
(plotHeight - yScale(value)) +
')'
);
})
.attr('height', yScale);
bar.on('mousemove', function () {
const index = bar.nodes().indexOf(this);
const threshold = counts.min + index * counts.delta;
if (raster.get('threshold') !== threshold) {
raster.set('threshold', threshold);
raster.changed();
}
});
bar.on('mouseover', function (event) {
const index = bar.nodes().indexOf(this);
let area = 0;
for (let i = counts.values.length - 1; i >= index; --i) {
area += resolution * resolution * counts.values[i];
}
tip.html(message(counts.min + index * counts.delta, area));
tip.style('display', 'block');
tip
.transition()
.style('left', chartRect.left + index * barWidth + barWidth / 2 + 'px')
.style('top', event.y - 60 + 'px')
.style('opacity', 1);
});
bar.on('mouseout', function () {
tip
.transition()
.style('opacity', 0)
.on('end', function () {
tip.style('display', 'none');
});
});
}
function message(value, area) {
const acres = (area / 4046.86)
.toFixed(0)
.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
return acres + ' acres at<br>' + value.toFixed(2) + ' VGI or above';
}index.html
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>栅格数据源</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
.rel {
position: relative
}
#plot {
pointer-events: none;
position: absolute;
bottom: 10px;
left: 10px;
}
.bar {
pointer-events: auto;
fill: #AFAFB9;
}
.bar.selected {
fill: green;
}
.tip {
position: absolute;
background: black;
color: white;
padding: 6px;
font-size: 12px;
border-radius: 4px;
margin-bottom: 10px;
display: none;
opacity: 0;
}
</style>
</head>
<body>
<div class="rel">
<div id="map" class="map"></div>
<div id="plot"></div>
</div>
<script type="module" src="main.js"></script>
</body>
</html>package.json
json
{
"name": "raster",
"dependencies": {
"ol": "10.11.0",
"d3-array": "^3.2.4",
"d3-scale": "^4.0.2",
"d3-selection": "^3.0.0",
"d3-transition": "^3.0.1"
},
"devDependencies": {
"vite": "^8.1.5"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}