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


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

新建tifs.js

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(){
    //本地测试文件 返回url
    function getFileUrl() {
      return new URL(`@/utils/dsm.tif`, import.meta.url).href;
    }
     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;
        //old  已废弃
       // 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();
//new
    axios
      .get(`https://api.maptiler.com/coordinates/transform/${west},${north}.json?key=g0y01TmlRNajMPkic9lG&s_srs=${code}&t_srs=4326`)
      .then(res => {
          let { x: w, y: n } = res.results[0];
      });
    axios
      .get(`https://api.maptiler.com/coordinates/transform/${east},${south}.json?key=g0y01TmlRNajMPkic9lG&s_srs=${code}&t_srs=4326`)
      .then(res => {
        let { x: e, y: s } = res.results[0];
      });
    //如果tif格式导出的时候设置成4326 这样就不用转换了,
    //新版本转换需要翻墙获取key,https://cloud.maptiler.com/auth/widget?next=https://cloud.maptiler.com/maps/
      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

 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,
            tileWidth: 8176,//新版本需要设置宽高
            tileHeight: 4032
          })
        );

        viewer.camera.setView({
          destination: rectangle,
        });
  })

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐