返回介绍

Change Tile Layer Style

发布于 2022-11-30 23:36:02 字数 4521 浏览 0 评论 0 收藏 0

更新修改 WebGL 瓦片图层样式

当您想根据应用的某些变化来调整修改 WebGL 瓦片图层的样式,您应该使用 layer.updateStyleVariables()方法。 即使样式是逐帧变化,图层也可以高效实时渲染。如果您需要完全替换图层的样式,您可以使用 layer.setStyle()方法。此方法不应该被客户端事件频繁调用(比如鼠标移动、拖动滑块等等。)

main.js

import 'ol/ol.css';
import GeoTIFF from 'ol/source/GeoTIFF';
import Map from 'ol/Map';
import TileLayer from 'ol/layer/WebGLTile';
import View from 'ol/View';

const max = 3000;
function normalize(value) {
  return ['/', value, max];
}

const red = normalize(['band', 1]);
const green = normalize(['band', 2]);
const blue = normalize(['band', 3]);
const nir = normalize(['band', 4]);

const trueColor = {
  color: ['array', red, green, blue, 1],
  gamma: 1.1,
};

const falseColor = {
  color: ['array', nir, red, green, 1],
  gamma: 1.1,
};

const ndvi = {
  color: [
    'interpolate',
    ['linear'],
    ['/', ['-', nir, red], ['+', nir, red]],
    // color ramp for NDVI values, ranging from -1 to 1
    -0.2,
    [191, 191, 191],
    -0.1,
    [219, 219, 219],
    0,
    [255, 255, 224],
    0.025,
    [255, 250, 204],
    0.05,
    [237, 232, 181],
    0.075,
    [222, 217, 156],
    0.1,
    [204, 199, 130],
    0.125,
    [189, 184, 107],
    0.15,
    [176, 194, 97],
    0.175,
    [163, 204, 89],
    0.2,
    [145, 191, 82],
    0.25,
    [128, 179, 71],
    0.3,
    [112, 163, 64],
    0.35,
    [97, 150, 54],
    0.4,
    [79, 138, 46],
    0.45,
    [64, 125, 36],
    0.5,
    [48, 110, 28],
    0.55,
    [33, 97, 18],
    0.6,
    [15, 84, 10],
    0.65,
    [0, 69, 0],
  ],
};

const layer = new TileLayer({
  style: trueColor,
  source: new GeoTIFF({
    normalize: false,
    sources: [
      {
        url: 'http://www.wenjiangs.com/wp-content/uploads/2022/docimg28/s2cloudless2020-16bits_sinlge-file_z0-4.tif',
      },
    ],
  }),
});

const map = new Map({
  target: 'map',
  layers: [layer],
  view: new View({
    projection: 'EPSG:4326',
    center: [0, 0],
    zoom: 2,
    maxZoom: 6,
  }),
});

const styles = {trueColor, falseColor, ndvi};
const styleSelector = document.getElementById('style');

function update() {
  const style = styles[styleSelector.value];
  layer.setStyle(style);
}
styleSelector.addEventListener('change', update);

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Change Tile Layer Style</title>
    <!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
    <script src="https://unpkg.com/elm-pep"></script>
    <!-- The lines below are only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v3/polyfill.min.js?features=fetch,requestAnimationFrame,Element.prototype.classList,TextDecoder"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/3.18.3/minified.js"></script>
    <style>
      .map {
        width: 100%;
        height:400px;
      }
    </style>
  </head>
  <body>
    <div id="map" class="map"></div>
    Set the layer style
    <select id="style">
      <option value="trueColor">True Color</option>
      <option value="falseColor">False Color</option>
      <option value="ndvi">NDVI</option>
    </select>
    <script src="main.js"></script>
  </body>
</html>

package.json

{
  "name": "cog-style",
  "dependencies": {
    "ol": "7.1.0"
  },
  "devDependencies": {
    "parcel": "^2.0.0"
  },
  "scripts": {
    "start": "parcel index.html",
    "build": "parcel build --public-url . index.html"
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文