[GIS] How to make a raster layer transparent via MapServer WMS

cartographymapserveropenlayers-2postgiswms

I loaded a PostGIS raster data through OpenLayers and MapServer.

The code inside the OpenLayers is as following :

localWMS = new OpenLayers.Layer.WMS( 
    "Local WMS layers",
    "http://127.0.0.1:8080/cgi-bin/mapserv.exe?MAP=C:/MapServer/raster.map",
    {
        layers: 'polygon',
        transparent: true,
        alpha:true
    },
    { 
        projection:new OpenLayers.Projection("EPSG:4326"),
        units:'m',
        isBaseLayer: false
    }
);

map.addLayer(localWMS);

And here is my map file, I already set opacity to each class of the layer but nothing happens:

LAYER
    DATA "PG:host=*** dbname=*** schema=public table=london where='rid=3' mode='1'"
    NAME "postgis"
    PROCESSING "SCALE=AUTO"
    PROCESSING "NODATA=-9999"
    STATUS DEFAULT
    TYPE RASTER
    UNITS METERS
    CLASS
      NAME "Minor Impact"
      EXPRESSION ([pixel] >= 0 AND [pixel] < 1.29525335532 )
      STYLE
        ANGLE 360
        OPACITY 50
        COLOR 122 182 245
        SYMBOL 0
        WIDTH 1
      END
    END
    CLASS
      NAME "Moderate Impact"
      EXPRESSION ([pixel] >= 1.29525335532 AND [pixel] < 2.59050671063 )
      STYLE
        ANGLE 360
        OPACITY 50
        COLOR 76 230 0
        SYMBOL 0
        WIDTH 1
      END
    END
    CLASS
      NAME "Severe Impact"
      EXPRESSION ([pixel] >= 2.59050671063 AND [pixel] < 5.69911476339 )
      STYLE
        ANGLE 360
        OPACITY 50
        COLOR 252 228 91
        SYMBOL 0
        WIDTH 1
      END
    END
    CLASS
      NAME "Catastrophic"
      EXPRESSION ([pixel] >=5.69911476339 )
      STYLE
        OPACITY 30
        COLOR 232 16 20
      END
    END
END

The map looks like following :enter image description here

Then, I tried to set one of the opacity to less than 1, the red color just disappeared.
enter image description here

But if I set the Opacity of the Layer to 50

LAYER
    DATA "PG:host=*******************'"
    NAME "postgis"
    PROCESSING "SCALE=AUTO"
    PROCESSING "NODATA=-9999"
    STATUS DEFAULT
    TYPE RASTER
    OPACITY 50
    UNITS METERS

Even more strange map appeared.
enter image description here

Does anyone know what's the problem about that? Or Does anyone know the proper way to set a transparent raster layer via MapServer?

Best Answer

probably what you need is to set both OFFSITE and TRANSPARENCY in the layer section, something like this:

LAYER
NAME 'Raster'
TYPE RASTER
CONNECTIONTYPE postgis
CONNECTION "user=postgres password=... dbname=... host=..."
DATA "bytea FROM rasters"
STATUS OFF
OFFSITE 0 0 0
TRANSPARENCY 100
END

being OFFSITE the color you want to be transparent and TRANSPARENCY which controls the transparency level.

Hope this helps,

Related Question