Search code examples
javascriptopenlayersgeoserver

How to add SLD Style to a WMS Layer dynamically through javascript


With OPENLAYERS + GEOSERVER + JAVASCRIPT + SERVLET + AJAX

I have a servlet which resceives ajax call and returns population of all countries . I want to add SLD in such a way that, the WMS layer will display red colour for countries with population lessthan 50,000 and Green Colour for countries with population greater than 50,000. So ultimately I want to design SLD style for a WMS Layer in javascript according to the results I obtain from the ajax call. Is it possible to apply SLD in javascript..? If so plz provide a sample code. Thanks for the patience.


Solution

  • I haven't tried it myself but here is how it can work:

    First define the SLD in your js-code

    var sld = '<?xml version="1.0" encoding="ISO-8859-1"?>';
    sld += '<StyledLayerDescriptor version="1.0.0"'; 
    sld += '    xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd" ';
    sld += '    xmlns="http://www.opengis.net/sld" ';
    sld += '    xmlns:ogc="http://www.opengis.net/ogc" ';
    sld += '    xmlns:xlink="http://www.w3.org/1999/xlink" ';
    sld += '    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">';
    sld += '  <NamedLayer>';
    sld += '    <Name>Attribute-based polygon</Name>';
    sld += '    <UserStyle>';
    sld += '      <Title>SLD Cook Book: Attribute-based polygon</Title>';
    sld += '      <FeatureTypeStyle>';
    sld += '        <Rule>';
    sld += '          <Name>SmallPop</Name>';
    sld += '          <Title>Less Than 200,000</Title>';
    sld += '          <ogc:Filter>';
    sld += '            <ogc:PropertyIsLessThan>';
    sld += '              <ogc:PropertyName>pop</ogc:PropertyName>';
    sld += '              <ogc:Literal>200000</ogc:Literal>';
    sld += '            </ogc:PropertyIsLessThan>';
    sld += '          </ogc:Filter>';
    sld += '          <PolygonSymbolizer>';
    sld += '            <Fill>';
    sld += '              <CssParameter name="fill">#66FF66</CssParameter>';
    sld += '            </Fill>';
    sld += '          </PolygonSymbolizer>';
    sld += '        </Rule>';
    sld += '        <Rule>';
    sld += '          <Name>MediumPop</Name>';
    sld += '          <Title>200,000 to 500,000</Title>';
    sld += '          <ogc:Filter>';
    sld += '            <ogc:And>';
    sld += '              <ogc:PropertyIsGreaterThanOrEqualTo>';
    sld += '                <ogc:PropertyName>pop</ogc:PropertyName>';
    sld += '                <ogc:Literal>200000</ogc:Literal>';
    sld += '              </ogc:PropertyIsGreaterThanOrEqualTo>';
    sld += '              <ogc:PropertyIsLessThan>';
    sld += '                <ogc:PropertyName>pop</ogc:PropertyName>';
    sld += '                <ogc:Literal>500000</ogc:Literal>';
    sld += '              </ogc:PropertyIsLessThan>';
    sld += '            </ogc:And>';
    sld += '          </ogc:Filter>';
    sld += '          <PolygonSymbolizer>';
    sld += '            <Fill>';
    sld += '              <CssParameter name="fill">#33CC33</CssParameter>';
    sld += '            </Fill>';
    sld += '          </PolygonSymbolizer>';
    sld += '        </Rule>';
    sld += '        <Rule>';
    sld += '          <Name>LargePop</Name>';
    sld += '          <Title>Greater Than 500,000</Title>';
    sld += '          <ogc:Filter>';
    sld += '            <ogc:PropertyIsGreaterThan>';
    sld += '              <ogc:PropertyName>pop</ogc:PropertyName>';
    sld += '              <ogc:Literal>500000</ogc:Literal>';
    sld += '            </ogc:PropertyIsGreaterThan>';
    sld += '          </ogc:Filter>';
    sld += '          <PolygonSymbolizer>';
    sld += '            <Fill>';
    sld += '              <CssParameter name="fill">#009900</CssParameter>';
    sld += '            </Fill>';
    sld += '          </PolygonSymbolizer>';
    sld += '        </Rule>';
    sld += '      </FeatureTypeStyle>';
    sld += '    </UserStyle>';
    sld += '  </NamedLayer>';
    sld += '</StyledLayerDescriptor>';
    

    This is taken straigt from the Geoserver SLD cookbook, which is a very good site for this kind of stuff. Do not forget to change the SLD accrdingly to your data (Layer name, Data properties and so on). In the cookbook you can also see how the data is supposed to look like.

    When you have the SLD defined you can simply create a new WMS layer using it:

    newWmsLayer = new OpenLayers.Layer.WMS.Post(layerName, wmsUrl,
        {
            layers: layerName,
            sld_body: sld
        });
    

    and finally add it to the map:

    map.addLayer(newWmsLayer);
    

    Of course if, this should update an existing layer you should remove the old one first.