geoserver动态样式
简介
geoserver支持设置动态样式。将样式(sld)中需要经常变化的值参数化,便可以在调用时通过传参来改变。
比如可以将颜色参数化,便可以在调用时传入色值来修改渲染出来的效果。
本例中的geoserver版本:2.20.3。
官网中说2.0.2以上版本支持该方法。
参数化语法
可以通过env函数设置参数,语法如下:
<ogc:Function name="env">
<ogc:Literal>color</ogc:Literal>
</ogc:Function>
也可以设置默认值:
<ogc:Function name="env">
<ogc:Literal>color</ogc:Literal>
<ogc:Literal>#ff0000</ogc:Literal>
</ogc:Function>
实例
下面是一个完整的sld文件
<?xml version="1.0" encoding="ISO-8859-1"?>
<StyledLayerDescriptor version="1.0.0"
xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/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">
<NamedLayer>
<Name>shili</Name>
<UserStyle>
<Title>shili</Title>
<FeatureTypeStyle>
<Rule>
<Title>canshuhua</Title>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">
<ogc:Function name="env">
<ogc:Literal>fillcolor</ogc:Literal>
<ogc:Literal>#00ffff</ogc:Literal>
</ogc:Function>
</CssParameter>
<CssParameter name="fill-opacity">
<ogc:Function name="env">
<ogc:Literal>fillopacity</ogc:Literal>
<ogc:Literal>0.5</ogc:Literal>
</ogc:Function>
</CssParameter>
</Fill>
<Stroke>
<CssParameter name="stroke">
<ogc:Function name="env">
<ogc:Literal>strokecolor</ogc:Literal>
<ogc:Literal>#00ffff</ogc:Literal>
</ogc:Function>
</CssParameter>
<CssParameter name="stroke-width">
<ogc:Function name="env">
<ogc:Literal>strokewidth</ogc:Literal>
<ogc:Literal>1</ogc:Literal>
</ogc:Function>
</CssParameter>
</Stroke>
</PolygonSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
这个实例中,我把fill
、fill-opacity
、stroke
、stroke-width
等四个属性做了参数化,并且分别设置了默认值。
默认效果如下:
调用时,如果在url后拼接上如下内容:
&env=fillcolor:%23ff0000;fillopacity:0.3;strokecolor:%23ff0000;strokewidth=2
注意:http需要将特殊字符进行转义,不然无法正常加载;#号对应的是 %23
动态参数效果如下: