PostGIS – How to Make UMN MapServer PostGIS Vector Layer Background Transparent

mapfilemapserverogrpostgis

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 ) outputcolor 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.

MAP
    PROJECTION
       "init=epsg:4326"
    END
    NAME "layername"
    STATUS on
    SIZE 600 600    
    Extent 0 0 20 20
    IMAGECOLOR 50 50 192 
       
  WEB    
    IMAGEPATH "../tmp"
    IMAGEPATH "../tmp"
    METADATA
      'wms_title'           'layername'
      'wms_onlineresource'  'http://localhost:8060/cgi-bin/mapserv.exe?map=c:\ms4w\apps\rendertest.map'
      'wms_srs'             'EPSG:4326'
     'wms_feature_info_mime_type' 'text/html'
      "WMS_ENABLE_REQUEST" "*"
    END #metadata
  END #web
    LAYER
      NAME "layername"
      TYPE POLYGON
      FEATURE POINTS 10 5 15 10 5 10 10 5 END END
      METADATA
      'wms_title' 'layername'
      "wms_srs" "EPSG:4326"
      END #metadata
    CLASS
        STYLE
            OPACITY 100
            COLOR  210 155 100
        END #style
    END #class
    
    END #layer
END

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

enter image description here

enter image description here

One more image from a WMS viewer with a polygon behind the triangle from WMS. WMS client is using &TRANSPARENT=TRUE.

enter image description here

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.

gdalinfo mstest4.png
Driver: PNG/Portable Network Graphics
Files: mstest4.png
Size is 345, 362
Image Structure Metadata:
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0,  362.0)
Upper Right (  345.0,    0.0)
Lower Right (  345.0,  362.0)
Center      (  172.5,  181.0)
Band 1 Block=345x1 Type=Byte, ColorInterp=Red
  Mask Flags: PER_DATASET ALPHA
Band 2 Block=345x1 Type=Byte, ColorInterp=Green
  Mask Flags: PER_DATASET ALPHA
Band 3 Block=345x1 Type=Byte, ColorInterp=Blue
  Mask Flags: PER_DATASET ALPHA
Band 4 Block=345x1 Type=Byte, ColorInterp=Alpha

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 and TRANSPARENT 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.