This project has moved and is read-only. For the latest updates, please go here.

Problems using GdalRasterLayer

Topics: General Topics
Feb 26, 2007 at 9:05 PM
I'm having problems correctly rendering an image using GdalRasterLayer.

I'm using SharpMap (change set 18222) in a simple winform, displaying image in a PictureBox control. The source is a b/w GeoTiff image. The rendered image is colored with 0,0,0 and 1,1,1 colors which makes it appear black.

Also using various GeoTiff images almost randomly throws memory exceptions

eg: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt...
at: GDAL.gdalPINVOKE.Band_ReadRaster(HandleRef jarg1, Int32 jarg2, Int32 jarg3, Int32 jarg4, Int32 jarg5, IntPtr jarg6, Int32 jarg7, Int32 jarg8, Int32 jarg9, Int32 jarg10, Int32 jarg11")
around: GetPreview method
line: band.ReadRaster(x1, y1, x1width, y1height, buffer, size.Width, size.Height, (int)GT.HorizontalPixelResolution, (int)GT.VerticalPixelResolution);)

I'm looking for any suggestions.

Feb 26, 2007 at 9:52 PM

The gdal raster extension doesn't handle all colour types correctly.
We hope to fix this issue soon.

Feb 26, 2007 at 11:50 PM

Btw. codekaizer is looking to contact you :)

Feb 26, 2007 at 11:57 PM

You can solve this problem by recompiling your SharpMap.Extensions project after making the following change to the source. In the ReadRaster method in the GDALRasterLayer.cs file, change the line:

band.ReadRaster(x1, y1, x1width, y1height, buffer, size.Width, size.Height, (int)GT.HorizontalPixelResolution, (int)GT.VerticalPixelResolution);


band.ReadRaster(x1, y1, x1width, y1height, buffer, size.Width, size.Height, 0, 0);

This simply takes the last two parameters and makes them zero. If you look up the GDAL reference material on the web, you'll find that this is the default values anyway. The reason that the layer has been working for some of your GeoTIFFs is because their pixel resolution values are < 1, thereby rounding down to 0 anyway!

I've asked for this to be changed in the source, so hopefully this won't be an ongoing issue. Also note that the constructor the for GDALRasterLayer opens the file in Update mode. This causes problems when run on a web host, so you might want to change the mode to ReadOnly (i.e. change the 1 to 0 in the call).

Feb 27, 2007 at 4:30 PM
Edited Feb 27, 2007 at 4:55 PM

Going through my references I've noticed an incorrect reference.Your suggestion works now so a big thank you :)


P.S. the rendered image is still the wrong color.
Mar 1, 2007 at 6:08 PM
Hi Goran,

I' knowing of the colour issue and I'm still working on it.

Mar 1, 2007 at 7:52 PM

Great and thanks!