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

GDAL Raster Layer histogram "index out of range" bug

Topics: SharpMap v0.9 / v1.x
Dec 29, 2009 at 4:21 PM

I am trying to add a new GDAL Raster Layer  using the GDAL Raster Layer in  sharpmap.extensions

I get an "index out of range" exception in line 1004 of file  GDalRasterLayer.cs 

_histogram[ch[i]][(int) intVal[i]]++;

any ideas how to resolve that problem ?

my code includes


Dim gdalRaster As New GdalRasterLayer(Name, mFilename) mActualLayer = gdalRaster gdalRaster.TransparentColor = Color.FromArgb(29, 29, 29) gdalRaster.DisplayIR = True gdalRaster.ColorCorrect = True gdalRaster.DisplayCIR = True gdalRaster.SRID = 2100 gdalRaster.MaxVisible = 100000000 gdalRaster.MinVisible = 1 MapBox.Map.Layers.Add(gdalRaster)


System.ArgumentOutOfRangeException was unhandled
  Message="Ο δείκτης βρισκόταν εκτός περιοχής. Η τιμή του δεν πρέπει να είναι αρνητική και πρέπει να είναι μικρότερη από ή ίση με το μέγεθος της συλλογής.\r\nΌνομα παραμέτρου: index"
       σε System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
       σε System.ThrowHelper.ThrowArgumentOutOfRangeException()
       σε System.Collections.Generic.List`1.get_Item(Int32 index)
       σε SharpMap.Layers.GdalRasterLayer.GetPreview(Dataset dataset, Size size, Graphics g, BoundingBox displayBbox, ICoordinateSystem mapProjection, Map map) στο E:\From To Server\NTUA\GIS\Code\repositories\Sharpmap\SHARPMAP CODEPLEX\Trunk\SharpMap.Extensions\Layers\GdalRasterLayer.cs:γραμμή 1004
       σε SharpMap.Layers.GdalRasterLayer.Render(Graphics g, Map map) στο E:\From To Server\NTUA\GIS\Code\repositories\Sharpmap\SHARPMAP CODEPLEX\Trunk\SharpMap.Extensions\Layers\GdalRasterLayer.cs:γραμμή 440
       σε SharpMap.Map.GetMap() στο E:\From To Server\NTUA\GIS\Code\repositories\Sharpmap\SHARPMAP CODEPLEX\Trunk\SharpMap\Map\Map.cs:γραμμή 171
       σε SharpMap.Forms.MapImage.Refresh() στο E:\From To Server\NTUA\GIS\Code\repositories\Sharpmap\SHARPMAP CODEPLEX\Trunk\SharpMap.UI\Forms\MapImage.cs:γραμμή 370
       σε MapActions.AddNewLayer() στο E:\From To Server\NTUA\GIS\Code\LATEST\NTUA EEMRU GIS v0.9_SERVER\NTUA EEMRU GIS v0.9\PresentationLayer\Helper\Unused\MapActions.vb:γραμμή 364
       σε frmLayers.tsbAddLayer_Click(Object sender, EventArgs e) στο E:\From To Server\NTUA\GIS\Code\LATEST\NTUA EEMRU GIS v0.9_SERVER\NTUA EEMRU GIS v0.9\PresentationLayer\Forms\SecondaryWindows\frmLayers.vb:γραμμή 792
       σε System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
       σε System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)
       σε System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
       σε System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
       σε System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
       σε System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
       σε System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
       σε System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       σε System.Windows.Forms.Control.WndProc(Message& m)
       σε System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       σε System.Windows.Forms.ToolStrip.WndProc(Message& m)
       σε System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       σε System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       σε System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       σε System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       σε System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       σε System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       σε System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       σε System.Windows.Forms.Application.Run(ApplicationContext context)
       σε Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       σε Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       σε Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       σε My.MyApplication.Main(String[] Args) στο 17d14f5c-a337-4978-8281-53493378c1071.vb:γραμμή 81
       σε System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       σε System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       σε Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       σε System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       σε System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       σε System.Threading.ThreadHelper.ThreadStart()

Jan 2, 2010 at 10:41 PM

Hello Agelos,

what kind is your raster data? Could you send me a sample?



Jan 3, 2010 at 8:49 PM

my proffered file type is ASCII Grid (extension *.asc)

Grids are useful for representing geographic phenomena that vary continuously over space and for performing spatial modeling and analysis.

I want to show a colored representation of a grid on top of my map that is actually presenting precipitation values.

see the example images loaded to

you can download the files i use from here

this is a screen shot from my application using sharpmap

but i want to display it like this (this is a screen shot from another gis application) :

using some sort of value representation

This screenshots were created using the GeoTiff file format.

I guess i can also use Birary (.bgd) file type or a GeoTiff file format.

Jan 4, 2010 at 10:18 AM

Hello Agelos,

you need to:

  • Use a different displaying algorithm to display (elevation data).
    Unfortunatly this patch hasn't made it to the trunk, and cannot the elevation layer cannot be used as is)
  • Have some other program generate the GeoTiffs with the color you want.
  • Try Virtual Format to set up color transfroming. This may not work with the binaries provided via FWTools.
    In that case you'll have to compile GDAL yourself or raise an issue requesting to change from FWTools
    dependency to latest GDAL.



Jan 5, 2010 at 3:59 PM

i am considering your 2nd and 2rd bullet sujestions.

2nd option:

do anyone knows any other program i should use to convert to geotiffs ?

3rd option:

I am considering the virtual format but i cannot test the color transfortming using the color table tag but it does not work for me with VERSION: FWTools 2.4.2  (Jun 24, 2009)

do anyone knows any other program i should use to test the options of the vrt xml file ?


Jan 5, 2010 at 8:52 PM

Hello Agelos,

QGIS has a pretty good GDAL coverage, with FWTools a program called OpenEV (or something like that) is shipped. You can test surly test your settings using that.

Are you saying that sharpmap is not displaying the virtual raster at all? Could you post your vrt-xml file?



Jan 7, 2010 at 10:45 AM
Edited Jan 7, 2010 at 10:51 AM

Hello Agelos,

to display the ASCII Grid with sharpmap and the colors you want with sharpmap
leaving it as is, your virtual raster xml-file has to look somewhat like this:


<VRTDataset rasterXSize="251" rasterYSize="207">
  <SRS>PROJCS[&quot;WGS 84 / UTM zone 37N&quot;,GEOGCS[&quot;WGS 84&quot;,DATUM[&quot;WGS_1984&quot;,SPHEROID[&quot;WGS 84&quot;,6378137,298.257223563,AUTHORITY[&quot;EPSG&quot;,&quot;7030&quot;]],AUTHORITY[&quot;EPSG&quot;,&quot;6326&quot;]],PRIMEM[&quot;Greenwich&quot;,0],UNIT[&quot;degree&quot;,0.0174532925199433],AUTHORITY[&quot;EPSG&quot;,&quot;4326&quot;]],PROJECTION[&quot;Transverse_Mercator&quot;],PARAMETER[&quot;latitude_of_origin&quot;,0],PARAMETER[&quot;central_meridian&quot;,39],PARAMETER[&quot;scale_factor&quot;,0.9996],PARAMETER[&quot;false_easting&quot;,500000],PARAMETER[&quot;false_northing&quot;,0],UNIT[&quot;metre&quot;,1,AUTHORITY[&quot;EPSG&quot;,&quot;9001&quot;]],AUTHORITY[&quot;EPSG&quot;,&quot;32637&quot;]]</SRS>
  <GeoTransform> 5.2377000000000000e+005, 2.5500000000000000e+002, 0.0000000000000000e+000, 4.2054600000000000e+006, 0.0000000000000000e+000,-2.5500000000000000e+002</GeoTransform>
    <MDI key="AREA_OR_POINT">Area</MDI>
  <VRTRasterBand dataType="Byte" band="1">
      <SourceFilename relativeToVRT="1">contours_sample_polyline_play_polyline.asc</SourceFilename>
      <SourceProperties RasterXSize="251" RasterYSize="207" DataType="Int32" BlockXSize="251" BlockYSize="8"/>
      <SrcRect xOff="0" yOff="0" xSize="251" ySize="207"/>
      <DstRect xOff="0" yOff="0" xSize="251" ySize="207"/>
  <VRTRasterBand dataType="Byte" band="2">
      <SourceFilename relativeToVRT="1">contours_sample_polyline_play_polyline.asc</SourceFilename>
      <SourceProperties RasterXSize="251" RasterYSize="207" DataType="Int32" BlockXSize="251" BlockYSize="8"/>
      <SrcRect xOff="0" yOff="0" xSize="251" ySize="207"/>
      <DstRect xOff="0" yOff="0" xSize="251" ySize="207"/>
  <VRTRasterBand dataType="Byte" band="3">
      <SourceFilename relativeToVRT="1">contours_sample_polyline_play_polyline.asc</SourceFilename>
      <SourceProperties RasterXSize="251" RasterYSize="207" DataType="Int32" BlockXSize="251" BlockYSize="8"/>
      <SrcRect xOff="0" yOff="0" xSize="251" ySize="207"/>
      <DstRect xOff="0" yOff="0" xSize="251" ySize="207"/>


This virtual raster definition sets up a rgb raster from your data. You need to do this, because
GdalRasterLayer of sharpmap does not support palette/colortable based raster data. You might
have to set the color for no data value to some other value. I chose yellow, since the yellow areas
of the returned image are set transparent. I think the nodatavalue is not handled.



Jan 7, 2010 at 1:55 PM

Thanks FObermaier this seems to work fine on sharpmap too and displays nice colors!

weirdly enough using the ASCII Grid does not fail if i use it inside the vrt file even though it crashes if i use it by it's own.

now i have to find out how to show the colors i want for each category.

so if i understand correctly in order to show the following colors for each category (100,200,300...etc) as shown below

value : r , g , b
100: 128, 250, 1
200: 132, 230, 6
300: 136, 208, 12
400: 138, 197, 15
500: 143, 174, 21
600: 145, 162, 24
700: 149, 139, 30
800: 152, 124, 34
900: 155, 106, 38

i must convert the LUT values to much my needs:

red : <LUT>0:255,100:128,200:132,300:136,400:138,500:143,600:145,700:149,800:152,900:155,1000:255</LUT>

green : <LUT>0:255,100:250,200:230,300:208,400:197,500:174,600:162,700:139,800:124,900:106,1000:255</LUT>

blue : <LUT>0:0,100:1,200:6,300:12,400:15,500:21,600:24,700:30,800:34,900:38,1000:0</LUT>

i will test these using OpenEV or qGIS whick i think support vrt files.

I will post back my resutls.

Jan 31, 2010 at 10:03 PM

Hello agelos,

as of revision 63679 sharpmap can handle you ascii-grid data without the way around a vrt.

all you have to do is to provide an appropriate ColorBlend class to the GdalRasterLayer.




Feb 2, 2010 at 10:25 PM

Hello FObermaier,

The result is avsolutelly fantastic. Thank you so much.
Though after changing the color blend of the gdal raster layer i get an error.
With the command :
Dim c1 As Color = Color.FromArgb(128, 250, 1)
Dim c2 As Color = Color.FromArgb(143, 174, 21)
Dim c3 As Color = Color.FromArgb(155, 106, 38)

Dim selColB As SharpMap.Rendering.Thematics.ColorBlend
selColB = SharpMap.Rendering.Thematics.ColorBlend.ThreeColors(c1, c2, c3)

gfalLayerColorBlend = selColB
The error occures in SharpMap\Rendering\Thematics\ColorBlend.cs Line 118
float frac = (pos - _Positions[i - 1])/(_Positions[i] - _Positions[i - 1]);
called From SharpMap.Extensions\Layers\GdalRasterLayer.cs Line 1051
Color color = _colorBlend.GetColor(Convert.ToSingle(imageVal));
Array _Positions[i] is out of range
Any ideas on how to fix that ?
Thanks again,
Feb 2, 2010 at 10:43 PM

I also patched you MapImage.cs with the Improved MapImage control - Measure patch (

I uploaded my version there too :


Feb 3, 2010 at 8:59 AM
Edited Feb 3, 2010 at 1:36 PM

Hello Agelos,

I think using the ThreeColors static function of ColorBlend you get a ColorBlend class with a positions [0.0, 0.5, 1.0] which is not what you want.

You can either set ColorBlend.Positions to


setColB.Positions = new float[] {0f, 500f, 1000f}; 

or use the constructor 

setColB = new ColorBlend(new Color[] { c1, c2, c3 }, new float[] { 0f, 500f, 1000f });


Please make sure that the values in

  • the postitions vector are ascending,
  • the raster data do not exceed the range defined by the positions.

I also added MapBox Control to the SharpMap.UI.VS2008 project. Depending on the Compile time constants 'UseMapBox' and 'UseMapBoxAsMapImage'
you can use the full functionality of the MapBox control for your project.

Hth FObermaier