[gdal-dev] GDAL GeoTiff Corrupt on Write (C++ )

Dusted kris_nich at hotmail.co.uk
Fri Feb 27 03:28:01 PST 2015


I'm getting corrupt output when writing a GeoTiff using GDAL API (v1.10 -
C++). The raster geotransform is correct, the block is written in the
correct position but the pixels are written at random positions and values
within the block.

Example:  http://i.imgur.com/mntnAfK.png <http://i.imgur.com/mntnAfK.png>  

Method: Open a GDAL Raster --> copy projection info & size --> create output
GeoTiff --> write a block from array at offset.

Code:

//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;


Any pointers / help much appreciated!

Chris



--
View this message in context: http://osgeo-org.1560.x6.nabble.com/GDAL-GeoTiff-Corrupt-on-Write-C-tp5190440.html
Sent from the GDAL - Dev mailing list archive at Nabble.com.


More information about the gdal-dev mailing list