cesium加载.tif格式文件

最近项目中有需要直接加载三方给的后缀名tif格式的文件

javascript 复制代码
    <script src="https://cdn.jsdelivr.net/npm/geotiff"></script>
或者
yarn add geotiff
npm install geotiff

新建tifs.js

javascript 复制代码
import GeoTIFF, { fromBlob, fromUrl, fromArrayBuffer } from 'geotiff';
import GeoTIFFImage from 'geotiff/dist-node/geotiffimage';
var tiff;
var image;
var rectangle;
class GeoTiffUtil {
  constructor() {
  }
   async init(){
     this.tiff = await fromUrl('http://xxxx.tif');
    this.image = await this.tiff.getImage();
    let [west, south, east, north] = this.image.getBoundingBox();
    const code =
      this.image.geoKeys.ProjectedCSTypeGeoKey ||
      this.image.geoKeys.GeographicTypeGeoKey;

        let { x: w, y: n } = await (
       await fetch(
         `//epsgIo/trans?x=${west}&y=${north}&s_srs=${code}&t_srs=4326`
       )
     ).json();
     let { x: e, y: s } = await (
       await fetch(
         `//epsgIo/trans?x=${east}&y=${south}&s_srs=${code}&t_srs=4326`
       )
      ).json();
      const [red = [], green = [], blue = []] = await this.image.readRasters();
      // 将像素信息写入canvas
      const canvas = document.createElement("canvas");
      let width = this.image.getWidth();
      let height = this.image.getHeight();
      canvas.width = width;
      canvas.height = height;
      let ctx = canvas.getContext("2d");
      let imageData = ctx.createImageData(width, height);
      for (var i = 0; i < imageData.data.length / 4; i += 1) {
          imageData.data[i * 4 + 0] = red[i];
          imageData.data[i * 4 + 1] = green[i] || 0;
          imageData.data[i * 4 + 2] = blue[i] || 0;
          imageData.data[i * 4 + 3] = red[i] === 0 ? 0 : 255;
      }

      ctx.putImageData(imageData, 0, 0);

      return {canvas:canvas,rectangle:[w, s, e, n]};
  }
}
export default new GeoTiffUtil();

index.vue

javascript 复制代码
 import GeoTiffUtil from '@/utils/tifs.js';     
 GeoTiffUtil.init().then((res)=>{
      let rectangle = Cesium.Rectangle.fromDegrees(res.rectangle[0], res.rectangle[1],             
      res.rectangle[2],res.rectangle[3]);
        let du = res.canvas.toDataURL();
        viewer.imageryLayers.addImageryProvider(
          new Cesium.SingleTileImageryProvider({
            url: du,
            rectangle,
          })
        );

        viewer.camera.setView({
          destination: rectangle,
        });
  })
相关推荐
excel16 小时前
为什么在 Three.js 中平面能产生“起伏效果”?
前端
excel17 小时前
Node.js 断言与测试框架示例对比
前端
天蓝色的鱼鱼19 小时前
前端开发者的组件设计之痛:为什么我的组件总是难以维护?
前端·react.js
codingandsleeping19 小时前
使用orval自动拉取swagger文档并生成ts接口
前端·javascript
石金龙20 小时前
[译] Composition in CSS
前端·css
白水清风20 小时前
微前端学习记录(qiankun、wujie、micro-app)
前端·javascript·前端工程化
Ticnix20 小时前
函数封装实现Echarts多表渲染/叠加渲染
前端·echarts
用户221520442780020 小时前
new、原型和原型链浅析
前端·javascript
阿星做前端20 小时前
coze源码解读: space develop 页面
前端·javascript
叫我小窝吧20 小时前
Promise 的使用
前端·javascript