polygons.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. .. _ysld_cookbook.polygons:
  2. Polygons
  3. ========
  4. Polygons are two dimensional shapes that contain both an outer edge (or "stroke") and an inside (or "fill"). A polygon can be thought of as an irregularly-shaped point and is styled in similar ways to points.
  5. .. _ysld_cookbook_polygons_attributes:
  6. Example polygons layer
  7. ----------------------
  8. The :download:`polygons layer <artifacts/ysld_cookbook_polygon.zip>` used below contains county information for a fictional country. For reference, the attribute table for the polygons is included below.
  9. .. list-table::
  10. :widths: 30 40 30
  11. :header-rows: 1
  12. * - ``fid`` (Feature ID)
  13. - ``name`` (County name)
  14. - ``pop`` (Population)
  15. * - polygon.1
  16. - Irony County
  17. - 412234
  18. * - polygon.2
  19. - Tracker County
  20. - 235421
  21. * - polygon.3
  22. - Dracula County
  23. - 135022
  24. * - polygon.4
  25. - Poly County
  26. - 1567879
  27. * - polygon.5
  28. - Bearing County
  29. - 201989
  30. * - polygon.6
  31. - Monte Cristo County
  32. - 152734
  33. * - polygon.7
  34. - Massive County
  35. - 67123
  36. * - polygon.8
  37. - Rhombus County
  38. - 198029
  39. :download:`Download the polygons shapefile <artifacts/ysld_cookbook_polygon.zip>`
  40. .. _ysld_cookbook_polygons_simplepolygon:
  41. Simple polygon
  42. --------------
  43. This example shows a polygon filled in blue.
  44. .. figure:: ../../sld/cookbook/images/polygon_simplepolygon.png
  45. Simple polygon
  46. Code
  47. ~~~~
  48. :download:`Download the "Simple polygon" YSLD <artifacts/polygon_simplepolygon.ysld>`
  49. .. literalinclude:: artifacts/polygon_simplepolygon.ysld
  50. :language: yaml
  51. Details
  52. ~~~~~~~
  53. There is one rule in one feature style for this style, which is the simplest possible situation. (All subsequent examples will share this characteristic unless otherwise specified.) Styling polygons is accomplished via the polygon symbolizer (**lines 6-7**). **Line 7** specifies dark blue (``'#000080'``) as the polygon's fill color.
  54. .. note:: The light-colored borders around the polygons in the figure are artifacts of the renderer caused by the polygons being adjacent. There is no border in this style.
  55. .. _ysld_cookbook_polygons_simplepolygonwithstroke:
  56. Simple polygon with stroke
  57. --------------------------
  58. This example adds a 2 pixel white stroke to the :ref:`ysld_cookbook_polygons_simplepolygon` example.
  59. .. figure:: ../../sld/cookbook/images/polygon_simplepolygonwithstroke.png
  60. Simple polygon with stroke
  61. Code
  62. ~~~~
  63. :download:`Download the "Simple polygon with stroke" YSLD <artifacts/polygon_simplepolygonwithstroke.ysld>`
  64. .. literalinclude:: artifacts/polygon_simplepolygonwithstroke.ysld
  65. :language: yaml
  66. Details
  67. ~~~~~~~
  68. This example is similar to the :ref:`ysld_cookbook_polygons_simplepolygon` example above, with the addition of ``stroke`` parameters (**lines 7-8**). **Line 7** sets the color of stroke to white (``'#FFFFFF'``) and **line 8** sets the width of the stroke to 2 pixels.
  69. Transparent polygon
  70. -------------------
  71. This example builds on the :ref:`ysld_cookbook_polygons_simplepolygonwithstroke` example and makes the fill partially transparent by setting the opacity to 50%.
  72. .. figure:: ../../sld/cookbook/images/polygon_transparentpolygon.png
  73. Transparent polygon
  74. Code
  75. ~~~~
  76. :download:`Download the "Transparent polygon" YSLD <artifacts/polygon_transparentpolygon.ysld>`
  77. .. literalinclude:: artifacts/polygon_transparentpolygon.ysld
  78. :language: yaml
  79. Details
  80. ~~~~~~~
  81. This example is similar to the :ref:`ysld_cookbook_polygons_simplepolygonwithstroke` example, save for defining the fill's opacity in **line 10**. The value of 0.5 results in partially transparent fill that is 50% opaque. An opacity value of 1 would draw the fill as 100% opaque, while an opacity value of 0 would result in a completely transparent (0% opaque) fill. In this example, since the background is white, the dark blue looks lighter. Were the points imposed on a dark background, the resulting color would be darker.
  82. .. _ysld_cookbook_polygons_graphicfill:
  83. Graphic fill
  84. ------------
  85. This example fills the polygons with a tiled graphic.
  86. .. figure:: ../../sld/cookbook/images/polygon_graphicfill.png
  87. Graphic fill
  88. Code
  89. ~~~~
  90. :download:`Download the "Graphic fill" YSLD <artifacts/polygon_graphicfill.ysld>`
  91. .. literalinclude:: artifacts/polygon_graphicfill.ysld
  92. :language: yaml
  93. Details
  94. ~~~~~~~
  95. This style fills the polygon with a tiled graphic. This is known as an ``external`` in YSLD, to distinguish it from commonly-used shapes such as squares and circles that are "internal" to the renderer. **Lines 11-13** specify details for the graphic, with **line 12** setting the path and file name of the graphic and **line 13** indicating the file format (MIME type) of the graphic (``image/png``). Although a full URL could be specified if desired, no path information is necessary in **line 12** because this graphic is contained in the same directory as the YSLD. **Line 9** determines the height of the displayed graphic in pixels; if the value differs from the height of the graphic then it will be scaled accordingly while preserving the aspect ratio.
  96. .. figure:: ../../sld/cookbook/images/colorblocks.png
  97. Graphic used for fill
  98. Hatching fill
  99. -------------
  100. This example fills the polygons with a hatching pattern.
  101. .. figure:: ../../sld/cookbook/images/polygon_hatchingfill.png
  102. Hatching fill
  103. Code
  104. ~~~~
  105. :download:`Download the "Hatching fill" YSLD <artifacts/polygon_hatchingfill.ysld>`
  106. .. literalinclude:: artifacts/polygon_hatchingfill.ysld
  107. :language: yaml
  108. Details
  109. ~~~~~~~
  110. In this example, there is a ``fill-graphic`` parameter as in the :ref:`ysld_cookbook_polygons_graphicfill` example, but a ``mark`` (**lines 11-14**) is used instead of an ``external``. **Line 12** specifies a "times" symbol (an "x") be tiled throughout the polygon. **Line 13** sets the color to purple (``'#990099'``), **line 14** sets the width of the hatches to 1 pixel, and **line 9** sets the size of the tile to 16 pixels. Because hatch tiles are always square, the ``size`` sets both the width and the height.
  111. .. _ysld_cookbook_polygons_polygonwithdefaultlabel:
  112. Polygon with default label
  113. --------------------------
  114. This example shows a text label on the polygon. In the absence of any other customization, this is how a label will be displayed.
  115. .. figure:: ../../sld/cookbook/images/polygon_polygonwithdefaultlabel.png
  116. Polygon with default label
  117. Code
  118. ~~~~
  119. :download:`Download the "Polygon with default label" YSLD <artifacts/polygon_polygonwithdefaultlabel.ysld>`
  120. .. literalinclude:: artifacts/polygon_polygonwithdefaultlabel.ysld
  121. :language: yaml
  122. Details
  123. ~~~~~~~
  124. In this example there is a polygon symbolizer and a text symbolizer. **Lines 6-9** comprise the polygon symbolizer. The fill of the polygon is set on **line 7** to a light green (``'#40FF40'``) while the stroke of the polygon is set on **lines 8-9** to white (``'#FFFFFF'``) with a thickness of 2 pixels. The label is set in the text symbolizer on **lines 10-12**, with **line 11** determining what text to display, in this case the value of the "name" attribute. (Refer to the attribute table in the :ref:`ysld_cookbook_polygons_attributes` section if necessary.) All other details about the label are set to the renderer default, which here is Times New Roman font, font color black, and font size of 10 pixels.
  125. Label halo
  126. ----------
  127. This example alters the look of the :ref:`ysld_cookbook_polygons_polygonwithdefaultlabel` by adding a white halo to the label.
  128. .. figure:: ../../sld/cookbook/images/polygon_labelhalo.png
  129. Label halo
  130. Code
  131. ~~~~
  132. :download:`Download the "Label halo" YSLD <artifacts/polygon_labelhalo.ysld>`
  133. .. literalinclude:: artifacts/polygon_labelhalo.ysld
  134. :language: yaml
  135. Details
  136. ~~~~~~~
  137. This example is similar to the :ref:`ysld_cookbook_polygons_polygonwithdefaultlabel`, with the addition of a halo around the labels on **lines 12-14**. A halo creates a color buffer around the label to improve label legibility. **Line 14** sets the radius of the halo, extending the halo 3 pixels around the edge of the label, and **line 13** sets the color of the halo to white (``'#FFFFFF'``). Since halos are most useful when set to a sharp contrast relative to the text color, this example uses a white halo around black text to ensure optimum readability.
  138. .. _ysld_cookbook_polygons_polygonwithstyledlabel:
  139. Polygon with styled label
  140. -------------------------
  141. This example improves the label style from the :ref:`ysld_cookbook_polygons_polygonwithdefaultlabel` example by centering the label on the polygon, specifying a different font name and size, and setting additional label placement optimizations.
  142. .. figure:: ../../sld/cookbook/images/polygon_polygonwithstyledlabel.png
  143. Polygon with styled label
  144. Code
  145. ~~~~
  146. :download:`Download the "Polygon with styled label" YSLD <artifacts/polygon_polygonwithstyledlabel.ysld>`
  147. .. literalinclude:: artifacts/polygon_polygonwithstyledlabel.ysld
  148. :language: yaml
  149. Details
  150. ~~~~~~~
  151. This example is similar to the :ref:`ysld_cookbook_polygons_polygonwithdefaultlabel` example, with additional styling options within the text symbolizer on lines **13-21**. **Lines 13-16** set the font styling. **Line 13** sets the font family to be Arial, **line 14** sets the font size to 11 pixels, **line 15** sets the font style to "normal" (as opposed to "italic" or "oblique"), and **line 16** sets the font weight to "bold" (as opposed to "normal").
  152. The ``anchor`` parameter on **line 18** centers the label by positioning it 50% (or 0.5) of the way horizontally and vertically along the centroid of the polygon.
  153. Finally, there are two added touches for label placement optimization: **line 20** ensures that long labels are split across multiple lines by setting line wrapping on the labels to 60 pixels, and **line 21** allows the label to be displaced by up to 150 pixels. This ensures that labels are compacted and less likely to spill over polygon boundaries. Notice little Massive County in the corner, whose label is now displayed."
  154. Attribute-based polygon
  155. -----------------------
  156. This example styles the polygons differently based on the "pop" (Population) attribute.
  157. .. figure:: ../../sld/cookbook/images/polygon_attributebasedpolygon.png
  158. Attribute-based polygon
  159. Code
  160. ~~~~
  161. :download:`Download the "Attribute-based polygon" YSLD <artifacts/polygon_attributebasedpolygon.ysld>`
  162. .. literalinclude:: artifacts/polygon_attributebasedpolygon.ysld
  163. :language: yaml
  164. Details
  165. ~~~~~~~
  166. .. note:: Refer to the :ref:`ysld_cookbook_polygons_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:`ysld_cookbook_polygons_polygonwithstyledlabel` to see which attributes correspond to which polygons.
  167. Each polygon in our fictional country has a population that is represented by the population ("pop") attribute. This style contains three rules that alter the fill based on the value of "pop" attribute, with smaller values yielding a lighter color and larger values yielding a darker color.
  168. The three rules are designed as follows:
  169. .. list-table::
  170. :widths: 20 20 30 30
  171. :header-rows: 1
  172. * - Rule order
  173. - Rule name
  174. - Population (``pop``)
  175. - Color
  176. * - 1
  177. - SmallPop
  178. - Less than 200,000
  179. - ``#66FF66``
  180. * - 2
  181. - MediumPop
  182. - 200,000 to 500,000
  183. - ``#33CC33``
  184. * - 3
  185. - LargePop
  186. - Greater than 500,000
  187. - ``#009900``
  188. The order of the rules does not matter in this case, since each shape is only rendered by a single rule.
  189. The first rule, on **lines 5-10**, specifies the styling of polygons whose population attribute is less than 200,000. **Line 7** sets this filter, denoting the attribute ("pop"), to be "less than" the value of 200,000. The color of the polygon fill is set to a light green (``'#66FF66'``) on **line 10**.
  190. The second rule, on **lines 11-16**, is similar, specifying a style for polygons whose population attribute is greater than or equal to 200,000 but less than 500,000. The filter is set on **line 13**. This filter specifies two criteria instead of one: a "greater than or equal to" and a "less than" filter. These criteria are joined by ``AND``, which mandates that both filters need to be true for the rule to be applicable. The color of the polygon fill is set to a medium green on (``'#33CC33'``) on **line 16**.
  191. The third rule, on **lines 17-22**, specifies a style for polygons whose population attribute is greater than or equal to 500,000. The filter is set on **line 19**. The color of the polygon fill is the only other difference in this rule, which is set to a dark green (``'#009900'``) on **line 22**.
  192. Zoom-based polygon
  193. ------------------
  194. This example alters the style of the polygon at different zoom levels.
  195. .. figure:: ../../sld/cookbook/images/polygon_zoombasedpolygonlarge.png
  196. Zoom-based polygon: Zoomed in
  197. .. figure:: ../../sld/cookbook/images/polygon_zoombasedpolygonmedium.png
  198. Zoom-based polygon: Partially zoomed
  199. .. figure:: ../../sld/cookbook/images/polygon_zoombasedpolygonsmall.png
  200. Zoom-based polygon: Zoomed out
  201. Code
  202. ~~~~
  203. :download:`Download the "Zoom-based polygon" YSLD <artifacts/polygon_zoombasedpolygon.ysld>`
  204. .. literalinclude:: artifacts/polygon_zoombasedpolygon.ysld
  205. :language: yaml
  206. Details
  207. ~~~~~~~
  208. It is often desirable to make shapes larger at higher zoom levels when creating a natural-looking map. This example varies the thickness of the lines according to the zoom level. Polygons already do this by nature of being two dimensional, but another way to adjust styling of polygons based on zoom level is to adjust the thickness of the stroke (to be larger as the map is zoomed in) or to limit labels to only certain zoom levels. This is ensures that the size and quantity of strokes and labels remains legible and doesn't overshadow the polygons themselves.
  209. Zoom levels (or more accurately, scale 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 units of the map projection.
  210. .. note:: Determining the appropriate scale denominators (zoom levels) to use is beyond the scope of this example.
  211. This style contains three rules, defined as follows:
  212. .. list-table::
  213. :widths: 15 15 40 15 15
  214. :header-rows: 1
  215. * - Rule order
  216. - Rule name
  217. - Scale denominator
  218. - Stroke width
  219. - Label display?
  220. * - 1
  221. - Large
  222. - 1:100,000,000 or less
  223. - 7
  224. - Yes
  225. * - 2
  226. - Medium
  227. - 1:100,000,000 to 1:200,000,000
  228. - 4
  229. - No
  230. * - 3
  231. - Small
  232. - Greater than 1:200,000,000
  233. - 2
  234. - No
  235. The first rule, on **lines 5-20**, is for the smallest scale denominator, corresponding to when the view is "zoomed in". The scale rule is set on **line 6** such that the rule will apply only where the scale denominator is 100,000,000 or less. **Line 11** defines the fill as blue (``'#0000CC'``). Note that the fill is kept constant across all rules regardless of the scale denominator. As in the :ref:`ysld_cookbook_polygons_polygonwithdefaultlabel` or :ref:`ysld_cookbook_polygons_polygonwithstyledlabel` examples, the rule also contains a text symbolizer at **lines 12-20** for drawing a text label on top of the polygon. **Lines 15-18** set the font information to be Arial, 14 pixels, and bold with no italics. The label is centered both horizontally and vertically along the centroid of the polygon on by setting ``anchor`` to be ``[0.5, 0.5]`` (or 50%) on **line 20**. Finally, the color of the font is set to white (``'#FFFFFF'``) in **line 14**.
  236. The second rule, on **lines 21-27**, is for the intermediate scale denominators, corresponding to when the view is "partially zoomed". The scale rules on **lines 22** set the rule such that it will apply to any map with a scale denominator between 100,000,000 and 200,000,000. (The lower bound is inclusive and the upper bound is exclusive, so a zoom level of exactly 200,000,000 would *not* apply here.) Aside from the scale, there are two differences between this rule and the first: the width of the stroke is set to 4 pixels on **line 26** and a text symbolizer is not present so that no labels will be displayed.
  237. The third rule, on **lines 28-34**, is for the largest scale denominator, corresponding to when the map is "zoomed out". The scale rule is set on **line 29** such that the rule will apply to any map with a scale denominator of 200,000,000 or greater. Again, the only differences between this rule and the others are the width of the lines, which is set to 1 pixel on **line 33**, and the absence of a text symbolizer so that no labels will be displayed.
  238. The resulting style produces a polygon stroke that gets larger as one zooms in and labels that only display when zoomed in to a sufficient level.