通过OpenLayers 3将SLD应用于WMS层

时间:2016-12-28 12:29:57

标签: openlayers-3 geoserver sld

我想动态更改WMS图层的SLD。我可以通过STYLES param应用Geoserver保存的SLD但是当我想通过SLD_BODY应用SLD时它发送请求但没有任何反应。我认为一切都很好,但SLD不适​​用于图层。这是代码:

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 个答案:

答案 0 :(得分:0)

而不是你的sls请替换这个

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>'