transformation-func.rst 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. .. _transformation_func:
  2. Styling using Transformation Functions
  3. ======================================
  4. The Symbology Encoding 1.1 specification defines the following **transformation functions**:
  5. * ``Recode`` transforms a set of discrete attribute values into another set of values
  6. * ``Categorize`` transforms a continuous-valued attribute into a set of discrete values
  7. * ``Interpolate`` transforms a continuous-valued attribute into another continuous range of values
  8. These functions provide a concise way to compute styling parameters from feature attribute values.
  9. GeoServer implements them as :ref:`filter_function` with the same names.
  10. .. note::
  11. The GeoServer function syntax is slightly different to the SE 1.1 definition,
  12. since the specification defines extra syntax elements
  13. which are not available in GeoServer functions.
  14. These functions can make style documents more concise,
  15. since they express logic which would otherwise require
  16. many separate rules or complex Filter expressions,
  17. They even allow logic which is impossible to express any other way.
  18. A further advantage is that they often provide superior performance
  19. to explicit rules.
  20. One disadvantage of using these functions for styling is that
  21. they are not displayed in WMS legend graphics.
  22. Recode
  23. ------
  24. The ``Recode`` filter function transforms a set of discrete values for an attribute
  25. into another set of values.
  26. The function can be used within SLD styling parameters
  27. to convert the value of a feature attribute
  28. into specific values for a parameter such as color, size, width, opacity, etc.
  29. The recoding is defined by a set of *(input, output)* value pairs.
  30. Example
  31. ^^^^^^^
  32. Consider a chloropleth map of the US states dataset
  33. using the fill color to indicate the topographic regions for the states.
  34. The dataset has an attribute ``SUB_REGION`` containing the region code for each state.
  35. The ``Recode`` function is used to map each region code into a different color.
  36. The symbolizer for this style is:
  37. .. code-block:: xml
  38. <PolygonSymbolizer>
  39. <Fill>
  40. <CssParameter name="fill">
  41. <ogc:Function name="Recode">
  42. <!-- Value to transform -->
  43. <ogc:Function name="strTrim">
  44. <ogc:PropertyName>SUB_REGION</ogc:PropertyName>
  45. </ogc:Function>
  46. <!-- Map of input to output values -->
  47. <ogc:Literal>N Eng</ogc:Literal>
  48. <ogc:Literal>#6495ED</ogc:Literal>
  49. <ogc:Literal>Mid Atl</ogc:Literal>
  50. <ogc:Literal>#B0C4DE</ogc:Literal>
  51. <ogc:Literal>S Atl</ogc:Literal>
  52. <ogc:Literal>#00FFFF</ogc:Literal>
  53. <ogc:Literal>E N Cen</ogc:Literal>
  54. <ogc:Literal>#9ACD32</ogc:Literal>
  55. <ogc:Literal>E S Cen</ogc:Literal>
  56. <ogc:Literal>#00FA9A</ogc:Literal>
  57. <ogc:Literal>W N Cen</ogc:Literal>
  58. <ogc:Literal>#FFF8DC</ogc:Literal>
  59. <ogc:Literal>W S Cen</ogc:Literal>
  60. <ogc:Literal>#F5DEB3</ogc:Literal>
  61. <ogc:Literal>Mtn</ogc:Literal>
  62. <ogc:Literal>#F4A460</ogc:Literal>
  63. <ogc:Literal>Pacific</ogc:Literal>
  64. <ogc:Literal>#87CEEB</ogc:Literal>
  65. </ogc:Function>
  66. </CssParameter>
  67. </Fill>
  68. </PolygonSymbolizer>
  69. This style produces the following output:
  70. .. figure:: images/recode_usa_region.png
  71. Categorize
  72. ----------
  73. The ``Categorize`` filter function transforms a continuous-valued attribute
  74. into a set of discrete values.
  75. The function can be used within SLD styling parameters
  76. to convert the value of a feature attribute
  77. into specific values for a parameter such as color, size, width, opacity, etc.
  78. The categorization is defined by a list of alternating output values
  79. and data thresholds.
  80. The threshold values define the breaks between the input ranges.
  81. Inputs are converted into output values depending on which range they fall in.
  82. Example
  83. ^^^^^^^
  84. Consider a chloropleth map of the US states dataset
  85. using the fill color to indicate a categorization of the states by population.
  86. The dataset has attributes ``PERSONS`` and ``LAND_KM`` from which the population density
  87. is computed using the ``Div`` operator.
  88. This value is input to the ``Categorize`` function,
  89. which is used to assign different colors
  90. to the density ranges [ <= 20], [20 - 100], and [ > 100].
  91. The symbolizer for this style is:
  92. .. code-block:: xml
  93. <PolygonSymbolizer>
  94. <Fill>
  95. <CssParameter name="fill">
  96. <ogc:Function name="Categorize">
  97. <!-- Value to transform -->
  98. <ogc:Div>
  99. <ogc:PropertyName>PERSONS</ogc:PropertyName>
  100. <ogc:PropertyName>LAND_KM</ogc:PropertyName>
  101. </ogc:Div>
  102. <!-- Output values and thresholds -->
  103. <ogc:Literal>#87CEEB</ogc:Literal>
  104. <ogc:Literal>20</ogc:Literal>
  105. <ogc:Literal>#FFFACD</ogc:Literal>
  106. <ogc:Literal>100</ogc:Literal>
  107. <ogc:Literal>#F08080</ogc:Literal>
  108. </ogc:Function>
  109. </CssParameter>
  110. </Fill>
  111. </PolygonSymbolizer>
  112. This style produces the following output:
  113. .. figure:: images/categorize_usa_popdensity.png
  114. Interpolate
  115. -----------
  116. The ``Interpolate`` filter function transforms a continuous-valued attribute
  117. into another continuous range of values.
  118. The function can be used within SLD styling parameters
  119. to convert the value of a feature attribute
  120. into a continuous-valued parameter
  121. such as color, size, width, opacity, etc.
  122. The transformation is defined by a set of *(input, output)* control points
  123. chosen along a desired mapping curve.
  124. Piecewise interpolation along the curve is used
  125. to compute an output value for any input value.
  126. The function is able to compute either numeric or color values as output.
  127. This is known as the **interpolation method**,
  128. and is specified by an optional parameter with a value of ``numeric`` (the default) or ``color``.
  129. The *shape* of the mapping curve between control points is specified by the **interpolation mode**,
  130. which is an optional parameter with values of
  131. ``linear`` (the default), ``cubic``, or ``cosine``.
  132. Example
  133. ^^^^^^^
  134. Interpolating over color ranges allows concise definition of
  135. continuously-varying colors for chloropleth (thematic) maps.
  136. As an example, consider a map of the US states dataset
  137. using the fill color to indicate the population of the states.
  138. The dataset has an attribute ``PERSONS`` containing the population of each state.
  139. The population values lie in the range 0 to around 30,000,000.
  140. The interpolation curve is defined by three control points which assign colors to the
  141. population levels 0, 9,000,000 and 23,000,000.
  142. The colors for population values are computed by
  143. piecewise linear interpolation along this curve.
  144. For example, a state with a population of
  145. 16,000,000 is displayed with a color midway between the ones
  146. for the middle and upper control points.
  147. States with populations greater than 23,000,000 are displayed with the last color.
  148. Because the interpolation is being performed over color values,
  149. the method parameter is supplied, with a value of ``color``.
  150. Since the default linear interpolation is used,
  151. no interpolation mode is supplied,
  152. The symbolizer for this style is:
  153. .. code-block:: xml
  154. <PolygonSymbolizer>
  155. <Fill>
  156. <CssParameter name="fill">
  157. <ogc:Function name="Interpolate">
  158. <!-- Property to transform -->
  159. <ogc:PropertyName>PERSONS</ogc:PropertyName>
  160. <!-- Mapping curve definition pairs (input, output) -->
  161. <ogc:Literal>0</ogc:Literal>
  162. <ogc:Literal>#fefeee</ogc:Literal>
  163. <ogc:Literal>9000000</ogc:Literal>
  164. <ogc:Literal>#00ff00</ogc:Literal>
  165. <ogc:Literal>23000000</ogc:Literal>
  166. <ogc:Literal>#ff0000</ogc:Literal>
  167. <!-- Interpolation method -->
  168. <ogc:Literal>color</ogc:Literal>
  169. <!-- Interpolation mode - defaults to linear -->
  170. </ogc:Function>
  171. </CssParameter>
  172. </Fill>
  173. </PolygonSymbolizer>
  174. This symbolizer produces the following output:
  175. .. figure:: images/interpolate_usa_pop.png