transformation.rst 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. .. _css_examples_transformation:
  2. Using transformation functions
  3. ==============================
  4. The transformation functions supported described in SLD and described in the :ref:`equivalent SLD chapter <transformation_func>`
  5. are also available in CSS, the following shows examples of how they can be used.
  6. Recode
  7. ------
  8. The ``Recode`` filter function transforms a set of discrete values for an attribute into another set of values,
  9. by applying a *(input, output)* mapping onto the values of the variable/expression that is provided
  10. as the first input of the function.
  11. Consider a chloropleth map of the US states dataset
  12. using the fill color to indicate the topographic regions for the states.
  13. The dataset has an attribute ``SUB_REGION`` containing the region code for each state.
  14. The ``Recode`` function is used to map each region code into a different color.
  15. .. note::
  16. It is to be noted that the following example specifies colors as hex string as opposed to native
  17. CSS color names, this is because the function syntax is expressed in CQL, which does not have
  18. support for native CSS color names.
  19. .. code-block:: css
  20. * {
  21. fill: [recode(strTrim(SUB_REGION),
  22. 'N Eng', '#6495ED',
  23. 'Mid Atl', '#B0C4DE',
  24. 'S Atl', '#00FFFF',
  25. 'E N Cen', '#9ACD32',
  26. 'E S Cen', '#00FA9A',
  27. 'W N Cen', '#FFF8DC',
  28. 'W S Cen', '#F5DEB3',
  29. 'Mtn', '#F4A460',
  30. 'Pacific', '#87CEEB')];
  31. stroke: lightgrey;
  32. label: [STATE_ABBR];
  33. font-family: 'Arial';
  34. font-fill: black;
  35. label-anchor: 0.5 0.5;
  36. }
  37. .. figure:: ../../sld/tipstricks/images/recode_usa_region.png
  38. :align: center
  39. Categorize
  40. ----------
  41. The ``Categorize`` filter function transforms a continuous-valued attribute
  42. into a set of discrete values by assiging ranges of values and turning them
  43. into a color, size, width, opacity, etc.
  44. In the following example a coropleth map is build associating a color to the state population density
  45. in the ranges [ <= 20], [20 - 100], and [ > 100].
  46. .. code-block:: css
  47. * {
  48. fill: [categorize(
  49. PERSONS / LAND_KM,
  50. '#87CEEB',
  51. 20,
  52. '#FFFACD',
  53. 100,
  54. '#F08080')];
  55. stroke : lightgrey;
  56. label: [STATE_ABBR];
  57. font-family: 'Arial';
  58. font-fill: black;
  59. label-anchor: 0.5 0.5;
  60. }
  61. .. figure:: ../../sld/tipstricks/images/categorize_usa_popdensity.png
  62. :align: center
  63. Interpolate
  64. -----------
  65. The ``Interpolate`` filter function transforms a continuous-valued attribute
  66. into another continuous range of values by applying piecewise interpolation.
  67. The result will work for numeric values such as size, width, opacity
  68. when operating in ``numeric`` **interpolation method** (the default), and for colors when working in ``color`` mode.
  69. The type of curve fitting the specified points can be either ``linear`` (the default), ``cubic`` or ``cosine``,
  70. these values are known as the **interpolation mode**.
  71. Both the interpolation method and mode are optional, and if provided, they are added at the end of the
  72. input list.
  73. In the following example the state population is mapped to a continuous color scale in a rather
  74. compact way using the interpolate function:
  75. .. code-block:: css
  76. * {
  77. fill: [Interpolate(
  78. PERSONS,
  79. 0, '#FEFEEE',
  80. 9000000, '#00FF00',
  81. 23000000, '#FF0000',
  82. 'color',
  83. 'linear')];
  84. stroke : lightgrey;
  85. label: [STATE_ABBR];
  86. font-family: 'Arial';
  87. font-fill: black;
  88. label-anchor: 0.5 0.5;
  89. }
  90. .. figure:: ../../sld/tipstricks/images/interpolate_usa_pop.png
  91. :align: center