简体   繁体   English

如何通过javascript动态将SLD样式添加到WMS层

[英]How to add SLD Style to a WMS Layer dynamically through javascript

With OPENLAYERS + GEOSERVER + JAVASCRIPT + SERVLET + AJAX 使用OPENLAYERS + GEOSERVER + JAVASCRIPT + SERVLET + AJAX

I have a servlet which resceives ajax call and returns population of all countries . 我有一个servlet,它接收ajax调用并返回所有国家的人口。 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. 我想以这样一种方式添加SLD:WMS层将为人口少于50,000的国家显示红色,为人口超过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. 因此,最终我想根据从ajax调用获得的结果为javascript设计WMS层的SLD样式。 Is it possible to apply SLD in javascript..? 是否可以在javascript ..中应用SLD? If so plz provide a sample code. 如果是这样,请提供示例代码。 Thanks for the patience. 感谢您的耐心配合。

I haven't tried it myself but here is how it can work: 我自己还没有尝试过,但是它可以这样工作:

First define the SLD in your js-code 首先在您的js代码中定义SLD

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. 这是从Geoserver SLD Cookbook上直接获得的 ,该书是此类内容的一个很好的网站。 Do not forget to change the SLD accrdingly to your data (Layer name, Data properties and so on). 不要忘记根据您的数据(层名称,数据属性等)更改SLD。 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: 定义了SLD后,您可以简单地使用它创建一个新的WMS层:

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. 当然,如果要更新现有图层,则应先删除旧图层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM