Atlaslog
Atlaslog / Examples / OpenLayers / Numpytile
OpenLayers · Runnable Example

Numpytile

Runnable OpenLayers example: Numpytile. 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-numpytile.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-numpytile.html
<!--
  示例:二进制数组瓦片
  要点:tileLoadFunction 把数值数组解码成画布图像,再交给瓦片显示
  依赖:OpenLayers 10.3.1 · jsDelivr CDN
-->
<!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>二进制数组瓦片</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}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
  <span>解码瓦片 <b id="n">0</b> 张</span>
  <span>点击读取 <b id="val">—</b></span>
  <span>每张瓦片为 256×256 的 Uint8 数组</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;
  }

  var SIZE = 256;

  /* 模拟服务端返回的二进制数组:这里按瓦片坐标直接算出来 */
  function readArray(z, x, y) {
    var arr = new Uint8Array(SIZE * SIZE);
    for (var py = 0; py < SIZE; py++) {
      for (var px = 0; px < SIZE; px++) {
        var u = (x + px / SIZE) / Math.pow(2, z);
        var v = (y + py / SIZE) / Math.pow(2, z);
        var t = 0.5 + 0.36 * Math.sin(u * 26.0) * Math.cos(v * 21.0) + 0.1 * Math.sin(u * 70.0);
        arr[py * SIZE + px] = Math.max(0, Math.min(255, Math.round(t * 255)));
      }
    }
    return arr;
  }

  function toImage(arr) {
    var c = document.createElement('canvas');
    c.width = c.height = SIZE;
    var ctx = c.getContext('2d');
    var img = ctx.createImageData(SIZE, SIZE);
    for (var i = 0; i < arr.length; i++) {
      var t = arr[i] / 255;
      var o = i * 4;
      img.data[o] = Math.round(20 + t * 230);
      img.data[o + 1] = Math.round(70 + (1 - Math.abs(t - 0.5) * 2) * 150);
      img.data[o + 2] = Math.round(200 - t * 170);
      img.data[o + 3] = 255;
    }
    ctx.putImageData(img, 0, 0);
    return c.toDataURL('image/png');
  }

  var decoded = 0;
  var tileGrid = ol.tilegrid.createXYZ({ tileSize: SIZE, maxZoom: 14 });

  /* 官方示例用 numpy 序列化的 .npy 瓦片;这里数组在页面内生成,
     解码路径保持一致:数组 → 画布 → 瓦片图像 */
  var source = new ol.source.TileImage({
    tileGrid: tileGrid,
    attributions: '数值数组瓦片',
    tileUrlFunction: function (tc) { return tc[0] + '/' + tc[1] + '/' + tc[2]; },
    tileLoadFunction: function (tile, url) {
      var parts = url.split('/').map(Number);
      var arr = readArray(parts[0], parts[1], parts[2]);
      tile.getImage().src = toImage(arr);
      decoded++;
      document.getElementById('n').textContent = decoded;
    }
  });

  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'}) }),
             new ol.layer.Tile({ source: source, opacity: 0.85 })],
    view: new ol.View({ center: ol.proj.fromLonLat([116.391263, 39.907359]), zoom: 10, maxZoom: 14 }),
    controls: ol.control.defaults.defaults({ rotate: false })
  });

  map.on('singleclick', function (evt) {
    var z = Math.round(map.getView().getZoom());
    var tc = tileGrid.getTileCoordForCoordAndZ(evt.coordinate, z);
    if (!tc) return;
    var ext = tileGrid.getTileCoordExtent(tc);
    var px = Math.floor((evt.coordinate[0] - ext[0]) / (ext[2] - ext[0]) * SIZE);
    var py = Math.floor((ext[3] - evt.coordinate[1]) / (ext[3] - ext[1]) * SIZE);
    var arr = readArray(tc[0], tc[1], tc[2]);
    document.getElementById('val').textContent =
      arr[py * SIZE + px] + '(瓦片 ' + tc.join('/') + ' 像素 ' + px + ',' + py + ')';
  });
})();
</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 · EXAMPLEOffscreen Canvas OpenLayers · EXAMPLEOgc Map Tiles OpenLayers · EXAMPLEOgc Map Tiles Geographic