通過(guò)SLD_BODY動(dòng)態(tài)改變geoserver的圖層樣式
分類(lèi)專(zhuān)欄:?開(kāi)源WebGIS
目錄
geoserver的WMS服務(wù)支持url參數(shù)上傳sld_body來(lái)動(dòng)態(tài)定義訪問(wèn)圖層的樣式,本文以GetMap以及GetLegendGraphic為例進(jìn)行說(shuō)明踱承。
一定要注意樣式格式的正確性迂猴,樣式不能有縮進(jìn),否則容易報(bào)錯(cuò)
圖層名稱(chēng)一定要在UserLayer里的Name體現(xiàn)
樣式文本不要過(guò)大,url有字?jǐn)?shù)上限
盡量使用程序生成需要的樣式文本,不要自行編輯
先從geoserver取下來(lái)標(biāo)準(zhǔn)的polygon樣式,目的是保證格式正確性拭卿,標(biāo)準(zhǔn)樣式如下:
<?xml version="1.0"encoding="UTF-8"?>
xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd"
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">
<!-- a Named Layer is the basic building block of an SLD document -->
default_polygon
<!-- Styles can have names, titles and abstracts -->
Default Polygon
A sample style that draws a polygon
<!-- FeatureTypeStyles describe how to render different features -->
<!-- A FeatureTypeStyle for rendering polygons -->
rule1
Gray Polygon with Black Outline
A polygon with a gray fill and a 1 pixel black outline
#AAAAAA
#000000
1
將Rule部分修改或擴(kuò)充多個(gè),以滿足自身需求為準(zhǔn)贱纠,舉例:
rule1
my title
key
1
key
5
#00ff00
1.0
記得要將sld_body轉(zhuǎn)換URLEncode峻厚,同時(shí)不需要寫(xiě)layer參數(shù)。
http://localhost:8080/geoserver/wms?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&sld_body=layer<%2FName>rule1<%2FName>title<%2FTitle><Filter><PropertyIsLessThan><PropertyName>key<%2FPropertyName><Literal>5<%2FLiteral><%2FPropertyIsLessThan><%2FFilter><PolygonSymbolizer><Fill><CssParameter%20name%3D"fill">%23ff0000<%2FCssParameter><CssParameter%20name%3D"fill-opacity">1.0<%2FCssParameter><%2FFill><Stroke%2F><%2FPolygonSymbolizer><%2FRule><%2FFeatureTypeStyle><%2FUserStyle><%2FNamedLayer><%2FStyledLayerDescriptor>&WIDTH=256&HEIGHT=256&SRS=EPSG%3A4326&STYLES=&BBOX=106.171875%2C21.796875%2C106.5234375%2C22.1484375
同樣不需要寫(xiě)layer參數(shù)并巍。
varsld ='你的樣式文本';
varwms =newol.layer.Tile({
source:newol.source.TileWMS({
url:'http://localhost:8080/geoserver/wms',
params: {'VERSION':'1.1.1', sld_body: sld}
? ? })
});
這里要寫(xiě)layer參數(shù),因?yàn)槭潜靥畹摹?/p>
http://localhost:8080/geoserver/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=20&HEIGHT=20&sld_body=你的樣式文本
一般都是sld_body引起的换途,建議先用標(biāo)準(zhǔn)的樣式進(jìn)行測(cè)試懊渡,逐步修改樣式,使用XML格式化網(wǎng)站進(jìn)行測(cè)試军拟。