4

I'm using Mapserver to display a raster in browser using a WMS request from OpenLayers. At the moment I have just one raster, and used the following method of styling it:

MAP      
    ###########
    NAME         "ny_pop"                                                                             
    UNITS         meters    
    SIZE          256 256
    STATUS        OFF
    DEBUG         ON
    IMAGECOLOR    128 128 255
    CONFIG        "MS_ERRORFILE" "ms.log"

    ###########    
    PROJECTION
        "init=epsg:900913"
    END

    ###########
    OUTPUTFORMAT
      NAME "png"
      DRIVER AGG/PNG
      MIMETYPE "image/png"
      IMAGEMODE RGB
      EXTENSION "png"
      FORMATOPTION "GAMMA=0.75"
    END

    ###########    
    WEB             
        METADATA
            "wms_title"           "WMS population layer"
            "wms_onlineresource"  "http://mysite.com/mapserv.cgi?map=population.map&"            
            "wms_enable_request"  "GetMap GetFeatureInfo GetCapabilities"
        END
    END

    ###########    
    LAYER
        NAME      "new_york_pop"
        DATA      /home/mysite/webapps/htdocs/data/ny_pop_den.tif
        STATUS    OFF
        TYPE      RASTER   
        METADATA
            "wms_title"  "WMS NY Raster"
            "wms_srs"    "EPSG:900913"
        END

        CLASSITEM "[pixel]"

        # class using simple string comparison, equivelent to ([pixel] = 0)
        CLASS
            EXPRESSION "0"
            STYLE
              COLOR 0 0 0
            END
        END

          # class using an EXPRESSION using only [pixel].
        CLASS
            EXPRESSION ([pixel] > 0 AND [pixel] < 5000)
            STYLE
              COLOR 215 25 28
            END
        END

        CLASS
            EXPRESSION ([pixel] >= 5000 AND [pixel] < 10000)
            STYLE
              COLOR 253 174 97
            END 
    END

END

I want to include several more rasters layers in this mapfile. Is there a way that I can apply the style to multiple layers by creating a style object and applying it to each layer? I'm not sure if this is the correct terminology as I could not spot how to do this on the mapserver site.

I thought about generating this using php, or copy/pasting this layer structure. If I go the copy/paste route the mapfile would become extremely long and hard to read.

1 Answer 1

2

Not yet.. Here's what we're left with...eg: the copy and paste method. Example:

LAYER                                                   #-> BEGIN LAYER DEFINITION: Land Cover <-#
    NAME    landcover                                   # Layer identifier
    DATA    "data/wgs84/landcover_360.tif"              # Name of the data (in this case, string identifying a raster)
    STATUS  DEFAULT                                     # Default (always on) <-OR-> ON/OFF (passed into query string)
    TYPE    RASTER                                      # Data type (point, line, polygon or raster)
    OPACITY 100                                         # Layer transparency (0-100 <-OR-> ALPHA)
    OFFSITE 0 0 0                                       # Image background colour

    CLASS                                               #-> BEGIN CLASS
        NAME    "Land Cover: Urban"                     # Name of class (used in legend)
        EXPRESSION ([pixel] = 2)                        # Attribute query

        STYLE                                           #-> BEGIN STYLE
            OPACITY 100                                 # Transparency
            COLOR 0 0 0                                 # Fill colour
        END                                             #-> END STYLE
    END                                                 #-> END CLASS

    CLASS                                               #-> BEGIN CLASS
        NAME    "Land Cover: Agriculture"               # Name of class (used in legend)
        EXPRESSION ([pixel] = 1)                        # Attribute query

        STYLE                                           #-> BEGIN STYLE
            OPACITY 80                                  # Transparency
            COLOR 0 0 0                                 # Fill colour
        END                                             #-> END STYLE
    END                                                 #-> END CLASS
END                                                     #-> END LAYER DEFINITION: Land Cover <-#

LAYER                                                   #-> BEGIN LAYER DEFINITION: Roads <-#
    NAME    roads                                       # Layer identifier
    DATA    "data/wgs84/roads_360.tif"                  # Name of the data (in this case, string identifying a raster)
    STATUS  DEFAULT                                     # Default (always on) <-OR-> ON/OFF (passed into query string)
    TYPE    RASTER                                      # Data type (point, line, polygon or raster)
    OPACITY 100                                         # Layer transparency (0-100 <-OR-> ALPHA)
    OFFSITE 0 0 0                                       # Image background colour

    CLASS                                               #-> BEGIN CLASS
        NAME    "Roads: Within 2km"                     # Name of class (used in legend)
        EXPRESSION ([pixel] = 2)                        # Attribute query

        STYLE                                           #-> BEGIN STYLE
            OPACITY 100                                 # Transparency
            COLOR 0 0 0                                 # Fill colour
        END                                             #-> END STYLE
    END                                                 #-> END CLASS

    CLASS                                               #-> BEGIN CLASS
        NAME    "Roads: Between 2 and 15km"             # Name of class (used in legend)
        EXPRESSION ([pixel] = 1)                        # Attribute query

        STYLE                                           #-> BEGIN STYLE
            OPACITY 50                                  # Transparency
            COLOR 0 0 0                                 # Fill colour
        END                                             #-> END STYLE
    END                                                 #-> END CLASS
END                                                     #-> END LAYER DEFINITION: Roads <-#

Style holds parameters for symbolization and styling. Multiple styles may be applied within a CLASS or LABEL.

This object appeared in 4.0 and the intention is to separate logic from looks. The final intent is to have named styles (Not yet supported) that will be re-usable through the mapfile. This is the way of defining the appearance of an object (a CLASS or a LABEL).

2
  • wow - thanks! It's great to see examples of clear mapfiles. I'm going to update my commenting after reading your clear example. I think I'll probably try and generate the file in python to make it easier to handle.
    – djq
    Commented Jan 29, 2012 at 22:35
  • Cheers! I am a nut for clear and well commented code so by all means, copy me! ;) Commented Jan 29, 2012 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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