.. _transformation_func: Styling using Transformation Functions ====================================== The Symbology Encoding 1.1 specification defines the following **transformation functions**: * ``Recode`` transforms a set of discrete attribute values into another set of values * ``Categorize`` transforms a continuous-valued attribute into a set of discrete values * ``Interpolate`` transforms a continuous-valued attribute into another continuous range of values These functions provide a concise way to compute styling parameters from feature attribute values. GeoServer implements them as :ref:`filter_function` with the same names. .. note:: The GeoServer function syntax is slightly different to the SE 1.1 definition, since the specification defines extra syntax elements which are not available in GeoServer functions. These functions can make style documents more concise, since they express logic which would otherwise require many separate rules or complex Filter expressions, They even allow logic which is impossible to express any other way. A further advantage is that they often provide superior performance to explicit rules. One disadvantage of using these functions for styling is that they are not displayed in WMS legend graphics. Recode ------ The ``Recode`` filter function transforms a set of discrete values for an attribute into another set of values. The function can be used within SLD styling parameters to convert the value of a feature attribute into specific values for a parameter such as color, size, width, opacity, etc. The recoding is defined by a set of *(input, output)* value pairs. Example ^^^^^^^ Consider a chloropleth map of the US states dataset using the fill color to indicate the topographic regions for the states. The dataset has an attribute ``SUB_REGION`` containing the region code for each state. The ``Recode`` function is used to map each region code into a different color. The symbolizer for this style is: .. code-block:: xml SUB_REGION N Eng #6495ED Mid Atl #B0C4DE S Atl #00FFFF E N Cen #9ACD32 E S Cen #00FA9A W N Cen #FFF8DC W S Cen #F5DEB3 Mtn #F4A460 Pacific #87CEEB This style produces the following output: .. figure:: images/recode_usa_region.png Categorize ---------- The ``Categorize`` filter function transforms a continuous-valued attribute into a set of discrete values. The function can be used within SLD styling parameters to convert the value of a feature attribute into specific values for a parameter such as color, size, width, opacity, etc. The categorization is defined by a list of alternating output values and data thresholds. The threshold values define the breaks between the input ranges. Inputs are converted into output values depending on which range they fall in. Example ^^^^^^^ Consider a chloropleth map of the US states dataset using the fill color to indicate a categorization of the states by population. The dataset has attributes ``PERSONS`` and ``LAND_KM`` from which the population density is computed using the ``Div`` operator. This value is input to the ``Categorize`` function, which is used to assign different colors to the density ranges [ <= 20], [20 - 100], and [ > 100]. The symbolizer for this style is: .. code-block:: xml PERSONS LAND_KM #87CEEB 20 #FFFACD 100 #F08080 This style produces the following output: .. figure:: images/categorize_usa_popdensity.png Interpolate ----------- The ``Interpolate`` filter function transforms a continuous-valued attribute into another continuous range of values. The function can be used within SLD styling parameters to convert the value of a feature attribute into a continuous-valued parameter such as color, size, width, opacity, etc. The transformation is defined by a set of *(input, output)* control points chosen along a desired mapping curve. Piecewise interpolation along the curve is used to compute an output value for any input value. The function is able to compute either numeric or color values as output. This is known as the **interpolation method**, and is specified by an optional parameter with a value of ``numeric`` (the default) or ``color``. The *shape* of the mapping curve between control points is specified by the **interpolation mode**, which is an optional parameter with values of ``linear`` (the default), ``cubic``, or ``cosine``. Example ^^^^^^^ Interpolating over color ranges allows concise definition of continuously-varying colors for chloropleth (thematic) maps. As an example, consider a map of the US states dataset using the fill color to indicate the population of the states. The dataset has an attribute ``PERSONS`` containing the population of each state. The population values lie in the range 0 to around 30,000,000. The interpolation curve is defined by three control points which assign colors to the population levels 0, 9,000,000 and 23,000,000. The colors for population values are computed by piecewise linear interpolation along this curve. For example, a state with a population of 16,000,000 is displayed with a color midway between the ones for the middle and upper control points. States with populations greater than 23,000,000 are displayed with the last color. Because the interpolation is being performed over color values, the method parameter is supplied, with a value of ``color``. Since the default linear interpolation is used, no interpolation mode is supplied, The symbolizer for this style is: .. code-block:: xml PERSONS 0 #fefeee 9000000 #00ff00 23000000 #ff0000 color This symbolizer produces the following output: .. figure:: images/interpolate_usa_pop.png