SLD

Author:Jeff McKenna
Contact:jmckenna at gatewaygeomatics.com
Author:Yewondwossen Assefa
Contact:assefa at dmsolutions.ca
Last Updated:

14-01-2011

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.

Ce document assume que vous êtes déjà familier avec les aspects suivants de MapServer :

  • Développement d’application MapServer et configuration des fichiers .map.

  • La familiarité avec la spécification WMS est un avantage. Un lien vers le document de spécification WMS est inclus dans la section suivante.

Support coté serveur

Information général

Il y a 2 façons à une requête WMS pour passer un document SLD avec une requête GeMap à MapServer :

  • paramètre SLD pointant vers un SLD distant (SLD=http://URL_TO_SLD).

  • paramètre SLD_BODY pour envoyer la définition SLD dans l’URL.

Ces deux méthodes sont à la fois disponibles par MapServer. Un exemple d’une requête serait :

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

Tester la requête SLD distante.

Le SLD dans la requête ci-dessus suit:

<?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 du même 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

Tous les exemples donnés dans ce document sont des utilisations en direct de SLD valides et une installation de MapServer avec le support SLD.

Additional WMS features related to SLDs have also been developed:

Table1. Fonctionnalités WMS supplémentaires

Fonctionnalités

Supportées

Remarques

Méthode GET : SLD URL

Oui

 

Méthode GET : SLD_BODY

Oui

Item supplémentaire

Describer Layer

Oui

 
GetLegendGraphic

Oui

 
GetStyles

Oui

Utilises MapScript pour récupérer le 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

Les tableaux suivants donnent beaucoup de détails supplémentaires sur la gestion du SLD dans MapServer.

Table2. Named Layers and User Layers

Fonctionnalités

Supportées

Remarques

Couches nommées

Oui

 

Couches utilisateur

Non

 

Table3. Named Layers

Fonctionnalités

Supportées

Remarques

LayerFeatureConstraints

Oui

Since MapServer 7.0.1

Styles nommés

Oui

 

Styles utilisateur

Oui

 

Table 4. Styles utilisateur

Fonctionnalités

Supportées

Remarques

Name

Non

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

Non

Aucune utilisation dans l’environnement de MapServer

Résumé

Non

Aucune utilisation dans l’environnement de MapServer

IsDefault

Non

Seulement un style est disponible par couche

FeatureTypeStyle

Oui

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

Table 5. FeatureTypeStyle

Fonctionnalités

Supportées

Remarques

Name

Non

Aucune utilisation dans l’environnement de MapServer

Title

Non

Aucune utilisation dans l’environnement de MapServer

Résumé

Non

Aucune utilisation dans l’environnement de MapServer

FeatureTypeName

Non

Aucune utilisation dans l’environnement de MapServer

SemanticTypeIdentifier

Non

Encore un élément expérimental dans les spécifications SLD

Rule

Oui

 

Table 6. Rule

Fonctionnalités

Supportées

Remarques

Name

Oui

 
Title

Oui

 

Résumé

Non

Aucune utilisation dans l’environnement de MapServer

LegendGraphic

Oui

 
Filter

Oui

 
ElseFilter

Oui

 
MinScaleDenominator

Oui

 
MaxScaleDenominator

Oui

 
LineSymbolizer

Oui

 
PolygonSymbolizer

Oui

 
PointSymbolizer

Oui

 
TextSymbolizer

Oui

 
RasterSymbolizer

Oui

S’applique pour les rasters 8-bit

  • Filter et 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 et MaxScaleDenominator sont convertis en minscale et maxscale dans 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

Fonctionnalités

Supportées

Remarques

Geometry

Non

MapServer uses the data geometry for the rendering
Stroke: GraphicFill

Non

Solid color is used
Stroke: GraphicStroke

Oui

Draws the symbol along the line
Stroke (CssParameter): stroke

Oui

Couleurs RVB sont supportées

Stroke (CssParameter): width

Oui

 
Stroke (CssParameter): opacity

Oui

Seulement pour le pilote AGG et MapServer en version >= 5.2

Stroke (CssParameter): linejoin and linecap

Non

Non supporté dans MapServer

Stroke (CssParameter): dasharray

Oui

 
Stroke (CssParameter): dashoffset

Non

 
PerperdicularOffset (only in SLD 1.1.0)

Oui

Offset values of the style object will be set
InitialGap(GraphicStroke SLD 1.1.0)

Non

 
Gap (GraphicStroke parameter SLD 1.1.0)

Non

 

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

Fonctionnalités

Supportées

Remarques

Geometry

Non

 
Stroke

Oui

Strokes are the same as for the LineSymbolizer
Fill

Oui

Was developed to support symbol fill polygons in addition to solid fill
Fill-opacity

Oui

Seulement disponible pour le pilote AGG et MapServer en version >= 5.2

PerperdicularOffset

Non

paramètre SLD 1.1.0

Displacement

Oui

Paramètre SLD 1.1.0. Définit offsetx/y dans 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

Fonctionnalités

Supportées

Remarques

Geometry

Non

 
Graphic: Mark symbol

Oui

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

Oui

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

Oui

Support ajouté dans MapServer 5.4

Size

Oui

 
Rotation

Oui

Support ajouté dans MapServer 5.4

Displacement

Oui

SLD 1.1.0 Parameter. Support added in MapServer 5.4
AnchorPoint

Non

 

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

Fonctionnalités

Supportées

Remarques

Geometry

Non

 
Label

Oui

 
Font(font-family)

Oui

Font names used are those available in MapServer font file. If no fonts are available there, default bitmap fonts are used
Font-style (Italic, ...)

Oui

 
Font-weight

Oui

 
Font-size

Oui

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

Oui

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

Oui

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

Oui

Only solid color is available

Notes on the TextSymbolizer:

  • 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

Fonctionnalités

Supportées

Remarques

Geometry

Non

 
Opacity

Oui

 
ChannelSelection

Non

 
OverlapBehaviour

Non

 
ColorMap

Oui

 
ContrastEnhancement

Non

 
ShadedRelief

Non

 
ImageOutline

Non

 

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

Fonctionnalités

Supportées

Remarques

Color

Oui

 
Opacity

Non

 
Quantity

Oui

 
Label

Non

 

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

Fonctionnalités

Supportées

Remarques

Categorize

Oui

 

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)

Fonctionnalités

Supportées

Remarques

FeatureTypeConstraint

Oui

Only one occurrence

Table 14. FeatureTypeConstraint (MapServer 7.0.1 or above)

Fonctionnalités

Supportées

Remarques

FeatureTypeName

Non

 
Filter

Oui

 
Extent

Non

 

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>

Support coté client

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.

Exemple PHP/MapScript qui génère un SLD depuis un Mapfile

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php

// define variables
define( "MAPFILE", "D:/ms4w/apps/cadastra/map/cadastra.map" );
define( "MODULE", "php_mapscript.dll" );

// load the mapscript module
if (!extension_loaded("MapScript")) dl(MODULE);

// open map
$oMap = ms_newMapObj( MAPFILE );

// get the parcel layer
$oLayer = $oMap->getLayerByName("parcel");

// force visibilty of the layer
$oLayer->set('status', MS_ON);

// generate the sld for that layer
$SLD = $oLayer->generateSLD();

// save sld to a file
$fp = fopen("parcel-sld.xml", "a");
fputs( $fp, $SLD );
fclose($fp);

?>

Gestion des styles nommés

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.
  • Fonctions MapScript pour parser et appliquer du SLD.

  • requête SLD_BODY supporté sur le client et côté serveur.

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.