polygon.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. .. _css_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. .. _css_cookbook_polygons_attributes:
  6. Example polygons layer
  7. ----------------------
  8. The :download:`polygons layer <../../sld/cookbook/artifacts/sld_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. * - **fid** (Feature ID)
  12. - **name** (County name)
  13. - **pop** (Population)
  14. * - polygon.1
  15. - Irony County
  16. - 412234
  17. * - polygon.2
  18. - Tracker County
  19. - 235421
  20. * - polygon.3
  21. - Dracula County
  22. - 135022
  23. * - polygon.4
  24. - Poly County
  25. - 1567879
  26. * - polygon.5
  27. - Bearing County
  28. - 201989
  29. * - polygon.6
  30. - Monte Cristo County
  31. - 152734
  32. * - polygon.7
  33. - Massive County
  34. - 67123
  35. * - polygon.8
  36. - Rhombus County
  37. - 198029
  38. :download:`Download the polygons shapefile <../../sld/cookbook/artifacts/sld_cookbook_polygon.zip>`
  39. .. _css_cookbook_polygons_simplepolygon:
  40. Simple polygon
  41. --------------
  42. This example shows a polygon filled in blue.
  43. .. figure:: ../../sld/cookbook/images/polygon_simplepolygon.png
  44. :align: center
  45. *Simple polygon*
  46. Code
  47. ~~~~
  48. .. code-block:: css
  49. :linenos:
  50. * {
  51. fill: #000080;
  52. }
  53. Details
  54. ~~~~~~~
  55. This simple rule applies a dark blue (``#000080``) fill to all the polygons in the dataset.
  56. .. 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.
  57. .. _css_cookbook_polygons_simplepolygonwithstroke:
  58. Simple polygon with stroke
  59. --------------------------
  60. This example adds a 2 pixel white stroke to the :ref:`css_cookbook_polygons_simplepolygon` example.
  61. .. figure:: ../../sld/cookbook/images/polygon_simplepolygonwithstroke.png
  62. :align: center
  63. *Simple polygon with stroke*
  64. Code
  65. ~~~~
  66. .. code-block:: css
  67. :linenos:
  68. * {
  69. fill: #000080;
  70. stroke: #FFFFFF;
  71. stroke-width: 2;
  72. }
  73. Details
  74. ~~~~~~~
  75. This example is similar to the :ref:`css_cookbook_polygons_simplepolygon` example above, with the addition of the "stroke" and "stroke-width" attributes, that add a white, 2 pixels wide border around each polygon.
  76. Transparent polygon
  77. -------------------
  78. This example builds on the :ref:`css_cookbook_polygons_simplepolygonwithstroke` example and makes the fill partially transparent by setting the opacity to 50%.
  79. .. figure:: ../../sld/cookbook/images/polygon_transparentpolygon.png
  80. :align: center
  81. *Transparent polygon*
  82. Code
  83. ~~~~
  84. .. code-block:: css
  85. :linenos:
  86. * {
  87. fill: #000080;
  88. fill-opacity: 0.5;
  89. stroke: #FFFFFF;
  90. stroke-width: 2;
  91. }
  92. Details
  93. ~~~~~~~
  94. This example is similar to the :ref:`css_cookbook_polygons_simplepolygonwithstroke` example, save for defining the fill's opacity in **line 3**. 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.
  95. .. _css_cookbook_polygons_graphicfill:
  96. Graphic fill
  97. ------------
  98. This example fills the polygons with a tiled graphic.
  99. .. figure:: ../../sld/cookbook/images/polygon_graphicfill.png
  100. :align: center
  101. *Graphic fill*
  102. Code
  103. ~~~~
  104. .. code-block:: css
  105. :linenos:
  106. * {
  107. fill: url("colorblocks1.png");
  108. fill-mime: 'image/png';
  109. }
  110. Details
  111. ~~~~~~~
  112. This style fills the polygon with a tiled graphic.
  113. The graphic is selected providing a url for the fill, which in this case is meant to the relative to the ``styles`` directory contained within the data directory (an absolute path could have been provided, as well as a internet reference). **Line 3** specifies that the image itself is a png (by default the code assumes jpegs are used and will fail to parse the file unless we specify its mime type).
  114. The size of the image is not specified, meaning the native size is going to be used. In case a rescale is desired, the "fill-size" attribute can be used to force a different size.
  115. .. figure:: ../../sld/cookbook/images/colorblocks.png
  116. :align: center
  117. *Graphic used for fill*
  118. Hatching fill
  119. -------------
  120. This example fills the polygons with a hatching pattern.
  121. .. figure:: ../../sld/cookbook/images/polygon_hatchingfill.png
  122. :align: center
  123. *Hatching fill*
  124. Code
  125. ~~~~
  126. .. code-block:: scss
  127. :linenos:
  128. * {
  129. fill: symbol("shape://times");
  130. :fill {
  131. size: 16;
  132. stroke: #990099;
  133. stroke-width: 1px;
  134. }
  135. }
  136. Details
  137. ~~~~~~~
  138. In this example the fill is specified to be the "shape://times" symbol, which is going to be tiled creating a cross-hatch effect.
  139. The details of the hatch are specified at **line 3***, where the pseudo-selector ":fill" is used to match the contents of the fill, and specify that we want a symbol large 16 pixels (the larger the symbol, the coarser the cross hatch will be), and painted with a 1 pixel wide purple stroke.
  140. .. _css_cookbook_polygons_polygonwithdefaultlabel:
  141. Polygon with default label
  142. --------------------------
  143. This example shows a text label on the polygon. In the absence of any other customization, this is how a label will be displayed.
  144. .. figure:: ../../sld/cookbook/images/polygon_polygonwithdefaultlabel.png
  145. :align: center
  146. *Polygon with default label*
  147. Code
  148. ~~~~
  149. .. code-block:: css
  150. :linenos:
  151. * {
  152. fill: #40FF40;
  153. stroke: white;
  154. stroke-width: 2;
  155. label: [name];
  156. font-fill: black;
  157. }
  158. Details
  159. ~~~~~~~
  160. The single rule in the CSS applies to all feature: first it fills all polygons a light green with white outline, and then applies the "name" attribute as the label, using the default font (Times), with black color and default font size (10 px).
  161. Label halo
  162. ----------
  163. This example alters the look of the :ref:`css_cookbook_polygons_polygonwithdefaultlabel` by adding a white halo to the label.
  164. .. figure:: ../../sld/cookbook/images/polygon_labelhalo.png
  165. :align: center
  166. *Label halo*
  167. Code
  168. ~~~~
  169. .. code-block:: css
  170. :linenos:
  171. * {
  172. fill: #40FF40;
  173. stroke: white;
  174. stroke-width: 2;
  175. label: [name];
  176. font-fill: black;
  177. halo-color: white;
  178. halo-radius: 3;
  179. }
  180. Details
  181. ~~~~~~~
  182. This example builds on :ref:`css_cookbook_polygons_polygonwithdefaultlabel`, with the addition of a halo around the labels on **lines 7-8**. A halo creates a color buffer around the label to improve label legibility. **Line 9** sets the radius of the halo, extending the halo 3 pixels around the edge of the label, and **line 8** sets the color of the halo to white. 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.
  183. .. _css_cookbook_polygons_polygonwithstyledlabel:
  184. Polygon with styled label
  185. -------------------------
  186. This example improves the label style from the :ref:`css_cookbook_polygons_polygonwithdefaultlabel` example by centering the label on the polygon, specifying a different font name and size, and setting additional label placement optimizations.
  187. .. figure:: ../../sld/cookbook/images/polygon_polygonwithstyledlabel.png
  188. :align: center
  189. *Polygon with styled label*
  190. Code
  191. ~~~~
  192. .. code-block:: css
  193. :linenos:
  194. * {
  195. fill: #40FF40;
  196. stroke: white;
  197. stroke-width: 2;
  198. label: [name];
  199. font-family: Arial;
  200. font-size: 11px;
  201. font-style: normal;
  202. font-weight: bold;
  203. font-fill: black;
  204. label-anchor: 0.5 0.5;
  205. label-auto-wrap: 60;
  206. label-max-displacement: 150;
  207. }
  208. Details
  209. ~~~~~~~
  210. This example is similar to the :ref:`css_cookbook_polygons_polygonwithdefaultlabel` example, with additional styling options for the labels.
  211. The font is setup to be Arial, 11 pixels, "normal" (as opposed to "italic") and bold.
  212. The "label-anchor" affects where the label is placed relative to the centroid of the polygon, centering the label by positioning it 50% (or 0.5) of the way horizontally along the centroid of the polygon, as well as vertically in exactly the same way.
  213. Finally, there are two added touches for label placement optimization: The "label-auto-wrap" attribute ensures that long labels are split across multiple lines by setting line wrapping on the labels to 60 pixels, whilst the "label-max-displacement" 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.
  214. Attribute-based polygon
  215. -----------------------
  216. This example styles the polygons differently based on the "pop" (Population) attribute.
  217. .. figure:: ../../sld/cookbook/images/polygon_attributebasedpolygon.png
  218. :align: center
  219. *Attribute-based polygon*
  220. Code
  221. ~~~~
  222. .. code-block:: css
  223. :linenos:
  224. [parseLong(pop) < 200000] {
  225. fill: #66FF66;
  226. }
  227. [parseLong(pop) >= 200000] [parseLong(pop) < 500000] {
  228. fill: #33CC33;
  229. }
  230. [parseLong(pop) >= 500000] {
  231. fill: #009900;
  232. }
  233. Details
  234. ~~~~~~~
  235. .. note:: Refer to the :ref:`css_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:`css_cookbook_polygons_polygonwithstyledlabel` to see which attributes correspond to which polygons.
  236. 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.
  237. The three rules are designed as follows:
  238. .. list-table::
  239. :widths: 20 20 30 30
  240. * - **Rule order**
  241. - **Rule name**
  242. - **Population** ("pop")
  243. - **Color**
  244. * - 1
  245. - SmallPop
  246. - Less than 200,000
  247. - ``#66FF66``
  248. * - 2
  249. - MediumPop
  250. - 200,000 to 500,000
  251. - ``#33CC33``
  252. * - 3
  253. - LargePop
  254. - Greater than 500,000
  255. - ``#009900``
  256. The order of the rules does not matter in this case, since each shape is only rendered by a single rule.
  257. The first rule fills light green all polygons whose "pop" attribute is below 200,000, the second paints medium green all poygons whose "pop" attribute is between 200,000 and 500,000, while the third rule paints dark green the remaining polygons.
  258. What's interesting in the filters is the use of the "parseLong" filter function: this function is necessary because the "pop" attribute is a string, leaving it as is we would have a string comparison, whilst the function turns it into a number, ensuring proper numeric comparisons instead.
  259. Zoom-based polygon
  260. ------------------
  261. This example alters the style of the polygon at different zoom levels.
  262. .. figure:: ../../sld/cookbook/images/polygon_zoombasedpolygonlarge.png
  263. :align: center
  264. *Zoom-based polygon: Zoomed in*
  265. .. figure:: ../../sld/cookbook/images/polygon_zoombasedpolygonmedium.png
  266. :align: center
  267. *Zoom-based polygon: Partially zoomed*
  268. .. figure:: ../../sld/cookbook/images/polygon_zoombasedpolygonsmall.png
  269. :align: center
  270. *Zoom-based polygon: Zoomed out*
  271. Code
  272. ~~~~
  273. .. code-block:: css
  274. :linenos:
  275. * {
  276. fill: #0000CC;
  277. stroke: black;
  278. }
  279. [@sd < 100M] {
  280. stroke-width: 7;
  281. label: [name];
  282. label-anchor: 0.5 0.5;
  283. font-fill: white;
  284. font-family: Arial;
  285. font-size: 14;
  286. font-weight: bold;
  287. }
  288. [@sd > 100M] [@sd < 200M] {
  289. stroke-width: 4;
  290. }
  291. [@sd > 200M] {
  292. stroke-width: 1;
  293. }
  294. Details
  295. ~~~~~~~
  296. 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.
  297. 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.
  298. .. note:: Determining the appropriate scale denominators (zoom levels) to use is beyond the scope of this example.
  299. This style contains three rules, defined as follows:
  300. .. list-table::
  301. :widths: 15 15 40 15 15
  302. * - **Rule order**
  303. - **Rule name**
  304. - **Scale denominator**
  305. - **Stroke width**
  306. - **Label display?**
  307. * - 1
  308. - Large
  309. - 1:100,000,000 or less
  310. - 7
  311. - Yes
  312. * - 2
  313. - Medium
  314. - 1:100,000,000 to 1:200,000,000
  315. - 4
  316. - No
  317. * - 3
  318. - Small
  319. - Greater than 1:200,000,000
  320. - 2
  321. - No
  322. The first rule (**lines 1-4**) defines the attributes that are not scale dependent: dark blue fill, black outline.
  323. The second (**lines 6-14**) rule provides specific overrides for the higher zoom levels, asking for a large stroke (7 pixels) and a label, which is only visible at this zoom level. The label is white, bold, Arial 14 pixels, its contents are coming form the "name" attribute.
  324. The third rule (**lines 16-18**) specifies a stroke width of 4 pixels for medium zoom levels, whilst for low zoom levels the stroke width is set to 1 pixel by the last rule (**lines 20-22**).
  325. 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.