Apply SLD to WMS Layer by OpenLayers 3

1.4k views Asked by At

I want to change SLD of WMS layer dynamically. I Can apply Geoserver saved SLD by STYLES param but when I want to apply SLD by SLD_BODY it send request but nothing happen. I think everything is well but SLD does not apply to layer. Here is the code:

var SLD = '<?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>pmo:'+ layer.Name + '</Name>'+
    '<UserStyle>'+
        '<Name>pmo:' + layer.Name + '</Name>' +
        '<Title>A small red flag</Title>'+
        '<Abstract>A sample of how to use an SVG based symbolizer</Abstract>'+
        '<FeatureTypeStyle>'+
        '<Rule>'+
            '<Title>Red flag</Title>'+
            '<PointSymbolizer>'+
            '<Graphic>'+
                '<ExternalGraphic>'+
                '<OnlineResource xlink:type="simple" xlink:href="burg02.svg" />'+
                '<Format>image/svg+xml</Format>'+
                '</ExternalGraphic>'+
                '<Size>'+
                '<ogc:Literal>20</ogc:Literal>'+
                '</Size>'+
            '</Graphic>'+
            '</PointSymbolizer>'+
        '</Rule>'+
        '</FeatureTypeStyle>'+
    '</UserStyle>'+
    '</NamedLayer>'+
    '</StyledLayerDescriptor>';
    SLD = encodeURI(SLD);

    var source = layer.WMSLayer.getSource();
    source.updateParams({ 'STYLES': '' ,'SLD_BODY': SLD });
1

There are 1 answers

1
Anusha kurra On

Instead of your sld pls replace this

var sld='<?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></Name><UserStyle><Name></Name><Title>A small red flag</Title><Abstract>A sample of how to use an SVG based symbolizer</Abstract><FeatureTypeStyle><FeatureTypeName>Feature</FeatureTypeName><SemanticTypeIdentifier>generic:geometry</SemanticTypeIdentifier><SemanticTypeIdentifier>simple</SemanticTypeIdentifier><Rule><Title>Red flag</Title><PointSymbolizer><Graphic><ExternalGraphic><OnlineResource xlink:type="simple" xlink:href="https://svn.osgeo.org/qgis/trunk/qgis/images/svg/gpsicons/anchor.svg" /><Format>image/svg+xml</Format></ExternalGraphic><Size><ogc:Literal>20</ogc:Literal></Size></Graphic></PointSymbolizer></Rule></FeatureTypeStyle></UserStyle></NamedLayer></StyledLayerDescriptor>'