[gdal-dev] The problem when using ReadRaster(C#) method

Even Rouault even.rouault at mines-paris.org
Wed Aug 31 07:22:00 EDT 2011


Selon Tamas Szekeres <szekerest at gmail.com>:

> You should make sure about the actual representation of the raster image,
> whether it represents the pixel as floats as you have used float arrays in
> the code. The last 2 parameters are used to define the byte offset of the
> pixels and lines in the image ( for more info see the
>
RasterIO<http://www.gdal.org/classGDALRasterBand.html#5497e8d29e743ee9177202cb3f61c3c7>documentation).

Tamas, I would expect ReadRaster() to automatically convert to GDT_Float32 if
the provided buffer is of type float[]. As far as I can see in gdal_csharp.i, it
looks like they are doing so. So at worst, you could see loss of precisions if
the type of the array used doesn't exactly match the data type of the raster,
but you shouldn't see random values.

hitweiran, for a packed buffer as the one you use, the last parameters that give
the pixel size and line stride in bytes can be left to 0. In that case, the
function assumes the provided buffer is packed. So you should be fine.

All in all, I'm not sure why it doesn't work for you.

Did you try inspecting the values of your raster with gdal utilities first ?
Like gdallocationinfo for example.

>
> Further C# related samples can be found here:
> http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/csharp/apps/GDALRead.cs
>
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/csharp/apps/GDALReadDirect.cs
>
> Best regards,
>
> Tamas
>
>
>
> 2011/8/31 hitweiran <hitweiran at sina.com>
>
> > I want to read a multiband image(in format of .img) by using method
> > ReadRaster, however, I encount some problems. I just can't understand why
> > the data loaded to memory is wrong?
> > the code is like this
> >
> > OSGeo.GDAL.Dataset dataSet =
> > OSGeo.GDAL.Gdal.Open(@"G:\building.img",OSGeo.GDAL.Access.GA_ReadOnly);
> > int width = dataSet.RasterXSize;
> > int height = dataSet.RasterYSize;
> > int Band_Numbers = dataSet.RasterCount;
> > float [] buffer_first = new float [width * height];
> > OSGeo.GDAL.Band One_Band = dataSet.GetRasterBand(1);
> > One_Band.ReadRaster(0, 0, width, height,buffer_first, width, height, 0,0);
> >
> > However, the output is wrong!
> > I watch the variable "buffer_first" in watch window, and discoverse that
> > the
> > data is quite terrible-some data is such as 2.818478E+21,5.065476E+32, or
> > -6.131138E-11 and so on;
> > So I suspect my datatype is mismatch to data in img file, or the last two
> > arguments in ReadRaster is mistake, because I don't know the meaning of
> > last
> > two parameters.
> > Thanks a lot for your answer!
> >
> > --
> > View this message in context:
> >
>
http://osgeo-org.1803224.n2.nabble.com/The-problem-when-using-ReadRaster-C-method-tp6745826p6745826.html
> > Sent from the GDAL - Dev mailing list archive at Nabble.com.
> > _______________________________________________
> > gdal-dev mailing list
> > gdal-dev at lists.osgeo.org
> > http://lists.osgeo.org/mailman/listinfo/gdal-dev
> >
>




More information about the gdal-dev mailing list