This project has moved. For the latest updates, please go here.

ColorBlend and float/double Rasters

Topics: SharpMap v0.9 / v1.x
Jun 29, 2015 at 11:30 AM
Right now it appears float/double rasters only render as black and white, regardless of what values I use for the colorblend ... Am I doing something wrong or is this a limitation of the library at present? Thanks! Below you'll find my sample code...
   var rLayer = new SharpMap.Layers.GdalRasterLayer(System.IO.Path.GetFileNameWithoutExtension(result.FileName), result.FileName);
   var ds = OSGeo.GDAL.Gdal.Open(rLayer.Filename, OSGeo.GDAL.Access.GA_ReadOnly);
   var b = ds.GetRasterBand(1); //note gdal uses base 1 index for raster indices...
   double min = 0, max = 0, mean = 0, stddev = 0;
    b.ComputeStatistics(false, out min, out max, out mean, out stddev, null, null);
     rLayer.ColorBlend = SharpMap.Rendering.Thematics.ColorBlend.RedToBlue; //new SharpMap.Rendering.Thematics.ColorBlend(new System.Drawing.Color[] { System.Drawing.Color.Red, System.Drawing.Color.Blue}, new float[] { (float)min, (float)max });
                    for (int i = 0; i < rLayer.ColorBlend.Positions.Length; ++i)
                        rLayer.ColorBlend.Positions[i] = (float)(min + (max - min) * rLayer.ColorBlend.Positions[i]);
                    rLayer.ColorBlend = new SharpMap.Rendering.Thematics.ColorBlend(rLayer.ColorBlend.Colors, rLayer.ColorBlend.Positions); //forcing update of min/max in colorblend
Jun 29, 2015 at 1:57 PM
ColorBlend only comes into action when ColorInterp = Undefined.
Is that the case for your raster file (gdalinfo)?
Jun 29, 2015 at 6:02 PM
Edited Jun 29, 2015 at 6:06 PM
Thanks for the reply... Unfortunately, no, I generate these float/double graphs through an application that uses GRASS, and even turning off exporting the colormap within GRASS results in the raster defaulting to grayindex. After posting I spent some time looking through the source code, and it appears that this is caused by the gdalrasterlayer always defaulting to the raster color table (over any color table defined for the layer).

There's probably a good reason for this behavior, but I was able to fix the problem by adding an if block above ~line 958 of gdalrasterlayer.cs that checks whether _colorblend has been previously defined, and if so, uses that value instead of checking the raster color table. There may be a good reason that wasn't done in the past, but it works for a variety of rasters I've tested; perhaps worth adding in a future version? While I was looking through the same file, I was also able to add about 20 lines that allow one to set the opacity for the layer; it appears the other layer types support adjusting opacity (either through a method or by defining the brush), and at least for my application (overlaying multiple rasters on imagery), this is useful.
Jun 30, 2015 at 6:00 AM
A patch file is always welcome...
Jul 1, 2015 at 2:38 AM
Uploaded. Also added parallel rendering support when raster tile size is specified, which improves rendering speed by a factor of 2-3 for ~250 MB .sid on my 6 core system at home...
Jul 2, 2015 at 8:36 AM
Thanks. I'll take a look asap.