To style labels with SLD, you can use a TextSymbolizer, which includes settings to configure the label content, the label location, and styling settings such as the font and the color. To enhance the readability of labels, users commonly want to add a box around a label. The OGC SLD/SE standards don’t support this capability, But LuciadLightspeed and LuciadFusion do support it by means of an SLD/SE extension.

To add a box around label text:

  1. Add the well-known mark square as a graphic to the TextSymbolizer. You can configure the desired stroke and fill settings for the box on the graphic.

  2. Add the vendor option graphic-resize=stretch to the TextSymbolizer. With this setting, you make sure to stretch the well-known mark square to contain the label.

  3. Optionally, add the vendor option graphic-margin to the TextSymbolizer. This vendor option takes an integer value to represent the desired padding in pixels between the label and its box.

The order of these elements is important. Place them at the end of the TextSymbolizer block.

This example adds a label with a box to a line geometry:

Program: Defining a text style with a box
<?xml version='1.0' encoding='UTF-8'?>
<FeatureTypeStyle xmlns="http://www.opengis.net/se"
                  xmlns:ogc="http://www.opengis.net/ogc"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://www.opengis.net/se http://schemas.opengis.net/se/1.1.0/FeatureStyle.xsd   http://www.opengis.net/ogc http://schemas.opengis.net/filter/1.1.0/filter.xsd"
                  version="1.1.0">
  <Rule>
    <Name>Text_style_with_label_box</Name>
    <TextSymbolizer>
      <Label>
        <ogc:Literal>MyLabel</ogc:Literal>
      </Label>
      <Font>
        <SvgParameter name="font-style">
          <ogc:Literal>normal</ogc:Literal>
        </SvgParameter>
        <SvgParameter name="font-weight">
          <ogc:Literal>normal</ogc:Literal>
        </SvgParameter>
        <SvgParameter name="font-size">
          <ogc:Literal>10</ogc:Literal>
        </SvgParameter>
        <SvgParameter name="font-family">Lucida Sans</SvgParameter>
      </Font>
      <LabelPlacement>
        <LinePlacement>
          <PerpendicularOffset>-16.0</PerpendicularOffset>
        </LinePlacement>
      </LabelPlacement>
      <Graphic>
        <Mark>
          <WellKnownName>square</WellKnownName>
          <Fill>
            <SvgParameter name="fill-opacity">
              <ogc:Literal>1.0</ogc:Literal>
            </SvgParameter>
            <SvgParameter name="fill">#FFFF00</SvgParameter>
          </Fill>
          <Stroke>
            <SvgParameter name="stroke-opacity">
              <ogc:Literal>1.0</ogc:Literal>
            </SvgParameter>
            <SvgParameter name="stroke">#0000FF</SvgParameter>
            <SvgParameter name="stroke-width">
              <ogc:Literal>3</ogc:Literal>
            </SvgParameter>
          </Stroke>
        </Mark>
      </Graphic>
      <VendorOption name="graphic-resize">stretch</VendorOption>
      <VendorOption name="graphic-margin">4</VendorOption>
    </TextSymbolizer>
    <LineSymbolizer>
      <Stroke>
        <SvgParameter name="stroke-width">
          <ogc:Literal>1</ogc:Literal>
        </SvgParameter>
        <SvgParameter name="stroke">
          <ogc:Literal>#FF0000</ogc:Literal>
        </SvgParameter>
        <SvgParameter name="stroke-opacity">
          <ogc:Literal>1.0</ogc:Literal>
        </SvgParameter>
      </Stroke>
    </LineSymbolizer>
  </Rule>
</FeatureTypeStyle>