使用 GDAL API (v1.10 - C++) 编写 GeoTiff 时,我得到了损坏的输出。栅格地理变换是正确的,块被写入正确的位置,但像素被写入块内的随机位置和值。
示例:http: //i.imgur.com/mntnAfK.png
方法:打开一个 GDAL Raster --> 复制投影信息和大小 --> 创建输出 GeoTiff --> 从数组中的偏移处写入一个块。
代码:
//Open the input DEM
const char* demFName = "/Users/mount.dem";
const char* outFName = "/Users/geodata/out_test.tif";
auto poDataset = ioUtils::openDem(demFName);
double adfGeoTransform[6];
poDataset->GetGeoTransform( adfGeoTransform );
//Setup driver
const char *pszFormat = "GTiff";
GDALDriver *poDriver;
poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
char *pszSRS_WKT = NULL;
GDALRasterBand *poBand;
//Get size from input Raster
int xSize = poDataset->GetRasterXSize();
int ySize = poDataset->GetRasterYSize();
//Set output Dataset
GDALDataset *poDstDS;
char **papszOptions = NULL;
//Create output geotiff
poDstDS = poDriver->Create( outFName, xSize, ySize, 1, GDT_Byte, papszOptions );
//Get the geotrans from the input geotrans
poDataset->GetGeoTransform( adfGeoTransform );
poDstDS->SetGeoTransform( adfGeoTransform );
poDstDS->SetProjection( poDataset->GetProjectionRef() );
//Create some data to write
unsigned char rData[512*512];
//Assign some values other than 0
for (int col=0; col < 512; col++){
for (int row=0; row < 512; row++){
rData[col*row] = 50;
}
}
//Write some data
poBand = poDstDS->GetRasterBand(1);
poBand->RasterIO( GF_Write, 200, 200, 512, 512,
rData, 512, 512, GDT_Byte, 0, 0 );
//Close
GDALClose( (GDALDatasetH) poDstDS );
std::cout << "Done" << std::endl;
任何我出错的想法/指针都非常感谢。