lines.rst 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. .. _sld_cookbook_lines:
  2. Lines
  3. =====
  4. While lines can also seem to be simple shapes, having length but no width, there are many options and tricks for making
  5. lines display nicely.
  6. .. warning:: The code examples shown on this page are **not the full SLD code**, as they omit the SLD header and footer information for the sake of brevity. Please use the links to download the full SLD for each example.
  7. .. _sld_cookbook_lines_attributes:
  8. Example lines layer
  9. -------------------
  10. The :download:`lines layer <artifacts/sld_cookbook_line.zip>` used in the examples below contains road information for a
  11. fictional country. For reference, the attribute table for the points in this layer is included below.
  12. .. list-table::
  13. :widths: 30 40 30
  14. * - **fid** (Feature ID)
  15. - **name** (Road name)
  16. - **type** (Road class)
  17. * - line.1
  18. - Latway
  19. - highway
  20. * - line.2
  21. - Crescent Avenue
  22. - secondary
  23. * - line.3
  24. - Forest Avenue
  25. - secondary
  26. * - line.4
  27. - Longway
  28. - highway
  29. * - line.5
  30. - Saxer Avenue
  31. - secondary
  32. * - line.6
  33. - Ridge Avenue
  34. - secondary
  35. * - line.7
  36. - Holly Lane
  37. - local-road
  38. * - line.8
  39. - Mulberry Street
  40. - local-road
  41. * - line.9
  42. - Nathan Lane
  43. - local-road
  44. * - line.10
  45. - Central Street
  46. - local-road
  47. * - line.11
  48. - Lois Lane
  49. - local-road
  50. * - line.12
  51. - Rocky Road
  52. - local-road
  53. * - line.13
  54. - Fleet Street
  55. - local-road
  56. * - line.14
  57. - Diane Court
  58. - local-road
  59. * - line.15
  60. - Cedar Trail
  61. - local-road
  62. * - line.16
  63. - Victory Road
  64. - local-road
  65. * - line.17
  66. - Highland Road
  67. - local-road
  68. * - line.18
  69. - Easy Street
  70. - local-road
  71. * - line.19
  72. - Hill Street
  73. - local-road
  74. * - line.20
  75. - Country Road
  76. - local-road
  77. * - line.21
  78. - Main Street
  79. - local-road
  80. * - line.22
  81. - Jani Lane
  82. - local-road
  83. * - line.23
  84. - Shinbone Alley
  85. - local-road
  86. * - line.24
  87. - State Street
  88. - local-road
  89. * - line.25
  90. - River Road
  91. - local-road
  92. :download:`Download the lines shapefile <artifacts/sld_cookbook_line.zip>`
  93. .. _sld_cookbook_lines_simpleline:
  94. Simple line
  95. -----------
  96. This example specifies lines be colored black with a thickness of 3 pixels.
  97. .. figure:: images/line_simpleline.png
  98. :align: center
  99. *Simple line*
  100. Code
  101. ~~~~
  102. :download:`View and download the full "Simple line" SLD <artifacts/line_simpleline.sld>`
  103. .. code-block:: xml
  104. :linenos:
  105. <FeatureTypeStyle>
  106. <Rule>
  107. <LineSymbolizer>
  108. <Stroke>
  109. <CssParameter name="stroke">#000000</CssParameter>
  110. <CssParameter name="stroke-width">3</CssParameter>
  111. </Stroke>
  112. </LineSymbolizer>
  113. </Rule>
  114. </FeatureTypeStyle>
  115. Details
  116. ~~~~~~~
  117. There is one ``<Rule>`` in one ``<FeatureTypeStyle>`` for this SLD, which is the simplest possible situation. (All
  118. subsequent examples will contain one ``<Rule>`` and one ``<FeatureTypeStyle>`` unless otherwise specified.) Styling
  119. lines is accomplished via the ``<LineSymbolizer>`` (**lines 3-8**). **Line 5** specifies the color of the line to be
  120. black (``#000000``), while **line 6** specifies the width of the lines to be 3 pixels.
  121. Line with border
  122. ----------------
  123. This example shows how to draw lines with borders (sometimes called "cased lines").
  124. In this case the lines are drawn with a 3 pixel blue center and a 1 pixel wide gray border.
  125. .. figure:: images/line_linewithborder.png
  126. :align: center
  127. *Line with border*
  128. Code
  129. ~~~~
  130. :download:`View and download the full "Line with border" SLD <artifacts/line_linewithborder.sld>`
  131. .. code-block:: xml
  132. :linenos:
  133. <FeatureTypeStyle>
  134. <Rule>
  135. <LineSymbolizer>
  136. <Stroke>
  137. <CssParameter name="stroke">#333333</CssParameter>
  138. <CssParameter name="stroke-width">5</CssParameter>
  139. <CssParameter name="stroke-linecap">round</CssParameter>
  140. </Stroke>
  141. </LineSymbolizer>
  142. </Rule>
  143. </FeatureTypeStyle>
  144. <FeatureTypeStyle>
  145. <Rule>
  146. <LineSymbolizer>
  147. <Stroke>
  148. <CssParameter name="stroke">#6699FF</CssParameter>
  149. <CssParameter name="stroke-width">3</CssParameter>
  150. <CssParameter name="stroke-linecap">round</CssParameter>
  151. </Stroke>
  152. </LineSymbolizer>
  153. </Rule>
  154. </FeatureTypeStyle>
  155. Details
  156. ~~~~~~~
  157. Lines in SLD have no notion of a "fill", only "stroke". Thus, unlike points or polygons, it is not possible to style the
  158. "edge" of the line geometry. It is, however, possible to achieve this effect by drawing each line twice: once with a
  159. certain width and again with a slightly smaller width. This gives the illusion of fill and stroke by obscuring the
  160. larger lines everywhere except along the edges of the smaller lines.
  161. Since every line is drawn twice, the order of the rendering is *very* important.
  162. GeoServer renders ``<FeatureTypeStyle>``\ s in the order that they are presented in the SLD.
  163. In this style, the gray border lines
  164. are drawn first via the first ``<FeatureTypeStyle>``, followed by the blue center lines in a second
  165. ``<FeatureTypeStyle>``. This ensures that the blue lines are not obscured by the gray lines,
  166. and also ensures proper rendering at intersections, so that the blue lines "connect".
  167. In this example, **lines 1-11** comprise the first ``<FeatureTypeStyle>``, which is the outer line (or "stroke").
  168. **Line 5** specifies the color of the line to be dark gray (``#333333``), **line 6** specifies the width of this line
  169. to be 5 pixels, and in **line 7** a ``stroke-linecap`` parameter of ``round``
  170. renders the ends of the line as rounded instead of flat.
  171. (When working with bordered lines using a round line cap ensures that the border connects properly at the ends of the lines.)
  172. **Lines 12-22** comprise the second ``<FeatureTypeStyle>``, which is the inner line (or "fill"). **Line 16**
  173. specifies the color of the line to be a medium blue (``#6699FF``), **line 17** specifies the width of this line to be 3
  174. pixels, and **line 18** again renders the edges of the line to be rounded instead of flat.
  175. The result is a 3 pixel blue line with a 1 pixel gray border, since the 5 pixel gray line will display 1 pixel on each
  176. side of the 3 pixel blue line.
  177. Dashed line
  178. -----------
  179. This example alters the :ref:`sld_cookbook_lines_simpleline` to create a dashed line consisting of 5 pixels of drawn
  180. line alternating with 2 pixels of blank space.
  181. .. figure:: images/line_dashedline.png
  182. :align: center
  183. *Dashed line*
  184. Code
  185. ~~~~
  186. :download:`View and download the full "Dashed line" SLD <artifacts/line_dashedline.sld>`
  187. .. code-block:: xml
  188. :linenos:
  189. <FeatureTypeStyle>
  190. <Rule>
  191. <LineSymbolizer>
  192. <Stroke>
  193. <CssParameter name="stroke">#0000FF</CssParameter>
  194. <CssParameter name="stroke-width">3</CssParameter>
  195. <CssParameter name="stroke-dasharray">5 2</CssParameter>
  196. </Stroke>
  197. </LineSymbolizer>
  198. </Rule>
  199. </FeatureTypeStyle>
  200. Details
  201. ~~~~~~~
  202. In this example, **line 5** sets the color of the lines to be blue (``#0000FF``) and **line 6** sets the width of the
  203. lines to be 3 pixels. **Line 7** determines the composition of the line dashes. The value of ``5 2`` creates a
  204. repeating pattern of 5 pixels of drawn line, followed by 2 pixels of omitted line.
  205. Offset line
  206. -----------
  207. This example alters the :ref:`sld_cookbook_lines_simpleline` to add a perpendicular offset line on the left side
  208. of the line, at five pixels distance.
  209. .. figure:: images/line_offset.png
  210. :align: center
  211. *Offset line*
  212. Code
  213. ~~~~
  214. :download:`View and download the full "Dashed line" SLD <artifacts/line_offset.sld>`
  215. .. code-block:: xml
  216. :linenos:
  217. <FeatureTypeStyle>
  218. <Rule>
  219. <LineSymbolizer>
  220. <Stroke>
  221. <CssParameter name="stroke">#000000</CssParameter>
  222. </Stroke>
  223. </LineSymbolizer>
  224. <LineSymbolizer>
  225. <Stroke>
  226. <CssParameter name="stroke">#FF0000</CssParameter>
  227. <CssParameter name="stroke-dasharray">5 2</CssParameter>
  228. </Stroke>
  229. <PerpendicularOffset>5</PerpendicularOffset>
  230. </LineSymbolizer>
  231. </Rule>
  232. </FeatureTypeStyle>
  233. Details
  234. ~~~~~~~
  235. In this example, the first line symbolizer just paints the lines black.
  236. **line 8** begins a second lines symbolizer, sets the color of the lines to be red (``#FF0000``) at line 10 and
  237. determines the composition of the line dashes at **Line 11**.
  238. **Line 13** finally specifies a perpendicular offset of 5 pixels (positive, thus on the left side).
  239. Railroad (hatching)
  240. -------------------
  241. This example uses hatching to create a railroad style. Both the line and the hatches are black, with a 2 pixel
  242. thickness for the main line and a 1 pixel width for the perpendicular hatches.
  243. .. note:: This example leverages an SLD extension in GeoServer. Hatching is not part of the standard SLD 1.0 specification.
  244. .. figure:: images/line_railroad.png
  245. :align: center
  246. *Railroad (hatching)*
  247. Code
  248. ~~~~
  249. :download:`View and download the full "Railroad (hatching)" SLD <artifacts/line_railroad.sld>`
  250. .. code-block:: xml
  251. :linenos:
  252. <FeatureTypeStyle>
  253. <Rule>
  254. <LineSymbolizer>
  255. <Stroke>
  256. <CssParameter name="stroke">#333333</CssParameter>
  257. <CssParameter name="stroke-width">3</CssParameter>
  258. </Stroke>
  259. </LineSymbolizer>
  260. <LineSymbolizer>
  261. <Stroke>
  262. <GraphicStroke>
  263. <Graphic>
  264. <Mark>
  265. <WellKnownName>shape://vertline</WellKnownName>
  266. <Stroke>
  267. <CssParameter name="stroke">#333333</CssParameter>
  268. <CssParameter name="stroke-width">1</CssParameter>
  269. </Stroke>
  270. </Mark>
  271. <Size>12</Size>
  272. </Graphic>
  273. </GraphicStroke>
  274. </Stroke>
  275. </LineSymbolizer>
  276. </Rule>
  277. </FeatureTypeStyle>
  278. Details
  279. ~~~~~~~
  280. In this example there are two ``<LineSymbolizer>``\ s.
  281. The first symbolizer, on **lines 3-8**, draws a standard line, with **line 5** drawing the lines as dark gray
  282. (``#333333``) and **line 6** setting the width of the lines to be 2 pixels.
  283. The hatching is invoked in the second symbolizer, on **lines 9-24**. **Line 14** specifies that the symbolizer draw a vertical line
  284. hatch (``shape://vertline``) perpendicular to the line geometry. **Lines 16-17** set the hatch color to dark gray
  285. (``#333333``) and width to 1 pixel. Finally, **line 20** specifies both the length of the hatch and the distance
  286. between each hatch to both be 12 pixels.
  287. Spaced graphic symbols
  288. ----------------------
  289. This example uses a graphic stroke along with dash arrays to create a "dot and space" line type.
  290. Adding the dash array specification allows to control the amount of space between one symbol and the next one.
  291. Without using the dash
  292. array the lines would be densely populated with dots, each one touching the previous one.
  293. .. note:: This example may not work in other systems using SLD, since they may not support combining the use of ``stroke-dasharray`` and ``GraphicStroke``.
  294. While the SLD is spec-compliant, the SLD specification does not state what this combination is supposed to produce.
  295. .. figure:: images/line_dashspace.png
  296. :align: center
  297. *Spaced symbols along a line*
  298. Code
  299. ~~~~
  300. :download:`View and download the full "Spaced symbols" SLD <artifacts/line_dashspace.sld>`
  301. .. code-block:: xml
  302. :linenos:
  303. <FeatureTypeStyle>
  304. <Rule>
  305. <LineSymbolizer>
  306. <Stroke>
  307. <GraphicStroke>
  308. <Graphic>
  309. <Mark>
  310. <WellKnownName>circle</WellKnownName>
  311. <Fill>
  312. <CssParameter name="fill">#666666</CssParameter>
  313. </Fill>
  314. <Stroke>
  315. <CssParameter name="stroke">#333333</CssParameter>
  316. <CssParameter name="stroke-width">1</CssParameter>
  317. </Stroke>
  318. </Mark>
  319. <Size>4</Size>
  320. </Graphic>
  321. </GraphicStroke>
  322. <CssParameter name="stroke-dasharray">4 6</CssParameter>
  323. </Stroke>
  324. </LineSymbolizer>
  325. </Rule>
  326. </FeatureTypeStyle>
  327. Details
  328. ~~~~~~~
  329. This example, like others before, uses a ``GraphicStroke`` to place a graphic symbol along a line. The symbol, defined
  330. at **lines 7-16** is a 4 pixel gray circle with a dark gray outline. The spacing between symbols is controlled with
  331. the ``stroke-dasharray`` at **line 20**, which specifies 4 pixels of pen-down (just enough to draw the circle) and 6 pixels of pen-up,
  332. to provide the spacing.
  333. .. _sld_cookbook_lines_defaultlabel:
  334. Alternating symbols with dash offsets
  335. -------------------------------------
  336. This example shows how to create a complex line style which alternates a dashed line and a graphic symbol.
  337. The code builds on features shown in the previous examples:
  338. * ``stroke-dasharray`` controls pen-down/pen-up behavior to generate dashed lines
  339. * ``GraphicStroke`` places symbols along a line
  340. * combining the two allows control of symbol spacing
  341. This also shows the usage of a `dash offset`, which controls where rendering starts
  342. in the dash array.
  343. For example, with a dash array of ``5 10`` and a dash offset of ``7`` the
  344. renderer starts drawing the pattern 7 pixels from the beginning. It skips the 5 pixels pen-down
  345. section and 2 pixels of the pen-up section, then draws the remaining 8 pixels of pen-up, then 5 down, 10 up, and so on.
  346. The example shows how to use these features to create two synchronized sequences of dash arrays,
  347. one drawing line segments and the other symbols.
  348. .. note:: This example may not work in other systems using SLD, since they may not support combining the use of ``stroke-dasharray`` and ``GraphicStroke``.
  349. While the SLD is spec-compliant, the SLD specification does not state what this combination is supposed to produce.
  350. .. figure:: images/line_dashdot.png
  351. :align: center
  352. *Alternating dash and symbol*
  353. Code
  354. ~~~~
  355. :download:`View and download the full "Spaced symbols" SLD <artifacts/line_dashdot.sld>`
  356. .. code-block:: xml
  357. :linenos:
  358. <FeatureTypeStyle>
  359. <Rule>
  360. <LineSymbolizer>
  361. <Stroke>
  362. <CssParameter name="stroke">#0000FF</CssParameter>
  363. <CssParameter name="stroke-width">1</CssParameter>
  364. <CssParameter name="stroke-dasharray">10 10</CssParameter>
  365. </Stroke>
  366. </LineSymbolizer>
  367. <LineSymbolizer>
  368. <Stroke>
  369. <GraphicStroke>
  370. <Graphic>
  371. <Mark>
  372. <WellKnownName>circle</WellKnownName>
  373. <Stroke>
  374. <CssParameter name="stroke">#000033</CssParameter>
  375. <CssParameter name="stroke-width">1</CssParameter>
  376. </Stroke>
  377. </Mark>
  378. <Size>5</Size>
  379. </Graphic>
  380. </GraphicStroke>
  381. <CssParameter name="stroke-dasharray">5 15</CssParameter>
  382. <CssParameter name="stroke-dashoffset">7.5</CssParameter>
  383. </Stroke>
  384. </LineSymbolizer>
  385. </Rule>
  386. </FeatureTypeStyle>
  387. Details
  388. ~~~~~~~
  389. In this example two ``LineSymbolizer``\ s use ``stroke-dasharray`` and different symbology
  390. to produce a sequence of alternating dashes and symbols. The first symbolizer
  391. (**lines 3-9**) is a simple dashed line alternating 10 pixels of pen-down with 10 pixels of pen-up.
  392. The second symbolizer (**lines 10-27**) alternates a 5 pixel empty circle with 15 pixels of white space.
  393. The circle symbol is produced by a ``Mark`` element, with its symbology specified
  394. by ``stroke`` parameters (**lines 17-18**).
  395. The spacing between symbols is controlled with
  396. the ``stroke-dasharray`` (**line 24**), which specifies 5 pixels of pen-down (just enough to draw the circle) and 15 pixels of pen-up.
  397. In order to have the two sequences positioned correctly the second one uses a ``stroke-dashoffset`` of 7.5 (**line 25**).
  398. This makes the sequence start with 12.5
  399. pixels of white space, then a circle (which is then centered between the two line segments of the other pattern),
  400. then 15 pixels of white space, and so on.
  401. Line with default label
  402. -----------------------
  403. This example shows a text label on the simple line. This is how a label will be displayed in the absence of any other
  404. customization.
  405. .. figure:: images/line_linewithdefaultlabel.png
  406. :align: center
  407. *Line with default label*
  408. Code
  409. ~~~~
  410. :download:`View and download the full "Line with default label" SLD <artifacts/line_linewithdefaultlabel.sld>`
  411. .. code-block:: xml
  412. :linenos:
  413. <FeatureTypeStyle>
  414. <Rule>
  415. <LineSymbolizer>
  416. <Stroke>
  417. <CssParameter name="stroke">#FF0000</CssParameter>
  418. </Stroke>
  419. </LineSymbolizer>
  420. <TextSymbolizer>
  421. <Label>
  422. <ogc:PropertyName>name</ogc:PropertyName>
  423. </Label>
  424. <LabelPlacement>
  425. <LinePlacement />
  426. </LabelPlacement>
  427. <Fill>
  428. <CssParameter name="fill">#000000</CssParameter>
  429. </Fill>
  430. </TextSymbolizer>
  431. </Rule>
  432. </FeatureTypeStyle>
  433. Details
  434. ~~~~~~~
  435. In this example, there is one rule with a ``<LineSymbolizer>`` and a ``<TextSymbolizer>``. The ``<LineSymbolizer>``
  436. (**lines 3-7**) draws red lines (``#FF0000``). Since no width is specified, the default is set to 1 pixel. The
  437. ``<TextSymbolizer>`` (**lines 8-15**) determines the labeling of the lines. **Lines 9-11** specify that the text of
  438. the label will be determined by the value of the "name" attribute for each line. (Refer to the attribute table in the
  439. :ref:`sld_cookbook_lines_attributes` section if necessary.) **Line 13** sets the text color to black. All other
  440. details about the label are set to the renderer default, which here is Times New Roman font, font color black, and font
  441. size of 10 pixels.
  442. .. _sld_cookbook_lines_labelfollowingline:
  443. Label following line
  444. --------------------
  445. This example renders the text label to follow the contour of the lines.
  446. .. note:: Labels following lines is an SLD extension specific to GeoServer. It is not part of the SLD 1.0 specification.
  447. .. figure:: images/line_labelfollowingline.png
  448. :align: center
  449. *Label following line*
  450. Code
  451. ~~~~
  452. :download:`View and download the full "Label following line" SLD <artifacts/line_labelfollowingline.sld>`
  453. .. code-block:: xml
  454. :linenos:
  455. <FeatureTypeStyle>
  456. <Rule>
  457. <LineSymbolizer>
  458. <Stroke>
  459. <CssParameter name="stroke">#FF0000</CssParameter>
  460. </Stroke>
  461. </LineSymbolizer>
  462. <TextSymbolizer>
  463. <Label>
  464. <ogc:PropertyName>name</ogc:PropertyName>
  465. </Label>
  466. <LabelPlacement>
  467. <LinePlacement />
  468. </LabelPlacement>
  469. <Fill>
  470. <CssParameter name="fill">#000000</CssParameter>
  471. </Fill>
  472. <VendorOption name="followLine">true</VendorOption>
  473. </TextSymbolizer>
  474. </Rule>
  475. </FeatureTypeStyle>
  476. Details
  477. ~~~~~~~
  478. As the :ref:`sld_cookbook_lines_defaultlabel` example showed, the default label behavior isn't optimal. The label
  479. is displayed at a tangent to the line itself, leading to uncertainty as to which label corresponds to which line.
  480. This example is similar to the :ref:`sld_cookbook_lines_defaultlabel` example with the exception of **lines 12-18**.
  481. **Line 18** sets the option to have the label follow the line, while **lines 12-14** specify that the label is placed
  482. along a line. If ``<LinePlacement />`` is not specified in an SLD, then ``<PointPlacement />`` is assumed, which isn't
  483. compatible with line-specific rendering options.
  484. .. note:: Not all labels are shown due to label conflict resolution. See the next section on :ref:`sld_cookbook_lines_optimizedlabel` for an example of how to maximize label display.
  485. .. _sld_cookbook_lines_optimizedlabel:
  486. Optimized label placement
  487. -------------------------
  488. This example optimizes label placement for lines such that the maximum number of labels are displayed.
  489. .. note:: This example uses options that are specific to GeoServer and are not part of the SLD 1.0 specification.
  490. .. figure:: images/line_optimizedlabel.png
  491. :align: center
  492. *Optimized label*
  493. Code
  494. ~~~~
  495. :download:`View and download the full "Optimized label" SLD <artifacts/line_optimizedlabel.sld>`
  496. .. code-block:: xml
  497. :linenos:
  498. <FeatureTypeStyle>
  499. <Rule>
  500. <LineSymbolizer>
  501. <Stroke>
  502. <CssParameter name="stroke">#FF0000</CssParameter>
  503. </Stroke>
  504. </LineSymbolizer>
  505. <TextSymbolizer>
  506. <Label>
  507. <ogc:PropertyName>name</ogc:PropertyName>
  508. </Label>
  509. <LabelPlacement>
  510. <LinePlacement />
  511. </LabelPlacement>
  512. <Fill>
  513. <CssParameter name="fill">#000000</CssParameter>
  514. </Fill>
  515. <VendorOption name="followLine">true</VendorOption>
  516. <VendorOption name="maxAngleDelta">90</VendorOption>
  517. <VendorOption name="maxDisplacement">400</VendorOption>
  518. <VendorOption name="repeat">150</VendorOption>
  519. </TextSymbolizer>
  520. </Rule>
  521. </FeatureTypeStyle>
  522. Details
  523. ~~~~~~~
  524. GeoServer uses "conflict resolution" to ensure that labels aren't drawn on top of other labels, obscuring them both.
  525. This accounts for the reason why many lines don't have labels in the previous example,
  526. :ref:`sld_cookbook_lines_labelfollowingline`. While this setting can be toggled, it is usually a good idea to leave it
  527. on and use other label placement options to ensure that labels are drawn as often as desired and in the correct places.
  528. This example does just that.
  529. This example is similar to the previous example, :ref:`sld_cookbook_lines_labelfollowingline`. The only differences are contained in **lines 18-21**. **Line 19** sets the maximum angle that the label will follow. This sets the label to never bend more than 90 degrees to prevent the label from becoming illegible due to a pronounced curve or angle. **Line 20** sets the maximum displacement of the label to be 400 pixels. In order to resolve conflicts with overlapping labels, GeoServer will attempt to move the labels such that they are no longer overlapping. This value sets how far the label can be moved relative to its original placement. Finally, **line 21** sets the labels to be repeated every 150 pixels. A feature will typically receive only one label, but this can cause confusion for long lines. Setting the label to repeat ensures that the line is always labeled locally.
  530. .. _sld_cookbook_lines_optimizedstyledlabel:
  531. Optimized and styled label
  532. --------------------------
  533. This example improves the style of the labels from the :ref:`sld_cookbook_lines_optimizedlabel` example.
  534. .. figure:: images/line_optimizedstyledlabel.png
  535. :align: center
  536. *Optimized and styled label*
  537. Code
  538. ~~~~
  539. :download:`View and download the full "Optimized and styled label" SLD <artifacts/line_optimizedstyledlabel.sld>`
  540. .. code-block:: xml
  541. :linenos:
  542. <FeatureTypeStyle>
  543. <Rule>
  544. <LineSymbolizer>
  545. <Stroke>
  546. <CssParameter name="stroke">#FF0000</CssParameter>
  547. </Stroke>
  548. </LineSymbolizer>
  549. <TextSymbolizer>
  550. <Label>
  551. <ogc:PropertyName>name</ogc:PropertyName>
  552. </Label>
  553. <LabelPlacement>
  554. <LinePlacement />
  555. </LabelPlacement>
  556. <Fill>
  557. <CssParameter name="fill">#000000</CssParameter>
  558. </Fill>
  559. <Font>
  560. <CssParameter name="font-family">Arial</CssParameter>
  561. <CssParameter name="font-size">10</CssParameter>
  562. <CssParameter name="font-style">normal</CssParameter>
  563. <CssParameter name="font-weight">bold</CssParameter>
  564. </Font>
  565. <VendorOption name="followLine">true</VendorOption>
  566. <VendorOption name="maxAngleDelta">90</VendorOption>
  567. <VendorOption name="maxDisplacement">400</VendorOption>
  568. <VendorOption name="repeat">150</VendorOption>
  569. </TextSymbolizer>
  570. </Rule>
  571. </FeatureTypeStyle>
  572. Details
  573. ~~~~~~~
  574. This example is similar to the :ref:`sld_cookbook_lines_optimizedlabel`. The only difference is in the font information, which is contained in **lines 18-23**. **Line 19** sets the font family to be "Arial", **line 20** sets the font size to 10, **line 21** sets the font style to "normal" (as opposed to "italic" or "oblique"), and **line 22** sets the font weight to "bold" (as opposed to "normal").
  575. Attribute-based line
  576. --------------------
  577. This example styles the lines differently based on the "type" (Road class) attribute.
  578. .. figure:: images/line_attributebasedline.png
  579. :align: center
  580. *Attribute-based line*
  581. Code
  582. ~~~~
  583. :download:`View and download the full "Attribute-based line" SLD <artifacts/line_attributebasedline.sld>`
  584. .. code-block:: xml
  585. :linenos:
  586. <FeatureTypeStyle>
  587. <Rule>
  588. <Name>local-road</Name>
  589. <ogc:Filter>
  590. <ogc:PropertyIsEqualTo>
  591. <ogc:PropertyName>type</ogc:PropertyName>
  592. <ogc:Literal>local-road</ogc:Literal>
  593. </ogc:PropertyIsEqualTo>
  594. </ogc:Filter>
  595. <LineSymbolizer>
  596. <Stroke>
  597. <CssParameter name="stroke">#009933</CssParameter>
  598. <CssParameter name="stroke-width">2</CssParameter>
  599. </Stroke>
  600. </LineSymbolizer>
  601. </Rule>
  602. </FeatureTypeStyle>
  603. <FeatureTypeStyle>
  604. <Rule>
  605. <Name>secondary</Name>
  606. <ogc:Filter>
  607. <ogc:PropertyIsEqualTo>
  608. <ogc:PropertyName>type</ogc:PropertyName>
  609. <ogc:Literal>secondary</ogc:Literal>
  610. </ogc:PropertyIsEqualTo>
  611. </ogc:Filter>
  612. <LineSymbolizer>
  613. <Stroke>
  614. <CssParameter name="stroke">#0055CC</CssParameter>
  615. <CssParameter name="stroke-width">3</CssParameter>
  616. </Stroke>
  617. </LineSymbolizer>
  618. </Rule>
  619. </FeatureTypeStyle>
  620. <FeatureTypeStyle>
  621. <Rule>
  622. <Name>highway</Name>
  623. <ogc:Filter>
  624. <ogc:PropertyIsEqualTo>
  625. <ogc:PropertyName>type</ogc:PropertyName>
  626. <ogc:Literal>highway</ogc:Literal>
  627. </ogc:PropertyIsEqualTo>
  628. </ogc:Filter>
  629. <LineSymbolizer>
  630. <Stroke>
  631. <CssParameter name="stroke">#FF0000</CssParameter>
  632. <CssParameter name="stroke-width">6</CssParameter>
  633. </Stroke>
  634. </LineSymbolizer>
  635. </Rule>
  636. </FeatureTypeStyle>
  637. Details
  638. ~~~~~~~
  639. .. note:: Refer to the :ref:`sld_cookbook_lines_attributes` to see the attributes for the layer. This example has eschewed labels in order to simplify the style, but you can refer to the example :ref:`sld_cookbook_lines_optimizedstyledlabel` to see which attributes correspond to which points.
  640. There are three types of road classes in our fictional country, ranging from back roads to high-speed freeways:
  641. "highway", "secondary", and "local-road". In order to handle each case separately, there is more than one
  642. ``<FeatureTypeStyle>``, each containing a single rule. This ensures that each road type is rendered in order, as each
  643. ``<FeatureTypeStyle>`` is drawn based on the order in which it appears in the SLD.
  644. The three rules are designed as follows:
  645. .. list-table::
  646. :widths: 20 30 30 20
  647. * - **Rule order**
  648. - **Rule name / type**
  649. - **Color**
  650. - **Size**
  651. * - 1
  652. - local-road
  653. - ``#009933`` (green)
  654. - 2
  655. * - 2
  656. - secondary
  657. - ``#0055CC`` (blue)
  658. - 3
  659. * - 3
  660. - highway
  661. - ``#FF0000`` (red)
  662. - 6
  663. **Lines 2-16** comprise the first ``<Rule>``. **Lines 4-9** set the filter for this rule, such that the "type"
  664. attribute has a value of "local-road". If this condition is true for a particular line, the rule is rendered according
  665. to the ``<LineSymbolizer>`` which is on **lines 10-15**. **Lines 12-13** set the color of the line to be a dark green
  666. (``#009933``) and the width to be 2 pixels.
  667. **Lines 19-33** comprise the second ``<Rule>``. **Lines 21-26** set the filter for this rule, such that the "type"
  668. attribute has a value of "secondary". If this condition is true for a particular line, the rule is rendered according
  669. to the ``<LineSymbolizer>`` which is on **lines 27-32**. **Lines 29-30** set the color of the line to be a dark blue
  670. (``#0055CC``) and the width to be 3 pixels, making the lines slightly thicker than the "local-road" lines and also a
  671. different color.
  672. **Lines 36-50** comprise the third and final ``<Rule>``. **Lines 38-43** set the filter for this rule, such that the
  673. "type" attribute has a value of "primary". If this condition is true for a particular line, the rule is rendered
  674. according to the ``<LineSymbolizer>`` which is on **lines 44-49**. **Lines 46-47** set the color of the line to be a
  675. bright red (``#FF0000``) and the width to be 6 pixels, so that these lines are rendered on top of and thicker than the
  676. other two road classes. In this way, the "primary" roads are given priority in the map rendering.
  677. Zoom-based line
  678. ---------------
  679. This example alters the :ref:`sld_cookbook_lines_simpleline` style at different zoom levels.
  680. .. figure:: images/line_zoombasedlinelarge.png
  681. :align: center
  682. *Zoom-based line: Zoomed in*
  683. .. figure:: images/line_zoombasedlinemedium.png
  684. :align: center
  685. *Zoom-based line: Partially zoomed*
  686. .. figure:: images/line_zoombasedlinesmall.png
  687. :align: center
  688. *Zoom-based line: Zoomed out*
  689. Code
  690. ~~~~
  691. :download:`View and download the full "Zoom-based line" SLD <artifacts/line_zoombasedline.sld>`
  692. .. code-block:: xml
  693. :linenos:
  694. <FeatureTypeStyle>
  695. <Rule>
  696. <Name>Large</Name>
  697. <MaxScaleDenominator>180000000</MaxScaleDenominator>
  698. <LineSymbolizer>
  699. <Stroke>
  700. <CssParameter name="stroke">#009933</CssParameter>
  701. <CssParameter name="stroke-width">6</CssParameter>
  702. </Stroke>
  703. </LineSymbolizer>
  704. </Rule>
  705. <Rule>
  706. <Name>Medium</Name>
  707. <MinScaleDenominator>180000000</MinScaleDenominator>
  708. <MaxScaleDenominator>360000000</MaxScaleDenominator>
  709. <LineSymbolizer>
  710. <Stroke>
  711. <CssParameter name="stroke">#009933</CssParameter>
  712. <CssParameter name="stroke-width">4</CssParameter>
  713. </Stroke>
  714. </LineSymbolizer>
  715. </Rule>
  716. <Rule>
  717. <Name>Small</Name>
  718. <MinScaleDenominator>360000000</MinScaleDenominator>
  719. <LineSymbolizer>
  720. <Stroke>
  721. <CssParameter name="stroke">#009933</CssParameter>
  722. <CssParameter name="stroke-width">2</CssParameter>
  723. </Stroke>
  724. </LineSymbolizer>
  725. </Rule>
  726. </FeatureTypeStyle>
  727. Details
  728. ~~~~~~~
  729. It is often desirable to make shapes larger at higher zoom levels when creating a natural-looking map. This example
  730. varies the thickness of the lines according to the zoom level (or more accurately, scale denominator). Scale
  731. denominators refer to the scale of the map. A scale denominator of 10,000 means the map has a scale of 1:10,000 in the
  732. units of the map projection.
  733. .. note:: Determining the appropriate scale denominators (zoom levels) to use is beyond the scope of this example.
  734. This style contains three rules. The three rules are designed as follows:
  735. .. list-table::
  736. :widths: 15 25 40 20
  737. * - **Rule order**
  738. - **Rule name**
  739. - **Scale denominator**
  740. - **Line width**
  741. * - 1
  742. - Large
  743. - 1:180,000,000 or less
  744. - 6
  745. * - 2
  746. - Medium
  747. - 1:180,000,000 to 1:360,000,000
  748. - 4
  749. * - 3
  750. - Small
  751. - Greater than 1:360,000,000
  752. - 2
  753. The order of these rules does not matter since the scales denominated in each rule do not overlap.
  754. The first rule (**lines 2-11**) is the smallest scale denominator, corresponding to when the view is "zoomed in". The
  755. scale rule is set on **line 4**, so that the rule will apply to any map with a scale denominator of 180,000,000 or
  756. less. **Line 7-8** draws the line to be dark green (``#009933``) with a width of 6 pixels.
  757. The second rule (**lines 12-22**) is the intermediate scale denominator, corresponding to when the view is "partially
  758. zoomed". **Lines 14-15** set the scale such that the rule will apply to any map with scale denominators between
  759. 180,000,000 and 360,000,000. (The ``<MinScaleDenominator>`` is inclusive and the ``<MaxScaleDenominator>`` is
  760. exclusive, so a zoom level of exactly 360,000,000 would *not* apply here.) Aside from the scale, the only difference
  761. between this rule and the previous is the width of the lines, which is set to 4 pixels on **line 19**.
  762. The third rule (**lines 23-32**) is the largest scale denominator, corresponding to when the map is "zoomed out". The
  763. scale rule is set on **line 25**, so that the rule will apply to any map with a scale denominator of 360,000,000 or
  764. greater. Again, the only other difference between this rule and the others is the width of the lines, which is set to
  765. 2 pixels on **line 29**.
  766. The result of this style is that lines are drawn with larger widths as one zooms in and smaller widths as one zooms out.