Styling a layer with SLD is a two-step process:

This tutorial offers examples of the most common use cases for completing those two steps. For more detail about the concepts and API, see The SLD styling model.

Decoding an SLD file

To convert an SLD file containing the XML-encoded version of the SLD styling to a Java domain object, you can use the TLcdSLDFeatureTypeStyleDecoder.decode method.

Program: Decoding an SLD file
    TLcdSLDFeatureTypeStyleDecoder decoder = new TLcdSLDFeatureTypeStyleDecoder();
    String sldFile = "Data/Shp/Usa/rivers.sld";
    TLcdSLDFeatureTypeStyle sldStyle = decoder.decodeFeatureTypeStyle(sldFile);

If the SLD is not stored in a file, you can either decode directly from an InputStream, or use the static utility method to decode directly from a String. For example:

Program: Decoding SLD from an InputStream
    TLcdSLDFeatureTypeStyleDecoder decoder = new TLcdSLDFeatureTypeStyleDecoder();
    try (InputStream is = new TLcdInputStreamFactory().createInputStream("Data/Shp/Usa/rivers.sld")) {
      TLcdSLDFeatureTypeStyle sldStyle = decoder.decodeFeatureTypeStyle(is);
Program: Decoding SLD from a String
    String sldAsString = "<?xml version='1.0' encoding='UTF-8'?>\n"
                         + "<FeatureTypeStyle xmlns=\"\" xmlns:ogc=\"\"\n"
                         + "                  xmlns:xsi=\"\"\n"
                         + "                  xsi:schemaLocation=\"\"\n"
                         + "                  version=\"1.1.0\">\n"
                         + "  <Name>Rivers</Name>\n"
                         + "  <Description>\n"
                         + "    <Title>Rivers</Title>\n"
                         + "    <Abstract>Visualizes objects as blue lines.</Abstract>\n"
                         + "  </Description>\n"
                         + "  <Rule>\n"
                         + "    <LineSymbolizer>\n"
                         + "      <Stroke>\n"
                         + "        <SvgParameter name=\"stroke\">#2020ff</SvgParameter>\n"
                         + "        <SvgParameter name=\"stroke-width\">1</SvgParameter>\n"
                         + "      </Stroke>\n"
                         + "    </LineSymbolizer>\n"
                         + "  </Rule>\n"
                         + "</FeatureTypeStyle>";
    TLcdSLDFeatureTypeStyle sldStyle = TLcdSLDFeatureTypeStyleDecoder.decodeFromString(sldAsString);

Creating an SLD-styled Lightspeed layer

You can use the TLcdSLDFeatureTypeStyle to style your layer. This is illustrated for vector layers and raster layers in these examples:

Program: Creating an SLD-styled Lightspeed vector layer
    TLcdSLDFeatureTypeStyle sldStyle = new TLcdSLDFeatureTypeStyleDecoder()

    TLspLayer layer = TLspShapeLayerBuilder.newBuilder()
Program: Creating an SLD-styled Lightspeed raster layer
    TLcdSLDFeatureTypeStyle sldStyle = new TLcdSLDFeatureTypeStyleDecoder()

    ILspEditableStyledLayer layer = TLspRasterLayerBuilder.newBuilder()

Updating the SLD style on a Lightspeed layer

If you have an existing Lightspeed layer and you want to switch to SLD styling, or update the current SLD style, you can use the configureForSLDStyling method:

Program: Applying an SLD style to an existing Lightspeed vector layer
    if (layer instanceof TLspLayer) {
      ((TLspLayer) layer).configureForSLDStyling(sldStyle);
Program: Applying an SLD style to an existing Lightspeed raster layer
    if (layer instanceof TLspRasterLayer) {
      ((TLspRasterLayer) layer).configureForSLDStyling(sldStyle);