raster.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. .. _css_cookbook_rasters:
  2. Rasters
  3. =======
  4. Rasters are geographic data displayed in a grid. They are similar to image files such as PNG files, except that instead of each point containing visual information, each point contains geographic information in numerical form. Rasters can be thought of as a georeferenced table of numerical values.
  5. One example of a raster is a Digital Elevation Model (DEM) layer, which has elevation data encoded numerically at each georeferenced data point.
  6. Example raster
  7. --------------
  8. The :download:`raster layer <../../sld/cookbook/artifacts/sld_cookbook_raster.zip>` that is used in the examples below contains elevation data for a fictional world. The data is stored in EPSG:4326 (longitude/latitude) and has a data range from 70 to 256. If rendered in grayscale, where minimum values are colored black and maximum values are colored white, the raster would look like this:
  9. .. figure:: ../../sld/cookbook/images/raster.png
  10. :align: center
  11. *Raster file as rendered in grayscale*
  12. :download:`Download the raster file <../../sld/cookbook/artifacts/sld_cookbook_raster.zip>`
  13. .. _css_cookbook_raster_twocolorgradient:
  14. Two-color gradient
  15. ------------------
  16. This example shows a two-color style with green at lower elevations and brown at higher elevations.
  17. .. figure:: ../../sld/cookbook/images/raster_twocolorgradient.png
  18. :align: center
  19. *Two-color gradient*
  20. Code
  21. ~~~~
  22. .. code-block:: css
  23. :linenos:
  24. * {
  25. raster-channels: auto;
  26. raster-color-map:
  27. color-map-entry(#008000, 70)
  28. color-map-entry(#663333, 256);
  29. }
  30. Details
  31. ~~~~~~~
  32. There is a single rule which applies a color map to the raster data.
  33. The "raster-channels" attribute activates raster symbolization, the "auto" value is indicates that we are going to use the default choice of bands to symbolize the output (either gray or RBG/RGBA depending on the input data). There is also the possibility of providing a band name or a list of band names in case we want to choose specific bands out of a multiband input, e.g., "1" or "1 3 7".
  34. The "raster-color-map" attribute builds a smooth gradient between two colors corresponding to two elevation values. Each "color-map-entry" represents one entry or anchor in the gradient:
  35. * The first argument is the color
  36. * The second argument is the value at which we anchor the color
  37. * An optional third argument could specify the opacity of the pixels, as a value between 0 (fully transparent) and 1 (fully opaque). The default, when not specified, is 1, fully opaque.
  38. **Line 4** sets the lower value of 70, which is styled a opaque dark green (``#008000``), and **line 5** sets the upper value of 256, which is styled a opaque dark brown (``#663333``). All data values in between these two quantities will be linearly interpolated: a value of 163 (the midpoint between 70 and 256) will be colored as the midpoint between the two colors (in this case approximately ``#335717``, a muddy green).
  39. Transparent gradient
  40. --------------------
  41. This example creates the same two-color gradient as in the :ref:`sld_cookbook_raster_twocolorgradient` as in the example above but makes the entire layer mostly transparent by setting a 30% opacity.
  42. .. figure:: ../../sld/cookbook/images/raster_transparentgradient.png
  43. :align: center
  44. *Transparent gradient*
  45. Code
  46. ~~~~
  47. .. code-block:: css
  48. :linenos:
  49. * {
  50. raster-channels: auto;
  51. raster-opacity: 0.3;
  52. raster-color-map: color-map-entry(#008000, 70)
  53. color-map-entry(#663333, 256);
  54. }
  55. Details
  56. ~~~~~~~
  57. This example is similar to the :ref:`sld_cookbook_raster_twocolorgradient` example save for the addition of **line 3**, which sets the opacity of the layer to 0.3 (or 30% opaque). An opacity value of 1 means that the shape is drawn 100% opaque, while an opacity value of 0 means that the shape is rendered as completely transparent. The value of 0.3 means that the raster partially takes on the color and style of whatever is drawn beneath it. Since the background is white in this example, the colors generated from the "raster-color-map" look lighter, but were the raster imposed on a dark background the resulting colors would be darker.
  58. Brightness and contrast
  59. -----------------------
  60. This example normalizes the color output and then increases the brightness by a factor of 2.
  61. .. figure:: ../../sld/cookbook/images/raster_brightnessandcontrast.png
  62. :align: center
  63. *Brightness and contrast*
  64. Code
  65. ~~~~
  66. .. code-block:: css
  67. :linenos:
  68. * {
  69. raster-channels: auto;
  70. raster-contrast-enhancement: normalize;
  71. raster-gamma: 0.5;
  72. raster-color-map: color-map-entry(#008000, 70)
  73. color-map-entry(#663333, 256);
  74. }
  75. Details
  76. ~~~~~~~
  77. This example is similar to the :ref:`sld_cookbook_raster_twocolorgradient`, save for the addition of the contrast enhancement and gamma attributes on **lines 3-4**. **Line 3** normalizes the output by increasing the contrast to its maximum extent. **Line 4** then adjusts the brightness by a factor of 0.5. Since values less than 1 make the output brighter, a value of 0.5 makes the output twice as bright.
  78. Three-color gradient
  79. --------------------
  80. This example creates a three-color gradient in primary colors. In addition, we want to avoid displaying data outside of the chosen range, leading some data not to be rendered at all.
  81. .. figure:: ../../sld/cookbook/images/raster_threecolorgradient.png
  82. :align: center
  83. *Three-color gradient*
  84. Code
  85. ~~~~
  86. .. code-block:: css
  87. :linenos:
  88. * {
  89. raster-channels: auto;
  90. raster-color-map:
  91. color-map-entry(black, 150, 0)
  92. color-map-entry(blue, 150)
  93. color-map-entry(yellow, 200)
  94. color-map-entry(red, 250)
  95. color-map-entry(black, 250, 0)
  96. }
  97. Details
  98. ~~~~~~~
  99. This example creates a three-color gradient, with two extra rules to make ranges of color disappear. The color map behavior is such that any value below the lowest entry gets the same color as that entry, and any value above the last entry gets the same color as the last entry, while everything in between is linearly interpolated (all values must be provided from lower to higher).
  100. **Line 4** associates value 150 and below with a transparent color (0 opacity, that is, fully transparent), and so does **line 8**, which makes transparent every value above 250.
  101. The lines in the middle create a gradient going from blue, to yellow, to red.
  102. Alpha channel
  103. -------------
  104. This example creates an "alpha channel" effect such that higher values are increasingly transparent.
  105. .. figure:: ../../sld/cookbook/images/raster_alphachannel.png
  106. :align: center
  107. *Alpha channel*
  108. Code
  109. ~~~~
  110. .. code-block:: css
  111. :linenos:
  112. * {
  113. raster-channels: auto;
  114. raster-color-map: color-map-entry(#008000, 70)
  115. color-map-entry(#663333, 256, 0);
  116. }
  117. Details
  118. ~~~~~~~
  119. An alpha channel is another way of referring to variable transparency. Much like how a gradient maps values to colors, each entry in a "raster-color-map" can have a value for opacity (with the default being 1.0 or completely opaque).
  120. In this example, there is a "raster-color-map" with two entries: **line 3** specifies the lower bound of 70 be colored dark green (``#008000``), while **line 4** specifies the upper bound of 256 also be colored dark green but with an opacity value of 0. This means that values of 256 will be rendered at 0% opacity (entirely transparent). Just like the gradient color, the opacity is also linearly interpolated such that a value of 163 (the midpoint between 70 and 256) is rendered at 50% opacity.
  121. Discrete colors
  122. ---------------
  123. This example shows a gradient that is not linearly interpolated but instead has values mapped precisely to one of three specific colors.
  124. .. figure:: ../../sld/cookbook/images/raster_discretecolors.png
  125. :align: center
  126. *Discrete colors*
  127. Code
  128. ~~~~
  129. .. code-block:: css
  130. :linenos:
  131. * {
  132. raster-channels: auto;
  133. raster-color-map-type: intervals;
  134. raster-color-map: color-map-entry(#008000, 150)
  135. color-map-entry(#663333, 256);
  136. }
  137. Details
  138. ~~~~~~~
  139. Sometimes color bands in discrete steps are more appropriate than a color gradient. The "raster-color-map-type: intervals" attribute sets the display to output discrete colors instead of a gradient. The values in each entry correspond to the upper bound for the color
  140. band such that colors are mapped to values less than the value of one entry but greater than or equal to the next lower entry. For example, **line 4** colors all values less than 150 to dark green (``#008000``) and **line 5** colors all values less than 256 but greater than or equal to 150 to dark brown (``#663333``).
  141. Many color gradient
  142. -------------------
  143. This example shows a gradient interpolated across eight different colors.
  144. .. figure:: ../../sld/cookbook/images/raster_manycolorgradient.png
  145. :align: center
  146. *Many color gradient*
  147. Code
  148. ~~~~
  149. .. code-block:: css
  150. :linenos:
  151. * {
  152. raster-channels: auto;
  153. raster-color-map:
  154. color-map-entry(black, 95)
  155. color-map-entry(blue, 110)
  156. color-map-entry(green, 135)
  157. color-map-entry(red, 160)
  158. color-map-entry(purple, 185)
  159. color-map-entry(yellow, 210)
  160. color-map-entry(cyan, 235)
  161. color-map-entry(white, 256)
  162. }
  163. Details
  164. ~~~~~~~
  165. This example is similar to the previous ones, and creates a color gradient between 8 colors as reported in the following table
  166. .. list-table::
  167. :widths: 15 25 30
  168. * - **Entry number**
  169. - **Value**
  170. - **Color**
  171. * - 1
  172. - 95
  173. - Black
  174. * - 2
  175. - 110
  176. - Blue
  177. * - 3
  178. - 135
  179. - Green
  180. * - 4
  181. - 160
  182. - Red
  183. * - 5
  184. - 185
  185. - Purple
  186. * - 6
  187. - 210
  188. - Yellow
  189. * - 7
  190. - 235
  191. - Cyan
  192. * - 8
  193. - 256
  194. - White