java通过gdal解析单波段tiff并根据业务生成新的tiff文件

7 篇文章 0 订阅
/**
     * 通用新增为tif上色 现在tiff不上色
     *
     * @param tifUrl 文件地址
     * @return levels索引位置对应生成的文件地址
     */
    public String tiffProcess(String tifUrl) {
        Dataset open = gdal.Open(tifUrl, gdalconstConstants.GA_ReadOnly);
        /*
          获取gtiff 数据
         */
        //行数 高
        int iYSize = open.getRasterYSize();
        //列数 宽
        int iXSize = open.getRasterXSize();
        //仿射矩阵
        double[] doubles = open.GetGeoTransform();
        //地图投影信息
        String projection = open.GetProjection();
        //将数据写成数组
        Band sourceBand = open.GetRasterBand(1);
        double[] doubles1 = new double[2];
        sourceBand.ComputeRasterMinMax(doubles1);
        double[][] data = new double[iYSize][iXSize];
        for (int i = 0; i < iYSize; i++) {
            double[] d1 = new double[iXSize];
            sourceBand.ReadRaster(0, i, iXSize, 1, d1);    //读取一行数据
            for (int i1 = 0; i1 < d1.length; i1++) {
                if (d1[i1] > 0) {
                    data[i][i1] = d1[i1];
                }
            }
        }


        /*
          生成tiff
         */
        Driver driver = gdal.GetDriverByName("GTiff");
        String newFile = tifUrl.substring(0, tifUrl.length() - 4) + ".tif";
        Dataset create = driver.Create(newFile, iXSize, iYSize, 1, gdalconst.GDT_Byte);
        //设置仿射矩阵
        create.SetGeoTransform(doubles);
        //设置投影信息
        create.SetProjection(projection);
        //设置波段
        Band targetBand = create.GetRasterBand(1);
        double[] colorRange = new double[2];
        sourceBand.ComputeRasterMinMax(colorRange);
        targetBand.SetNoDataValue(0);
        //设置波段值
        int[] temp = new int[1];
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                temp[0] = convertPixel(colorRange[0], colorRange[1], data[i][j]);
                if (temp[0] != 0) {
                    targetBand.WriteRaster(j, i, 1, 1, temp);
                }
            }
        }


        open.delete();
        create.delete();

        return newFile;
    }

    /**
     * 像素波段等级区间判断转换 1-4 高-低
     *
     * @param min 像素最小值
     * @param max 像素最大值
     * @param now 比较值
     */
    public static int convertPixel(double min, double max, double now) {
    	//因波段值存在小数不好计算 这里默认相乘 可以根据实际业务定义
        min = 100000D * min;
        max = max * 100000D;
        now = now * 100000D;
        double v = (max - min) / 4;
        double v2 = v * 2;
        double v3 = v * 3;
        if (min <= now && (min + v) > now) {//高
            return 1;
        } else if ((min + v) <= now && (min + v2) > now) {//较高
            return 2;
        } else if ((min + v2) <= now && (min + v3) > now) { //中
            return 3;
        } else if ((min + v3) <= now && max >= now) { //低
            return 4;
        } else {
            return 0;
        }
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在three.js显示tiff文件,您需要使用gdal库将其加载为图像数据,然后将其转换为three.js支持的格式。以下是一些步骤: 1. 安装gdal库 使用以下命令安装gdal库: ``` pip install gdal ``` 2. 使用gdal加载tiff文件 使用以下代码加载tiff文件: ```python from osgeo import gdal # 打开tiff文件 ds = gdal.Open('path/to/tiff/file') # 读取tiff文件的图像数据 data = ds.ReadAsArray() # 获取图像数据的相关信息 width = ds.RasterXSize height = ds.RasterYSize channels = ds.RasterCount # 关闭tiff文件 ds = None ``` 3. 转换图像数据为three.js支持的格式 使用以下代码将图像数据转换为three.js支持的格式: ```javascript // 将图像数据转换为three.js支持的格式 var texture = new THREE.DataTexture(data, width, height, channels, THREE.RGBAFormat); // 设置纹理属性 texture.magFilter = THREE.LinearFilter; texture.minFilter = THREE.LinearFilter; texture.wrapS = THREE.ClampToEdgeWrapping; texture.wrapT = THREE.ClampToEdgeWrapping; // 创建材质 var material = new THREE.MeshBasicMaterial({ map: texture }); // 创建网格 var geometry = new THREE.PlaneGeometry(width, height); var mesh = new THREE.Mesh(geometry, material); // 添加网格到场景 scene.add(mesh); ``` 这些代码将创建一个平面网格,其纹理使用从tiff文件加载的图像数据。您可以根据需要调整平面网格的大小和位置。 请注意,由于gdal库是用C++编写的,因此在使用Python时可能会遇到一些性能问题。如果您需要更高的性能,请考虑使用其他库,例如OpenCV。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值