3

we are trying to make css style using geoserver's CSS extension, I have weather data in my table, like temp_max, temp_min, and rainfall which divided into classes depending on their range values, now we want to pass 'class_n' variable dynamically which is working fine, but we quite stuck on How to pass attribute names dynamically (temp_max, temp_min,...) ?

Also there is catch if propname is passed for rainfall then all color codes should be in reverse order! (if it so)

/* @title class 1 */
[ FEATURECLA = 'temp_max_deg_c' ] >= env('class1',-1)] <= env('class2',-1) {
    fill: #bdffe9;
}

Here we can see I'm try to pass both attribute name which can be 'temp_max_deg_c' or 'temp_min_deg_c' depending upon user response, but its not working as expected

/* @title class 2 */
[env('propname','temp_max_deg_c') > env('class2',-1)] [temp_max_deg_c <= env('class3',-1)] {
    fill: #ffff06;
}

If we pass without dynamically it is working fine for class value

/* @title class 3 */
[temp_max_deg_c > env('class3',-1)] [temp_max_deg_c <= env('class4',-1)] {
    fill: #fcd47b;
}
/* @title class 4 */
[temp_max_deg_c > env('class4',-1)] {
    fill: #fb5604;
}
/* @title taluka */
* {
    stroke: #000000;
    stroke-width: 1;
    fill: #FFFFFF;
    label: [thnname];
    label-anchor: 0.5 0.5;
}

Same thing is written is xml sld which is working where PropertyName and Literal are dynamically passed into url

<Rule>
          <Name>Rainfall1</Name>
          <Name>Dynamic</Name>
          <ogc:Filter>
            <ogc:PropertyIsLessThanOrEqualTo>
              <ogc:PropertyName>rainfall_mm</ogc:PropertyName>
              <ogc:Function name="env">
                <ogc:Literal>rain1</ogc:Literal>
                <ogc:Literal>-1</ogc:Literal>
              </ogc:Function>
            </ogc:PropertyIsLessThanOrEqualTo>
          </ogc:Filter>
          <PolygonSymbolizer>
            <Fill>
              <CssParameter name="fill">#c7e2f4</CssParameter>
            </Fill>
          </PolygonSymbolizer>
        </Rule>

Edits as suggested by passing &viewparams=forecast_date:2023-03-02&env=forecast_for:temp_max_class now we managed to break our style like this after adjusting variables into the layer, temp_max, temp_min, and rainfall as specified before we are trying to style layer according to user input which can be one of the above variable, the below code work for only one attribute if specied into the css style

/* @title taluka */
* {
    stroke: #000000;
    stroke-width: 1;
    fill: #FFFFFF;
    label: [thnname];
    label-anchor: 0.5 0.5;
}

/* @title class recode */
* {
  fill: [    
    recode(temp_min_class,
        1,'#bdffe9', 2,'#ffff06',
        3,'#fcd47b', 4,'#fb5604')
        ];
}

but non of the methods work for env substitution,

  1. Using 'env'
/* @title class recode */
* {
  fill: [    
    recode(env('forecast_for', 'temp_max_class'),
        1,'#bdffe9', 2,'#ffff06',
        3,'#fcd47b', 4,'#fb5604')
        ];
}

this renders only primary layer (Taluka) and shows blank in color code.

  1. Using 'FEATURECLA'
/* @title class recode */
* {
  fill: [    
    recode([ FEATURECLA = env('forecast_for', 'temp_max_class') ],
        1,'#bdffe9', 2,'#ffff06',
        3,'#fcd47b', 4,'#fb5604')
        ];
}

which showing error

Error at line 13. Encountered "<EOF>" at line 2, column 15. Was expecting one of: <STRING_LITERAL> ... "true" ... "false" ... "point" ... "linestring" ... "polygon" ... "multipoint" ... "multilinestring" ... "multipolygon" ... "geometrycollection" ... "envelope" ... "srid" ... "(" ... ")" ... "[" ... "-" ... <INTEGER_LITERAL> ... <FLOATING_LITERAL> ... <DATE> ... <DATE_TIME> ... <IDENTIFIER> ... Parsing : recode(.

Please help with error

without env variable

With env variable, renders primary layer only, without color coded areas

6
  • Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short tour for more about how the site works
    – Ian Turton
    Commented Mar 2, 2023 at 8:27
  • edited question as suggested
    – SpiderMan
    Commented Mar 2, 2023 at 9:16
  • I still don't see where you show what you have tried for the attributes?
    – Ian Turton
    Commented Mar 2, 2023 at 9:30
  • 'temp_max_deg_c ' is attribute present in table, and others like 'temp_min_deg_c', we can find that I'm trying to pass it dynamically, like we do in XML sld, using env variable
    – SpiderMan
    Commented Mar 2, 2023 at 12:44
  • OK, so what's not working for you?#
    – Ian Turton
    Commented Mar 2, 2023 at 13:26

1 Answer 1

3

There is a filter function called "property" that can resolve a property based on the name it has been provided.

So something like this may work:

[ FEATURECLA = env('X', 'temp_min_deg_c) ] >= property(env('attname', 'class1')]

and then you provide the desired property name in the GetMap URL, adding &env=X:temp_max_deg_c;attname:class2.

3
  • Thank you for your response @Andrea Aime, we are trying to pass both [ FEATURECLA] = 'X' & class1 dynamically, where X and class1 should be passed from url. X can be 'temp_max_deg_c' or 'temp_min_deg_c'
    – SpiderMan
    Commented Mar 3, 2023 at 6:19
  • Example updated... Commented Mar 5, 2023 at 9:12
  • Please refer updated question
    – SpiderMan
    Commented Mar 6, 2023 at 11:05

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.