scalezoom.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. .. _ysld_reference_scalezoom:
  2. Scale and zoom
  3. ==============
  4. It is common for different :ref:`rules <ysld_reference_rules>` to be applied at different zoom levels on a web map.
  5. For example, on a roads layer, you would not want to display every single road when viewing the whole world. Or perhaps you may wish to styles the same features differently depending on the zoom level. For example: a cities layer styled using points at low zoom levels (when "zoomed out") and with polygon borders at higher zoom levels ("zoomed in").
  6. .. todo:: ADD FIGURE
  7. YSLD allows rules to be applied depending on the scale or zoom level. You can specify by scale, or you can define zoom levels in terms of scales and specify by zoom level.
  8. .. warning:: Be aware that scales for a layer (where a style is applied) may interact differently when the layer is contained in a map, if the map has a different coordinate reference system from the layer.
  9. Scale syntax
  10. ------------
  11. The syntax for using a scale conditional parameter in a rule is::
  12. rules:
  13. - ...
  14. scale: [<min>,<max>]
  15. ...
  16. where:
  17. .. list-table::
  18. :class: non-responsive
  19. :header-rows: 1
  20. :stub-columns: 1
  21. :widths: 20 10 50 20
  22. * - Attribute
  23. - Required?
  24. - Description
  25. - Default value
  26. * - ``min``
  27. - Yes
  28. - The minimum scale (inclusive) for which the rule will be applied. Value is a number, either decimal or integer.
  29. - N/A
  30. * - ``max``
  31. - Yes
  32. - The maximum scale (exclusive) for which the rule will be applied. Value is a number, either decimal or integer.
  33. - N/A
  34. .. note:: It is not possible to use an expression for any of these values.
  35. Use the literal strings ``min`` and ``max`` to denote where there are no lower or upper scale boundaries. For example, to denote that the scale is anything less than some ``<max>`` value::
  36. scale: [min,<max>]
  37. To denote that the scale is anything greater than or equal to some ``<min>`` value::
  38. scale: [<min>,max]
  39. .. note:: In the above examples, ``min`` and ``max`` are always literals, entered exactly like that, while ``<min>`` and ``<max>`` would be replaced by actual scalar values.
  40. If the scale parameter is omitted entirely, then the rule will apply at all scales.
  41. Scale examples
  42. --------------
  43. Three rules, all applicable at different scales::
  44. rule:
  45. - name: large_scale
  46. scale: [min,100000]
  47. symbolizers:
  48. - line:
  49. stroke-width: 3
  50. stroke-color: '#0165CD'
  51. - name: medium_scale
  52. scale: [100000,200000]
  53. symbolizers:
  54. - line:
  55. stroke-width: 2
  56. stroke-color: '#0165CD'
  57. - name: small_scale
  58. scale: [200000,max]
  59. symbolizers:
  60. - line:
  61. stroke-width: 1
  62. stroke-color: '#0165CD'
  63. This example will display lines with:
  64. * A stroke width of 3 at scales less than 100,000 (``large_scale``)
  65. * A stroke width of 2 at scales between 100,000 and 200,000 (``medium_scale``)
  66. * A stroke width of 1 at scales greater than 200,000 (``small_scale``)
  67. Given the rules above, the following arbitrary sample scales would map to the rules as follows:
  68. .. list-table::
  69. :header-rows: 1
  70. :stub-columns: 1
  71. * - Scale
  72. - Rule
  73. * - ``50000``
  74. - ``large_scale``
  75. * - ``100000``
  76. - ``medium_scale``
  77. * - ``150000``
  78. - ``medium_scale``
  79. * - ``200000``
  80. - ``small_scale``
  81. * - ``300000``
  82. - ``small_scale``
  83. Note the edge cases, since the ``min`` value is inclusive and the ``max`` value is exclusive.
  84. Scientific notation for scales
  85. ------------------------------
  86. To make comprehension easier and to lessen the chance of errors, scale values can be expressed in scientific notation.
  87. So a scale of ``500000000``, which is equal to 5 × 10^8 (a 5 with eight zeros), can be replaced by ``5e8``.
  88. Relationship between scale and zoom
  89. -----------------------------------
  90. When working with web maps, often it is more convenient to talk about zoom levels instead of scales. The relationship between zoom and scale is context dependent.
  91. For example, for EPSG:4326 with world boundaries, zoom level 0 (completely zoomed out) corresponds to a scale of approximately 279,541,000 with each subsequent zoom level having half the scale value. For EPSG:3857 (Web Mercator) with world boundaries, zoom level 0 corresponds to a scale of approximately 559,082,000, again with each subsequent zoom level having half the scale value.
  92. But since zoom levels are discrete (0, 1, 2, etc.) and scale levels are continuous, it's actually a range of scale levels that corresponds to a given zoom level.
  93. For example, if you have a situation where a zoom level 0 corresponds to a scale of 1,000,000 (and each subsequent zoom level is half that scale, as is common), you can set the scale values of your rules to be:
  94. * ``scale: [750000,1500000]`` (includes 1,000,000)
  95. * ``scale: [340000,750000]`` (includes 500,000)
  96. * ``scale: [160000,340000]`` (includes 250,000)
  97. * ``scale: [80000,160000]`` (includes 125,000)
  98. * etc.
  99. Also be aware of the inverse relationship between scale and zoom; **as the zoom level increases, the scale decreases.**
  100. Zoom syntax
  101. -----------
  102. In certain limited cases, it can be more useful to specify scales by way of zoom levels for predefined gridsets. These can be any predefined gridsets in GeoServer.
  103. Inside a rule, the syntax for using zoom levels is::
  104. rules:
  105. - ...
  106. zoom: [<min>, <max>]
  107. ...
  108. where:
  109. .. list-table::
  110. :class: non-responsive
  111. :header-rows: 1
  112. :stub-columns: 1
  113. :widths: 20 10 50 20
  114. * - Attribute
  115. - Required?
  116. - Description
  117. - Default value
  118. * - ``min``
  119. - Yes
  120. - The minimum zoom level for which the rule will be applied. Value is an integer.
  121. - N/A
  122. * - ``max``
  123. - Yes
  124. - The maximum zoom level for which the rule will be applied. Value is an integer.
  125. - N/A
  126. .. note:: It is not possible to use an expression for any of these values.
  127. As with scales, use the literal strings ``min`` and ``max`` to denote where there are no lower or upper scale boundaries. For example, to denote that the zoom level is anything less than some ``<max>`` value::
  128. zoom: [min,<max>]
  129. To denote that the zoom level is anything greater than or equal to some ``<min>`` value::
  130. zoom: [<min>,max]
  131. .. note:: In the above examples, ``min`` and ``max`` are always literals, entered exactly like that, while ``<min>`` and ``<max>`` would be replaced by actual scalar values.
  132. The ``scale`` and ``zoom`` parameters should not be used together in a rule (but if used, ``scale`` takes priority over ``zoom``).
  133. Specifying a grid
  134. -----------------
  135. While every web map can have zoom levels, the specific relationship between a zoom level and its scale is dependent on the gridset (spatial reference system, extent, etc.) used.
  136. So when specifying zoom levels in YSLD, you should also specify the grid.
  137. The ``grid`` parameter should remain at the top of the YSLD content, above any :ref:`ysld_reference_featurestyles` or :ref:`ysld_reference_rules`. The syntax is::
  138. grid:
  139. name: <string>
  140. where:
  141. .. list-table::
  142. :class: non-responsive
  143. :header-rows: 1
  144. :stub-columns: 1
  145. :widths: 20 10 50 20
  146. * - Property
  147. - Required?
  148. - Description
  149. - Default value
  150. * - ``name``
  151. - No
  152. - ``WGS84``, ``WebMercator``, or a name of a predefined gridset in GeoServer.
  153. - ``WebMercator``
  154. .. note:: As many web maps use "web mercator" (also known as EPSG:3857 or EPSG:900913), this is assumed to be the default if no ``grid`` is specified.
  155. .. warning:: As multiple gridsets can contain the same SRS, we recommend naming custom gridsets by something other than the EPSG code.
  156. Zoom examples
  157. -------------
  158. .. **Initial scale**
  159. .. Defining zoom levels based on an initial scale::
  160. .. grid:
  161. .. initial-scale: 6000000
  162. .. .. note::
  163. .. Using scientific notation::
  164. .. grid:
  165. .. initial-scale: 6e6
  166. .. would define zoom levels as follows:
  167. .. .. list-table::
  168. .. :header-rows: 1
  169. .. :stub-columns: 1
  170. .. * - Scale
  171. .. - Zoom level
  172. .. * - ``6000000``
  173. .. - ``0``
  174. .. * - ``3000000``
  175. .. - ``1``
  176. .. * - ``1500000``
  177. .. - ``2``
  178. .. * - ``750000``
  179. .. - ``3``
  180. .. * - ``<previous_scale> / 2``
  181. .. - ``<previous_zoom> + 1``
  182. .. One could define the following three rules::
  183. .. rules:
  184. .. - name: low_zoom
  185. .. zoom: (0,2)
  186. .. symbolizers:
  187. .. - line:
  188. .. stroke-width: 1
  189. .. stroke-color: '#0165CD'
  190. .. - name: medium_zoom
  191. .. zoom: (3,5)
  192. .. symbolizers:
  193. .. - line:
  194. .. stroke-width: 2
  195. .. stroke-color: '#0165CD'
  196. .. - name: high_zoom
  197. .. zoom: (6,)
  198. .. symbolizers:
  199. .. - line:
  200. .. stroke-width: 3
  201. .. stroke-color: '#0165CD'
  202. .. This example will display lines with:
  203. .. * A stroke width of 1 at zoom levels 0-2 (``low_zoom``)
  204. .. * A stroke width of 2 at zoom levels 3-5 (``medium_zoom``)
  205. .. * A stroke width of 3 at zoom levels 6 and greater (``high_zoom``)
  206. .. Adding the ``initial-level`` parameter would change the definitions of the zoom levels::
  207. .. grid:
  208. .. initial-scale: 6000000
  209. .. initial-level: 2
  210. .. .. list-table::
  211. .. :header-rows: 1
  212. .. :stub-columns: 1
  213. .. * - Scale
  214. .. - Zoom level
  215. .. * - ``24000000``
  216. .. - ``0``
  217. .. * - ``12000000``
  218. .. - ``1``
  219. .. * - ``6000000``
  220. .. - ``2``
  221. .. * - ``3000000``
  222. .. - ``3``
  223. .. * - ``<previous_scale> / 2``
  224. .. - ``<previous_zoom> + 1``
  225. .. Setting the ratio would adjust the multiplier between scales in adjacent zoom levels::
  226. .. grid:
  227. .. initial-scale: 6000000
  228. .. ratio: 4
  229. .. .. list-table::
  230. .. :header-rows: 1
  231. .. :stub-columns: 1
  232. .. * - Scale
  233. .. - Zoom level
  234. .. * - ``6000000``
  235. .. - ``0``
  236. .. * - ``1500000``
  237. .. - ``1``
  238. .. * - ``375000``
  239. .. - ``2``
  240. .. * - ``93750``
  241. .. - ``3``
  242. .. * - ``<previous_scale> / 4``
  243. .. - ``<previous_zoom> + 1``
  244. .. **List of scales**
  245. .. Defining zoom levels based on a list of scales::
  246. .. grid:
  247. .. scales:
  248. .. - 1000000
  249. .. - 500000
  250. .. - 100000
  251. .. - 50000
  252. .. - 10000
  253. .. .. note::
  254. .. Using scientific notation::
  255. .. grid:
  256. .. scales:
  257. .. - 1e6
  258. .. - 5e5
  259. .. - 1e5
  260. .. - 5e4
  261. .. - 1e4
  262. .. would define the list of zoom levels explicitly and completely:
  263. .. .. list-table::
  264. .. :header-rows: 1
  265. .. :stub-columns: 1
  266. .. * - Scale
  267. .. - Zoom level
  268. .. * - ``1000000``
  269. .. - ``0``
  270. .. * - ``500000``
  271. .. - ``1``
  272. .. * - ``100000``
  273. .. - ``2``
  274. .. * - ``50000``
  275. .. - ``3``
  276. .. * - ``10000``
  277. .. - ``4``
  278. Default gridset
  279. ~~~~~~~~~~~~~~~
  280. Given the default of web mercator (also known as EPSG:3857 or EPSG:900913), which requires no ``grid`` designation, this defines zoom levels as the following scale levels (rounded to the nearest whole number below):
  281. .. list-table::
  282. :header-rows: 1
  283. :stub-columns: 1
  284. * - Scale
  285. - Zoom level
  286. * - ``559082264``
  287. - ``0``
  288. * - ``279541132``
  289. - ``1``
  290. * - ``139770566``
  291. - ``2``
  292. * - ``69885283``
  293. - ``3``
  294. * - ``34942641``
  295. - ``4``
  296. * - ``17471321``
  297. - ``5``
  298. * - ``8735660``
  299. - ``6``
  300. * - ``4367830``
  301. - ``7``
  302. * - ``2183915``
  303. - ``8``
  304. * - ``<previous_scale> / 2``
  305. - ``<previous_zoom> + 1``
  306. Named gridsets
  307. ~~~~~~~~~~~~~~
  308. For the existing gridset of ``WGS84`` (often known as ``EPSG:4326``)::
  309. grid:
  310. name: WGS84
  311. This defines zoom levels as the following scale levels (rounded to the nearest whole number below):
  312. .. list-table::
  313. :header-rows: 1
  314. :stub-columns: 1
  315. * - Scale
  316. - Zoom level
  317. * - ``559082264``
  318. - ``0``
  319. * - ``279541132``
  320. - ``1``
  321. * - ``139770566``
  322. - ``2``
  323. * - ``69885283``
  324. - ``3``
  325. * - ``34942641``
  326. - ``4``
  327. * - ``17471321``
  328. - ``5``
  329. * - ``8735660``
  330. - ``6``
  331. * - ``4367830``
  332. - ``7``
  333. * - ``2183915``
  334. - ``8``
  335. * - ``<previous_scale> / 2``
  336. - ``<previous_zoom> + 1``
  337. Given a custom named gridset called ``NYLongIslandFtUS``, defined by a CRS of `EPSG:2263 <http://www.spatialreference.org/ref/epsg/2263/>`_ and using its full extent::
  338. grid:
  339. name: NYLongIslandFtUS
  340. This defines zoom levels as the following (rounded to the nearest whole number below):
  341. .. list-table::
  342. :header-rows: 1
  343. :stub-columns: 1
  344. * - Scale
  345. - Zoom level
  346. * - ``4381894``
  347. - ``0``
  348. * - ``2190947``
  349. - ``1``
  350. * - ``1095473``
  351. - ``2``
  352. * - ``547736``
  353. - ``3``
  354. * - ``273868``
  355. - ``4``
  356. * - ``136934``
  357. - ``5``
  358. * - ``68467``
  359. - ``6``
  360. * - ``34234``
  361. - ``7``
  362. * - ``17117``
  363. - ``8``
  364. * - ``<previous_scale> / 2``
  365. - ``<previous_zoom> + 1``
  366. .. note::
  367. These scale values can be verified in GeoServer on the :guilabel:`Gridsets` page under the definition for the gridset:
  368. .. figure:: img/scalezoom_customgridset.png
  369. Gridset defined in GeoServer
  370. Specifically, note the :guilabel:`Scale` values under :guilabel:`Tile Matrix Set`.