最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

openlayers - How to add SLD Style to a WMS Layer dynamically through javascript - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Jan 6, 2013 at 21:52 DileepKumarDileepKumar 591 silver badge8 bronze badges 1
  • You should migrate this question to gis.stackexchange – Ian Turton Commented Jan 7, 2013 at 8:35
Add a ment  | 

1 Answer 1

Reset to default 5

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/sld StyledLayerDescriptor.xsd" ';
sld += '    xmlns="http://www.opengis/sld" ';
sld += '    xmlns:ogc="http://www.opengis/ogc" ';
sld += '    xmlns:xlink="http://www.w3/1999/xlink" ';
sld += '    xmlns:xsi="http://www.w3/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.

发布评论

评论列表(0)

  1. 暂无评论