Sentinel Hub
Sentinel Hub示例。 Sentinel Hub process
本示例渲染一个瓦片图层,其数据源使用 Sentinel Hub Processing API 来生成瓦片。Processing API 需要访问令牌。上方的表单可用于提供 OAuth 客户端 id 和 secret。本示例使用的默认客户端 id 和 secret 受到严格的速率限制。为了让示例有良好表现,你应该注册自己的客户端 id 和 secret。提供这些信息后,数据源将获取访问令牌。如果访问令牌是通过其他方式获取的,则可以直接调用 source.setAuth() 方法传入令牌。详情请参阅 Sentinel Hub 身份验证文档。
有关以下内容的详细信息,请参阅其他示例:
Sentinel Hub Date Picker — 调用 source.setData() 更新输入数据的时间范围。
Sentinel Hub Custom Script — 调用 source.setEvalscript() 更新渲染瓦片时使用的 Evalscript。
main.js
js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import TileLayer from 'ol/layer/WebGLTile.js';
import {useGeographic} from 'ol/proj.js';
import SentinelHub from 'ol/source/SentinelHub.js';
useGeographic();
const source = new SentinelHub({
data: [
{
type: 'sentinel-2-l2a',
dataFilter: {
timeRange: {
from: '2024-05-30T00:00:00Z',
to: '2024-06-01T00:00:00Z',
},
},
},
],
evalscript: {
setup: () => ({
input: ['B12', 'B08', 'B04'],
output: {bands: 3},
}),
evaluatePixel: (sample) => [
2.5 * sample.B12,
2 * sample.B08,
2 * sample.B04,
],
},
});
const map = new Map({
layers: [new TileLayer({source})],
target: 'map',
view: new View({
center: [-121.75, 46.85],
zoom: 10,
minZoom: 7,
maxZoom: 13,
}),
});
document.getElementById('auth-form').addEventListener('submit', (event) => {
const clientId = event.target.elements['id'].value;
const clientSecret = event.target.elements['secret'].value;
source.setAuth({clientId, clientSecret});
});
source.on('change', () => {
if (source.getState() === 'error') {
alert(source.getError());
}
});index.html
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sentinel Hub</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
dialog {
top: 13rem;
}
#auth-form {
display: flex;
flex-direction: column;
}
#auth-form > label {
margin-bottom: 1rem;
}
#auth-form > input[type=submit] {
margin-top: 1rem;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<dialog id="auth-dialog" open>
<form method="dialog" id="auth-form">
<label>客户端 id
<br>
<input type="text" name="id" autofocus value="fa02a066-fc80-4cb4-af26-aae0af26cbf1">
</label>
<label>客户端密钥
<br>
<input type="password" name="secret" value="rate_limit_secret">
</label>
<input type="submit" value="show map">
</form>
</dialog>
<script type="module" src="main.js"></script>
</body>
</html>package.json
json
{
"name": "sentinel-hub",
"dependencies": {
"ol": "10.11.0"
},
"devDependencies": {
"vite": "^8.1.5"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}