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

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


Le vendredi 03 juin 2011 12:32:16, Goo Creations a écrit :
> Ah, thanks. This solved the problem.
> I however face the problem that I don't know which data type the image has.
> So I can't hardcode the array to unsigned short, since it might sometimes
> be a int32 or even a float. So is there a way to do this in a generic way?

I'm not sure what you are trying to do exactly. There are various 
possibilities. Use good all switch() case() to care for different data types, 
which can be combined with using C++ templates to write generic code to reduce 
the amount of copying&pasting, use GDALCopyWords() to convert an array from 
one type to another one...

> 
> Chris
> 
> On Fri, Jun 3, 2011 at 12:21 PM, Even Rouault
> 
> <even.rouault at mines-paris.org>wrote:
> > 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