我是 gdal 和 c++ 的新手,我正在尝试创建一个按块读取栅格的函数(使用 RasterIO 而不是 ReadBlock)对值执行某些操作并将结果写入新的栅格文件
我收到一个错误:
Test.exe 中 0x00007FFDFB517BC4 (gdal201.dll) 处的未处理异常:0xC0000005:访问冲突写入位置 0x00000230E2593000 行 rasterBand->RasterIO(GDALRWFlag::GF_Read, j * readCols, i * readRows, readCols, readRows, rasterBlock, readCols, readRows , GDALDataType::GDT_CFloat32, 0, 0)
我有一个非常相似的功能可以正常工作,但它逐行读取像素值
任何帮助表示赞赏
函数如下
谢谢
#include <iostream>
#include "gdal_priv.h"
using namespace std;
void SlopeBlock()
{
int readRows = 2048;
int readCols = 2048;
int nBlockXSize;
int nBlockYSize;
int rowOff = 0;
int colOff = 0;
int nRows = 0;
int nCols = 0;
double noData = -9999;
GDALAllRegister();
GDALDataset* dem = (GDALDataset*)GDALOpen("path/EUD_CP-DEMS_4500025000-AA.tif", GA_ReadOnly);
GDALDriver *gTIF = GetGDALDriverManager()->GetDriverByName(dem->GetDriverName());
double geoTransform[6];
dem->GetGeoTransform(geoTransform);
GDALRasterBand* rasterBand = dem->GetRasterBand(1);
nCols = rasterBand->GetXSize();
nRows = rasterBand->GetYSize();
noData = rasterBand->GetNoDataValue();
//rasterBand->GetBlockSize(&nBlockXSize, &nBlockYSize);
int nXBlocks = (rasterBand->GetXSize() + readCols - 1) / readCols;
int nYBlocks = (rasterBand->GetYSize() + readRows - 1) / readRows;
cout << nYBlocks << "___" << nXBlocks << "\n";
//Slope
GDALDataset *slope = gTIF->Create("path/slopeBlock.tif", nCols, nRows, 1, GDT_Float32, NULL);
slope->SetGeoTransform(geoTransform);
for (int i = 0; i < nYBlocks; i++)
{
for (int j = 0; j < nXBlocks; j++)
{
float* rasterBlock = (float*)CPLMalloc(readCols*readRows*sizeof(float));
float* rasterBlockOut = (float*)CPLMalloc(readCols*readRows*sizeof(float));
if (rasterBand != nullptr)
rasterBand->RasterIO(GDALRWFlag::GF_Read, j * readCols, i * readRows, readCols, readRows, rasterBlock, readCols, readRows, GDALDataType::GDT_CFloat32, 0, 0);
//Not sure if is working
for (int jb = 0; jb < readCols; jb++)
{
if (rasterBlock[jb] == noData)
{
rasterBlockOut[jb] = noData;
}
else
{
rasterBlockOut[jb] = rasterBlock[jb];
}
}
slope->GetRasterBand(1)->RasterIO(GDALRWFlag::GF_Write, j * readCols, i * readRows, readCols, readRows, rasterBlockOut, readCols, readRows, GDALDataType::GDT_CFloat32, 0, 0);
CPLFree(rasterBlock);
CPLFree(rasterBlockOut);
}
}
GDALClose(dem);
GDALClose(slope);
}