text.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. .. _ysld_reference_symbolizers_text:
  2. Text symbolizer
  3. ===============
  4. The text symbolizer styles labels of vector features. Labels can consist of text strings and/or graphics.
  5. Syntax
  6. ------
  7. The full syntax of a text symbolizer is::
  8. symbolizers:
  9. - text:
  10. fill-color: <color>
  11. fill-opacity: <expression>
  12. fill-graphic:
  13. <graphic_options>
  14. stroke-graphic:
  15. <graphic_options>
  16. stroke-graphic-fill:
  17. <graphic_options>
  18. label: <expression>
  19. font-family: <expression>
  20. font-size: <expression>
  21. font-style: <expression>
  22. font-weight: <expression>
  23. placement: <point|line>
  24. offset: <expression>
  25. anchor: <tuple>
  26. displacement: <tuple>
  27. rotation: <expression>
  28. priority: <expression>
  29. halo:
  30. radius: <expression>
  31. fill-color: <color>
  32. fill-opacity: <expression>
  33. fill-graphic:
  34. <graphic_options>
  35. graphic:
  36. symbols:
  37. <graphic_options>
  38. size: <expression>
  39. opacity: <expression>
  40. rotation: <expression>
  41. geometry: <expression>
  42. uom: <text>
  43. x-composite-base: <boolean>
  44. x-composite: <text>
  45. x-allowOverruns: <boolean>
  46. x-autoWrap: <expression>
  47. x-conflictResolution: <boolean>
  48. x-followLine: <boolean>
  49. x-forceLeftToRight: <boolean>
  50. x-goodnessOfFit: <expression>
  51. x-graphic-margin: <expression>
  52. x-graphic-resize: <none|proportional|stretch>
  53. x-group: <boolean>
  54. x-labelAllGroup: <boolean>
  55. x-repeat: <expression>
  56. x-maxAngleDelta: <expression>
  57. x-maxDisplacement: <expression>
  58. x-minGroupDistance: <expression>
  59. x-partials: <boolean>
  60. x-polygonAlign: <boolean>
  61. x-spaceAround: <expression>
  62. x-underlineText: <boolean>
  63. x-strikethroughText: <boolean>
  64. x-charSpacing: <expression>
  65. x-wordSpacing: <expression>
  66. x-inclusion: <text>
  67. where:
  68. .. list-table::
  69. :class: non-responsive
  70. :header-rows: 1
  71. :stub-columns: 1
  72. :widths: 20 10 50 20
  73. * - Property
  74. - Required?
  75. - Description
  76. - Default value
  77. * - ``fill-color``
  78. - No
  79. - Color of inside of the label.
  80. - ``'#808080'`` (gray)
  81. * - ``fill-opacity``
  82. - No
  83. - Opacity of fill of the label text. Valid values are a decimal value between ``0`` (completely transparent) and ``1`` (completely opaque).
  84. - ``1``
  85. * - ``fill-graphic``
  86. - No
  87. - A design to be used for the fill of the text label. Can either be a mark consisting of a common shape or a URL that points to a graphic. The ``<graphic_options>`` should consist of a mapping containing ``symbols:`` followed by an ``external:`` or ``mark:``, with appropriate parameters as detailed in the :ref:`ysld_reference_symbolizers_point` section.
  88. - None
  89. * - ``stroke-graphic``
  90. - No
  91. - A design or pattern to be used along the stroke around the label text. the output will always be a linear repeating pattern, and as such is not tied to the value of ``stroke-width``. Can either be a mark consisting of a common shape or a URL that points to a graphic. The ``<graphic_options>`` should consist of a mapping containing ``symbols:`` followed by an ``external:`` or ``mark:``, with appropriate parameters. Cannot be used with ``stroke-graphic-fill``.
  92. - N/A
  93. * - ``stroke-graphic-fill``
  94. - No
  95. - A design or pattern to be used for the fill of the stroke around the label text. This area that is to be filled is tied directly to the value of ``stroke-width``. Can either be a mark consisting of a common shape or a URL that points to a graphic. The ``<graphic_options>`` should consist of a mapping containing ``symbols:`` followed by an ``external:`` or ``mark:``, with appropriate parameters as detailed in the :ref:`ysld_reference_symbolizers_point` section. Cannot be used with ``stroke-graphic``.
  96. - N/A
  97. .. list-table::
  98. :class: non-responsive
  99. :header-rows: 1
  100. :stub-columns: 1
  101. :widths: 20 10 50 20
  102. * - Property
  103. - Required?
  104. - Description
  105. - Default value
  106. * - ``label``
  107. - Yes
  108. - Text to display. Often taken from an attribute but any valid expression that constructs a string will do.
  109. - N/A
  110. * - ``font-family``
  111. - No
  112. - Type of font to be used for the label. Options are system dependent; the full list of fonts available can be found via the GeoServer Server Status page.
  113. - ``serif``
  114. * - ``font-size``
  115. - No
  116. - Size of the font.
  117. - ``10``
  118. * - ``font-style``
  119. - No
  120. - Style of the font. Options are ``normal``, ``italic``, and ``oblique``.
  121. - ``normal``
  122. * - ``font-weight``
  123. - No
  124. - Weight of the font. Options are ``normal`` and ``bold``.
  125. - ``normal``
  126. * - ``placement``
  127. - No
  128. - Determines whether the label is to be drawn derived from a ``point`` or a ``line``.
  129. - ``point``
  130. * - ``offset``
  131. - No
  132. - Value (in pixels) for moving the drawn label relative to the location of the feature. A positive value will shift the label in the direction of its top, while a negative value will shift the label in the direction of its bottom. Only valid for when ``type`` is set to ``line``.
  133. - ``0``
  134. * - ``anchor``
  135. - No
  136. - Specify the center of the symbol relative to the feature location (centroid for lines and polygons). Value is an ``[x,y]`` tuple with decimal values from 0-1, with ``[0,0]`` meaning that the symbol is anchored to the bottom left of the label, and ``[1,1]`` meaning anchored to the top right of the label.
  137. - ``[0,0]``
  138. * - ``displacement``
  139. - No
  140. - Specifies a distance (in pixels) to which to move the label relative to the feature. Value is an ``[x,y]`` tuple with values expressed in pixels, so [10,5] will displace the label 10 pixels to the right and 5 pixels up. Only valid for when ``type`` is set to ``point``.
  141. - ``[0,0]``
  142. * - ``rotation``
  143. - No
  144. - Value (in degrees) or rotation of the label. Larger values increase counter-clockwise rotation. A value of ``180`` will make the label upside-down. Only valid for when ``type`` is set to ``point``.
  145. - ``0``
  146. * - ``priority``
  147. - No
  148. - The priority used when choosing which labels to display during conflict resolution. Higher priority values take precedence over lower priority values.
  149. - 1000
  150. * - ``halo``
  151. - No
  152. - Creates a shaded area around the label for easier legibility
  153. - No halo
  154. * - ``radius``
  155. - No
  156. - Size (in pixels) of the halo
  157. - ``1``
  158. * - ``fill-color``
  159. - No
  160. - Color of the halo
  161. - ``'#808080'``
  162. * - ``fill-opacity``
  163. - No
  164. - Specifies the level of transparency for the halo. Value of ``0`` means entirely transparent, while ``1`` means entirely opaque.
  165. - ``1``
  166. The following properties allow for a graphic to be displayed in addition to just a label. This is used when drawing "shields" (text over top of a graphic) such as in road signs.
  167. .. list-table::
  168. :class: non-responsive
  169. :header-rows: 1
  170. :stub-columns: 1
  171. :widths: 20 10 50 20
  172. * - Property
  173. - Required?
  174. - Description
  175. - Default value
  176. * - ``graphic``
  177. - No
  178. - Specifies whether a graphic is to be drawn for the label.
  179. - N/A (no graphic)
  180. * - ``symbols``
  181. - No
  182. - The details of the graphic. Consists of an ``external:`` or ``mark:`` section, with appropriate parameters as detailed in the :ref:`ysld_reference_symbolizers_point` section.
  183. - N/A
  184. * - ``size``
  185. - No
  186. - Size of the graphic in pixels. If the aspect ratio is not 1:1 (square), will apply to the *height* of the graphic only, with the width scaled proportionally.
  187. - ``16``
  188. * - ``opacity``
  189. - No
  190. - Specifies the level of transparency for the graphic. Value of ``0`` means entirely transparent, while ``1`` means entirely opaque.
  191. - ``1``
  192. * - ``rotation``
  193. - No
  194. - Value (in degrees) or rotation of the graphic. Larger values increase counter-clockwise rotation. A value of ``180`` will make the graphic upside-down.
  195. - ``0``
  196. .. include:: include/symbol.rst
  197. The following properties are equivalent to SLD "vendor options".
  198. .. list-table::
  199. :class: non-responsive
  200. :header-rows: 1
  201. :stub-columns: 1
  202. :widths: 20 10 50 20
  203. * - Property
  204. - Required?
  205. - Description
  206. - Default value
  207. * - ``x-allowOverruns``
  208. - No
  209. - Allows labels on lines to move slightly beyond the beginning or end of the line.
  210. - ``true``
  211. * - ``x-autoWrap``
  212. - No
  213. - The number of pixels beyond which a label will be wrapped over multiple lines. Cannot use with ``x-followLine``.
  214. - 0
  215. * - ``x-conflictResolution``
  216. - No
  217. - Enables conflict resolution, meaning no two labels will be allowed to overlap. Without conflict resolution, symbolizers can overlap with other labels.
  218. - ``true``
  219. * - ``x-followLine``
  220. - No
  221. - On linear geometries, the label will follow the shape of the current line, as opposed to being drawn at a tangent. Will override
  222. - ``false``
  223. * - ``x-forceLeftToRight``
  224. - No
  225. - Forces labels to a readable orientation, otherwise will follow the line orientation, possibly making the label look upside-down. This setting is useful when using symbol fonts to add direction markers along a line.
  226. - ``false``
  227. * - ``x-goodnessOfFit``
  228. - No
  229. - Percentage (expressed as a decimal between 0-1) of the label that must fit inside the geometry to permit the label to be drawn. Works only on polygon features.
  230. - 0.5
  231. * - ``x-graphic-margin``
  232. - No
  233. - Number of pixels between the stretched graphic and the text. Only applies when ``x-graphic-resize`` is set to ``stretch`` or ``proportional``.
  234. - 0
  235. * - ``x-graphic-resize``
  236. - No
  237. - Allows for stretching the graphic underneath a label to fit the label size. Options are ``none``, ``stretch`` or ``proportional``. Used in conjunction with ``x-graphic-margin``..
  238. - ``none``
  239. * - ``x-group``
  240. - No
  241. - Geometries with identical labels will be considered a single entity to be labeled. Used to control repeated labels.
  242. - ``false``
  243. * - ``x-labelAllGroup``
  244. - No
  245. - Used in conjunction with ``x-group``. When ``true`` all items in a group are labeled. When ``false``, only the largest geometry in the group is labeled. Valid for lines only.
  246. - ``false``
  247. * - ``x-repeat``
  248. - No
  249. - Desired distance (in pixels) between labels drawn on a group. If zero, only one label will be drawn. Used in conjunction with ``x-group``. Valid for lines only.
  250. - 0
  251. * - ``x-maxAngleDelta``
  252. - No
  253. - Maximum allowed angle (in degrees) between two characters in a curved label. Used in conjunction with ``x-followLine``. Values higher than ``30`` may cause loss of legibility of the label.
  254. - 22.5
  255. * - ``x-maxDisplacement``
  256. - No
  257. - Distance (in pixels) a label can be displaced from its natural position in an attempt to eliminate conflict with other labels.
  258. - 0
  259. * - ``x-minGroupDistance``
  260. - No
  261. - Minimum distance (in pixels) between two labels in the same label group. Used in conjunction with ``displacement`` or ``repeat`` to avoid having two labels too close to each other
  262. - No minimum distance
  263. * - ``x-partials``
  264. - No
  265. - Will display partial labels (truncated on the border of the display area).
  266. - ``false``
  267. * - ``x-polygonAlign``
  268. - No
  269. - Overrides manual rotation to align label rotation automatically. Valid for polygons only.
  270. - ``false``
  271. * - ``x-spaceAround``
  272. - No
  273. - Minimum distance (in pixels) between two labels. A negative value specifies the maximum overlap between two labels.
  274. - 0
  275. * - ``x-underlineText``
  276. - No
  277. - Instruct the renderer to underline labels.
  278. - ``false``
  279. * - ``x-strikethroughText``
  280. - No
  281. - Instruct the renderer to strikethrough labels.
  282. - ``false``
  283. * - ``x-charSpacing``
  284. - No
  285. - The option controls the amount of space between characters, a positive value increases it, a negative value shrinks it (and will eventually make characters overlap). The value is specified in pixels.
  286. - 0
  287. * - ``x-wordSpacing``
  288. - No
  289. - The option controls the amount of space between words, for this option only positive values (or zero) are accepted. The value is specified in pixels.
  290. - 0
  291. .. include:: include/composite.rst
  292. Examples
  293. --------
  294. Basic label
  295. ~~~~~~~~~~~
  296. Text symbolizers are used to draw labels on objects. The label text is usually linked to some attribute of the layer. Font options are available in the ``font-family``, ``font-size``, ``font-style``, and ``font-weight`` properties. The following example draws a label using the ``name`` attribute of the layer, and with a SansSerif font of size 12, gray color, bold and italic::
  297. feature-styles:
  298. - name: name
  299. rules:
  300. - title: fill-graphic
  301. symbolizers:
  302. - text:
  303. label: ${name}
  304. fill-color: '#555555'
  305. font-family: SansSerif
  306. font-size: 12
  307. font-style: italic
  308. font-weight: bold
  309. .. figure:: img/text_basic.png
  310. Basic label
  311. .. This didn't work at one point, but may work now.
  312. .. **Text placement**
  313. .. It can be useful to adjust how labels are placed relative to the geometry. The ``anchor`` and ``displacement`` parameters can be used to alter label placement. The following example modifies the previous example to display the labels anchored at their horizontal center and moved downwards by 10 pixels::
  314. .. feature-styles:
  315. .. - name: name
  316. .. rules:
  317. .. - title: fill-graphic
  318. .. symbolizers:
  319. .. - text:
  320. .. label: ${name}
  321. .. font-family: SansSerif
  322. .. font-size: 12
  323. .. font-style: italic
  324. .. font-weight: bold
  325. .. anchor: [0.5,0]
  326. .. displacement: [0,-20]
  327. Label with wrap
  328. ~~~~~~~~~~~~~~~
  329. Wrapping long labels can improve how well they fit on maps. This can be accomplished using the ``x-autoWrap`` property. This example wraps lines longer than 70 pixels::
  330. feature-styles:
  331. - name: name
  332. rules:
  333. - symbolizers:
  334. - polygon:
  335. stroke-width: 1
  336. fill-color: '#00DD77'
  337. - text:
  338. label: ${name}
  339. font-size: 12
  340. x-autoWrap: 70
  341. x-maxDisplacement: 100
  342. anchor: [0.5,-1]
  343. .. figure:: img/text_wrap.png
  344. Label with wrap
  345. Label with halo
  346. ~~~~~~~~~~~~~~~
  347. Surrounding labels with a halo will allow them to be visible even on complex maps with various background features. This can be accomplished using the ``halo`` family of properties. This example surrounds the label in a partially transparent white halo of radius 2::
  348. feature-styles:
  349. - name: name
  350. rules:
  351. - symbolizers:
  352. - polygon:
  353. stroke-width: 1
  354. fill-color: '#00DD77'
  355. - text:
  356. label: ${name}
  357. font-size: 12
  358. x-autoWrap: 70
  359. x-maxDisplacement: 100
  360. halo:
  361. radius: 2
  362. fill-color: '#FFFFFF'
  363. fill-opacity: 0.8
  364. anchor: [0.5,-1]
  365. .. figure:: img/text_halo.png
  366. Label with halo
  367. Grouped labels
  368. ~~~~~~~~~~~~~~
  369. Grouping and other properties can be used to better control where labels are placed. The ``x-group`` option combines all labels with identical text into a single label. This can be useful to show only a single label for a street rather than having a label on every block of the street. The ``x-goodnesOfFit`` option determines whether or not to draw labels based on how well they fit into the available space. The ``x-maxDisplacement`` option determines the maximum distance a label can be moved to avoid overlaps.
  370. The following example uses ``x-group`` to ensure only one label is drawn for each feature, and sets ``x-goodnesOfFit`` to zero so that labels will be drawn even if they have a poor fit::
  371. feature-styles:
  372. - name: name
  373. rules:
  374. - title: fill-graphic
  375. symbolizers:
  376. - text:
  377. label: ${name}
  378. fill-color: '#555555'
  379. font-family: SansSerif
  380. font-size: 12
  381. font-style: italic
  382. font-weight: bold
  383. x-group: true
  384. x-goodnessOfFit: 0.0
  385. x-maxDisplacement: 400
  386. .. figure:: img/text_group.png
  387. Grouped labels
  388. Labels following lines
  389. ~~~~~~~~~~~~~~~~~~~~~~
  390. In order to have a label follow a line (and not be drawn tangent to a line), the ``x-followLine`` option can be set. Other properties can be used in conjunction with this to achieve the best visual result. The following example has street names following the line of the street, with a maximum angle of 90 degrees, repeating every 150 pixels::
  391. feature-styles:
  392. - rules:
  393. - symbolizers:
  394. - line:
  395. stroke-color: '#EDEDFF'
  396. stroke-width: 10
  397. - text:
  398. label: name
  399. x-followLine: true
  400. x-maxAngleDelta: 90
  401. x-maxDisplacement: 400
  402. x-repeat: 150
  403. .. figure:: img/text_follow.png
  404. Labels following lines
  405. Labels avoiding obstacles
  406. ~~~~~~~~~~~~~~~~~~~~~~~~~
  407. The ``x-labelObstacle`` option is used to mark a different symbolizer as an obstacle that labels should avoid. This example draws labels and points on a line geometry, and also uses a point symbolizer to draw the vertices of the lines as points. It is those points which are set to be treated as obstacles to be avoided::
  408. feature-styles:
  409. - rules:
  410. - symbolizers:
  411. - line:
  412. stroke-color: '#00BBDD'
  413. stroke-width: 10
  414. - rules:
  415. - symbolizers:
  416. - point:
  417. geometry: ${vertices(the_geom)}
  418. x-labelObstacle: true
  419. symbols:
  420. - mark:
  421. shape: circle
  422. stroke-color: '#000000'
  423. fill-color: '#007777'
  424. - text:
  425. label: ${streetname}
  426. x-maxDisplacement: 400
  427. x-followLine: true
  428. .. figure:: img/text_labelobstacle.png
  429. Labels avoiding obstacles
  430. Road Shields
  431. ~~~~~~~~~~~~
  432. The ``graphic`` option is used to display a symbol behind a label. A common use for this is to display "highway shields" behind road numbers. This example uses a circle ``shape`` to draw state shields, and an ``external`` image to draw interstate shields, then draws road names on top. The ``x-graphic-resize`` and ``x-graphic-margin`` options are used to resize the graphics to fit the label text::
  433. feature-styles:
  434. - name: state
  435. rules:
  436. - filter: ${level ilike 'State'}
  437. symbolizers:
  438. - line:
  439. stroke-color: '#AAEE00'
  440. stroke-width: 4
  441. stroke-linecap: round
  442. - text:
  443. label: ${name}
  444. anchor: [0.5, 0.5]
  445. fill-color: black
  446. font-family: SansSerif
  447. font-weight: bold
  448. font-size: 8
  449. x-graphic-resize: stretch
  450. x-graphic-margin: 6
  451. graphic:
  452. symbols:
  453. - mark:
  454. shape: circle
  455. fill-color: white
  456. stroke-color: black
  457. - name: interstate
  458. rules:
  459. - filter: ${level ilike 'Interstate'}
  460. symbolizers:
  461. - line:
  462. stroke-color: '#99CC00'
  463. stroke-width: 6
  464. stroke-linecap: round
  465. - text:
  466. label: ${name}
  467. anchor: [0.5, 0.5]
  468. fill-color: white
  469. font-family: SansSerif
  470. font-weight: bold
  471. font-size: 8
  472. x-graphic-resize: stretch
  473. x-graphic-margin: 6
  474. graphic:
  475. symbols:
  476. - external:
  477. url: interstate.png
  478. format: image/png
  479. .. figure:: img/text_roadshields.png
  480. Road Shields