polygons.rst 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. .. _sld_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. .. 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.
  6. .. _sld_cookbook_polygons_attributes:
  7. Example polygons layer
  8. ----------------------
  9. The :download:`polygons layer <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.
  10. .. list-table::
  11. :widths: 30 40 30
  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/sld_cookbook_polygon.zip>`
  40. .. _sld_cookbook_polygons_simplepolygon:
  41. Simple polygon
  42. --------------
  43. This example shows a polygon filled in blue.
  44. .. figure:: images/polygon_simplepolygon.png
  45. :align: center
  46. *Simple polygon*
  47. Code
  48. ~~~~
  49. :download:`View and download the full "Simple polygon" SLD <artifacts/polygon_simplepolygon.sld>`
  50. .. code-block:: xml
  51. :linenos:
  52. <FeatureTypeStyle>
  53. <Rule>
  54. <PolygonSymbolizer>
  55. <Fill>
  56. <CssParameter name="fill">#000080</CssParameter>
  57. </Fill>
  58. </PolygonSymbolizer>
  59. </Rule>
  60. </FeatureTypeStyle>
  61. Details
  62. ~~~~~~~
  63. There is one ``<Rule>`` in one ``<FeatureTypeStyle>`` 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 ``<PolygonSymbolizer>`` (**lines 3-7**). **Line 5** specifies dark blue (``#000080``) as the polygon's fill color.
  64. .. 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.
  65. .. _sld_cookbook_polygons_simplepolygonwithstroke:
  66. Simple polygon with stroke
  67. --------------------------
  68. This example adds a 2 pixel white stroke to the :ref:`sld_cookbook_polygons_simplepolygon` example.
  69. .. figure:: images/polygon_simplepolygonwithstroke.png
  70. :align: center
  71. *Simple polygon with stroke*
  72. Code
  73. ~~~~
  74. :download:`View and download the full "Simple polygon with stroke" SLD <artifacts/polygon_simplepolygonwithstroke.sld>`
  75. .. code-block:: xml
  76. :linenos:
  77. <FeatureTypeStyle>
  78. <Rule>
  79. <PolygonSymbolizer>
  80. <Fill>
  81. <CssParameter name="fill">#000080</CssParameter>
  82. </Fill>
  83. <Stroke>
  84. <CssParameter name="stroke">#FFFFFF</CssParameter>
  85. <CssParameter name="stroke-width">2</CssParameter>
  86. </Stroke>
  87. </PolygonSymbolizer>
  88. </Rule>
  89. </FeatureTypeStyle>
  90. Details
  91. ~~~~~~~
  92. This example is similar to the :ref:`sld_cookbook_polygons_simplepolygon` example above, with the addition of the ``<Stroke>`` tag (**lines 7-10**). **Line 8** sets the color of stroke to white (``#FFFFFF``) and **line 9** sets the width of the stroke to 2 pixels.
  93. Transparent polygon
  94. -------------------
  95. This example builds on the :ref:`sld_cookbook_polygons_simplepolygonwithstroke` example and makes the fill partially transparent by setting the opacity to 50%.
  96. .. figure:: images/polygon_transparentpolygon.png
  97. :align: center
  98. *Transparent polygon*
  99. Code
  100. ~~~~
  101. :download:`View and download the full "Transparent polygon" SLD <artifacts/polygon_transparentpolygon.sld>`
  102. .. code-block:: xml
  103. :linenos:
  104. <FeatureTypeStyle>
  105. <Rule>
  106. <PolygonSymbolizer>
  107. <Fill>
  108. <CssParameter name="fill">#000080</CssParameter>
  109. <CssParameter name="fill-opacity">0.5</CssParameter>
  110. </Fill>
  111. <Stroke>
  112. <CssParameter name="stroke">#FFFFFF</CssParameter>
  113. <CssParameter name="stroke-width">2</CssParameter>
  114. </Stroke>
  115. </PolygonSymbolizer>
  116. </Rule>
  117. </FeatureTypeStyle>
  118. Details
  119. ~~~~~~~
  120. This example is similar to the :ref:`sld_cookbook_polygons_simplepolygonwithstroke` example, save for defining the fill's opacity in **line 6**. 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.
  121. .. _sld_cookbook_polygons_offset:
  122. Offset inner lines
  123. ------------------
  124. Shows how to draw inner buffer lines inside a polygon.
  125. .. figure:: images/polygon_offset.png
  126. :align: center
  127. *Offset buffer*
  128. Code
  129. ~~~~
  130. :download:`View and download the full "Inner offset lines" SLD <artifacts/polygon_offset.sld>`
  131. .. code-block:: xml
  132. :linenos:
  133. <FeatureTypeStyle>
  134. <Rule>
  135. <PolygonSymbolizer>
  136. <Stroke>
  137. <CssParameter name="stroke">#000000</CssParameter>
  138. <CssParameter name="stroke-width">2</CssParameter>
  139. </Stroke>
  140. </PolygonSymbolizer>
  141. <LineSymbolizer>
  142. <Stroke>
  143. <CssParameter name="stroke">#AAAAAA</CssParameter>
  144. <CssParameter name="stroke-width">3</CssParameter>
  145. </Stroke>
  146. <PerpendicularOffset>-2</PerpendicularOffset>
  147. </LineSymbolizer>
  148. </Rule>
  149. </FeatureTypeStyle>
  150. Details
  151. ~~~~~~~
  152. This example is similar to the :ref:`sld_cookbook_polygons_simplepolygonwithstroke` example, save
  153. for defining adding a ``<LineSymbolizer>>`` at **line 9**, where a light gray (**line 11**)
  154. 3 pixels wide (**line 12**) line is drawn as a inner buffer inside the polygon.
  155. **Line 14** controls the buffering distance, setting a inner buffer of 2 pixels.
  156. .. _sld_cookbook_polygons_graphicfill:
  157. Graphic fill
  158. ------------
  159. This example fills the polygons with a tiled graphic.
  160. .. figure:: images/polygon_graphicfill.png
  161. :align: center
  162. *Graphic fill*
  163. Code
  164. ~~~~
  165. :download:`View and download the full "Graphic fill" SLD <artifacts/polygon_graphicfill.sld>`
  166. .. code-block:: xml
  167. :linenos:
  168. <FeatureTypeStyle>
  169. <Rule>
  170. <PolygonSymbolizer>
  171. <Fill>
  172. <GraphicFill>
  173. <Graphic>
  174. <ExternalGraphic>
  175. <OnlineResource
  176. xlink:type="simple"
  177. xlink:href="colorblocks.png" />
  178. <Format>image/png</Format>
  179. </ExternalGraphic>
  180. <Size>93</Size>
  181. </Graphic>
  182. </GraphicFill>
  183. </Fill>
  184. </PolygonSymbolizer>
  185. </Rule>
  186. </FeatureTypeStyle>
  187. Details
  188. ~~~~~~~
  189. This style fills the polygon with a tiled graphic. This is known as an ``<ExternalGraphic>`` in SLD, to distinguish it from commonly-used shapes such as squares and circles that are "internal" to the renderer. **Lines 7-12** specify details for the graphic, with **line 10** setting the path and file name of the graphic and **line 11** 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 11** because this graphic is contained in the same directory as the SLD. **Line 13** 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.
  190. .. figure:: images/colorblocks.png
  191. :align: center
  192. *Graphic used for fill*
  193. Hatching fill
  194. -------------
  195. This example fills the polygons with a hatching pattern.
  196. .. note:: This example leverages an SLD extension in GeoServer. Hatching is not part of the standard SLD 1.0 specification.
  197. .. figure:: images/polygon_hatchingfill.png
  198. :align: center
  199. *Hatching fill*
  200. Code
  201. ~~~~
  202. :download:`View and download the full "Hatching fill" SLD <artifacts/polygon_hatchingfill.sld>`
  203. .. code-block:: xml
  204. :linenos:
  205. <FeatureTypeStyle>
  206. <Rule>
  207. <PolygonSymbolizer>
  208. <Fill>
  209. <GraphicFill>
  210. <Graphic>
  211. <Mark>
  212. <WellKnownName>shape://times</WellKnownName>
  213. <Stroke>
  214. <CssParameter name="stroke">#990099</CssParameter>
  215. <CssParameter name="stroke-width">1</CssParameter>
  216. </Stroke>
  217. </Mark>
  218. <Size>16</Size>
  219. </Graphic>
  220. </GraphicFill>
  221. </Fill>
  222. </PolygonSymbolizer>
  223. </Rule>
  224. </FeatureTypeStyle>
  225. Details
  226. ~~~~~~~
  227. In this example, there is a ``<GraphicFill>`` tag as in the :ref:`sld_cookbook_polygons_graphicfill` example, but a ``<Mark>`` (**lines 7-13**) is used instead of an ``<ExternalGraphic>``. **Line 8** specifies a "times" symbol (an "x") be tiled throughout the polygon. **Line 10** sets the color to purple (``#990099``), **line 11** sets the width of the hatches to 1 pixel, and **line 14** sets the size of the tile to 16 pixels. Because hatch tiles are always square, the ``<Size>`` sets both the width and the height.
  228. .. _sld_cookbook_polygons_polygonwithdefaultlabel:
  229. Polygon with default label
  230. --------------------------
  231. This example shows a text label on the polygon. In the absence of any other customization, this is how a label will be displayed.
  232. .. figure:: images/polygon_polygonwithdefaultlabel.png
  233. :align: center
  234. *Polygon with default label*
  235. Code
  236. ~~~~
  237. :download:`View and download the full "Polygon with default label" SLD <artifacts/polygon_polygonwithdefaultlabel.sld>`
  238. .. code-block:: xml
  239. :linenos:
  240. <FeatureTypeStyle>
  241. <Rule>
  242. <PolygonSymbolizer>
  243. <Fill>
  244. <CssParameter name="fill">#40FF40</CssParameter>
  245. </Fill>
  246. <Stroke>
  247. <CssParameter name="stroke">#FFFFFF</CssParameter>
  248. <CssParameter name="stroke-width">2</CssParameter>
  249. </Stroke>
  250. </PolygonSymbolizer>
  251. <TextSymbolizer>
  252. <Label>
  253. <ogc:PropertyName>name</ogc:PropertyName>
  254. </Label>
  255. </TextSymbolizer>
  256. </Rule>
  257. </FeatureTypeStyle>
  258. Details
  259. ~~~~~~~
  260. In this example there is a ``<PolygonSymbolizer>`` and a ``<TextSymbolizer>``. **Lines 3-11** comprise the ``<PolygonSymbolizer>``. The fill of the polygon is set on **line 5** 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 ``<TextSymbolizer>`` on **lines 12-16**, with **line 14** determining what text to display, in this case the value of the "name" attribute. (Refer to the attribute table in the :ref:`sld_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.
  261. Label halo
  262. ----------
  263. This example alters the look of the :ref:`sld_cookbook_polygons_polygonwithdefaultlabel` by adding a white halo to the label.
  264. .. figure:: images/polygon_labelhalo.png
  265. :align: center
  266. *Label halo*
  267. Code
  268. ~~~~
  269. :download:`View and download the full "Label halo" SLD <artifacts/polygon_labelhalo.sld>`
  270. .. code-block:: xml
  271. :linenos:
  272. <FeatureTypeStyle>
  273. <Rule>
  274. <PolygonSymbolizer>
  275. <Fill>
  276. <CssParameter name="fill">#40FF40</CssParameter>
  277. </Fill>
  278. <Stroke>
  279. <CssParameter name="stroke">#FFFFFF</CssParameter>
  280. <CssParameter name="stroke-width">2</CssParameter>
  281. </Stroke>
  282. </PolygonSymbolizer>
  283. <TextSymbolizer>
  284. <Label>
  285. <ogc:PropertyName>name</ogc:PropertyName>
  286. </Label>
  287. <Halo>
  288. <Radius>3</Radius>
  289. <Fill>
  290. <CssParameter name="fill">#FFFFFF</CssParameter>
  291. </Fill>
  292. </Halo>
  293. </TextSymbolizer>
  294. </Rule>
  295. </FeatureTypeStyle>
  296. Details
  297. ~~~~~~~
  298. This example is similar to the :ref:`sld_cookbook_polygons_polygonwithdefaultlabel`, with the addition of a halo around the labels on **lines 16-21**. A halo creates a color buffer around the label to improve label legibility. **Line 17** sets the radius of the halo, extending the halo 3 pixels around the edge of the label, and **line 19** 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.
  299. .. _sld_cookbook_polygons_polygonwithstyledlabel:
  300. Polygon with styled label
  301. -------------------------
  302. This example improves the label style from the :ref:`sld_cookbook_polygons_polygonwithdefaultlabel` example by centering the label on the polygon, specifying a different font name and size, and setting additional label placement optimizations.
  303. .. note:: The label placement optimizations discussed below (the ``<VendorOption>`` tags) are SLD extensions that are custom to GeoServer. They are not part of the SLD 1.0 specification.
  304. .. figure:: images/polygon_polygonwithstyledlabel.png
  305. :align: center
  306. *Polygon with styled label*
  307. Code
  308. ~~~~
  309. :download:`View and download the full "Polygon with styled label" SLD <artifacts/polygon_polygonwithstyledlabel.sld>`
  310. .. code-block:: xml
  311. :linenos:
  312. <FeatureTypeStyle>
  313. <Rule>
  314. <PolygonSymbolizer>
  315. <Fill>
  316. <CssParameter name="fill">#40FF40</CssParameter>
  317. </Fill>
  318. <Stroke>
  319. <CssParameter name="stroke">#FFFFFF</CssParameter>
  320. <CssParameter name="stroke-width">2</CssParameter>
  321. </Stroke>
  322. </PolygonSymbolizer>
  323. <TextSymbolizer>
  324. <Label>
  325. <ogc:PropertyName>name</ogc:PropertyName>
  326. </Label>
  327. <Font>
  328. <CssParameter name="font-family">Arial</CssParameter>
  329. <CssParameter name="font-size">11</CssParameter>
  330. <CssParameter name="font-style">normal</CssParameter>
  331. <CssParameter name="font-weight">bold</CssParameter>
  332. </Font>
  333. <LabelPlacement>
  334. <PointPlacement>
  335. <AnchorPoint>
  336. <AnchorPointX>0.5</AnchorPointX>
  337. <AnchorPointY>0.5</AnchorPointY>
  338. </AnchorPoint>
  339. </PointPlacement>
  340. </LabelPlacement>
  341. <Fill>
  342. <CssParameter name="fill">#000000</CssParameter>
  343. </Fill>
  344. <VendorOption name="autoWrap">60</VendorOption>
  345. <VendorOption name="maxDisplacement">150</VendorOption>
  346. </TextSymbolizer>
  347. </Rule>
  348. </FeatureTypeStyle>
  349. Details
  350. ~~~~~~~
  351. This example is similar to the :ref:`sld_cookbook_polygons_polygonwithdefaultlabel` example, with additional styling options within the ``<TextSymbolizer>`` on lines **12-35**. **Lines 16-21** set the font styling. **Line 17** sets the font family to be Arial, **line 18** sets the font size to 11 pixels, **line 19** sets the font style to "normal" (as opposed to "italic" or "oblique"), and **line 20** sets the font weight to "bold" (as opposed to "normal").
  352. The ``<LabelPlacement>`` tag on **lines 22-29** affects where the label is placed relative to the centroid of the polygon. **Line 21** centers the label by positioning it 50% (or 0.5) of the way horizontally along the centroid of the polygon. **Line 22** centers the label vertically in exactly the same way.
  353. Finally, there are two added touches for label placement optimization: **line 33** ensures that long labels are split across multiple lines by setting line wrapping on the labels to 60 pixels, and **line 34** 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."
  354. Attribute-based polygon
  355. -----------------------
  356. This example styles the polygons differently based on the "pop" (Population) attribute.
  357. .. figure:: images/polygon_attributebasedpolygon.png
  358. :align: center
  359. *Attribute-based polygon*
  360. Code
  361. ~~~~
  362. :download:`View and download the full "Attribute-based polygon" SLD <artifacts/polygon_attributebasedpolygon.sld>`
  363. .. code-block:: xml
  364. :linenos:
  365. <FeatureTypeStyle>
  366. <Rule>
  367. <Name>SmallPop</Name>
  368. <Title>Less Than 200,000</Title>
  369. <ogc:Filter>
  370. <ogc:PropertyIsLessThan>
  371. <ogc:PropertyName>pop</ogc:PropertyName>
  372. <ogc:Literal>200000</ogc:Literal>
  373. </ogc:PropertyIsLessThan>
  374. </ogc:Filter>
  375. <PolygonSymbolizer>
  376. <Fill>
  377. <CssParameter name="fill">#66FF66</CssParameter>
  378. </Fill>
  379. </PolygonSymbolizer>
  380. </Rule>
  381. <Rule>
  382. <Name>MediumPop</Name>
  383. <Title>200,000 to 500,000</Title>
  384. <ogc:Filter>
  385. <ogc:And>
  386. <ogc:PropertyIsGreaterThanOrEqualTo>
  387. <ogc:PropertyName>pop</ogc:PropertyName>
  388. <ogc:Literal>200000</ogc:Literal>
  389. </ogc:PropertyIsGreaterThanOrEqualTo>
  390. <ogc:PropertyIsLessThan>
  391. <ogc:PropertyName>pop</ogc:PropertyName>
  392. <ogc:Literal>500000</ogc:Literal>
  393. </ogc:PropertyIsLessThan>
  394. </ogc:And>
  395. </ogc:Filter>
  396. <PolygonSymbolizer>
  397. <Fill>
  398. <CssParameter name="fill">#33CC33</CssParameter>
  399. </Fill>
  400. </PolygonSymbolizer>
  401. </Rule>
  402. <Rule>
  403. <Name>LargePop</Name>
  404. <Title>Greater Than 500,000</Title>
  405. <ogc:Filter>
  406. <ogc:PropertyIsGreaterThan>
  407. <ogc:PropertyName>pop</ogc:PropertyName>
  408. <ogc:Literal>500000</ogc:Literal>
  409. </ogc:PropertyIsGreaterThan>
  410. </ogc:Filter>
  411. <PolygonSymbolizer>
  412. <Fill>
  413. <CssParameter name="fill">#009900</CssParameter>
  414. </Fill>
  415. </PolygonSymbolizer>
  416. </Rule>
  417. </FeatureTypeStyle>
  418. Details
  419. ~~~~~~~
  420. .. note:: Refer to the :ref:`sld_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:`sld_cookbook_polygons_polygonwithstyledlabel` to see which attributes correspond to which polygons.
  421. 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.
  422. The three rules are designed as follows:
  423. .. list-table::
  424. :widths: 20 20 30 30
  425. * - **Rule order**
  426. - **Rule name**
  427. - **Population** ("pop")
  428. - **Color**
  429. * - 1
  430. - SmallPop
  431. - Less than 200,000
  432. - ``#66FF66``
  433. * - 2
  434. - MediumPop
  435. - 200,000 to 500,000
  436. - ``#33CC33``
  437. * - 3
  438. - LargePop
  439. - Greater than 500,000
  440. - ``#009900``
  441. The order of the rules does not matter in this case, since each shape is only rendered by a single rule.
  442. The first rule, on **lines 2-16**, specifies the styling of polygons whose population attribute is less than 200,000. **Lines 5-10** set this filter, with **lines 6-9** setting the "less than" filter, **line 7** denoting the attribute ("pop"), and **line 8** the value of 200,000. The color of the polygon fill is set to a light green (``#66FF66``) on **line 13**.
  443. The second rule, on **lines 17-37**, 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 **lines 20-31**. This filter is longer than in the first rule because two criteria need to be specified instead of one: a "greater than or equal to" and a "less than" filter. Notice the ``And`` on **line 21** and **line 30**. This 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 34**.
  444. The third rule, on **lines 38-52**, specifies a style for polygons whose population attribute is greater than or equal to 500,000. The filter is set on **lines 41-46**. The color of the polygon fill is the only other difference in this rule, which is set to a dark green (``#009900``) on **line 49**.
  445. Zoom-based polygon
  446. ------------------
  447. This example alters the style of the polygon at different zoom levels.
  448. .. figure:: images/polygon_zoombasedpolygonlarge.png
  449. :align: center
  450. *Zoom-based polygon: Zoomed in*
  451. .. figure:: images/polygon_zoombasedpolygonmedium.png
  452. :align: center
  453. *Zoom-based polygon: Partially zoomed*
  454. .. figure:: images/polygon_zoombasedpolygonsmall.png
  455. :align: center
  456. *Zoom-based polygon: Zoomed out*
  457. Code
  458. ~~~~
  459. :download:`View and download the full "Zoom-based polygon" SLD <artifacts/polygon_zoombasedpolygon.sld>`
  460. .. code-block:: xml
  461. :linenos:
  462. <FeatureTypeStyle>
  463. <Rule>
  464. <Name>Large</Name>
  465. <MaxScaleDenominator>100000000</MaxScaleDenominator>
  466. <PolygonSymbolizer>
  467. <Fill>
  468. <CssParameter name="fill">#0000CC</CssParameter>
  469. </Fill>
  470. <Stroke>
  471. <CssParameter name="stroke">#000000</CssParameter>
  472. <CssParameter name="stroke-width">7</CssParameter>
  473. </Stroke>
  474. </PolygonSymbolizer>
  475. <TextSymbolizer>
  476. <Label>
  477. <ogc:PropertyName>name</ogc:PropertyName>
  478. </Label>
  479. <Font>
  480. <CssParameter name="font-family">Arial</CssParameter>
  481. <CssParameter name="font-size">14</CssParameter>
  482. <CssParameter name="font-style">normal</CssParameter>
  483. <CssParameter name="font-weight">bold</CssParameter>
  484. </Font>
  485. <LabelPlacement>
  486. <PointPlacement>
  487. <AnchorPoint>
  488. <AnchorPointX>0.5</AnchorPointX>
  489. <AnchorPointY>0.5</AnchorPointY>
  490. </AnchorPoint>
  491. </PointPlacement>
  492. </LabelPlacement>
  493. <Fill>
  494. <CssParameter name="fill">#FFFFFF</CssParameter>
  495. </Fill>
  496. </TextSymbolizer>
  497. </Rule>
  498. <Rule>
  499. <Name>Medium</Name>
  500. <MinScaleDenominator>100000000</MinScaleDenominator>
  501. <MaxScaleDenominator>200000000</MaxScaleDenominator>
  502. <PolygonSymbolizer>
  503. <Fill>
  504. <CssParameter name="fill">#0000CC</CssParameter>
  505. </Fill>
  506. <Stroke>
  507. <CssParameter name="stroke">#000000</CssParameter>
  508. <CssParameter name="stroke-width">4</CssParameter>
  509. </Stroke>
  510. </PolygonSymbolizer>
  511. </Rule>
  512. <Rule>
  513. <Name>Small</Name>
  514. <MinScaleDenominator>200000000</MinScaleDenominator>
  515. <PolygonSymbolizer>
  516. <Fill>
  517. <CssParameter name="fill">#0000CC</CssParameter>
  518. </Fill>
  519. <Stroke>
  520. <CssParameter name="stroke">#000000</CssParameter>
  521. <CssParameter name="stroke-width">1</CssParameter>
  522. </Stroke>
  523. </PolygonSymbolizer>
  524. </Rule>
  525. </FeatureTypeStyle>
  526. Details
  527. ~~~~~~~
  528. 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.
  529. 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.
  530. .. note:: Determining the appropriate scale denominators (zoom levels) to use is beyond the scope of this example.
  531. This style contains three rules, defined as follows:
  532. .. list-table::
  533. :widths: 15 15 40 15 15
  534. * - **Rule order**
  535. - **Rule name**
  536. - **Scale denominator**
  537. - **Stroke width**
  538. - **Label display?**
  539. * - 1
  540. - Large
  541. - 1:100,000,000 or less
  542. - 7
  543. - Yes
  544. * - 2
  545. - Medium
  546. - 1:100,000,000 to 1:200,000,000
  547. - 4
  548. - No
  549. * - 3
  550. - Small
  551. - Greater than 1:200,000,000
  552. - 2
  553. - No
  554. The first rule, on **lines 2-36**, is for the smallest scale denominator, corresponding to when the view is "zoomed in". The scale rule is set on **line 40** such that the rule will apply only where the scale denominator is 100,000,000 or less. **Line 7** 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:`sld_cookbook_polygons_polygonwithdefaultlabel` or :ref:`sld_cookbook_polygons_polygonwithstyledlabel` examples, the rule also contains a ``<TextSymbolizer>`` at **lines 14-35** for drawing a text label on top of the polygon. **Lines 19-22** 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 ``<AnchorPointX>`` and ``<AnchorPointY>`` to both be 0.5 (or 50%) on **lines 27-28**. Finally, the color of the font is set to white (``#FFFFFF``) in **line 33**.
  555. The second rule, on **lines 37-50**, is for the intermediate scale denominators, corresponding to when the view is "partially zoomed". The scale rules on **lines 39-40** set the rule such that it will apply to any map with a scale denominator between 100,000,000 and 200,000,000. (The ``<MinScaleDenominator>`` is inclusive and the ``<MaxScaleDenominator>`` 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 47** and a ``<TextSymbolizer>`` is not present so that no labels will be displayed.
  556. The third rule, on **lines 51-63**, is for the largest scale denominator, corresponding to when the map is "zoomed out". The scale rule is set on **line 53** 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 60**, and the absence of a ``<TextSymbolizer>`` so that no labels will be displayed.
  557. 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.