openlayers7的WebGLPointsLayer图层textureCoord生成工具

概要

小工具(TypeScript+Vue3),可生成openlayers7的WebGLPointsLayer图层textureCoord属性对应的match表达式数据。


/**
 * @description>>>>>>获得图标编码-纹理坐标映射数据
 * @param {Coordinate} numRows 纵向图标数量
 * @param {Coordinate} numCols 横向图标数量
 */
export function getTextureCoord(numRows: number = 50, numCols: number = 50) {
  let arr: any = [];
  // 生成纹理坐标
  Array(numRows * numCols).fill(0).map((_, index) => {
      // 行标
      const row = Math.floor(index / numCols);
      // 列标
      const col = index % numCols;
      //在字符串的开头添加指定字符,直到字符串达到指定的长度
      const id = String(row).padStart(2, "0") + String(col).padStart(2, "0"); // 生成编码
      const x1 = col / numCols; // 左上角 x 坐标
      const y1 = row / numRows; // 左上角 y 坐标
      const x2 = (col + 1) / numCols; // 右下角 x 坐标
      const y2 = (row + 1) / numRows; // 右下角 y 坐标
      arr.push(id, [x1, y1, x2, y2]); // 将编码和纹理坐标添加到数组
    });
  let groupSize = ((numRows * numCols) / (numRows / 2)) * 2; // 分组大小
  let numGroups = Math.ceil(arr.length / groupSize); // 计算分组数
  let obj: any = {};
  // 生成分组数据
  for (let i = 0; i < numGroups; i++) {
    let group = arr.slice(i * groupSize, (i + 1) * groupSize); // 分组数据
    let key = String.fromCharCode(97 + i); // 生成键名,从 'a' 到 'y'
    let value = ["match", ["get", "name"], ...group]; // 生成值数组,包含 'match'、['get', 'name'] 和分组数据
    value.push([(numRows - 1) / numRows, (numCols - 1) / numCols, 1, 1]); // 每组的默认数据
    obj[key] = value; // 将键和值添加到对象
    // 默认值
    if (i == numGroups - 1) {
      // 默认纹理坐标
      obj[String.fromCharCode(97 + i + 1)] = [(numRows - 1) / numRows,(numCols - 1) / numCols,1, 1,];
    }
  }
  let jsonString = JSON.stringify(obj)
  // 按钮点击事件处理程序
  // 创建一个 Blob 对象
  var blob = new Blob([jsonString], { type: "application/json" });
  // 创建一个下载链接
  var downloadLink = document.createElement("a");
  downloadLink.href = URL.createObjectURL(blob);
  downloadLink.download = "textureCoord.json";
  // 点击链接进行下载
  document.body.appendChild(downloadLink);
  downloadLink.click();
  // 清理下载链接
  document.body.removeChild(downloadLink);
}

数据样例:

  textureCoord: {
    "a": [
      "match",
      ["get", "name"],
      "0000",
      [0, 0, 0.02, 0.02],
      // "0001",
      // [0.02, 0, 0.04, 0.02],
      // ......略
      // "0049",
      // [0.98, 0, 1, 0.02],
      "0100",
      [0, 0.02, 0.02, 0.04],
      // "0101",
      // [0.02, 0.02, 0.04, 0.04],
      // "0102",
      // ......略
      // "0149",
      // [0.98, 0.02, 1, 0.04],
      // 默认值
      [0.98, 0.98, 1, 1]
    ],
    "b": [
      "match",
      ["get", "name"],
      "0200",
      [0, 0.04, 0.02, 0.06],
      // ......略
      // 默认值
      [0.98, 0.98, 1, 1]
      ],
      // ......略
      // 默认值
       "z": [0.98, 0.98, 1, 1]
    }

下载链接:https://download.csdn.net/download/qq_40236953/88628208

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
OpenLayers是一个开源的JavaScript库,用于在Web上创建交互式地应用程序。它支持加载各种地层,包括Geoserver层。下面是使用OpenLayers加载Geoserver层的一般步骤: 1. 引入OpenLayers库文件。你可以从OpenLayers官方网站下载最新版本的库文件,并将其引入到你的HTML文件中。 ```html <script src="path/to/openlayers.js"></script> ``` 2. 创建地容器。在HTML文件中创建一个具有唯一ID的`<div>`元素,用于容纳地。 ```html <div id="map"></div> ``` 3. 初始化地对象。在JavaScript代码中,使用OpenLayers的`Map`类来创建一个地对象,并指定地容器的ID。 ```javascript var map = new ol.Map({ target: 'map' }); ``` 4. 创建Geoserver层。使用OpenLayers的`TileLayer`类来创建一个Geoserver层,并指定Geoserver的层URL。 ```javascript var geoserverLayer = new ol.layer.Tile({ source: new ol.source.TileWMS({ url: 'http://your-geoserver-url.com/geoserver/wms', params: { 'LAYERS': 'your-layer-name' } }) }); ``` 5. 将Geoserver层添加到地中。使用`addLayer`方法将Geoserver层添加到地对象中。 ```javascript map.addLayer(geoserverLayer); ``` 6. 设置地。使用`View`类来设置地的中心点和缩放级别。 ```javascript var view = new ol.View({ center: ol.proj.fromLonLat([longitude, latitude]), zoom: 10 }); map.setView(view); ``` 以上是使用OpenLayers加载Geoserver层的基本步骤。你可以根据自己的需求进行进一步的定制和配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

無可言喻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值