5

In Geoserver, a have a SLD style with a rule that looks like this

    <Rule>
        <ogc:Filter>
            <ogc:PropertyIsGreaterThan>
                <ogc:Function name="env">
                  <ogc:PropertyName>
                      property
                  </ogc:PropertyName>
                </ogc:Function>

                <ogc:Function name="env">
                  <ogc:Literal>value1</ogc:Literal>
                  <ogc:Literal>0</ogc:Literal>
                </ogc:Function>

            </ogc:PropertyIsGreaterThan>
        </ogc:Filter>
        <PolygonSymbolizer>
            <Fill>
                <CssParameter name="fill">
                    #ff0000
                </CssParameter>
                <CssParameter name="fill-opacity">
                    0.5
                </CssParameter>
            </Fill>
        </PolygonSymbolizer>
    </Rule>

In the WMS request, i send an env parameter, specifing the property and the value1 parameters for dynamic sld substitution. The property parameter gets a value of an attribute name that exists on the layer I want to display the style with.

My solution won't work. I get an exception, saying:

The requested style cannot be used with this layer. The style specifies an attribute property and the layer is: ...

Is there a way to set the attribute name dynamically via sld substitution in Geoserver? And if yes, how can I achive that?

I am using Geoserver 2.7.2


Btw. this works:

            <ogc:PropertyIsGreaterThan>

                  <ogc:PropertyName>
                      Einwohner
                  </ogc:PropertyName>


                <ogc:Function name="env">
                  <ogc:Literal>value1</ogc:Literal>
                  <ogc:Literal>0</ogc:Literal>
                </ogc:Function>

            </ogc:PropertyIsGreaterThan>
5
  • does <ogc:PropertyName><ogc:Function name="env"> property </ogc:Function></ogc:PropertyName> work?
    – Ian Turton
    Commented Aug 31, 2015 at 15:58
  • Unfortunatly not, this would give a validation error: cvc-complex-type.2.4.d: Invalid content was found starting with element 'ogc:Function'. No child element is expected at this point.
    – hnrch
    Commented Sep 1, 2015 at 6:42
  • I am starting thinking about creating a string for SLD_BODY on the fly, although I find it to be a little hacky. Should also be less perfomant, I guess.
    – hnrch
    Commented Sep 1, 2015 at 6:46
  • Don't worry about validator, does it work?
    – Ian Turton
    Commented Sep 1, 2015 at 6:56
  • No, it does not. The error is gone, the layer gets rendered, but the filter is not working as expected. In fact, no matter what value value1 is, the filter now always includes all objects of the layer.
    – hnrch
    Commented Sep 1, 2015 at 15:32

6 Answers 6

3

I could not work this out. The feature of dynamically setting property names via env variables seems not to be supported at this moment.

I ended up with a different solution now (using the Geoserver REST API for styles) and I am quite happy with it.

Here is what I did: based on what the user enters on the GUI, I manually wrote sld (as a complete string). Then, I uploaded this string to the style REST endpoint of Geoserver, setting the correct content-header, as described here: http://docs.geoserver.org/2.7.1/user/rest/api/styles.html

Every style is getting a unique identifier. Before uploading a style, I try to download it via GET to see if it exists. If I get a 404, I do a POST to upload a new style. Otherwise, I upload my string with a PUT to modify the existing style. Once a style has been uploaded, I use the style Id in WMS GetMap requests.

0
1

degisken1 GIS_ID enter image description here

2
  • 4
    Welcome to GIS SE! As a new user be sure to take the Tour. Please always include code as text rather than pictures in your answers (and questions) so that it can become searchable and easy to copy for any testing. There is a format button {} available to make it easy.
    – PolyGeo
    Commented Oct 7, 2015 at 8:30
  • This answer saved my ass today.
    – ideamotor
    Commented Mar 4, 2016 at 21:48
1

I had been use SLD programming for polygon fill might be it will help you, have look my code,

    <Rule>
        <PolygonSymbolizer>
                <Fill>
                     <CssParameter name="fill">
                        <ogc:Function name="env">
                            <ogc:Literal>color</ogc:Literal>
                            <ogc:Literal>#E6E6E6</ogc:Literal>
                        </ogc:Function>
                     </CssParameter>
                </Fill>
        </PolygonSymbolizer>
    </Rule>
1

Well maybe too late, but for whom this could be of any help, this is an example of a filter using variable substitution that worked for me:

       <ogc:Filter>
          <ogc:And>
              <ogc:PropertyIsEqualTo>
                <ogc:PropertyName>agno</ogc:PropertyName>
                  <ogc:Function name="env">
                  <ogc:Literal>year</ogc:Literal>
                  <ogc:Literal>2010</ogc:Literal>
                  </ogc:Function>
              </ogc:PropertyIsEqualTo>
              <ogc:PropertyIsEqualTo> 
                <ogc:PropertyName>mes</ogc:PropertyName>
                  <ogc:Function name="env">
                  <ogc:Literal>month</ogc:Literal>
                  <ogc:Literal>5</ogc:Literal>
                  </ogc:Function>
              </ogc:PropertyIsEqualTo>
          </ogc:And>
      </ogc:Filter>

Then, in the WMS request, if I want to get the map of, lets say, December 1985, I send parameters like this:

...&env=year:1985;month:12&...

I think there is a mistake in the Geoserver online user manual, where is said:

...&env=name1:value1;name2=value2&...

Instead it should be:

...&env=name1:value1;name2:value2&...
1
  • Where is the difference for the last 2 lines of code?
    – SeGa
    Commented Aug 7, 2018 at 15:40
1

It's work! I request: GetMap and env param ..&env:myCol:population;break_1:75.79;..&.. <ogc:Filter> <ogc:PropertyIsGreaterThan> <!--I try this--> <ogc:Function name="property"> <ogc:Function name="env"> <ogc:Literal>myCol</ogc:Literal> </ogc:Function> </ogc:Function> <!--Instead of this--> <!--<ogc:PropertyName>c01</ogc:PropertyName>--> <ogc:Function name="env"> <ogc:Literal>break_1</ogc:Literal> </ogc:Function> </ogc:PropertyIsGreaterThan> </ogc:Filter>

-1

Post:
http://localhost:8080/geoserver/LPDB/wms?service=WMS&version=1.1.0&request=GetMap&layers=LPDB:lpdb_test_polygon&&env=color:40e0d0;opacity:1;&styles=&bbox=31.302,29.9575,31.3153,29.9621&width=954&height=330&srs=EPSG:4326&format=application/openlayers

SLD :

<?xml version="1.0" encoding="ISO-8859-1"?>
<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">
  <NamedLayer>
    <Name>Simple polygon with stroke</Name>
    <UserStyle>
      <Title>SLD Cook Book: Simple polygon with stroke</Title>
      <FeatureTypeStyle>
        <Rule>
          <PolygonSymbolizer>
            <Fill>
              <CssParameter name="fill">
                #<ogc:Function name="env">
                  <ogc:Literal>color</ogc:Literal>
                  <ogc:Literal>FF0000</ogc:Literal>
               </ogc:Function>
              </CssParameter>
              <CssParameter name="fill-opacity">
                <ogc:Function name="env">
                  <ogc:Literal>opacity</ogc:Literal>
                  <ogc:Literal>1</ogc:Literal>
               </ogc:Function>            
              </CssParameter>
            </Fill>

            <Stroke>
              <CssParameter name="stroke">#FFFFFF</CssParameter>
              <CssParameter name="stroke-width">2</CssParameter>
            </Stroke>
          </PolygonSymbolizer>
        </Rule>
      </FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.