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,
        });
  })
相关推荐
Yolanda941 分钟前
【项目经验】vue h5移动端禁止缩放
前端·javascript·vue.js
广州华水科技1 小时前
单北斗GNSS形变监测一体机在基础设施安全中的应用与技术优势
前端
EndingCoder1 小时前
案例研究:从 JavaScript 迁移到 TypeScript
开发语言·前端·javascript·性能优化·typescript
阿珊和她的猫3 小时前
React 路由:构建单页面应用的导航系统
前端·react.js·状态模式
Amumu121383 小时前
Vue脚手架(二)
前端·javascript·vue.js
花间相见3 小时前
【LangChain】—— Prompt、Model、Chain与多模型执行链
前端·langchain·prompt
lichenyang4534 小时前
从零开始构建 React 文档系统 - 完整实现指南
前端·javascript·react.js
比特森林探险记4 小时前
Hooks、状态管理
前端·javascript·react.js
landonVM4 小时前
Linux 上搭建 Web 服务器
linux·服务器·前端
css趣多多4 小时前
路由全局守卫
前端