Atlaslog
Atlaslog / Examples / OpenLayers / Stac Item
OpenLayers · Runnable Example

Stac Item

Runnable OpenLayers example: Stac Item. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

Open standalone → Chinese notes All OpenLayers examples
/demos/openlayers-stac-item.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-stac-item.html
<!--
  示例:STAC 条目定位
  要点:STAC 的 bbox 转成地图范围,asset 影像按 extent 贴到底图上
  依赖:OpenLayers 10.3.1 · jsDelivr CDN(底图 OSM,无 Key)
  替代:官方请求 STAC API 取条目,这里用页面内的条目 JSON 与生成的影像
-->
<!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>STAC 条目定位</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;font-variant-numeric:tabular-nums}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
  <span>条目 <b id="iid">—</b></span>
  <span>范围 <b id="bb">—</b></span>
  <span>云量 <b id="cl">—</b></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;
  }

  /* 一个最小可用的 STAC Item:bbox = [west, south, east, north] */
  var item = {
    type: 'Feature',
    id: 'S2B_50TMK_20260415_0_L2A',
    bbox: [116.10, 39.62, 116.72, 40.12],
    properties: {
      datetime: '2026-04-15T03:12:41Z',
      'eo:cloud_cover': 12.4,
      platform: 'sentinel-2b'
    },
    assets: {
      visual: { href: 'visual.tif', 'raster:bands': [{ scale: 0.0001 }] }
    }
  };

  var bbox = item.bbox;

  /* 生成一张与 bbox 同尺寸的示意图,替代 asset 影像 */
  function makeImage() {
    var c = document.createElement('canvas');
    c.width = 320;
    c.height = 260;
    var ctx = c.getContext('2d');
    var g = ctx.createLinearGradient(0, 0, 320, 260);
    g.addColorStop(0, 'rgba(58,110,84,.92)');
    g.addColorStop(0.5, 'rgba(148,152,92,.92)');
    g.addColorStop(1, 'rgba(96,86,68,.92)');
    ctx.fillStyle = g;
    ctx.fillRect(0, 0, 320, 260);
    for (var i = 0; i < 16; i++) {
      ctx.fillStyle = 'rgba(255,255,255,' + (0.05 + (i % 4) * 0.03) + ')';
      ctx.beginPath();
      ctx.arc((i * 53) % 320, (i * 71) % 260, 18 + (i % 5) * 5, 0, Math.PI * 2);
      ctx.fill();
    }
    return c.toDataURL();
  }

  /* bbox 的两个角点经 fromLonLat 转换后再组成 imageExtent */
  var sw = ol.proj.fromLonLat([bbox[0], bbox[1]]);
  var ne = ol.proj.fromLonLat([bbox[2], bbox[3]]);

  var imageLayer = new ol.layer.Image({
    opacity: 0.78,
    source: new ol.source.ImageStatic({
      url: makeImage(),
      imageExtent: [sw[0], sw[1], ne[0], ne[1]],
      projection: 'EPSG:3857'
    })
  });

  /* 条目范围轮廓 */
  var outline = new ol.Feature(new ol.geom.Polygon([[
    [sw[0], sw[1]], [ne[0], sw[1]], [ne[0], ne[1]], [sw[0], ne[1]], [sw[0], sw[1]]
  ]]));

  var outlineLayer = new ol.layer.Vector({
    source: new ol.source.Vector({ features: [outline] }),
    style: new ol.style.Style({
      stroke: new ol.style.Stroke({ color: '#d64545', width: 2, lineDash: [8, 5] })
    })
  });

  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'}) }),
      imageLayer,
      outlineLayer
    ],
    view: new ol.View({ center: ol.proj.fromLonLat([116.41, 39.87]), zoom: 10 }),
    controls: ol.control.defaults.defaults({ attribution: false, rotate: false })
  });

  map.getView().fit([sw[0], sw[1], ne[0], ne[1]], {
    padding: [40, 40, 40, 40],
    maxZoom: 12
  });

  document.getElementById('iid').textContent = item.id;
  document.getElementById('bb').textContent =
    bbox[0] + ', ' + bbox[1] + ' → ' + bbox[2] + ', ' + bbox[3];
  document.getElementById('cl').textContent =
    item.properties['eo:cloud_cover'] + '%';
})();
</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.

More OpenLayers examples

OpenLayers · EXAMPLEStadia Maps OpenLayers · EXAMPLEStamen OpenLayers · EXAMPLEStatic Attribution