SLD

Author:

Jeff McKenna

Contact:

jmckenna at gatewaygeomatics.com

Author:

Yewondwossen Assefa

Contact:

assefa at dmsolutions.ca

Last Updated:

2019-11-21

Introduction

This document describes the procedures for taking advantage of the Styled Layer Descriptor (SLD) support in WMS GetMap requests with MapServer. SLD support exists for the server side (ability to read an SLD and apply it with a GetMap request) and for the client side (includes sending SLD requests to server and generate SLD files on the fly from MapServer map file). SLD support was added to MapServer in version 4.2.

This document assumes that you are already familiar with the following aspects of MapServer:

  • MapServer application development and setting up .map files.

  • Familiarity with the WMS specification would be an asset. Links to the MapServer WMS documents are included in the next section.

Server Side Support

General Information

There are two ways a WMS request can pass an SLD document with a GetMap request to MapServer:

  • SLD parameter pointing to remote SLD (SLD=http://URL_TO_SLD).

  • SLD_BODY parameter to send the SLD definition in the URL.

These two methods are both available through MapServer. An example of a request would be:

https://demo.mapserver.org/cgi-bin/wms?SERVICE=wms&VERSION=1.1.1&REQUEST=GetMap
&LAYERS=country_bound
s&SLD=https://demo.mapserver.org/ogc-demos/map/sld/sld_line_simple.xml

Test the remote SLD request.

The SLD in the above request follows:

<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor version="1.0.0"
  xmlns="http://www.opengis.net/sld"
  xmlns:ogc="http://www.opengis.net/ogc"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.opengis.net/sld
  http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd">
  <NamedLayer>
    <Name>country_bounds</Name>
    <UserStyle>
      <Title>xxx</Title>
      <FeatureTypeStyle>
        <Rule>
          <LineSymbolizer>
            <Geometry>
              <ogc:PropertyName>center-line</ogc:PropertyName>
            </Geometry>
            <Stroke>
              <CssParameter name="stroke">#0000ff</CssParameter>
            </Stroke>
          </LineSymbolizer>
        </Rule>
      </FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

Version 1.1.0 of the same SLD

<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor version="1.1.0"
  xmlns="http://www.opengis.net/sld"
  xmlns:se="http://www.opengis.net/se"
  xmlns:ogc="http://www.opengis.net/ogc"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.opengis.net/sld
  http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd">
<NamedLayer>
  <se:Name>country_bounds</se:Name>
    <UserStyle>
      <se:Name>xxx</se:Name>
      <se:FeatureTypeStyle>
        <se:Rule>
          <se:LineSymbolizer>
            <se:Geometry>
              <ogc:PropertyName>center-line</ogc:PropertyName>
            </se:Geometry>
            <se:Stroke>
              <se:SvgParameter name="stroke">#0000ff</se:SvgParameter>
            </se:Stroke>
          </se:LineSymbolizer>
        </se:Rule>
      </se:FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

When MapServer gets a valid SLD through a request, it parses this SLD to extract all the styles attached to the NamedLayers, and then it applies these styles to the map before it is returned to the client. When applying the SLD, MapServer compares the <Name> parameter of the NamedLayers in the SLD document to the WMS layer names (WMS layer names are available in a GetCapabilities request).

Note

All the examples given in this document are live uses of valid SLDs and a MapServer installation with SLD support.

Additional WMS features related to SLDs have also been developed:

Table1. Additional WMS Features

Features

Supported

Notes

Method GET : SLD URL

Yes

Method GET : SLD_BODY

Yes

Additional item

Describer Layer

Yes

GetLegendGraphic

Yes

GetStyles

Yes

Uses MapScript to get the SLD

Note

As of MapServer version 4.2.3, the GetLegendGraphic request (see section 12 of the Styled Layer Descriptor Implementation Specification) works as follows: if the RULE keyword is absent from the request, an image containing the entire legend for the specified layer will be returned. This image consists of the layer name and a symbolization graphic and label for each class.

Specific SLD Elements Supported

The following tables give a lot of additional details about SLD support in MapServer.

Table2. Named Layers and User Layers

Features

Supported

Notes

Named Layers

Yes

User Layers

No

Table3. Named Layers

Features

Supported

Notes

LayerFeatureConstraints

Yes

Since MapServer 7.0.1

Named Styles

Yes

User Styles

Yes

Table 4. User Styles

Features

Supported

Notes

Name

No

This was removed at implementation time, since it does not fit with MapServer

Title

No

No use in the MapServer environment

Abstract

No

No use in the MapServer environment

IsDefault

No

Only one style is available per layer

FeatureTypeStyle

Yes

MapServer has a concept of one feature type style per layer (either point, line, polygon, or raster)

Table 5. FeatureTypeStyle

Features

Supported

Notes

Name

No

No use in the MapServer environment

Title

No

No use in the MapServer environment

Abstract

No

No use in the MapServer environment

FeatureTypeName

No

No use in the MapServer environment

SemanticTypeIdentifier

No

Still an experimental element in the SLD specifications

Rule

Yes

Table 6. Rule

Features

Supported

Notes

Name

Yes

Title

Yes

Abstract

No

No use in the MapServer environment

LegendGraphic

Yes

Filter

Yes

ElseFilter

Yes

MinScaleDenominator

Yes

MaxScaleDenominator

Yes

LineSymbolizer

Yes

PolygonSymbolizer

Yes

PointSymbolizer

Yes

TextSymbolizer

Yes

RasterSymbolizer

Yes

Applies for 8-bit rasters

  • Filter and ElseFilter

    For each rule containing a filter, there is a class created with the class expression set to reflect that filter. Available filters that can be used are Comparison Filters and Logical Filters (see the Filter Encoding HowTo). The ElseFilter parameters are converted into a class in MapServer and placed at the end of the class list with no expression set. They are used to render elements that did not fit into any other classes.

  • MinScaleDenomibator and MaxScaleDenominator are translated in minscale and maxscale in MapServer.

The following are examples of valid requests using the Filters:

This example enables spatial filtering using the BBOX parameter as a Filter for a selected area (Africa). Note that an ElseFilter will not work with a spatial filter.

Table 7. LineSymbolizer

Features

Supported

Notes

Geometry

No

MapServer uses the data geometry for the rendering

Stroke: GraphicFill

No

Solid color is used

Stroke: GraphicStroke

Yes

Draws the symbol along the line

Stroke (CssParameter): stroke

Yes

RGB colors are supported

Stroke (CssParameter): width

Yes

Stroke (CssParameter): opacity

Yes

Only for AGG driver and mapserver version >=5.2

Stroke (CssParameter): linejoin and linecap

No

Not supported in MapServer

Stroke (CssParameter): dasharray

Yes

Stroke (CssParameter): dashoffset

No

PerperdicularOffset (only in SLD 1.1.0)

Yes

Offset values of the style object will be set

InitialGap(GraphicStroke SLD 1.1.0)

No

Gap (GraphicStroke parameter SLD 1.1.0)

No

Note

SvgParameter instead of CssParameter are required for SLD 1.1.0.

The following are examples of valid requests using the LineSymbolizer:

Table 8. PolygonSymbolizer

Features

Supported

Notes

Geometry

No

Stroke

Yes

Strokes are the same as for the LineSymbolizer

Fill

Yes

Was developed to support symbol fill polygons in addition to solid fill

Fill-opacity

Yes

Only available for AGG driver and mapserver version >=5.2

PerperdicularOffset

No

SLD 1.1.0 parameter

Displacement

Yes

SLD 1.1.0 parameter. Sets offsetx/y in MapServer

A Fill can be a solid fill or be a Graphic Fill, which is either a well-known Mark symbol (e.g., square, circle, triangle, star, cross, x) or an ExternalGraphic element (e.g., gif, png) available through a URL. When a Mark symbol is used in an SLD, MapServer creates a corresponding symbol in the map file and uses it to render the symbols. When a ExternalGraphic is used, the file is saved locally and a pixmap symbol is created in the mapfile referring to the this file.

Note

The Web object IMAGEPATH is used to save the file.

The following are examples of valid requests using the PolygonSymbolizer:

Table 9. PointSymbolizer

Features

Supported

Notes

Geometry

No

Graphic: Mark symbol

Yes

Well-known names (square, circle, triangle, star, cross, X) are supported

Graphic: ExternalGraphic

Yes

Was developed to support symbol fill polygons in addition to solid fill

Opacity

Yes

Support added in MapServer 5.4

Size

Yes

Rotation

Yes

Support added in MapServer 5.4

Displacement

Yes

SLD 1.1.0 Parameter. Support added in MapServer 5.4

AnchorPoint

No

Note

Refer to the PolygonSymbolizer notes for how the Mark and ExternalGraphic symbols are applied in MapServer.

The following are examples of valid requests using the PointSymbolizer:

Table 10. TextSymbolizer

Features

Supported

Notes

Geometry

No

Label

Yes

MapServer behaviour changes for versions >= 8.0, see note below

Font(font-family)

Yes

Font names used are those available in MapServer font file. If no fonts are available there, default bitmap fonts are used

Font-style (Italic, …)

Yes

Font-weight

Yes

Font-size

Yes

If true-type fonts are not used, default bitmap sizes are given

LabelPlacement

Yes

PointPlacement is supported. LinePlacement is supported for versions >=5.2.1. Only PerperdicularOffset and IsAligned are supported for LinePlacement.

Halo

Yes

Supported (fill converted to outlinecolor, and radius is converted to outlinewidth. Note that outlinewidth is only available for AGG in >=5.2)

Fill

Yes

Only solid color is available

Notes on the TextSymbolizer:

  • Label: Starting with version >= 8.0 and in conformance with OGC standard, raw text in a Label tag is interpreted as an implicit Literal instead of an implicit PropertyName. Moreover, Literal and Function tags are supported, as well as mixed content. Supported Functions are MapServer functions returning a number.

  • Font names: when converting Font parameters to MapServer, the following rule is applied to get the font name: FontFamily-FontStyle-FontWeight. For example, if there is an SLD with a Font Family of arial, a Font Style of italic, and a Font weight equal to bold, the resulting MapServer font name is arial-bold-italic. Font Style and Weight are not mandatory and, if not available, they are not used in building the font name. When a Font Style or a Font Weight is set to normal in an SLD, it is also ignored in building the name. For example, if there is an SLD with a Font Family of arial, a Font Style of normal and a Font weight equals to bold, the resulting MapServer font name is arial-bold.

  • A TextSymbolizer can be used in MapServer on a Point, Line, or Polygon layer - in addition to other symbolizers used for these layers.

  • PointPacement: a point placement includes AnchorPoint (which is translated to Position in MapServer) Displacement (which is translated to Offset) and Angle (which is translated to Angle).

  • Angle setting (MapServer version >=5.4): by default the angle parameter is set to AUTO. For point features, users can use the PointPlacement to alter the value. For line features, the user can add a LinePlacement: If an ‘empty’ LinePlacement is part of the SLD, the angle will be set to FOLLOW, If a LinePlacement contains the PerpendicualarOffset parameter, the angle will be set to 0 and the PerpendicualarOffset will be used to set the offset values in the label object. SLD 1.1.0 introduces the IsAligned parameter for LinePlacement: if this parameter is set to false, the angle will be set to 0.

The following are examples of valid requests using the TextSymbolizer:

Table 11. RasterSymbolizer

Features

Supported

Notes

Geometry

No

Opacity

Yes

ChannelSelection

No

OverlapBehaviour

No

ColorMap

Yes

ContrastEnhancement

No

ShadedRelief

No

ImageOutline

No

The current support in MapServer includes only ColorMap parameter support. It can be used to classify 8-bit rasters. Inside the ColorMap parameters, the color and quantity parameters are extracted and used to do the classification.

Table 12. ColorMap

The following Features are available in SLD 1.0

Features

Supported

Notes

Color

Yes

Opacity

No

Quantity

Yes

Label

No

The following is an example of ColorMap usage for SLD 1.0.

If we have following ColorMap in an SLD:

<ColorMap>
  <ColorMapEntry color="#00ff00" quantity="22"/>
  <ColorMapEntry color="#00bf3f" quantity="30"/>
  <ColorMapEntry color="#007f7f" quantity="37"/>
  <ColorMapEntry color="#003fbf" quantity="45"/>
  <ColorMapEntry color="#0000ff" quantity="52"/>
  <ColorMapEntry color="#000000" quantity="60"/>
</ColorMap>

The six classes that are created are:

class 1: [pixel] >= 22 AND [pixel] < 30 with color 00ff00
class 2: [pixel] >= 30 AND [pixel] < 37 with color 00bf3f
class 3: [pixel] >= 37 AND [pixel] < 45 with color 007f7f
class 4: [pixel] >= 45 AND [pixel] < 52 with color 003fbf
class 5: [pixel] >= 52 AND [pixel] < 60 with color 0000ff
class 6: [pixel] = 60 with color 000000

Note

The ColorMapEntry quantity parameters should be in increasing order.

The following Features are available in SLD 1.1

Features

Supported

Notes

Categorize

Yes

The following is an example of and SLD 1.1.0 with a raster symbolizer

<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>landsat</se:Name>
<UserStyle>
<se:Name>xxx</se:Name>
<se:FeatureTypeStyle>
<se:Rule>
<se:RasterSymbolizer>
<se:Opacity>0.7</se:Opacity>
<se:ColorMap>
<se:Categorize fallbackValue="#78c818">
<se:LookupValue>Rasterdata</se:LookupValue>
<se:Value>#ffffff</se:Value>
<se:Threshold>22</se:Threshold>
<se:Value>#00ff00</se:Value>
<se:Threshold>30</se:Threshold>
<se:Value>#00bf3f</se:Value>
<se:Threshold>37</se:Threshold>
<se:Value>#007f7f</se:Value>
<se:Threshold>45</se:Threshold>
<se:Value>#003fbf</se:Value>
<se:Threshold>52</se:Threshold>
<se:Value>#0000ff</se:Value>
<se:Threshold>60</se:Threshold>
<se:Value>#000000</se:Value>
</se:Categorize>
</se:ColorMap>
</se:RasterSymbolizer>
</se:Rule>
</se:FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>

The classes that are created are:

class 1: [pixel] < 22 with color ffffff
class 2: [pixel] >= 22 AND [pixel] < 30 with color 00ff00
class 3: [pixel] >= 30 AND [pixel] < 37 with color 00bf3f
class 4: [pixel] >= 37 AND [pixel] < 45 with color 007f7f
class 5: [pixel] >= 45 AND [pixel] < 52 with color 003fbf
class 6: [pixel] >= 52 AND [pixel] < 60 with color 0000ff
class 7: [pixel] >= 60 with color 000000

Examples using 8 bits and 16 bits rasters can be seen at:

Table 13. LayerFeatureConstraints (MapServer 7.0.1 or above)

Features

Supported

Notes

FeatureTypeConstraint

Yes

Only one occurrence

Table 14. FeatureTypeConstraint (MapServer 7.0.1 or above)

Features

Supported

Notes

FeatureTypeName

No

Filter

Yes

Extent

No

A Filter can be applied through LayerFeatureConstraints/FeatureTypeConstraint to a named style or a user style. It is used to select which features are to be rendered. If the style has also filtering rules, they are combined with the filter of FeatureTypeConstraint (as well as with any FILTER or native filter set on the layer in the mapfile configuration)

<NamedLayer>
    <Name>Layer1</Name>
    <LayerFeatureConstraints>
        <FeatureTypeConstraint>
            <Filter>....</Filter>
        </FeatureTypeConstraint>
    </LayerFeatureConstraints>
    <NamedStyle>
        <Name>the_style</Name>
    </NamedStyle>
</NamedLayer>

Client Side Support

Client side support of the SLD consists of two parts:

  • The first part is using MapServer as a WMS client to send a GetMap request with an SLD. This is done using two metadata that can be placed at a layer level in a MapServer mapfile. These two metadata are:

    • wms_sld_url, which takes a valid URL as a value and appends SLD=xxx to the GetMap request.

    • wms_sld_body, which takes a valid SLD string and appends SLD_BODY=xxx to the GetMap request. If the value of wms_sld_body is set to AUTO, MapServer generates an SLD based on the classes found in the layer and send this SLD as the value of the SLD_BODY parameter in the GetMap request.

  • The other major item is the generation of an SLD document from MapServer classes. These functions are currently available through MapServer/MapScript interface. Here are the functions available:

    • on a map object: generatesld

    • on a layer object: generatesld

    Additional MapScript functions have been added or will be added to complement these functions:

    • on a map object: applysld

    • on a layer object: applysld

Note

When generating an SLD from MapServer classes, if there is a pixmap symbol you need to have this symbol available through a URL so it can be converted as an ExternalGraphic symbol in the SLD. To do this, you need to define the URL through a web object level metadata called WMS_SLD_SYMBOL_URL in your map file. The SLD generated uses this URL and concatenates the name of the pixmap symbol file to get the value that is generated as the ExternaGraphic URL.

PHP/MapScript Example that Generates an SLD from a Mapfile

The following is a small script that calls the generateSLD() function to create an SLD for a specific layer in a mapfile:

 1<?php
 2
 3// define variables
 4define( "MAPFILE", "D:/ms4w/apps/cadastra/map/cadastra.map" );
 5define( "MODULE", "php_mapscript.dll" );
 6
 7// load the mapscript module
 8if (!extension_loaded("MapScript")) dl(MODULE);
 9
10// open map
11$oMap = ms_newMapObj( MAPFILE );
12
13// get the parcel layer
14$oLayer = $oMap->getLayerByName("parcel");
15
16// force visibilty of the layer
17$oLayer->set('status', MS_ON);
18
19// generate the sld for that layer
20$SLD = $oLayer->generateSLD();
21
22// save sld to a file
23$fp = fopen("parcel-sld.xml", "a");
24fputs( $fp, $SLD );
25fclose($fp);
26
27?>

Named Styles support

Named styles support are introduced in MapServer 5.2. The support is base on MS RFC 39: Support of WMS/SLD Named Styles.

MapServer 5.2 introduces the possibility to assign a group to a series of classes defined on a layer object using two new non-mandatory keywords CLASSGROUP (at the layer level) and GROUP at the class level:

LAYER
    ...
    CLASSGROUP "group1"
    ...
    CLASS
        NAME "name1"
        GROUP "group1"
        ...
    END
    CLASS
        NAME "name2"
        GROUP "group2"
        ...
    END
    CLASS
        NAME "name3"
        GROUP "group1"
        ...
    END
    ...

At rendering time, if the CLASSGROUP is defined, only classes that have the same group name would be used. Based on this concept, WMS/SLD support uses the class groups as named styles. Each group of classes is considered equivalent to a named style:

  • The GetCapbilities request will output all the styles that are available

  • The GetMap request can use the STYLES parameter to specify a named style

  • The GetLegendGraphic can use the STYLES parameter to specify a named style

Other Items Implemented

  • Support of filled polygons with Mark and ExternalGraphic symbols.

  • MapScript functions to parse and apply SLD.

  • SLD_BODY request support on client and server side.

Issues Found During Implementation

  • Limitation of the FilterEncoding to comparison and logical filters. The spatial filters were not made available since it required major changes in MapServer WMS support.