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,
        });
  })
相关推荐
开开心心就好32 分钟前
电脑息屏工具,一键黑屏超方便
开发语言·javascript·电脑·scala·erlang·perl
江号软件分享33 分钟前
有效保障隐私,如何安全地擦除电脑上的敏感数据
前端
web守墓人2 小时前
【前端】ikun-markdown: 纯js实现markdown到富文本html的转换库
前端·javascript·html
Savior`L2 小时前
CSS知识复习5
前端·css
许白掰2 小时前
Linux入门篇学习——Linux 工具之 make 工具和 makefile 文件
linux·运维·服务器·前端·学习·编辑器
中微子6 小时前
🔥 React Context 面试必考!从源码到实战的完整攻略 | 99%的人都不知道的性能陷阱
前端·react.js
秋田君7 小时前
深入理解JavaScript设计模式之命令模式
javascript·设计模式·命令模式
中微子7 小时前
React 状态管理 源码深度解析
前端·react.js
风吹落叶花飘荡8 小时前
2025 Next.js项目提前编译并在服务器
服务器·开发语言·javascript
加减法原则8 小时前
Vue3 组合式函数:让你的代码复用如丝般顺滑
前端·vue.js