working.rst 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. .. _sld_working:
  2. Working with SLD
  3. ================
  4. This section describes how to create, view and troubleshoot SLD styling in GeoServer.
  5. Creating
  6. --------
  7. GeoServer comes with some basic styles defined in its catalog.
  8. Any number of new styles can be added to the catalog.
  9. Styles can also be specified **externally** to the server,
  10. either to define a complete map,
  11. or to extend the server style catalog using **library mode**.
  12. Catalog Styles
  13. ^^^^^^^^^^^^^^
  14. Styles in the catalog can be viewed, edited and validated via the :ref:`styling_webadmin_styles` menu of the :ref:`web_admin`.
  15. They may also be created and accessed via the REST :ref:`rest_api_styles` API.
  16. There are two types of Catalog Styles: Symbology Encoding styles (the default) and Style Layer Descriptor styles.
  17. Symbology Encoding Styles
  18. ~~~~~~~~~~~~~~~~~~~~~~~~~
  19. A Symbology Encoding style consists of a :file:`Symbology Encoding` document used to specify the styling of a single layer.
  20. In GeoServer, this is more commonly referred to as a style.
  21. GeoServer supports the use of a :ref:`sld_reference_sld` document containing a single ``<NamedLayer>`` element, which
  22. contains a single ``<UserStyle>`` element to specify the styling.
  23. * When used in this fashion the layer name is ignored, since the style may be applied to many different layers.
  24. * When using an :ref:`sld_reference_sld` generated by another application keep in mind only the first ``<NamedLayer>``
  25. is used, any subsequent content is ignored.
  26. Every layer (featuretype) registered with GeoServer must have at least one symbology encoding style associated with it,
  27. which is the default style for rendering the layer.
  28. Any number of additional styles can be associated with a layer.
  29. This allows layers to have appropriate styles advertised in the WMS ``GetCapabilities`` document.
  30. A layer's styles can be changed using the :ref:`data_webadmin_layers` page of the :ref:`web_admin`.
  31. .. note:: When adding a layer and a style for it to GeoServer at the same time, the style should be added first,
  32. so that the new layer can be associated with the style immediately.
  33. Style Layer Descriptor Styles
  34. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  35. A Style Layer Descriptor is a :ref:`sld_reference_sld` document containing any number of ``<NamedLayer>`` and ``<UserLayer>`` elements,
  36. each of which may contain any number of ``<UserStyle>`` or ``<NamedStyle>`` elements.
  37. Within a Style Layer Descriptor document, the name of any ``<NamedLayer>`` elements should match a layer (or layer group) in the catalog.
  38. Likewise, any ``<NamedStyle>`` elements should refer to a style in the catalog.
  39. Style Layer Descriptor styles can define new layers of styled data, by using the :ref:`sld_reference_inlinefeature` element to
  40. provide feature data.
  41. Within GeoServer, when Style Layer Descriptor styles are used they are typically in the form of style groups. Style groups can
  42. be added to layer groups as an alternative way of defining a collection of styled layers, using either the
  43. :ref:`Web Administration interface <data_webadmin_layergroups>` or the :api:`REST API <layergroups.yaml>`.
  44. Style Layer Descriptor styles can still be assigned to layers and used like a layer style, in which case only the first
  45. ``<NamedLayer>`` will be used. Style Layer Descriptor styles can also be used as an External Style, via the geoserver styles
  46. endpoint (``/geoserver/styles``) or the geoserver REST api.
  47. External Styles
  48. ^^^^^^^^^^^^^^^
  49. Styling can be defined externally to the server in a number of ways:
  50. * An internet-accessible SLD document can be provided via
  51. the ``SLD=url`` parameter in a WMS :ref:`wms_getmap` GET request
  52. * An SLD document can be provided directly in a
  53. WMS :ref:`wms_getmap` GET request using the ``SLD_BODY=style`` parameter.
  54. The SLD XML must be URL-encoded.
  55. * A :ref:`sld_reference_sld` element can be included in a WMS ``GetMap`` POST request XML document.
  56. In all of these cases, if the WMS ``layers`` parameter is not supplied
  57. then the map content is defined completely
  58. by the layers and styles present in the external SLD.
  59. If the ``layers`` parameter is present, then styling operates in :ref:`sld_library_mode`.
  60. The structure of an external style is the same as a Style Layer Descriptor style, as described above.
  61. External styles can define new layers of styled data,
  62. by using the SLD :ref:`sld_reference_inlinefeature` element to provide feature data.
  63. This can be used to implement dynamic feature highlighting, for example.
  64. External styling may be generated dynamically by client applications,
  65. This provides a powerful way for clients to control styling effects.
  66. .. _sld_library_mode:
  67. Library Mode
  68. ^^^^^^^^^^^^
  69. In **library mode** externally-defined styles are treated as a *style library*,
  70. which acts as an extension to the server style catalog.
  71. Library mode occurs when map layers and styles are specified using the ``layers`` and ``styles`` WMS parameters,
  72. and additional styling is supplied externally
  73. using one of the methods described in the previous section.
  74. The styles in the external style document
  75. take precedence over the catalog styles during rendering.
  76. Style lookup in library mode operates as follows:
  77. * For each layer in the ``layers`` list, the applied style is either
  78. a named style specified in the ``styles`` list (if present), or the layer default style
  79. * For a **named** style, if the external style document has a ``<NamedLayer>...<UserStyle>``
  80. with matching layer name and style name, then it is used.
  81. Otherwise, the style name is searched for in the catalog.
  82. If it is not found there, an error occurs.
  83. * For a **default** style, the external style document is
  84. searched to find a ``<NamedLayer>`` element with the layer name.
  85. If it contains a ``<UserStyle>`` with the ``<IsDefault>`` element having the value ``1``
  86. then that style is used.
  87. Otherwise, the default server style for the layer (which must exist) is used.
  88. Generally it is simpler and more performant to use styles from the server catalog.
  89. However, library mode can be useful if it is required to style a map containing many layers and
  90. where only a few of them need to have their styling defined externally.
  91. Viewing
  92. -------
  93. Once a style has been associated with a layer, the resulting rendering of the layer data
  94. can be viewed by using the :ref:`layerpreview`.
  95. The most convenient output format to use is the built-in OpenLayers viewer.
  96. Styles can be modified while the view is open, and their effect is visible as
  97. soon as the map view is panned or zoomed.
  98. Alternate styles can be viewed by specifying them in the ``styles`` WMS request parameter.
  99. To view the effect of compositing multiple styled layers, several approaches are available:
  100. * Create a **layer group** for the desired layers using the :ref:`data_webadmin_layergroups` page, and preview it.
  101. Non-default styles can be specified for layers if required.
  102. * Submit a WMS :ref:`wms_getmap` GET request specifying multiple layers in the ``layers`` parameter,
  103. and the corresponding styles in the ``styles`` parameter (if non-default styles are required).
  104. * Submit a WMS ``GetMap`` POST request containing a :ref:`sld_reference_sld` element
  105. specifying server layers, optional layers of inline data,
  106. and either named catalog styles or user-defined styling for each layer.
  107. Troubleshooting
  108. ---------------
  109. SLD is a type of programming language, not unlike creating a web page or building a script.
  110. As such, problems may arise that may require troubleshooting.
  111. Syntax Errors
  112. ^^^^^^^^^^^^^
  113. To minimize syntax errors when creating the SLD,
  114. it is recommended to use a text editor that is designed to work with XML
  115. (such as the :guilabel:`Style Editor` provided in the GeoServer UI).
  116. XML editors can make finding syntax errors easier by providing syntax highlighting and (sometimes) built-in error checking.
  117. The GeoServer :guilabel:`Style Editor` allows validating a document against the SLD XML schema.
  118. This is not mandatory, but is recommended to do before saving styles.
  119. Semantic Errors
  120. ^^^^^^^^^^^^^^^
  121. Semantic errors cannot be caught by SLD validation,
  122. but show up when a style is applied during map rendering.
  123. Most of the time this will result in a map displaying no features (a blank map),
  124. but some errors will prevent the map from rendering at all.
  125. The easiest way to fix semantic errors in an SLD is to try to isolate the error.
  126. If the SLD is long with many rules and filters, try temporarily removing some of them to see if the errors go away.
  127. In some cases the server will produce a WMS Exception document which may help to identify the error.
  128. It is also worth checking the server log to see if any error messages have been recorded.