MapServer Symbology – How to Style MapServer Layers Easily

mapfilemapserversldstylesymbology

I am working with MapServer WMS in the form of mapfiles and consuming them into a OpenLayers map.

I am trying to find out a way to ease the symbolization process of the various layers in a mapfile.

I have not started diving into the STYLE directive yet, but before I do that I'd like to know whether there's an easy way to (let's say) preview the style of my layers (an application?) and export it into a (e.g.) SLD which the MapServer can read.

I don't know if this is the best (or feasible) way to go about styling mapfile's layers, but I am open to any kind of suggestion.

Best Answer

You can't use SLD to internally style a MapServer WMS map, you'll have to use the STYLE directives.

However

MapServer will take an SLD as part of a GetMap request, either by reference (you link to a URL of the SLD), using the SLD=[your link to your SLD]& parameter or directly as part of the SLD_BODY=[your SLD]& parameter.

You can review this in a browser.

Exercise

Consider a MapServer WMS providing radon information.

A GetMap request using the default style might look like:

http://ogc.bgs.ac.uk/cgi-bin/BGS_BGS-HPA_Radon_Potential/wms?language=eng&VERSION=1.3.0&REQUEST=GetMap&CRS=EPSG:4326&BBOX=50.179445759563,-4.41340973640857,50.9615344710795,-3.2834397199341&WIDTH=877&HEIGHT=607&LAYERS=GBR_BGS-HPA_625K_RADON_POTENTIAL&STYLES=&EXCEPTIONS=XML&FORMAT=image/png&BGCOLOR=0xFEFFFF&TRANSPARENT=TRUE&

Giving a map like:

GetMap default style

The map file definition of the style is:

    CLASS
        # Dummy for legend title
        MINSCALEDENOM 250000
        NAME "Bq m3"
        EXPRESSION ""
        STYLE
            COLOR 255 255 255
        END
    END
    CLASS
        MINSCALEDENOM 250000
        NAME "> 499.99"
        EXPRESSION ">499.99"
        STYLE
            COLOR 255 0 0
        END
    END
    CLASS
        MINSCALEDENOM 250000
        NAME "200 - 499.99"
        EXPRESSION "200 - 499.99"
        STYLE
            COLOR 230 152 0
        END
    END
    CLASS
        MINSCALEDENOM 250000
        NAME "100 - 199.99"
        EXPRESSION "100 - 199.99"
        STYLE
            COLOR 255 255 0
        END
    END
    CLASS
        MINSCALEDENOM 250000
        NAME "50 - 99.99"
        # EXPRESSION "50 - 99.99"
        EXPRESSION "[50 - 99.99]"
        STYLE
            COLOR 190 210 255
        END
    END
    CLASS
        MINSCALEDENOM 250000
        NAME "20 - 49.99"
        EXPRESSION "20 - 49.99"
        STYLE
            COLOR 0 112 255
        END
    END
    CLASS
        MINSCALEDENOM 250000
        NAME "< 20"
        EXPRESSION "< 20"
        STYLE
            COLOR 0 77 168
        END
    END

But a general user can't see this. A user though can make a GetStyles request like:

http://ogc.bgs.ac.uk/cgi-bin/BGS_BGS-HPA_Radon_Potential/wms?version=1.3.0&service=WMS&request=GetStyles&sld_version=1.1.0&layers=GBR_BGS-HPA_625K_RADON_POTENTIAL&STYLE=default&

Which is MapServers effort at converting the internal style to SLD, so here it gives us:

<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor version="1.1.0" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:se="http://www.opengis.net/se" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <NamedLayer>
        <se:Name>GBR_BGS-HPA_625K_RADON_POTENTIAL</se:Name>
        <UserStyle>
            <se:FeatureTypeStyle>
                <se:Rule>
                    <se:Name>Bq m3</se:Name>
                    <ogc:Filter><ogc:PropertyIsEqualTo><ogc:PropertyName>AM_GFRn</ogc:PropertyName><ogc:Literal></ogc:Literal></ogc:PropertyIsEqualTo></ogc:Filter>
                    <se:MinScaleDenominator>250000.000000</se:MinScaleDenominator>
                    <se:PolygonSymbolizer>
                        <se:Fill>
                            <se:SvgParameter name="fill">#ffffff</se:SvgParameter>
                        </se:Fill>
                    </se:PolygonSymbolizer>
                </se:Rule>
                <se:Rule>
                    <se:Name>&gt; 499.99</se:Name>
                    <ogc:Filter><ogc:PropertyIsEqualTo><ogc:PropertyName>AM_GFRn</ogc:PropertyName><ogc:Literal>>499.99</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Filter>
                    <se:MinScaleDenominator>250000.000000</se:MinScaleDenominator>
                    <se:PolygonSymbolizer>
                        <se:Fill>
                            <se:SvgParameter name="fill">#ff0000</se:SvgParameter>
                        </se:Fill>
                    </se:PolygonSymbolizer>
                </se:Rule>
                <se:Rule>
                    <se:Name>200 - 499.99</se:Name>
                    <ogc:Filter><ogc:PropertyIsEqualTo><ogc:PropertyName>AM_GFRn</ogc:PropertyName><ogc:Literal>200 - 499.99</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Filter>
                    <se:MinScaleDenominator>250000.000000</se:MinScaleDenominator>
                    <se:PolygonSymbolizer>
                        <se:Fill>
                            <se:SvgParameter name="fill">#e69800</se:SvgParameter>
                        </se:Fill>
                    </se:PolygonSymbolizer>
                </se:Rule>
                <se:Rule>
                    <se:Name>100 - 199.99</se:Name>
                    <ogc:Filter><ogc:PropertyIsEqualTo><ogc:PropertyName>AM_GFRn</ogc:PropertyName><ogc:Literal>100 - 199.99</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Filter>
                    <se:MinScaleDenominator>250000.000000</se:MinScaleDenominator>
                    <se:PolygonSymbolizer>
                        <se:Fill>
                            <se:SvgParameter name="fill">#ffff00</se:SvgParameter>
                        </se:Fill>
                    </se:PolygonSymbolizer>
                </se:Rule>
                <se:Rule>
                    <se:Name>50 - 99.99</se:Name>
                    <ogc:Filter><ogc:PropertyIsEqualTo><ogc:PropertyName>AM_GFRn</ogc:PropertyName><ogc:Literal>50 - 99.99</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Filter>
                    <se:MinScaleDenominator>250000.000000</se:MinScaleDenominator>
                    <se:PolygonSymbolizer>
                        <se:Fill>
                            <se:SvgParameter name="fill">#bed2ff</se:SvgParameter>
                        </se:Fill>
                    </se:PolygonSymbolizer>
                </se:Rule>
                <se:Rule>
                    <se:Name>20 - 49.99</se:Name>
                    <ogc:Filter><ogc:PropertyIsEqualTo><ogc:PropertyName>AM_GFRn</ogc:PropertyName><ogc:Literal>20 - 49.99</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Filter>
                    <se:MinScaleDenominator>250000.000000</se:MinScaleDenominator>
                    <se:PolygonSymbolizer>
                        <se:Fill>
                            <se:SvgParameter name="fill">#0070ff</se:SvgParameter>
                        </se:Fill>
                    </se:PolygonSymbolizer>
                </se:Rule>
                <se:Rule>
                    <se:Name>&lt; 20</se:Name>
                    <ogc:Filter><ogc:PropertyIsEqualTo><ogc:PropertyName>AM_GFRn</ogc:PropertyName><ogc:Literal>&lt; 20</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Filter>
                    <se:MinScaleDenominator>250000.000000</se:MinScaleDenominator>
                    <se:PolygonSymbolizer>
                        <se:Fill>
                            <se:SvgParameter name="fill">#004da8</se:SvgParameter>
                        </se:Fill>
                    </se:PolygonSymbolizer>
                </se:Rule>
            </se:FeatureTypeStyle>
        </UserStyle>
    </NamedLayer>
</StyledLayerDescriptor>

We could take this style, modify it, put it on a server, and request it as part of a GetMap request like:

http://ogc.bgs.ac.uk/cgi-bin/BGS_BGS-HPA_Radon_Potential/wms?language=eng&VERSION=1.3.0&REQUEST=GetMap&CRS=EPSG:4326&BBOX=50.179445759563,-4.41340973640857,50.9615344710795,-3.2834397199341&WIDTH=877&HEIGHT=607&LAYERS=GBR_BGS-HPA_625K_RADON_POTENTIAL&STYLES=&EXCEPTIONS=XML&FORMAT=image/png&BGCOLOR=0xFEFFFF&TRANSPARENT=TRUE&sld=http://ogc.bgs.ac.uk/sld/radon1.sld&

Giving us:

GetMap external style