[GIS] Geoserver optional symbology with variable substitution in SLD

geoserversldsymbologywms

I am trying to write an SLD that optionally takes a symbology for a PolygonSymbolizer and I can't work out how to do default to no symbology if the symbology env parameter is not set.

I have used the Variable Substitution example at http://docs.geoserver.org/stable/en/user/styling/sld-extensions/substitution.html as my basis.

<?xml version="1.0" encoding="UTF-8"?>
<sld:StyledLayerDescriptor xmlns="http://www.opengis.net/sld"
xmlns:sld="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc"
xmlns:gml="http://www.opengis.net/gml" version="1.0.0">
  <sld:NamedLayer>
    <sld:Name>Configurable Polygon Style with Defaults</sld:Name>
    <sld:UserStyle>
      <sld:Name>Configurable Polygon Style with Defaults</sld:Name>
      <sld:Title>Configurable Polygon Style with Defaults</sld:Title>
      <sld:FeatureTypeStyle>
        <sld:Name>configurable_polygon</sld:Name>
        <sld:Rule>

          <sld:PolygonSymbolizer>


            <sld:Fill>
              <sld:GraphicFill>
                <sld:Graphic>
                  <sld:Mark>
                    <sld:WellKnownName>
                      <ogc:Function name="env">
                        <ogc:Literal>symbology</ogc:Literal>
                        <ogc:Literal>solid</ogc:Literal>
                      </ogc:Function>

                    </sld:WellKnownName>
                    <sld:Stroke>
                      <sld:CssParameter name="stroke">
                        <ogc:Function name="env">
                          <ogc:Literal>fill-color</ogc:Literal>
                          <ogc:Literal>#1A4F57</ogc:Literal>
                        </ogc:Function>
                      </sld:CssParameter>
                      <sld:CssParameter name="stroke-opacity">
                        <ogc:Function name="env">
                          <ogc:Literal>fill-opacity</ogc:Literal>
                          <ogc:Literal>0.8</ogc:Literal>
                        </ogc:Function>
                      </sld:CssParameter>
                      <sld:CssParameter name="stroke-width">
                        <ogc:Function name="env">
                          <ogc:Literal>fill-width</ogc:Literal>
                          <ogc:Literal>0.5</ogc:Literal>
                        </ogc:Function>
                      </sld:CssParameter>
                    </sld:Stroke>
                  </sld:Mark>
                  <sld:Size>
                    <ogc:Function name="env">
                      <ogc:Literal>fill-size</ogc:Literal>
                      <ogc:Literal>10</ogc:Literal>
                    </ogc:Function>
                  </sld:Size>
                </sld:Graphic>
              </sld:GraphicFill>
              <sld:CssParameter name="fill">
                <ogc:Function name="env">
                  <ogc:Literal>fill-color</ogc:Literal>
                  <ogc:Literal>#37747d</ogc:Literal>
                </ogc:Function>
              </sld:CssParameter>
              <sld:CssParameter name="fill-opacity">
                <ogc:Function name="env">
                  <ogc:Literal>fill-opacity</ogc:Literal>
                  <ogc:Literal>0.8</ogc:Literal>
                </ogc:Function>
              </sld:CssParameter>
            </sld:Fill>
            <sld:Stroke>
              <sld:CssParameter name="stroke">
                <ogc:Function name="env">
                  <ogc:Literal>stroke-color</ogc:Literal>
                  <ogc:Literal>#1A4F57</ogc:Literal>
                </ogc:Function>
              </sld:CssParameter>
              <sld:CssParameter name="stroke-opacity">
                <ogc:Function name="env">
                  <ogc:Literal>stroke-opacity</ogc:Literal>
                  <ogc:Literal>0.8</ogc:Literal>
                </ogc:Function>
              </sld:CssParameter>
            </sld:Stroke>
          </sld:PolygonSymbolizer>
        </sld:Rule>
      </sld:FeatureTypeStyle>
    </sld:UserStyle>
  </sld:NamedLayer>
</sld:StyledLayerDescriptor>

I want to be able to essentially test whether the 'symbology' env parameter in the WMS request has been set and apply the enclosed styling otherwise default to having no <sld:GraphicFill>.

Does anybody have any pointers?

Best Answer

You have to add a filter that disables the rule completely if the env variable was not set