2

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>
  • does <ogc:PropertyName><ogc:Function name="env"> property </ogc:Function></ogc:PropertyName> work? – Ian Turton Aug 31 '15 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 Sep 1 '15 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 Sep 1 '15 at 6:46
  • Don't worry about validator, does it work? – Ian Turton Sep 1 '15 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 Sep 1 '15 at 15:32
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.

1

degisken1 GIS_ID enter image description here

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

  • 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 Oct 7 '15 at 8:30
  • This answer saved my ass today. – ideamotor Mar 4 '16 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&...
  • Where is the difference for the last 2 lines of code? – SeGa Aug 7 '18 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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