textsymbolizer.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. .. _sld_reference_textsymbolizer:
  2. TextSymbolizer
  3. ==============
  4. A **TextSymbolizer** styles features as **text labels**.
  5. Text labels are positioned either at points or along linear paths
  6. derived from the geometry being labelled.
  7. Labelling is a complex operation, and effective labelling
  8. is crucial to obtaining legible and visually pleasing cartographic output.
  9. For this reason SLD provides many options to control label placement.
  10. To improve quality even more GeoServer provides additional options and parameters.
  11. The usage of the standard and extended options are described in greater detail
  12. in the following section on :ref:`sld_reference_labeling`.
  13. Syntax
  14. ------
  15. A ``<TextSymbolizer>`` contains the following elements:
  16. .. list-table::
  17. :widths: 20 20 60
  18. * - **Tag**
  19. - **Required?**
  20. - **Description**
  21. * - ``<Geometry>``
  22. - No
  23. - The geometry to be labelled.
  24. * - ``<Label>``
  25. - No
  26. - The text content for the label.
  27. * - ``<Font>``
  28. - No
  29. - The font information for the label.
  30. * - ``<LabelPlacement>``
  31. - No
  32. - Sets the position of the label relative to its associated geometry.
  33. * - ``<Halo>``
  34. - No
  35. - Creates a colored background around the label text, for improved legibility.
  36. * - ``<Fill>``
  37. - No
  38. - The fill style of the label text.
  39. * - ``<Graphic>``
  40. - No
  41. - A graphic to be displayed behind the label text.
  42. See :ref:`sld_reference_graphic` for content syntax.
  43. * - ``<Priority>``
  44. - No
  45. - The priority of the label during conflict resolution.
  46. Content may contains :ref:`expressions <sld_reference_parameter_expressions>`.
  47. See also :ref:`labeling_priority`.
  48. * - ``<VendorOption>``
  49. - 0..N
  50. - A GeoServer-specific option.
  51. See :ref:`sld_reference_labeling` for descriptions of the available options.
  52. Any number of options may be specified.
  53. Geometry
  54. ^^^^^^^^
  55. The ``<Geometry>`` element is optional.
  56. If present, it specifies the featuretype property from which to obtain the geometry to label,
  57. using a ``<PropertyName>`` element.
  58. See also :ref:`geometry_transformations` for GeoServer extensions for specifying geometry.
  59. Any kind of geometry may be labelled with a ``<TextSymbolizer>``.
  60. For non-point geometries, a representative point is used (such as the centroid of a line or polygon).
  61. Label
  62. ^^^^^
  63. The ``<Label>`` element specifies the text that will be rendered as the label.
  64. It allows content of mixed type, which means that the content
  65. can be a mixture of string data and :ref:`sld_filter_expression`.
  66. These are concatenated to form the final label text.
  67. If a label is provided directly by a feature property,
  68. the content is a single ``<PropertyName>``.
  69. Multiple properties can be included in the label,
  70. and property values can be manipulated by filter expressions and functions.
  71. Additional "boilerplate" text can be provided as well.
  72. Whitespace can be preserved by surrounding it with XML ``<![CDATA[`` ``]]>`` delimiters.
  73. If this element is omitted, no label is rendered.
  74. Font
  75. ^^^^
  76. The ``<Font>`` element specifies the font to be used for the label.
  77. A set of ``<CssParameter>`` elements specify the details of the font.
  78. The ``name`` **attribute** indicates what aspect of the font is described,
  79. using the standard CSS/SVG font model.
  80. The **content** of the element supplies the
  81. value of the font parameter.
  82. The value may contain :ref:`expressions <sld_reference_parameter_expressions>`.
  83. .. list-table::
  84. :widths: 30 15 55
  85. * - **Parameter**
  86. - **Required?**
  87. - **Description**
  88. * - ``name="font-family"``
  89. - No
  90. - The family name of the font to use for the label.
  91. Default is ``Times``.
  92. * - ``name="font-style"``
  93. - No
  94. - The style of the font. Options are ``normal``, ``italic``, and ``oblique``. Default is ``normal``.
  95. * - ``name="font-weight"``
  96. - No
  97. - The weight of the font. Options are ``normal`` and ``bold``. Default is ``normal``.
  98. * - ``name="font-size"``
  99. - No
  100. - The size of the font in pixels. Default is ``10``.
  101. LabelPlacement
  102. ^^^^^^^^^^^^^^
  103. The ``<LabelPlacement>`` element specifies the placement of the label relative to the geometry being labelled.
  104. There are two possible sub-elements: ``<PointPlacement>`` or ``<LinePlacement>``.
  105. Exactly one of these must be specified.
  106. .. list-table::
  107. :widths: 20 20 60
  108. * - **Tag**
  109. - **Required?**
  110. - **Description**
  111. * - ``<PointPlacement>``
  112. - No
  113. - Labels a geometry at a single point
  114. * - ``<LinePlacement>``
  115. - No
  116. - Labels a geometry along a linear path
  117. PointPlacement
  118. ^^^^^^^^^^^^^^
  119. The ``<PointPlacement>`` element indicates the label is placed
  120. at a labelling point derived from the geometry being labelled.
  121. The position of the label relative to the labelling point may be controlled by the
  122. following sub-elements:
  123. .. list-table::
  124. :widths: 20 20 60
  125. * - **Tag**
  126. - **Required?**
  127. - **Description**
  128. * - ``<AnchorPoint>``
  129. - No
  130. - The location within the label bounding box that is aligned with the label point.
  131. The location is specified by ``<AnchorPointX>`` and ``<AnchorPointY>`` sub-elements,
  132. with values in the range [0..1].
  133. Values may contain :ref:`expressions <sld_reference_parameter_expressions>`.
  134. * - ``<Displacement>``
  135. - No
  136. - Specifies that the label point should be offset from the original point.
  137. The offset is specified by ``<DisplacementX>`` and ``<DisplacementY>`` sub-elements,
  138. with values in pixels.
  139. Values may contain :ref:`expressions <sld_reference_parameter_expressions>`.
  140. Default is ``(0, 0)``.
  141. * - ``<Rotation>``
  142. - No
  143. - The rotation of the label in clockwise degrees
  144. (negative values are counterclockwise).
  145. Value may contain :ref:`expressions <sld_reference_parameter_expressions>`.
  146. Default is ``0``.
  147. The anchor point justification, displacement offsetting, and rotation are applied in that order.
  148. LinePlacement
  149. ^^^^^^^^^^^^^
  150. The ``<LinePlacement>`` element indicates the label
  151. is placed along a linear path derived from the geometry being labelled.
  152. The position of the label relative to the linear path may be controlled by the
  153. following sub-element:
  154. .. list-table::
  155. :widths: 20 20 60
  156. * - **Tag**
  157. - **Required?**
  158. - **Description**
  159. * - ``<PerpendicularOffset>``
  160. - No
  161. - The offset from the linear path, in pixels.
  162. Positive values offset to the left of the line, negative to the right.
  163. Value may contain :ref:`expressions <sld_reference_parameter_expressions>`.
  164. Default is ``0``.
  165. The appearance of text along linear paths can be further controlled
  166. by the vendor options ``followLine``, ``maxDisplacement``, ``repeat``, ``labelAllGroup``, and ``maxAngleDelta``.
  167. These are described in :ref:`sld_reference_labeling`.
  168. Halo
  169. ^^^^
  170. A halo creates a colored background around the label text, which improves readability in low contrast situations.
  171. Within the ``<Halo>`` element there are two sub-elements which control the appearance of the halo:
  172. .. list-table::
  173. :widths: 20 20 60
  174. * - **Tag**
  175. - **Required?**
  176. - **Description**
  177. * - ``<Radius>``
  178. - No
  179. - The halo radius, in pixels.
  180. Value may contain :ref:`expressions <sld_reference_parameter_expressions>`.
  181. Default is ``1``.
  182. * - ``<Fill>``
  183. - No
  184. - The color and opacity of the halo
  185. via ``CssParameter`` elements for ``fill`` and ``fill-opacity``.
  186. See :ref:`sld_reference_fill` for full syntax.
  187. The parameter values may contain :ref:`expressions <sld_reference_parameter_expressions>`.
  188. Default is a **white** fill (``#FFFFFF``) at **100%** opacity.
  189. Fill
  190. ^^^^
  191. The ``<Fill>`` element specifies the fill style for the label text.
  192. The syntax is the same as that of the ``PolygonSymbolizer`` :ref:`sld_reference_fill` element.
  193. The default fill color is **black** (``#FFFFFF``) at **100%** opacity..
  194. Graphic
  195. ^^^^^^^
  196. The ``<Graphic>`` element specifies a graphic symbol to be displayed behind the label text (if any).
  197. A classic use for this is to display "highway shields" behind road numbers
  198. provided by feature attributes.
  199. The element content has the same syntax as the ``<PointSymbolizer>`` :ref:`sld_reference_graphic` element.
  200. Graphics can be provided by internal :ref:`mark symbols <pointsymbols>`, or by external images or SVG files.
  201. Their size and aspect ratio can be changed to match the text displayed with them
  202. by using the vendor options :ref:`labeling_graphic_resize` and :ref:`labeling_graphic_margin`.
  203. Example
  204. -------
  205. The following symbolizer is taken from the :ref:`sld_cookbook_points` section in the :ref:`sld_cookbook`.
  206. .. code-block:: xml
  207. :linenos:
  208. <TextSymbolizer>
  209. <Label>
  210. <ogc:PropertyName>name</ogc:PropertyName>
  211. </Label>
  212. <Font>
  213. <CssParameter name="font-family">Arial</CssParameter>
  214. <CssParameter name="font-size">12</CssParameter>
  215. <CssParameter name="font-style">normal</CssParameter>
  216. <CssParameter name="font-weight">bold</CssParameter>
  217. </Font>
  218. <LabelPlacement>
  219. <PointPlacement>
  220. <AnchorPoint>
  221. <AnchorPointX>0.5</AnchorPointX>
  222. <AnchorPointY>0.0</AnchorPointY>
  223. </AnchorPoint>
  224. <Displacement>
  225. <DisplacementX>0</DisplacementX>
  226. <DisplacementY>25</DisplacementY>
  227. </Displacement>
  228. <Rotation>-45</Rotation>
  229. </PointPlacement>
  230. </LabelPlacement>
  231. <Fill>
  232. <CssParameter name="fill">#990099</CssParameter>
  233. </Fill>
  234. </TextSymbolizer>
  235. The symbolizer labels features with the text from the ``name`` property.
  236. The font is Arial in bold at 12 pt size, filled in purple.
  237. The labels are centered on the point along their lower edge,
  238. then displaced 25 pixels upwards,
  239. and finally rotated 45 degrees counterclockwise.
  240. The displacement takes effect before the rotation during rendering,
  241. so the 25 pixel vertical displacement is itself rotated 45 degrees.
  242. .. figure:: img/text_pointwithrotatedlabel.png
  243. :align: center
  244. *Point with rotated label*
  245. Scalable Font Size
  246. ------------------
  247. The font size can also be set depending on the scale denominator as follows:
  248. .. code-block:: xml
  249. :linenos:
  250. <CssParameter name="font-size">
  251. <ogc:Function name="Categorize">
  252. <!-- Value to transform -->
  253. <ogc:Function name="env">
  254. <ogc:Literal>wms_scale_denominator</ogc:Literal>
  255. </ogc:Function>
  256. <!-- Output values and thresholds -->
  257. <!-- Ranges: -->
  258. <!-- [scale <= 300, font 12] -->
  259. <!-- [scale 300 - 2500, font 10] -->
  260. <!-- [scale > 2500, font 8] -->
  261. <ogc:Literal>12</ogc:Literal>
  262. <ogc:Literal>300</ogc:Literal>
  263. <ogc:Literal>10</ogc:Literal>
  264. <ogc:Literal>2500</ogc:Literal>
  265. <ogc:Literal>8</ogc:Literal>
  266. </ogc:Function>
  267. </CssParameter>
  268. The above example would display text at different sizes depending on the scale
  269. denominator setting. A font size of **12** for scale denominator of less than or equal
  270. to 300, a font size of **10** for scale denominator from 300-2500 and a font size of **8** for scale
  271. denominator greater than 2500.