Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm serving a NDVI coverage via Mapserver. So, one band, float32 values, [-1.0, 1.0]. I want to display the NDVI in greyscale, and generate a legend. I've tried 2 things:

  • Create two dummy classes, just for testing:

Dummy classes

So, my legend looks like this


And the image, obviously, is red and blue:

But I'd like to see the image in greyscale... The I've tried the next thing

The problem is now I can't see any legend, because I've not created any class. I'd like to see all the colors of the grayscale image in my legend.

How could I do it?

Many thanks in advance

share|improve this question
up vote 1 down vote accepted

Honestly, a red and blue scaled NDVI map is not particularly representative. Check this example for a well colored NDVI and EVI map:

The colors are predefined in GRASS GIS, see (you could extract easily the color ramp from the source code, ping me if you need help).

share|improve this answer
No, I don't want red and blue colors. I was just testing how the legend is generated based on the CLASS elements of the mapfile. My problem is I can see the image in grey levels by using the PROCESSING "SCALE=-1.0, 1.0" option and no CLASS elements. But in that case, I can't get a legend graphic, because it's constructed based on the CLASS elements (I guess)... – jorgeas80 Sep 28 '11 at 9:16
BTW, thanks for the GRASS ref. I was able to generate a legend with GRASS, by applying the grey color to the band and using d.legend -s map=mymap@mymapset. So, GRASS can generate a legend based only in the raster file, and Mapserver needs a mapfile with CLASS elements (am I right?). Now, I could create a HTML legend, using the output image from GRASS, I guess... – jorgeas80 Sep 28 '11 at 11:16
Yes, GRASS only needs the raster map. Your approach may work. – markusN Sep 29 '11 at 13:54

I'm a little confused whether your wish for grayscale output is for display purposes, or for output format type (8-bit output). In any case, if the desire is solely for display, then obviously you could create a simple 2-Color B&W image by using the RGB values for Black and White in your 2-color example above:

  NAME "black"
  EXPRESSION ([pixel] < 2000)
    COLOR 0 0 0

  NAME "white"
  EXPRESSION ([pixel] >= 2000)
    COLOR 255 255 255

If on the other hand you want more that 2 colors in your grayscale output, you can simply eliminate the CLASS block entirely. With a single band image, MapServer will automatically render a grayscale image that is scaled from 0 - 255 using the range of values in the data.

Finally, if you want to control the ranges over which the scaling occurs, you can do the following:

NAME "Grayscale"
EXPRESSION ([pixel] >= 0 and [pixel] < 5000)
  COLORRANGE 0 0 0   255 255 255
  DATARANGE 0   5000


This can also be made more elaborate with multiple scales:

      NAME "low scale"
      EXPRESSION ([pixel] >= 0 and [pixel] < 500)
        COLORRANGE 0 0 0   100 100 100
        DATARANGE 0   2000

    NAME "high scale"
    EXPRESSION ([pixel] >= 500 and [pixel] < 5000)
      COLORRANGE 100 100 100   255 255 255
      DATARANGE 500  5000

As far as legends go, to my knowledge there is no way in MapServer to create an image that shows the linearly interpolation gradient from one color to another.

Matplotlib, or Protoviz, or Imagemagick might be of use to you in constructing a legend image.

share|improve this answer
Yes, I only have display purposes. About eliminating the CLASS elements, that is one the tests I did (the second one). But as I said, then I don't have a generated legend. I could generate a legend that shows the gradient by using GRASS, and I'm working with this now. But I didn't test the COLORRANGE/DATARANGE commands. This could be another way. Thanks! – jorgeas80 Oct 26 '11 at 15:03

I would test that instruction (not tested) :


See the following documentation section.

share|improve this answer
Many thanks. I've tested it, but I actually get the only raster band without that option. – jorgeas80 Sep 28 '11 at 9:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.