11

Is it possible to use simple Math in an SLD file on Geoserver? Suppose I want to read a text size property from a database and multiply its value, how can I do this?

<sld:TextSymbolizer>
    ...
    <sld:CssParameter name="font-size">
        <ogc:PropertyName>text_size</ogc:PropertyName> * 2.1
    </sld:CssParameter>
    ...
</sld:TextSymbolizer>

This simple approach obviously doesn't work, so I thought there must be some way to achieve this.

12

You can use the math functions ogc:Mul ogc:Div ogc:Add & ogc:Sub to do simple maths on properties.

So your example would become:

<sld:CssParameter name="font-size">
    <ogc:Mul>
       <ogc:PropertyName>text_size</ogc:PropertyName>
       <ogc:Literal>2.5</ogc:Literal>
    </ogc:Mul>
</sld:CssParameter>

The only issue is that technically the SLD specification may not allow you to use a function in a CssParameter. However GeoServer is much more forgiving and will allow it.

If you ever need more complex functions see the reference page.

  • works like a charm – Bruno Schäpper Jan 21 '15 at 8:46
  • I found that <ogc:Mult> did not work for me, but <ogc:Mul> did. – Simon O'Keefe Jun 8 '16 at 1:33
0

As far as I'm aware GeoServer cannot do any math embedded into the SLD.
You can however create a rule in the SLD that can read <ogc:PropertyName>text_size</ogc:PropertyName> with a filter, and define your own text size.
The GeoServer SLD Cookbook has some examples of using <ogc:Filter>

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.