Skip to content

Sentinel Hub 自定义 Format ​

Sentinel Hub 自定义 Format示例。 Sentinel Hub process

本示例演示如何使用自定义格式从 Sentinel Hub 渲染瓦片。从下拉框中选择一种格式,然后点击更新按钮以更改格式。更多信息请参阅 Sentinel Hub 示例。

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,
    ],
  },
  format: 'image/png',
});

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});
});

const format = document.getElementById('format');

function updateInputData() {
  source.setFormat(format.value);
}

format.addEventListener('change', () => updateInputData());
updateInputData();

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>
    <form id="format-form">
      <label>图像格式:
        <br>
        <select name="format" id="format">
          <option value="image/png">image/png</option>
          <option value="image/jpeg">image/jpeg</option>
          <option value="image/webp">image/webp</option>
        </select>
      </label>
      <input type="submit" value="update">
    </form>
    <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-custom-format",
  "dependencies": {
    "ol": "10.11.0"
  },
  "devDependencies": {
    "vite": "^8.1.5"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}