# Geoserver SLD: Dynamic Variable Substitution

I'm facing a problem with Geoserver and its styling mechanism that I'm not sure whether it's possible to be solved or not. But first, a quick context...

Context

We want to use Geoserver and OpenLayers to display a map in our application. In this app, we will be able to choose a Brazilian state from a select field in order to see every city from this state. Also, consider that each city will be filled with a specific color.

The colors will be sent through a WMS Request instead of computed or defined inside the SLD, for example, using data from our layer dataset. That's the problem... One more thing to take into account is that some states in Brazil have more than 400 cities.

Solving the Problem?

The first approach we tried consists of creating a specific Rule for each city. However it turned out to be unfeasible because we have more than 300 cities to be displayed in some states. Imagine how big this SLD file would be!

The second approach we thought about was to dinamically get each color from the QueryString. For example, when displaying the city named "Salvador", it must search the QueryString for the parameter color-salvador. But we don't know how to it. Actually, it seems impossible. Am I right?

Take a look at the SLD below.

<Rule>
<Name>City Rule</Name>
<Title>City</Title>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">
#<ogc:Function name="env">

<!-- Problem here. We need to define this value dinamically -->
<ogc:Literal>234545</ogc:Literal>
</ogc:Function>
</CssParameter>
</Fill>
</PolygonSymbolizer>
</Rule>


What we would like to do is instead of defining the literal salvador, inside the env function, we would like to use a Property like city-name that comes from our layer dataset. Something like this...

<Rule>
<Name>City Rule</Name>
<Title>City</Title>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">
#<ogc:Function name="env">
<ogc:Literal>
<ogc:PropertyName>city_name</ogc:PropertyName>-color
</ogc:Literal>
<ogc:Literal>234545</ogc:Literal>
</ogc:Function>
</CssParameter>
</Fill>
</PolygonSymbolizer>
</Rule>


<ogc:Literal>
<ogc:PropertyName>city_name</ogc:PropertyName>-color
</ogc:Literal>


You need to use the function Concatenate

<ogc:Function name="Concatenate">
<ogc:PropertyName>city_name</ogc:PropertyName>
<ogc:Literal>-color</ogc:Literal>
</ogc:Function>

• Can I use this code in place of ogc:Literal, inside the "env" function? Unfortunately, I can't try it now. I'll give it a try on next Monday. – Marlon Aug 28 '15 at 10:45
• yes it will replace the literal in the env function – Ian Turton Aug 28 '15 at 10:46
• But that's not the idea. I need the env function because I'll use this concatenated value to get a variable from the querystring using the Variable Substitution mechanism. – Marlon Aug 28 '15 at 10:58