substitution.rst 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. .. _sld_variable_substitution:
  2. Variable substitution in SLD
  3. =============================
  4. Variable substitution in SLD is a GeoServer extension (starting in version 2.0.2) that allows passing values from WMS requests into SLD styles.
  5. This allows dynamically setting values such as colors, fonts, sizes and filter thresholds.
  6. Variables are specified in WMS ``GetMap`` requests by using the ``env`` request parameter followed by a list of ``name:value`` pairs separated by semicolons::
  7. ...&env=name1:value1;name2=value2&...
  8. In an SLD the variable values are accessed using the ``env`` function.
  9. The function retrieves a substitution variable value specified in the current request:
  10. .. code-block:: xml
  11. <ogc:Function name="env">
  12. <ogc:Literal>size</ogc:Literal>
  13. </ogc:Function>
  14. A default value can be provided. It will be used if the variable was not specified in the request:
  15. .. code-block:: xml
  16. <ogc:Function name="env">
  17. <ogc:Literal>size</ogc:Literal>
  18. <ogc:Literal>6</ogc:Literal>
  19. </ogc:Function>
  20. The ``env`` function can be used in an SLD anywhere an OGC expression is allowed.
  21. For example, it can be used in ``CSSParameter`` elements, in size and offset elements, and in rule filter expressions.
  22. It is also accepted in some places where full expressions are not allowed, such as in the ``Mark/WellKnownName`` element.
  23. Predefined Variables
  24. --------------------
  25. GeoServer has predefined variables which provide information about specific properties of the request output.
  26. These are useful when SLD parameters need to depend on output dimensions.
  27. The predefined variables are:
  28. .. list-table::
  29. :widths: 20 25 55
  30. * - **Name**
  31. - **Type**
  32. - **Description**
  33. * - ``wms_bbox``
  34. - ``ReferencedEnvelope``
  35. - the georeferenced extent of the request output
  36. * - ``wms_crs``
  37. - ``CoordinateReferenceSystem``
  38. - the definition of the output coordinate reference system
  39. * - ``wms_srs``
  40. - ``String``
  41. - the code for the output coordinate reference system
  42. * - ``wms_width``
  43. - ``Integer``
  44. - the width (in pixels) of the output image
  45. * - ``wms_height``
  46. - ``Integer``
  47. - the height (in pixels) of the output image
  48. * - ``wms_scale_denominator``
  49. - ``Integer``
  50. - the denominator of the output map scale
  51. * - ``kmlOutputMode``
  52. - Either ``vector`` or empty
  53. - this variable gets set to ``vector`` when the kml generator is writing out vector features as placemarks, as opposed to ground overlays
  54. Example
  55. -------
  56. The following SLD symbolizer has been parameterized in three places, with default values provided in each case:
  57. .. code-block:: xml
  58. <PointSymbolizer>
  59. <Graphic>
  60. <Mark>
  61. <WellKnownName><ogc:Function name="env">
  62. <ogc:Literal>name</ogc:Literal>
  63. <ogc:Literal>square</ogc:Literal>
  64. </ogc:Function>
  65. </WellKnownName>
  66. <Fill>
  67. <CssParameter name="fill">
  68. #<ogc:Function name="env">
  69. <ogc:Literal>color</ogc:Literal>
  70. <ogc:Literal>FF0000</ogc:Literal>
  71. </ogc:Function>
  72. </CssParameter>
  73. </Fill>
  74. </Mark>
  75. <Size>
  76. <ogc:Function name="env">
  77. <ogc:Literal>size</ogc:Literal>
  78. <ogc:Literal>6</ogc:Literal>
  79. </ogc:Function>
  80. </Size>
  81. </Graphic>
  82. </PointSymbolizer>
  83. :download:`Download the full SLD style <artifacts/parpoint.sld>`
  84. When no variables are provided in the WMS request, the SLD uses the default values and renders the sample ``sf:bugsites`` dataset as shown:
  85. .. figure:: images/default.png
  86. *Default rendering*
  87. If the request is changed to specify the following variable values::
  88. &env=color:00FF00;name:triangle;size:12
  89. the result is instead:
  90. .. figure:: images/triangles.png
  91. *Rendering with variables supplied*