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,
- 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.
- 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