5

Is there any way to use the conditional statement in SLD? somehow using if_then_else like this Pseudocode:

if (10 =< variable =< 100) {
    then Multiply variable by 2
} else {
    then Multiply it by 4
}

Or if it is also possible to have a multiple if_then_else

if (10 =< variable =< 100) {
    then Multiply variable by 2
} if (100 < variable =< 200) {
    then Multiply it by 3
} else {
    then Multiply it by 4
}

Example.sld

<ogc:Filter>
    <ogc:Function name="if_then_else">
        <ogc:PropertyName>variable</ogc:PropertyName>
        <!-- check if the "variable" is between 10 and 1000 -->
        <ogc:PropertyIsBetween>
            <ogc:LowerBoundary>
                <ogc:Literal>10</ogc:Literal>
            </ogc:LowerBoundary>
            <ogc:UpperBoundary>
                <ogc:Literal>100</ogc:Literal>
            </ogc:UpperBoundary>
        </ogc:PropertyIsBetween>

        <!-- if it is between this range then multiply it by 2 -->
        <ogc:Mul>
            <ogc:PropertyName>variable</ogc:PropertyName>
            <ogc:Literal>2</ogc:Literal>
        </ogc:Mul>

        <!-- else multiply it by 4 -->
        <ogc:Mul>
            <ogc:PropertyName>variable</ogc:PropertyName>
            <ogc:Literal>4</ogc:Literal>
        </ogc:Mul>
    </ogc:Function>
</ogc:Filter>

1 Answer 1

6

You can use something like this:

<ogc:Mul>
    <ogc:PropertyName>variable</ogc:PropertyName>
    <ogc:Function name="if_then_else">
        <ogc:Function name="between">
            <ogc:PropertyName>variable</ogc:PropertyName>
            <ogc:Literal>10</ogc:Literal>
            <ogc:Literal>100</ogc:Literal>
        </ogc:Function>
        <ogc:Literal>2</ogc:Literal>
        <ogc:Literal>4</ogc:Literal>
    </ogc:Function>
</ogc:Mul>

If you need more if statement you can change

<ogc:Literal>4</ogc:Literal>

with new if_then_else

More information about functions in sld expressions can be found on http://docs.geoserver.org/stable/en/user/filter/function_reference.html

1
  • 5+ years later, and this is still far more informative than anything found in the docs found at the link you reference! There's nothing in there to make it clear that you can chain functions
    – Auspex
    Feb 9 at 15:50

Your Answer

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

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