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,
        });
  })
相关推荐
天蓝色的鱼鱼11 分钟前
画1万个图形就卡成PPT?试试这款国产高性能2D引擎
前端·javascript
wuxia211815 分钟前
用Node.js为网站首页绑定数据
javascript·node.js
云水一下20 分钟前
JavaScript 从零基础到精通系列:异步编程与网络请求
前端·javascript
卡卡军38 分钟前
🌈 react-sketch-ruler v3 升级之旅:当 React 遇上跨框架标尺引擎
前端·react.js
Asmewill42 分钟前
DeepAgents学习笔记三(Backend记忆存储)
前端
Alan Lu Pop1 小时前
前端开发助手
前端·智能体
程序员鱼皮1 小时前
我用 GitHub 仓库养 AI 龙虾,自动开发上线项目!保姆级教程
前端·人工智能·ai·程序员·github·编程·ai编程
27669582921 小时前
京东随机变速滑块拼图验证码识别(京东E卡)
java·服务器·前端·python·京东滑块·京东变速滑块·京东e卡绑卡
এ慕ོ冬℘゜1 小时前
手写生产级 jQuery Toast 轻量提示组件|零插件依赖、动画流畅、极简高
前端·javascript·jquery
Oneslide2 小时前
UI设计-企业OA风格
前端