[gdal-dev] Writing raster - 0 in every even column

Even Rouault even.rouault at mines-paris.org
Fri Jun 3 06:21:53 EDT 2011


Le vendredi 03 juin 2011 12:13:41, Goo Creations a écrit :
> Hi all
> 
> I have a strange problem when writing to a GDAL dataset. I'm currently
> doing the following:
> 
> *GDALDataset *mInputDataset = (GDALDataset*) GDALOpen("in.tif",
> GA_ReadOnly);
> GDALDataType type = mInputDataset->GetRasterBand(1)->GetRasterDataType();
> int mWidth = mInputDataset->GetRasterXSize();
> int mHeight = mInputDataset->GetRasterYSize();
> 
> GDALDataset *mOutputDataset = mInputDataset->GetDriver()->Create("out.tif",
> mWidth, mHeight, mBands, type, 0);
> 
> int *data = <<get data for entire band>>
> mOutputDataset->GetRasterBand(1)->RasterIO(GF_Write, 0, 0, aligner.width(),
> aligner.height(), data, aligner.width(), aligner.height(), type, 0, 0);
> mOutputDataset->FlushCache();
> *
> 
> The input image provided has a datatype of UINT16. When I hardcode all
> "type" variables in the above example to GDT_Int32, then the image is
> correctly created, but when I leave the example as is (hence with
> GDT_UInt16 assigned to "type"), then I get a strange problem that every
> second column has a value of 0. So every odd column has the correct values
> in, but every even column has values of 0. I've checked the data array,
> and all the values in there are correct.
> 
> Does anyone know why this is happening?

Yes, the type parameter must be consistent with the C type of the data array, 
otherwise weird things can happen, including crashes.

In your exemple data is of type int*, so type must be set to GDT_Int32.
If you want to write GDT_UInt16, then data must be declared as being of type 
unsigned short*



More information about the gdal-dev mailing list