index.rst 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. .. _qgis:
  2. Generating SLD styles with QGIS
  3. ===============================
  4. QGIS includes a sophisticated style editor with many map rendering possibilities. Styles generated
  5. with QGIS can then be exported (with limitations) to SLD for usage with GeoServer.
  6. QGIS style exporting abilities have been evolving over time, as a reference:
  7. * For vector data QGIS exports SLD 1.1 styles that can be read by GeoServer. In order to get
  8. the suitable results it's important to use QGIS 3.0 or newer, and GeoServer 2.13.x or newer.
  9. * Raster data styling export is new in QGIS 3.4.5 (yet to be released at the time of writing).
  10. This new version exports SLD 1.0 styles with vendor extensions to support contrast stretching that most recent GeoServer versions support properly. For older QGIS versions limited export functionality is available using the SLD4Raster plugin.
  11. For the export it is advised to use the :guilabel:`Save As` functionality available in the style dialog,
  12. as indicated below in this guide. Other plugins exist that streamline the export process, but they
  13. may ruin the style trying to adapt it to older GeoServer versions (e.g., translating it
  14. down to SLD 1.0 by simple text processing means), or rewrite it entirely.
  15. .. warning:: Despite the progress in the last years, it is known that not all QGIS rendering
  16. options are supported by SLD and/or by GeoServer (e.g. shapeburst symbology),
  17. and that support for exporting some parts is simply missing (e.g.. expression based symbology is
  18. supported in SLD, but QGIS won't export it). If you are interested, both projects would welcome
  19. sponsoring to improve the situation.
  20. Exporting vector symbology
  21. --------------------------
  22. This is a step by step guide to style a GeoServer demo layer, ``sfdem``.
  23. #. Open :command:`QGIS` (minimum version 3.0)
  24. #. Load the :file:`states.shp` dataset from the GeoServer data directory, :file:`<GEOSERVER_DATA_DIR>/data/shapefiles/states.shp`
  25. #. Double click the layer to open the :guilabel:`Properties` dialog and switch to the :guilabel:`Symbology` page.
  26. #. Choose a `Graduated` rendering, on the ``PERSONS`` column, and click on :guilabel:`Classify` button to generate `1.5` standard deviations, select the `spectral` color ramp, switch mode to `Quantile` and finally and click on the :guilabel:`Classify` button to generate a 5 classes map, as shown in figure.
  27. .. figure:: images/qgis-vector-style.png
  28. :align: center
  29. QGIS vector styling
  30. #. Switch to the :guilabel:`Labels` page, choose `Single labels``, label with the ``STATE NAME`` attribute and choose your preferred text rendering options, as shown in figure
  31. .. figure:: images/qgis-label-style.png
  32. :align: center
  33. QGIS labelling
  34. #. The layer renders as follows:
  35. .. figure:: images/qgis-vector-render.png
  36. :align: center
  37. QGIS raster styling
  38. #. Go back At the :guilabel:`Properties` dialog, from the bottom of the :guilabel:`Styles` page, choose :menuselection:`Style --> Save Style`.
  39. .. figure:: images/qgis-vector-saveas.png
  40. :align: center
  41. *Export using Save As...*
  42. #. Choose export in the SLD format, placing the file in the desired location.
  43. .. figure:: images/qgis-choose-format.png
  44. :align: center
  45. Choosing export format...
  46. #. Go in GeoServer, create a new style, use the :guilabel:`Upload a new style` dialog to choose the exported file, and click on `upload` link.
  47. .. figure:: images/gs-vector-upload.png
  48. :align: center
  49. Uploading style in GeoServer...
  50. #. Click on guilabel:`Apply`.
  51. #. Change to the :guilabel:`Layer preview` tab, click on the :guilabel:`Preview on Layer` link to choose ``topp:states`` to verify proper rendering.
  52. .. figure:: images/gs-vector-preview.png
  53. :align: center
  54. Previewing style in GeoServer...
  55. #. Eventually switch to the :guilabel:`Publishing` tab, search for ``states``, and select :guilabel:`Default` or :guilabel:`Associated` checkbox to publish the layer to use the new style permanently.
  56. .. figure:: images/gs-vector-associate.png
  57. :align: center
  58. Associating style in GeoServer...
  59. Exporting raster symbology
  60. --------------------------
  61. The following are a couple of examples on how to export raster layers' symbology in QGIS and how to use the resulting SLD to style layers in GeoServer.
  62. .. warning:: As mentioned above, this functionality has some limitations:
  63. * :guilabel:`Hillshading` vendor options are not fully supported by GeoServer so you can't choose the `Band` and the position of the sun (`Altitude` and `Azimuth`), the `Multidirectional` option is not supported too
  64. * GeoServer is not able to interpret the :guilabel:`Color Rendering` options yet
  65. This is a step by step guide to style a GeoServer demo layer, ``sfdem``.
  66. #. Open QGIS (minimum version 3.4.5)
  67. #. Load the :file:`sfdem.tif` raster from the GeoServer data directory, :file:`<GEOSERVER_DATA_DIR>/data/sf/sfdem.tif`
  68. #. Double click the layer to open the :guilabel:`Properties` dialog and switch to the :guilabel:`Symbology` page.
  69. #. Choose a `Singleband pseudocolor` rendering, Generate :guilabel:`Min / Max Value Settings` using :guilabel:`Mean +/- standard deviation` with using ``1.5`` standard deviations. Generate a 5 classes :guilabel:`Linear` interpolated map, as shown in figure.
  70. .. figure:: images/qgis-raster-style.png
  71. :align: center
  72. QGIS raster styling
  73. #. The layer renders as follows:
  74. .. figure:: images/qgis-raster-render.png
  75. :align: center
  76. QGIS raster styling
  77. #. Return to the layer's :guilabel:`Properties` dialog :guilabel:`Symbology` page, at the bottom of the page choose :menuselection:`Style --> Save Style`.
  78. .. figure:: images/qgis-raster-saveas.png
  79. :align: center
  80. Export using Save As...
  81. #. Choose export in the SLD format, placing the file in the desired location
  82. .. figure:: images/qgis-choose-format.png
  83. :align: center
  84. Choosing export format...
  85. #. Go in GeoServer, create a new style, use the :guilabel:`Upload a new style` dialog to choose the exported file, and click on `upload` link.
  86. .. figure:: images/gs-raster-upload.png
  87. :align: center
  88. Uploading style in GeoServer...
  89. #. Click on guilabel:`Apply` then change to the :guilabel:`Layer preview` tab. Click on the :guilabel:`Preview on Layer` link to choose ``sfdem`` to verify proper rendering.
  90. .. figure:: images/gs-raster-preview.png
  91. :align: center
  92. Previewing style in GeoServer...
  93. #. Finally switch to the :guilabel:`Publishing` tab, search for ``sfdem`` layer, and select :guilabel:`Default` or :guilabel:`Associated` checkbox to publish ``sfdem`` with the new style.
  94. .. figure:: images/gs-raster-associate.png
  95. :align: center
  96. Associating style in GeoServer...
  97. The next example shows how to style an aerial image instead.
  98. #. Download an aerial image (for example from `USGS Landsat image archives <https://landsatlook.usgs.gov/sentinel2/viewer.html>`_) if you do not already have one. Give it a name (``aerial`` in this example) and :guilabel:`save it as GeoTIFF`
  99. .. figure:: images/landsat_usgs_sentinel2.png
  100. :align: center
  101. aerial.tiff
  102. #. Open GeoServer, :guilabel:`create a new Store` (see :ref:`Add a Store <data_webadmin_stores_add_a_store>`), :guilabel:`add a GeoTIFF Raster Data Source` to the Store and :guilabel:`connect` it to your ``aerial.tif`` file
  103. #. In GeoServer, :guilabel:`create a new Layer` (see :ref:`Add a Layer <data_webadmin_layers_add_a_layer>`) choosing the Store you have created in the previous step
  104. #. Open QGIS (minimum version 3.4.5)
  105. #. Load the ``aerial.tif`` raster
  106. #. Double click the layer to open the :guilabel:`Properties` dialog and switch to the :guilabel:`Symbology` page
  107. #. Choose a `Multiband color` rendering, set the :guilabel:`bands` (Red band == Band 1 (red), Green band == Band 2 (Green), Blue band == Band 3 (Blue)), generate :guilabel:`Min / Max Value Settings` using ``5,0 - 95,0 % range`` of :guilabel:`Cumulative count cut` and select ``Stretch to MinMax`` as :guilabel:`Contrast enhancement` option, as shown in the picture below
  108. .. figure:: images/qgis-sentinel2-raster-style.png
  109. :align: center
  110. QGIS layer properties - Symbology
  111. #. The layer renders as follows:
  112. .. figure:: images/qgis-sentinel2-raster-rendering.png
  113. :align: center
  114. QGIS layer rendering
  115. #. :guilabel:`Save the Style` as SLD
  116. #. Go in GeoServer, use the generated SLD to :guilabel:`create a new style`, choose the ``aerial`` layer through the :guilabel:`Preview on Layer` link and verify if the layer is properly rendered (see the previous example for further details)
  117. .. figure:: images/gs-sentinel2-raster-rendering.png
  118. :align: center
  119. GeoServer layer rendering
  120. #. Finally :guilabel:`Publish` the ``aerial`` layer with the new style as described in the previous example.