How to set background transparent in UMN Mapserver mapfile for the layer coming from postgis.
executed map file is
MAP
PROJECTION
"init=epsg:4326"
END
NAME "layername"
STATUS on
SIZE 600 600
Extent 91.8467262763108039 23.6930707734033987 92.8966836092481998 25.5986088978219009
IMAGECOLOR 192 192 192
IMAGEQUALITY 95
IMAGETYPE png
OUTPUTFORMAT
NAME png
DRIVER 'GD/PNG'
MIMETYPE 'image/png'
IMAGEMODE RGBA
TRANSPARENT ON
EXTENSION 'png'
END
WEB
IMAGEPATH "../tmp"
IMAGEPATH "../tmp"
METADATA
'wms_title' 'layername'
'wms_onlineresource' 'http://my.host.com/cgi-bin/mapserv?map=wms.map&'
'wms_srs' 'EPSG:4326'
'wms_feature_info_mime_type' 'text/html'
END
END
LAYER
NAME "layername"
offsite 0 0 0
TYPE POLYGON
CONNECTIONTYPE POSTGIS
CONNECTION "host=host user=user password=password port=port dbname=dbname"
DATA "geom from (select a.poly_no, a.geom,a.f2 from landslideearlywarning.ner_master a) USING UNIQUE poly_no USING srid=4326"
METADATA
'wms_title' 'layername'
"wms_include_items" "all"
"wms_srs" "EPSG:4326"
"WMS_ENABLE_REQUEST" "*"
END
CLASS
NAME "No Warning"
EXPRESSION ([f2] =0)
STYLE
OPACITY 100
COLOR 210 155 100
END
END
END
END
Im able to view the layer but area other than data is coming in white color.
How to remove the background (white ) color other than data
Best Answer
I edited your mapfile a bit. Now it is close to shortest possible WMS configuration file. Data comes from an inline feature, a triagle. You use PostGIS but that is not important, it does not affect rendering.
Notice that I made the background pretty blue
IMAGECOLOR 50 50 192
for making is well visible.This WMS GetMap request returns a png with transparency
http://localhost/cgi-bin/mapserv.exe?map=c:\ms4w\apps\rendertest.map&REQUEST=GetMap&SERVICE=WMS&VERSION=1.3.0&WIDTH=345&HEIGHT=362&LAYERS=layername&TRANSPARENT=TRUE&FORMAT=image%2Fpng&BBOX=0.45,2.44,15.65,16.93&CRS=EPSG:4326&STYLES=
and this is how it looks (light gray is transparent, this image is captured from browser).
As a comparison the result with
&TRANSPARENT=FALSE
One more image from a WMS viewer with a polygon behind the triangle from WMS. WMS client is using
&TRANSPARENT=TRUE
.Finally I saved the transparent image that was returned by the WMS GetMap and checked it with gdalinfo. It reveals that nodata is not handled by marking the background color as transparent but with an alpha band.
The conclusion is that for getting transparent png images from MapServer WMS all that needs to be done is to make the GetMap requests with
&TRANSPARENT TRUE
. The MapServer documentation may not be totally up to date when it deals with the special outputformat andTRANSPARENT ON
. If you use MapServer through WMS you can forget that. Keep on mind that outputformat must be PNG, just as you obviously used. JPEG does not support transparency.