我還沒有嘗試過自己,但這裏是它如何工作的:
首先定義SLD在你的js代碼
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>';
這是從the Geoserver SLD cookbook straigt拍攝,這是一個非常這種東西的好網站。不要忘記根據數據更改SLD(圖層名稱,數據屬性等)。在食譜中,你也可以看到數據應該是什麼樣子。
當你有SLD定義,你可以用它簡單地創建一個新的WMS層:
newWmsLayer = new OpenLayers.Layer.WMS.Post(layerName, wmsUrl,
{
layers: layerName,
sld_body: sld
});
,並最終將其添加到地圖:
map.addLayer(newWmsLayer);
當然,如果,這應該更新現有的圖層應先移除舊圖層。
您應該將此問題遷移到gis.stackexchange –