tutorial.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. .. _css_tutorial:
  2. 教程: 使用 CSS 样式化数据
  3. ===============================
  4. 本教程将展示如何使用 CSS 来设置图层的样式,以及等效的 SLD 代码。
  5. 要使用本教程,你需要 :ref:`CSS 扩展 <css_install>` 以及来自 `默认 GeoServer 配置 <https://github.com/geoserver/geoserver/tree/main/data/release/data/shapefiles>`_ 的 ``states`` 图层.
  6. 为 states 图层创建样式
  7. -------------------------------------
  8. .. highlight:: xml
  9. 默认 ``states`` 图层的 SLD 文件看起来像这样::
  10. <?xml version="1.0" encoding="ISO-8859-1"?>
  11. <StyledLayerDescriptor
  12. version="1.0.0"
  13. xmlns="http://www.opengis.net/sld"
  14. xmlns:ogc="http://www.opengis.net/ogc"
  15. xmlns:xlink="http://www.w3.org/1999/xlink"
  16. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  17. xmlns:gml="http://www.opengis.net/gml"
  18. xsi:schemaLocation="http://www.opengis.net/sld
  19. http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd
  20. ">
  21. <NamedLayer>
  22. <Name>USA states population</Name>
  23. <UserStyle>
  24. <Name>population</Name>
  25. <Title>Population in the United States</Title>
  26. <Abstract>A sample filter that filters the United States into three
  27. categories of population, drawn in different colors</Abstract>
  28. <FeatureTypeStyle>
  29. <Rule>
  30. <Title>&lt; 2M</Title>
  31. <ogc:Filter>
  32. <ogc:PropertyIsLessThan>
  33. <ogc:PropertyName>PERSONS</ogc:PropertyName>
  34. <ogc:Literal>2000000</ogc:Literal>
  35. </ogc:PropertyIsLessThan>
  36. </ogc:Filter>
  37. <PolygonSymbolizer>
  38. <Fill>
  39. <!-- CssParameters allowed are fill (the color) and fill-opacity -->
  40. <CssParameter name="fill">#4DFF4D</CssParameter>
  41. <CssParameter name="fill-opacity">0.7</CssParameter>
  42. </Fill>
  43. </PolygonSymbolizer>
  44. </Rule>
  45. <Rule>
  46. <Title>2M - 4M</Title>
  47. <ogc:Filter>
  48. <ogc:PropertyIsBetween>
  49. <ogc:PropertyName>PERSONS</ogc:PropertyName>
  50. <ogc:LowerBoundary>
  51. <ogc:Literal>2000000</ogc:Literal>
  52. </ogc:LowerBoundary>
  53. <ogc:UpperBoundary>
  54. <ogc:Literal>4000000</ogc:Literal>
  55. </ogc:UpperBoundary>
  56. </ogc:PropertyIsBetween>
  57. </ogc:Filter>
  58. <PolygonSymbolizer>
  59. <Fill>
  60. <!-- CssParameters allowed are fill (the color) and fill-opacity -->
  61. <CssParameter name="fill">#FF4D4D</CssParameter>
  62. <CssParameter name="fill-opacity">0.7</CssParameter>
  63. </Fill>
  64. </PolygonSymbolizer>
  65. </Rule>
  66. <Rule>
  67. <Title>&gt; 4M</Title>
  68. <!-- like a linesymbolizer but with a fill too -->
  69. <ogc:Filter>
  70. <ogc:PropertyIsGreaterThan>
  71. <ogc:PropertyName>PERSONS</ogc:PropertyName>
  72. <ogc:Literal>4000000</ogc:Literal>
  73. </ogc:PropertyIsGreaterThan>
  74. </ogc:Filter>
  75. <PolygonSymbolizer>
  76. <Fill>
  77. <!-- CssParameters allowed are fill (the color) and fill-opacity -->
  78. <CssParameter name="fill">#4D4DFF</CssParameter>
  79. <CssParameter name="fill-opacity">0.7</CssParameter>
  80. </Fill>
  81. </PolygonSymbolizer>
  82. </Rule>
  83. <Rule>
  84. <Title>Boundary</Title>
  85. <LineSymbolizer>
  86. <Stroke>
  87. <CssParameter name="stroke-width">0.2</CssParameter>
  88. </Stroke>
  89. </LineSymbolizer>
  90. <TextSymbolizer>
  91. <Label>
  92. <ogc:PropertyName>STATE_ABBR</ogc:PropertyName>
  93. </Label>
  94. <Font>
  95. <CssParameter name="font-family">Times New Roman</CssParameter>
  96. <CssParameter name="font-style">Normal</CssParameter>
  97. <CssParameter name="font-size">14</CssParameter>
  98. </Font>
  99. <LabelPlacement>
  100. <PointPlacement>
  101. <AnchorPoint>
  102. <AnchorPointX>0.5</AnchorPointX>
  103. <AnchorPointY>0.5</AnchorPointY>
  104. </AnchorPoint>
  105. </PointPlacement>
  106. </LabelPlacement>
  107. </TextSymbolizer>
  108. </Rule>
  109. </FeatureTypeStyle>
  110. </UserStyle>
  111. </NamedLayer>
  112. </StyledLayerDescriptor>
  113. 现在,让我们从完成相同任务的 CSS 文件开始。
  114. 首先,进入样式页面,点击 :guilabel:`add a new style` 链接来开始新样式。在“新样式”页面中,执行以下操作::
  115. * 将新样式命名为任何您喜欢的名称,例如 ``csstutorial``
  116. * 选择 ``CSS``格式
  117. * 在 :guilabel:`Generate a default style` 下拉菜单中选择 ``Polygon`` 然后点击 :guilabel:`Generate...`
  118. .. figure:: images/tutorial_new_style.png
  119. Creating a new CSS style
  120. .. highlight:: css
  121. 这样就创建了一个示例样式,其源代码与此类似(颜色可能不同) ::
  122. /* @title cyan polygon */
  123. * {
  124. stroke: #000000;
  125. stroke-width: 0.5;
  126. fill: #0099cc;
  127. }
  128. 这展示了 CSS 样式的基本元素:
  129. 一个 **选择器** ,用于标识要样式化的部分数据。这里,选择器是 ``*``, 表示所有数据都应该使用样式属性。
  130. 在花括号(``{}``)内的 **属性** 指定受影响的特性应该如何样式化。属性由用冒号(``:``)分隔的名称/值对组成。
  131. 我们也可以看到基本的样式一个多边形(``fill``),和它的轮廓((``stroke``)。
  132. .. seealso::
  133. :ref:`css_filters` 和 :ref:`css_properties` 列表页面提供了有关 CSS 样式中可用选项的更多信息。
  134. .. highlight:: xml
  135. 在继续之前,让我们保存样式并在状态层预览它:
  136. * 点击“应用”保存图层,并启用样式预览
  137. * 现在在“样式编辑器页面”,切换到“图层预览”标签,点击“预览图层”链接,然后在对话框中选择“状态”图层
  138. * 样式编辑器现在应该显示状态层填充和描边
  139. .. figure:: images/tutorial_preview.png
  140. Previewing the CSS style with the state layer
  141. 让我们使用这些基础知识开始转换状态样式。SLD的第一条规则适用于 ``PERSONS`` 字段小于200万的州::
  142. <Rule>
  143. <Title>&lt; 2M</Title>
  144. <ogc:Filter>
  145. <ogc:PropertyIsLessThan>
  146. <ogc:PropertyName>PERSONS</ogc:PropertyName>
  147. <ogc:Literal>2000000</ogc:Literal>
  148. </ogc:PropertyIsLessThan>
  149. </ogc:Filter>
  150. <PolygonSymbolizer>
  151. <Fill>
  152. <!-- CssParameters allowed are fill (the color) and fill-opacity -->
  153. <CssParameter name="fill">#4DFF4D</CssParameter>
  154. <CssParameter name="fill-opacity">0.7</CssParameter>
  155. </Fill>
  156. </PolygonSymbolizer>
  157. </Rule>
  158. .. highlight:: css
  159. 使用基于 :doc:`CQL</tutorials/cql/cql_tutorial>` 的选择器,并复制 csparameters 的名称和值,我们得到::
  160. [PERSONS < 2000000] {
  161. fill: #4DFF4D;
  162. fill-opacity: 0.7;
  163. }
  164. .. highlight:: xml
  165. 对于第二种样式,我们有一个 ``PropertyIsBetween`` 过滤器,它不直接转换为 CSS ::
  166. <Rule>
  167. <Title>2M - 4M</Title>
  168. <ogc:Filter>
  169. <ogc:PropertyIsBetween>
  170. <ogc:PropertyName>PERSONS</ogc:PropertyName>
  171. <ogc:LowerBoundary>
  172. <ogc:Literal>2000000</ogc:Literal>
  173. </ogc:LowerBoundary>
  174. <ogc:UpperBoundary>
  175. <ogc:Literal>4000000</ogc:Literal>
  176. </ogc:UpperBoundary>
  177. </ogc:PropertyIsBetween>
  178. </ogc:Filter>
  179. <PolygonSymbolizer>
  180. <Fill>
  181. <!-- CssParameters allowed are fill (the color) and fill-opacity -->
  182. <CssParameter name="fill">#FF4D4D</CssParameter>
  183. <CssParameter name="fill-opacity">0.7</CssParameter>
  184. </Fill>
  185. </PolygonSymbolizer>
  186. </Rule>
  187. .. highlight:: css
  188. 但是,``PropertyIsBetween`` 可以很容易地被两个比较选择器的组合取代。在 CSS 中,您可以简单地将多个选择器一个接一个地放置到一个规则上。只有空格分隔的选择器必须全部满足才能应用样式。通过逗号 (``,``)分隔,可以将多个这样的组附加到一个规则。如果一个特性匹配规则的任何逗号分隔组,则应用该样式。因此 CSS 的第二条等价规则是::
  189. [PERSONS >= 2000000] [PERSONS < 4000000] {
  190. fill: #FF4D4D;
  191. fill-opacity: 0.7;
  192. }
  193. 第三条规则的处理方式与第一条基本相同::
  194. [PERSONS >= 4000000] {
  195. fill: #4D4DFF;
  196. fill-opacity: 0.7;
  197. }
  198. .. highlight:: xml
  199. 第四条也是最后一条规则有点不同。它适用于所有州的标签和大纲::
  200. <Rule>
  201. <Title>Boundary</Title>
  202. <LineSymbolizer>
  203. <Stroke>
  204. <CssParameter name="stroke-width">0.2</CssParameter>
  205. </Stroke>
  206. </LineSymbolizer>
  207. <TextSymbolizer>
  208. <Label>
  209. <ogc:PropertyName>STATE_ABBR</ogc:PropertyName>
  210. </Label>
  211. <Font>
  212. <CssParameter name="font-family">Times New Roman</CssParameter>
  213. <CssParameter name="font-style">Normal</CssParameter>
  214. <CssParameter name="font-size">14</CssParameter>
  215. </Font>
  216. <LabelPlacement>
  217. <PointPlacement>
  218. <AnchorPoint>
  219. <AnchorPointX>0.5</AnchorPointX>
  220. <AnchorPointY>0.5</AnchorPointY>
  221. </AnchorPoint>
  222. </PointPlacement>
  223. </LabelPlacement>
  224. </TextSymbolizer>
  225. </Rule>
  226. .. highlight:: scss
  227. 与迄今为止的所有规则不同,这引入了将提取的值 (``STATE_ABBR``) 直接渲染到地图中的想法。为此,您可以将包裹在方形括号 (``[]``) 中的CQL表达式用作 CSS 属性的值。 还有必要将包含空格的值,如 ``Times New Roman``, 用单引号或双引号 (``"``, ``'``)括起来。考虑到这些细节,让我们来编写规则::
  228. * {
  229. stroke-width: 0.2;
  230. label: [STATE_ABBR];
  231. label-anchor: 0.5 0.5;
  232. font-family: "Times New Roman";
  233. font-fill: black;
  234. font-style: normal;
  235. font-size: 14;
  236. }
  237. 综上所述,您现在应该有一个看起来像::
  238. [PERSONS < 2000000] {
  239. fill: #4DFF4D;
  240. fill-opacity: 0.7;
  241. }
  242. [PERSONS >= 2000000] [PERSONS < 4000000] {
  243. fill: #FF4D4D;
  244. fill-opacity: 0.7;
  245. }
  246. [PERSONS >= 4000000] {
  247. fill: #4D4DFF;
  248. fill-opacity: 0.7;
  249. }
  250. * {
  251. stroke-width: 0.2;
  252. label: [STATE_ABBR];
  253. label-anchor: 0.5 0.5;
  254. font-family: "Times New Roman";
  255. font-fill: black;
  256. font-style: normal;
  257. font-size: 14;
  258. }
  259. 单击表单底部的 :guilabel:`Apply` 按钮以保存更改。
  260. .. figure:: images/tutorial_noborder.png
  261. CSS style applied to the ``states`` layer
  262. 你会发现边界消失了!在 GeoServer CSS 模块中,每种类型的符号都有一个“ key ”属性来控制是否应用它。没有这些“ key ”属性,下级属性将被忽略。这些“ key ”属性是:
  263. * **fill**, 它控制是否应用多边形填充。这指定了用于填充的颜色或图形。
  264. * **stroke**, 控制是否应用线条和多边形轮廓线。它指定笔画的颜色(或图形填充)。
  265. * **mark**, 它控制是否绘制点标记。这标识了要使用的已知标记或图像URL。
  266. * **label**, 它控制是否在地图上绘制标签。这标识用于标记映射的文本,通常作为 CQL 表达式。
  267. * **halo-radius**, 它控制是否在标签周围绘制光环。这将指定此类光晕的大小。
  268. .. seealso::
  269. :ref:`css_properties` 页以获取有关其他属性的信息。
  270. 因为我们没有指定 ``stroke`` 颜色,所以没有应用描边。让我们添加它,取代最终的规则,使它现在看起来像这样 ::
  271. * {
  272. stroke: black;
  273. stroke-width: 0.2;
  274. label: [STATE_ABBR];
  275. label-anchor: 0.5 0.5;
  276. font-family: "Times New Roman";
  277. font-fill: black;
  278. font-style: normal;
  279. font-size: 14;
  280. }
  281. .. figure:: images/tutorial_border.png
  282. Border added to style
  283. 改进风格
  284. ------------------
  285. 移除重复的属性
  286. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  287. 我们现在拥有的样式只有 23 行,相对于最初的 103 行 XML 来说,这是一个很好的改进。然而,我们仍然到处重复 ``fill-opacity`` 属性。
  288. 我们可以将其移动到 ``*`` 规则中,并将其应用于任何地方。这是可行的,因为 GeoServer CSS 模块模拟了 *级联* :而SLD使用了一个“ 画家模型 ”,其中每个规则都是独立处理的,级联样式允许你提供一般样式属性,只覆盖特定功能的特定属性。
  289. 这将样式减少到只有 21 行::
  290. [PERSONS < 2000000] {
  291. fill: #4DFF4D;
  292. }
  293. [PERSONS > 2000000] [PERSONS < 4000000] {
  294. fill: #FF4D4D;
  295. }
  296. [PERSONS > 4000000] {
  297. fill: #4D4DFF;
  298. }
  299. * {
  300. fill-opacity: 0.7;
  301. stroke-width: 0.2;
  302. label: [STATE_ABBR];
  303. label-anchor: 0.5 0.5;
  304. font-family: "Times New Roman";
  305. font-fill: black;
  306. font-style: normal;
  307. font-size: 14;
  308. }
  309. 比例相关样式
  310. ^^^^^^^^^^^^^^^^^^^^^^
  311. 这种风格的标签很好,但在较低的缩放级别,它们似乎有点拥挤。我们可以很容易地将标签移动到一个规则,直到刻度分母低于 2000000 才会激活。 我们确实希望在所有缩放级别上保持 stroke 和不透明度填充,以便将它们与标签属性分开。
  312. 在main (``*``) 规则中保持以下属性::
  313. * {
  314. fill-opacity: 0.7;
  315. stroke-width: 0.2;
  316. }
  317. 移除所有其他的,将它们移动到一个新规则中::
  318. [@sd < 20M] {
  319. label: [STATE_ABBR];
  320. label-anchor: 0.5 0.5;
  321. font-family: "Times New Roman";
  322. font-fill: black;
  323. font-style: normal;
  324. font-size: 14;
  325. }
  326. 为图例设置标题
  327. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  328. 到目前为止,我们还没有为任何样式规则设置标题。这实际上不会在查看地图时造成任何问题,但 GeoServer 会在自动生成图例图形时使用标题。如果没有标题, GeoServer 会依赖于名称,而在 CSS 模块中,名称是根据每个规则的过滤器生成的。 标题通常不是 CSS 的一部分,所以 GeoServer 会在每个规则之前的特殊格式注释中查找它们。我们可以添加这样的标题::
  329. /* @title Population < 2M */
  330. [PERSONS < 2000000] {
  331. ...
  332. /* @title 2M < Population < 4M */
  333. [PERSONS > 2000000] [PERSONS < 4000000] {
  334. ...
  335. /* @title Population > 4M */
  336. [PERSONS > 4000000] {
  337. ...
  338. /* @title Boundaries */
  339. * {
  340. ...
  341. 由于 CSS 转换为 SLD 的方式,每个 SLD 规则都是若干 CSS 规则的组合。这是通过将标题与单词 “with” 结合起来处理的。如果规则的标题被省略,那么 SLD 输出中就不包括它。
  342. 最终的CSS应该是这样的::
  343. /* @title Population < 2M */
  344. [PERSONS < 2000000] {
  345. fill: #4DFF4D;
  346. fill-opacity: 0.7;
  347. }
  348. /* @title 2M < Population < 4M */
  349. [PERSONS >= 2000000] [PERSONS < 4000000] {
  350. fill: #FF4D4D;
  351. fill-opacity: 0.7;
  352. }
  353. /* @title Population > 4M */
  354. [PERSONS >= 4000000] {
  355. fill: #4D4DFF;
  356. fill-opacity: 0.7;
  357. }
  358. /* @title Boundaries */
  359. * {
  360. stroke: black;
  361. stroke-width: 0.2;
  362. fill-opacity: 0.7;
  363. }
  364. [@sd < 20M] {
  365. label: [STATE_ABBR];
  366. label-anchor: 0.5 0.5;
  367. font-family: "Times New Roman";
  368. font-fill: black;
  369. font-style: normal;
  370. font-size: 14;
  371. }
  372. .. figure:: images/tutorial_finalstyle.png
  373. Final style with rule names
  374. 应用规则嵌套
  375. ^^^^^^^^^^^^^^^^^^^^^
  376. 作为最后一种变体,可以通过利用规则嵌套使样式更紧凑::
  377. * {
  378. stroke: black;
  379. stroke-width: 0.2;
  380. fill-opacity: 0.7;
  381. /* @title Population < 2M */
  382. [PERSONS < 2000000] {
  383. fill: #4DFF4D;
  384. };
  385. /* @title 2M < Population < 4M */
  386. [PERSONS >= 2000000] [PERSONS < 4000000] {
  387. fill: #FF4D4D;
  388. };
  389. /* @title Population > 4M */
  390. [PERSONS >= 4000000] {
  391. fill: #4D4DFF;
  392. };
  393. /* Labelling */
  394. [@sd < 20M] {
  395. label: [STATE_ABBR];
  396. label-anchor: 0.5 0.5;
  397. font-family: "Times New Roman";
  398. font-fill: black;
  399. font-style: normal;
  400. font-size: 14;
  401. }
  402. }
  403. CSS 工作室
  404. ^^^^^^^^^^^^
  405. 更多详细信息, 请访问下一部分, `CSS workshop <../workshop/index.html>`_。 该研讨会过去曾教授 CSS 扩展并被采用于课程设置,现已移植到用户文档中。