Thanks, GDALCopyWords was what I was looking for<br><br>Thanks<br><br>Chris<br><br><div class="gmail_quote">On Fri, Jun 3, 2011 at 12:39 PM, Even Rouault <span dir="ltr"><<a href="mailto:even.rouault@mines-paris.org">even.rouault@mines-paris.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">Le vendredi 03 juin 2011 12:32:16, Goo Creations a écrit :<br>
<div class="im">> Ah, thanks. This solved the problem.<br>
> I however face the problem that I don't know which data type the image has.<br>
> So I can't hardcode the array to unsigned short, since it might sometimes<br>
> be a int32 or even a float. So is there a way to do this in a generic way?<br>
<br>
</div>I'm not sure what you are trying to do exactly. There are various<br>
possibilities. Use good all switch() case() to care for different data types,<br>
which can be combined with using C++ templates to write generic code to reduce<br>
the amount of copying&pasting, use GDALCopyWords() to convert an array from<br>
one type to another one...<br>
<div><div></div><div class="h5"><br>
><br>
> Chris<br>
><br>
> On Fri, Jun 3, 2011 at 12:21 PM, Even Rouault<br>
><br>
> <<a href="mailto:even.rouault@mines-paris.org">even.rouault@mines-paris.org</a>>wrote:<br>
> > Le vendredi 03 juin 2011 12:13:41, Goo Creations a écrit :<br>
> > > Hi all<br>
> > ><br>
> > > I have a strange problem when writing to a GDAL dataset. I'm currently<br>
> > > doing the following:<br>
> > ><br>
> > > *GDALDataset *mInputDataset = (GDALDataset*) GDALOpen("in.tif",<br>
> > > GA_ReadOnly);<br>
> > > GDALDataType type =<br>
> > > mInputDataset->GetRasterBand(1)->GetRasterDataType(); int mWidth =<br>
> > > mInputDataset->GetRasterXSize();<br>
> > > int mHeight = mInputDataset->GetRasterYSize();<br>
> > ><br>
> > > GDALDataset *mOutputDataset =<br>
> ><br>
> > mInputDataset->GetDriver()->Create("out.tif",<br>
> ><br>
> > > mWidth, mHeight, mBands, type, 0);<br>
> > ><br>
> > > int *data = <<get data for entire band>><br>
> > > mOutputDataset->GetRasterBand(1)->RasterIO(GF_Write, 0, 0,<br>
> ><br>
> > aligner.width(),<br>
> ><br>
> > > aligner.height(), data, aligner.width(), aligner.height(), type, 0, 0);<br>
> > > mOutputDataset->FlushCache();<br>
> > > *<br>
> > ><br>
> > > The input image provided has a datatype of UINT16. When I hardcode all<br>
> > > "type" variables in the above example to GDT_Int32, then the image is<br>
> > > correctly created, but when I leave the example as is (hence with<br>
> > > GDT_UInt16 assigned to "type"), then I get a strange problem that every<br>
> > > second column has a value of 0. So every odd column has the correct<br>
> ><br>
> > values<br>
> ><br>
> > > in, but every even column has values of 0. I've checked the data array,<br>
> > > and all the values in there are correct.<br>
> > ><br>
> > > Does anyone know why this is happening?<br>
> ><br>
> > Yes, the type parameter must be consistent with the C type of the data<br>
> > array,<br>
> > otherwise weird things can happen, including crashes.<br>
> ><br>
> > In your exemple data is of type int*, so type must be set to GDT_Int32.<br>
> > If you want to write GDT_UInt16, then data must be declared as being of<br>
> > type<br>
> > unsigned short*<br>
</div></div></blockquote></div><br>