rasters.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. .. _ysld_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 <artifacts/ysld_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. Raster file as rendered in grayscale
  11. :download:`Download the raster shapefile <artifacts/ysld_cookbook_raster.zip>`
  12. .. _ysld_cookbook_raster_twocolorgradient:
  13. Two-color gradient
  14. ------------------
  15. This example shows a two-color style with green at lower elevations and brown at higher elevations.
  16. .. figure:: ../../sld/cookbook/images/raster_twocolorgradient.png
  17. Two-color gradient
  18. Code
  19. ~~~~
  20. :download:`Download the "Two-color gradient" YSLD <artifacts/raster_twocolorgradient.ysld>`
  21. .. code-block:: yaml
  22. title: 'YSLD Cook Book: Two color gradient'
  23. feature-styles:
  24. - name: name
  25. rules:
  26. - symbolizers:
  27. - raster:
  28. opacity: 1.0
  29. color-map:
  30. type: ramp
  31. entries:
  32. - ['#008000',1,70,'']
  33. - ['#663333',1,256,'']
  34. Details
  35. ~~~~~~~
  36. There is one rule in one feature style for this example, which is the simplest possible situation. All subsequent examples will share this characteristic. Styling of rasters is done via the raster symbolizer (**lines 2-7**).
  37. This example creates a smooth gradient between two colors corresponding to two elevation values. The gradient is created via the ``color-map`` on **lines 8-12**. Each entry in the ``color-map`` represents one entry or anchor in the gradient. **Line 11** sets the lower value of 70 and color to a dark green (``'#008000'``). **Line 12** sets the upper value of 256 and color to a dark brown (``'#663333'``). **Line 9** sets the type to ``ramp``, which means that 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).
  38. Transparent gradient
  39. --------------------
  40. This example creates the same two-color gradient as in the :ref:`ysld_cookbook_raster_twocolorgradient` as in the example above but makes the entire layer mostly transparent by setting a 30% opacity.
  41. .. figure:: ../../sld/cookbook/images/raster_transparentgradient.png
  42. Transparent gradient
  43. Code
  44. ~~~~
  45. :download:`Download the "Transparent gradient" YSLD <artifacts/raster_transparentgradient.ysld>`
  46. .. code-block:: yaml
  47. title: 'YSLD Cook Book: Transparent gradient'
  48. feature-styles:
  49. - name: name
  50. rules:
  51. - symbolizers:
  52. - raster:
  53. opacity: 0.3
  54. color-map:
  55. type: ramp
  56. entries:
  57. - ['#008000',1,70,'']
  58. - ['#663333',1,256,'']
  59. Details
  60. ~~~~~~~
  61. This example is similar to the :ref:`ysld_cookbook_raster_twocolorgradient` example save for the addition of **line 7**, 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 ``color-map`` look lighter, but were the raster imposed on a dark background the resulting colors would be darker.
  62. Brightness and contrast
  63. -----------------------
  64. This example normalizes the color output and then increases the brightness by a factor of 2.
  65. .. figure:: ../../sld/cookbook/images/raster_brightnessandcontrast.png
  66. Brightness and contrast
  67. Code
  68. ~~~~
  69. :download:`Download the "Brightness and contrast" YSLD <artifacts/raster_brightnessandcontrast.ysld>`
  70. .. code-block:: yaml
  71. title: 'YSLD Cook Book: Brightness and contrast'
  72. feature-styles:
  73. - name: name
  74. rules:
  75. - symbolizers:
  76. - raster:
  77. opacity: 1
  78. color-map:
  79. type: ramp
  80. entries:
  81. - ['#008000',1,70,'']
  82. - ['#663333',1,256,'']
  83. contrast-enhancement:
  84. mode: normalize
  85. gamma: 0.5
  86. Details
  87. ~~~~~~~
  88. This example is similar to the :ref:`ysld_cookbook_raster_twocolorgradient`, save for the addition of the ``contrast-enhancement`` parameter on **lines 13-15**. **Line 14** normalizes the output by increasing the contrast to its maximum extent. **Line 15** 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.
  89. As with previous examples, **lines 8-12** determine the ``color-map``, with **line 11** setting the lower bound (70) to be colored dark green (``'#008000'``) and **line 12** setting the upper bound (256) to be colored dark brown (``'#663333'``).
  90. Three-color gradient
  91. --------------------
  92. This example creates a three-color gradient in primary colors.
  93. .. figure:: ../../sld/cookbook/images/raster_threecolorgradient.png
  94. Three-color gradient
  95. Code
  96. ~~~~
  97. :download:`Download the "Three-color gradient" YSLD <artifacts/raster_threecolorgradient.ysld>`
  98. .. code-block:: yaml
  99. title: 'YSLD Cook Book: Three color gradient'
  100. feature-styles:
  101. - name: name
  102. rules:
  103. - symbolizers:
  104. - raster:
  105. opacity: 1
  106. color-map:
  107. type: ramp
  108. entries:
  109. - ['#0000FF',1,150,'']
  110. - ['#FFFF00',1,200,'']
  111. - ['#FF0000',1,250,'']
  112. Details
  113. ~~~~~~~
  114. This example creates a three-color gradient based on a ``color-map`` with three entries on **lines 8-13**: **line 11** specifies the lower bound (150) be styled in blue (``'#0000FF'``), **line 12** specifies an intermediate point (200) be styled in yellow (``'#FFFF00'``), and **line 13** specifies the upper bound (250) be styled in red (``'#FF0000'``).
  115. Since our data values run between 70 and 256, some data points are not accounted for in this style. Those values below the lowest entry in the color map (the range from 70 to 149) are styled the same color as the lower bound, in this case blue. Values above the upper bound in the color map (the range from 251 to 256) are styled the same color as the upper bound, in this case red.
  116. Alpha channel
  117. -------------
  118. This example creates an "alpha channel" effect such that higher values are increasingly transparent.
  119. .. figure:: ../../sld/cookbook/images/raster_alphachannel.png
  120. Alpha channel
  121. Code
  122. ~~~~
  123. :download:`Download the "Alpha channel" YSLD <artifacts/raster_alphachannel.ysld>`
  124. .. code-block:: yaml
  125. title: 'YSLD Cook Book: Alpha channel'
  126. feature-styles:
  127. - name: name
  128. rules:
  129. - symbolizers:
  130. - raster:
  131. opacity: 1
  132. color-map:
  133. type: ramp
  134. entries:
  135. - ['#008000',1,70,'']
  136. - ['#008000',0,256,'']
  137. Details
  138. ~~~~~~~
  139. An alpha channel is another way of referring to variable transparency. Much like how a gradient maps values to colors, each entry in a ``color-map`` can have a value for opacity (with the default being 1.0 or completely opaque).
  140. In this example, there is a ``color-map`` with two entries: **line 11** specifies the lower bound of 70 be colored dark green (``'#008000'``), while **line 13** 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.
  141. Discrete colors
  142. ---------------
  143. This example shows a gradient that is not linearly interpolated but instead has values mapped precisely to one of three specific colors.
  144. .. figure:: ../../sld/cookbook/images/raster_discretecolors.png
  145. Discrete colors
  146. Code
  147. ~~~~
  148. :download:`Download the "Discrete colors" YSLD <artifacts/raster_discretecolors.ysld>`
  149. .. code-block:: yaml
  150. title: 'YSLD Cook Book: Discrete colors'
  151. feature-styles:
  152. - name: name
  153. rules:
  154. - symbolizers:
  155. - raster:
  156. opacity: 1
  157. color-map:
  158. type: intervals
  159. entries:
  160. - ['#008000',1,150,'']
  161. - ['#663333',1,256,'']
  162. Details
  163. ~~~~~~~
  164. Sometimes color bands in discrete steps are more appropriate than a color gradient. The ``type: intervals`` parameter added to the ``color-map`` on **line 9** sets the display to output discrete colors instead of a gradient. The values in each entry correspond to the upper bound for the color 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 11** colors all values less than 150 to dark green (``'#008000'``) and **line 12** colors all values less than 256 but greater than or equal to 150 to dark brown (``'#663333'``).
  165. Many color gradient
  166. -------------------
  167. This example shows a gradient interpolated across eight different colors.
  168. .. figure:: ../../sld/cookbook/images/raster_manycolorgradient.png
  169. Many color gradient
  170. Code
  171. ~~~~
  172. :download:`Download the "Many color gradient" YSLD <artifacts/raster_manycolorgradient.ysld>`
  173. .. code-block:: yaml
  174. title: 'YSLD Cook Book: Many color gradient'
  175. feature-styles:
  176. - name: name
  177. rules:
  178. - symbolizers:
  179. - raster:
  180. opacity: 1
  181. color-map:
  182. type: ramp
  183. entries:
  184. - ['#000000',1,95,'']
  185. - ['#0000FF',1,110,'']
  186. - ['#00FF00',1,135,'']
  187. - ['#FF0000',1,160,'']
  188. - ['#FF00FF',1,185,'']
  189. - ['#FFFF00',1,210,'']
  190. - ['#00FFFF',1,235,'']
  191. - ['#FFFFFF',1,256,'']
  192. Details
  193. ~~~~~~~
  194. A ``color-map`` can include up to 255 entries.
  195. This example has eight entries (**lines 11-18**):
  196. .. list-table::
  197. :widths: 15 25 30 30
  198. :header-rows: 1
  199. * - Entry number
  200. - Value
  201. - Color
  202. - RGB code
  203. * - 1
  204. - 95
  205. - Black
  206. - ``'#000000'``
  207. * - 2
  208. - 110
  209. - Blue
  210. - ``'#0000FF'``
  211. * - 3
  212. - 135
  213. - Green
  214. - ``'#00FF00'``
  215. * - 4
  216. - 160
  217. - Red
  218. - ``'#FF0000'``
  219. * - 5
  220. - 185
  221. - Purple
  222. - ``'#FF00FF'``
  223. * - 6
  224. - 210
  225. - Yellow
  226. - ``'#FFFF00'``
  227. * - 7
  228. - 235
  229. - Cyan
  230. - ``'#00FFFF'``
  231. * - 8
  232. - 256
  233. - White
  234. - ``'#FFFFFF'``