GDAL-影像按照波段进行切片(c#)

       public void SplitImagery(string pFilePath)
        {
            Dataset pDataset = Gdal.Open(pFilePath, Access.GA_ReadOnly);
            double[] pGeoTransform = new double[6];
            pDataset.GetGeoTransform(pGeoTransform);
            string pProjection = pDataset.GetProjectionRef();
            Driver pDriver = pDataset.GetDriver();
            int imgSizeX = pDataset.RasterXSize;
            int imgSizeY = pDataset.RasterYSize;
            int iBandCount = pDataset.RasterCount;
            string pDirectoryPath = System.IO.Path.GetDirectoryName(pFilePath);
            string pFileName = System.IO.Path.GetFileNameWithoutExtension(pFilePath);
            string pFileExtentsion = System.IO.Path.GetExtension(pFilePath);
            for (int iBand = 0; iBand < iBandCount; iBand++)
            {
                Band pBand = pDataset.GetRasterBand(iBand+1);
                const int block_size = 2048;//裁剪块的大小
                int rowChunkIndex = 0;
                for (int yIndex = 0; yIndex < imgSizeY; yIndex += block_size)
                {
                    rowChunkIndex++;
                    int columnChunkIndex = 0;
                    for (int xIndex = 0; xIndex < imgSizeX; xIndex += block_size)
                    {
                        columnChunkIndex++;
                        string pSplitedFilePath = pDirectoryPath + "\\" + pFileName+"_"+ iBand + "_"+ rowChunkIndex+"_"+ columnChunkIndex + pFileExtentsion;
                        //定义两个变量来保存分块大小
                        int nXBK = block_size;//列
                        int nYBK = block_size;//行
                        //如果最下面和最右边的块不够256,剩下多少读取多少
                        if (yIndex + block_size > imgSizeY)     //最下面的剩余块
                            nYBK = imgSizeY - yIndex;
                        if (xIndex + block_size > imgSizeX)     //最右侧的剩余块
                            nXBK = imgSizeX - xIndex;
                        //将图像数据集读到pBuf指针中
                        double[] buffer = new double[nXBK * nYBK];
                        pBand.ReadRaster(xIndex, yIndex, nXBK, nYBK, buffer, nXBK, nYBK, 0, 0);
                        double[] newBuffer = new double[nXBK * nYBK];
                        for (int i = 0; i < nXBK; i++)
                        {
                            for (int j = 0; j < nYBK; j++)
                            {
                                double val =  buffer[j * nXBK + i];

                                newBuffer[j * nXBK + i] = val;//分块读入计算,分块写入
                            }
                        }
                        //定义DataType为64位会产生成倍的数据冗余,但是调试安全
                        Dataset pSpliteDataset = pDriver.Create(pSplitedFilePath, nXBK, nYBK, 1, DataType.GDT_Float64, null);
                        //重新定义裁剪的影像的仿射变换
                        double[] pSpliteGeoTransform = new double[6];
                        pSpliteGeoTransform[0] = pGeoTransform[0] + pGeoTransform[1] * xIndex + pGeoTransform[2] * yIndex;
                        pSpliteGeoTransform[1] = pGeoTransform[1];
                        pSpliteGeoTransform[2] = pGeoTransform[2];
                        pSpliteGeoTransform[3] = pGeoTransform[3] + pGeoTransform[4] * xIndex + pGeoTransform[5] * yIndex;
                        pSpliteGeoTransform[4] = pGeoTransform[4];
                        pSpliteGeoTransform[5] = pGeoTransform[5];
                        pSpliteDataset.SetGeoTransform(pSpliteGeoTransform);
                        pSpliteDataset.SetProjection(pProjection);
                        pSpliteDataset.WriteRaster(0, 0, nXBK, nYBK, newBuffer, nXBK, nYBK, 1, new int[] { 1 }, 0, 0, 0);

                        buffer = null;
                        newBuffer = null;
                        pSpliteDataset.FlushCache();
                        GC.Collect();
                    }
                }
            }
        }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值