Sunday, November 24, 2013

Fast tip: Drawing raster NO DATA values with MapServer

MapServer is a great way to draw web maps, using it either as a WMS server or creating image files directly.
Is the raster has a No Data value, MapServer reads it from the GDAL library and doesn't draw the pixels with the value. But what happens if you want to draw the pixels with No Data values?

Fast answer 

The solution, that I found here, is setting the LAYER property PROCESSING in this way:

PROCESSING "NODATA=OFF"
which disables the regular MapServer behaviour, which is, quite logically,  not drawing the pixels with the NODATA value.
Once this is done, just create a CLASS with the No Data Value  ¡in the EXPRESSION tag, so the pixels are coloured as you prefer.

Why did I need this stuff

Maybe this is not a common need, but when drawing weather radar images, is a good idea to indicate the areas not covered by the radar in some colour, so it's easy to distinguish between zones where there is no rain from the zones where no data is available. Like in these examples:

Catalan Weather Service radar with the range visible in gray
 The Catalan Weather Service radar in La Panadella with the range visible in gray, using the no data values
http://www.aemet.es/ca/eltiempo/observacion/radar?w=0
The Spanish Agency radar network, with the not covered areas in gray.

Strangely, finding the solution was quite difficult, so I put it in this post. Maybe somebody, someday, will find faster than me.

Some links:

4 comments:

  1. Hi Roger:

    Firstly, congratulations for your blog, you really tackle nice topics, good content and clear explanations about GIS. MapServer is a powerful tool to render WMS maps but simetimes is horrible due to the fact that updated doc is missing in official documentation and people have to find some solutions for concrete problems.

    Regarding your entry, I have a question with legends for colour maps for rasters in Mapserver and I was wondering if you could throw some light with colour maps in MapSevrer for a generic raster like Radar displayed layers. I envy your horizontal colorramp bar.

    I used SLD styles enabling SLD format in mapfile's metadata description but without success.

    I tried with CLASSES defined in layer-based file, like this:

    CLASS
    NAME "ColorMap"
    EXPRESSION ([pixel] >=0.0 AND [pixel] <=1.0)
    STYLE
    COLORRANGE 0 0 0 255 255 255
    DATARANGE 0.0 1.0
    RANGEITEM "pixel"
    END
    END

    and I got desired visualisation but generated legend

    http://servername/ogc/wms?version=1.3.0&service=WMS&request=GetLegendGraphic&sld_version=1.1.0&layer=LAND_NAME&format=image/png&STYLE=default

    is not ramp scaled. Only a squared box without colour and "ColorMap" label.

    Have you got some ideas about this inconvenience?

    Thank you in advance

    Jorge López

    ReplyDelete
  2. Hola Jorge!

    I haven't tried exacly this with mapserver, but legends have given me problems also when creating some vector symbols.

    So, just making a guess, mapserver draws the legend using one color square or symbol for every class. Since you have only one class, no squares can be made.
    There are ways to force the legend values to be different from the classes used to draw, like using the KEYIMAGE key in the CLASS section.
    Also, html legends can be set to make complicated legends:
    http://mapserver.org/output/html_legend.html

    I can't give you a better answer...

    ReplyDelete
    Replies
    1. Thanks Roger,

      I will give a try one again trying to generate legend, otherwise I can attach a pre-generated image for legend. If success, I will inform you about the solution.

      See you

      Jorge

      Delete