通过SLD_BODY动态改变geoserver的图层样式

目录

要点

使用

取标准样式

修改为自定义规则

GetMap使用

URL调用方式

Openlayers调用方式

GetLegendGraphic使用

访问报错问题


geoserver的WMS服务支持url参数上传sld_body来动态定义访问图层的样式,本文以GetMap以及GetLegendGraphic为例进行说明。


要点

  • 一定要注意样式格式的正确性,样式不能有缩进,否则容易报错
  • 图层名称一定要在UserLayer里的Name体现
  • 样式文本不要过大,url有字数上限
  • 尽量使用程序生成需要的样式文本,不要自行编辑

使用

取标准样式

先从geoserver取下来标准的polygon样式,目的是保证格式正确性,标准样式如下:

<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor version="1.0.0" 
 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 -->
  <NamedLayer>
    <Name>default_polygon</Name>
    <UserStyle>
    <!-- Styles can have names, titles and abstracts -->
      <Title>Default Polygon</Title>
      <Abstract>A sample style that draws a polygon</Abstract>
      <!-- FeatureTypeStyles describe how to render different features -->
      <!-- A FeatureTypeStyle for rendering polygons -->
      <FeatureTypeStyle>
        <Rule>
          <Name>rule1</Name>
          <Title>Gray Polygon with Black Outline</Title>
          <Abstract>A polygon with a gray fill and a 1 pixel black outline</Abstract>
          <PolygonSymbolizer>
            <Fill>
              <CssParameter name="fill">#AAAAAA</CssParameter>
            </Fill>
            <Stroke>
              <CssParameter name="stroke">#000000</CssParameter>
              <CssParameter name="stroke-width">1</CssParameter>
            </Stroke>
          </PolygonSymbolizer>
        </Rule>
      </FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

修改为自定义规则

将Rule部分修改或扩充多个,以满足自身需求为准,举例:

<Rule>
    <Name>rule1</Name>
    <Title>my title</Title>
    <Filter>
        <And>
            <PropertyIsGreaterThanOrEqualTo>
                <PropertyName>key</PropertyName>
                <Literal>1</Literal>
            </PropertyIsGreaterThanOrEqualTo>
            <PropertyIsLessThan>
                <PropertyName>key</PropertyName>
                <Literal>5</Literal>
            </PropertyIsLessThan>
        </And>
    </Filter>
    <PolygonSymbolizer>
        <Fill>
            <CssParameter name="fill">#00ff00</CssParameter>
            <CssParameter name="fill-opacity">1.0</CssParameter>
        </Fill>
        <Stroke/>
    </PolygonSymbolizer>
</Rule>

GetMap使用

URL调用方式

记得要将sld_body转换URLEncode,同时不需要写layer参数。

http://localhost:8080/geoserver/wms?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&sld_body=<StyledLayerDescriptor%20xmlns%3D"http%3A%2F%2Fwww.opengis.net%2Fsld"%20xmlns%3Aogc%3D"http%3A%2F%2Fwww.opengis.net%2Fogc"%20xmlns%3Axlink%3D"http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink"%20xmlns%3Axsi%3D"http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema-instance"%20version%3D"1.0.0"%20xsi%3AschemaLocation%3D"http%3A%2F%2Fwww.opengis.net%2Fsld%20StyledLayerDescriptor.xsd"><NamedLayer><Name>layer<%2FName><UserStyle><FeatureTypeStyle><Rule><Name>rule1<%2FName><Title>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

Openlayers调用方式

同样不需要写layer参数。

var sld = '你的样式文本';
	var wms = new ol.layer.Tile({
	    source: new ol.source.TileWMS({
	        url: 'http://localhost:8080/geoserver/wms',
	        params: {'VERSION': '1.1.1', sld_body: sld}
	    })
	});

GetLegendGraphic使用

这里要写layer参数,因为是必填的。

http://localhost:8080/geoserver/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=20&HEIGHT=20&sld_body=你的样式文本

访问报错问题

一般都是sld_body引起的,建议先用标准的样式进行测试,逐步修改样式,使用XML格式化网站进行测试。

转载自:https://blog.csdn.net/u013323965/article/details/83833432

You may also like...