Thematic map creation with SLD is now much easier
While the SLD styling language used in GeoServer is very powerful, it’s no secret that it’s somewhat verbose to author. One reason for this is the need to define multiple rules to specify different styles based on attribute values. The OGC Symbology Encoding specification introduced transformation functions which can help make attribute-based styling more concise. The functions Recode, Categorize, and Interpolate transform values in several different ways. They can be used to convert attribute values directly into styling parameters such as color, width, and opacity. These have recently been added to GeoServer as Filter Functions, so they’re now available to help make your SLDs shorter and more readable.
The first two functions are analogous to SQL’s “inline CASE expression”. The Recode function transforms a set of discrete values into another set of values. A typical use in styling is to produce a chloropleth map by converting the values of an attribute into a set of colors. As an example, in the map below states have a REGION attribute with values such as “N Eng” and “Mtn”. This would normally require multiple rules to style with different fill colors, but this can be expressed more concisely by using Recode to convert the REGION values into colors directly for use in the Fill styling parameter.
The Categorize function is similar to Recode, but transforms attribute value ranges into a set of discrete values. A list of threshold values determines the input ranges, each of which is then assigned an output value. For example, in the map below, the population density of the states is computed as the population divided by the land area. Categorize is used to convert density ranges into color values (in this case, three different colors show densities of less than 20, between 20 and 100, and greater than 100 persons per square kilometer).
The Interpolate function is the most powerful of the three. It transforms a continuous-valued attribute into another continuous range of values, using a curve to define the mapping between them. Input values are interpolated along the curve to compute a corresponding output value. The function even provides the ability to specify the kind of interpolation used, with options of linear, cosine or cubic curves. It is also able to interpolate across RGB color values as the output domain. This makes it easy to create thematic maps using smoothly varying colors to represent a continuous attribute. In the map below, Interpolate is used to compute a continuously-varying color range from an attribute containing the population of each state.
A more in-depth explanation and the XML code for the above examples is available in the GeoServer User Guide Styling section.