kmlstyling.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. .. _google-earth-kml-styling:
  2. KML Styling
  3. ===========
  4. Introduction
  5. ------------
  6. Keyhole Markup Langauge (KML), when created and output by GeoServer, is styled using `Styled Layer Descriptors <http://en.wikipedia.org/wiki/Styled_Layer_Descriptor>`_ (SLD). This is the same approach used to style standard WMS output formats, but is a bit different from how Google Earth is normally styled, behaving more like Cascading Style Sheets (CSS). The style of the map is specified in the SLD file as a series of rules, and then the data matching those rules is styled appropriately in the KML output. For those unfamiliar with SLD, a good place to start is the :ref:`sld_intro`. The remainder of this guide contains information about how to construct SLD documents in order to impact the look of KML produced by GeoServer.
  7. Contents
  8. ````````
  9. :ref:`kml-styling-basic`
  10. :ref:`kml-styling-sld-hand`
  11. :ref:`kml-styling-sld-structure`
  12. :ref:`kml-styling-points`
  13. :ref:`kml-styling-lines`
  14. :ref:`kml-styling-polygons`
  15. :ref:`kml-styling-text-labels`
  16. :ref:`kml-styling-descriptions`
  17. .. _kml-styling-basic:
  18. SLD Generation from CSS
  19. -----------------------
  20. The CSS extension provides the facility to generate SLD files using a lightweight "Cascading Style Sheet" syntax. The CSS GUI provides a live map preview as you are editing your style in addition to an attribute reference for the current layer.
  21. The generated styles will work seamlessly with KML output from GeoServer.
  22. .. _kml-styling-sld-hand:
  23. Creating SLD by hand
  24. --------------------
  25. One can edit the SLD files directly instead of using the CSS extension. For the most complete exploration of editing SLDs see the :ref:`styling` section. The examples below show how some of the basic styles show up in Google Earth.
  26. .. _kml-styling-sld-structure:
  27. SLD Structure
  28. -------------
  29. The following is a skeleton of a SLD document. It can be used as a base on which to expand upon to create more interesting and complicated styles.
  30. .. code-block:: xml
  31. <StyledLayerDescriptor version="1.0.0"
  32. xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd"
  33. xmlns="http://www.opengis.net/sld"
  34. xmlns:ogc="http://www.opengis.net/ogc"
  35. xmlns:xlink="http://www.w3.org/1999/xlink"
  36. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  37. <NamedLayer>
  38. <Name>Default Line</Name>
  39. <UserStyle>
  40. <Title>My Style</Title>
  41. <Abstract>A style</Abstract>
  42. <FeatureTypeStyle>
  43. <Rule>
  44. <!-- symbolizers go here -->
  45. </Rule>
  46. </FeatureTypeStyle>
  47. </UserStyle>
  48. </NamedLayer>
  49. </StyledLayerDescriptor>
  50. *Figure 3: Basic SLD structure*
  51. In order to test the code snippets in this document, create an SLD with the content as shown in Figure 3, and then add the specific code you wish to test in the space that says ``<!-- symbolizers go here -->``. To view, edit, or add SLD files to GeoServer, navigate to **Config** -> **Data** -> **Styles**.
  52. .. _kml-styling-points:
  53. Points
  54. ------
  55. In SLD, styles for points are specified via a PointSymbolizer. An empty PointSymbolizer element will result in a default KML style:
  56. .. code-block:: xml
  57. <PointSymbolizer>
  58. </PointSymbolizer>
  59. .. figure:: pointDefault.png
  60. :align: center
  61. *Figure 4: Default point*
  62. Three aspects of points that can be specified are *color*, *opacity*, and the *icon*.
  63. Point Color
  64. ```````````
  65. The color of a point is specified with a ``CssParameter`` element and a ``fill`` attribute. The color is specified as a six digit hexadecimal code.
  66. .. code-block:: xml
  67. <PointSymbolizer>
  68. <Graphic>
  69. <Mark>
  70. <Fill>
  71. <CssParameter name="fill">#ff0000</CssParameter>
  72. </Fill>
  73. </Mark>
  74. </Graphic>
  75. </PointSymbolizer>
  76. .. figure:: pointColor.png
  77. :align: center
  78. *Figure 5: Setting the point color (#ff0000 = 100% red)*
  79. Point Opacity
  80. `````````````
  81. The opacity of a point is specified with a CssParameter element and a ``fill-opacity`` attribute. The opacity is specified as a floating point number between **0** and **1**, with 0 being completely transparent, and 1 being completely opaque.
  82. .. code-block:: xml
  83. <PointSymbolizer>
  84. <Graphic>
  85. <Mark>
  86. <Fill>
  87. <CssParameter name="fill-opacity">0.5</CssParameter>
  88. </Fill>
  89. </Mark>
  90. </Graphic>
  91. </PointSymbolizer>
  92. .. figure:: pointOpacity.png
  93. :align: center
  94. *Figure 6: Setting the point opacity (0.5 = 50% opaque)*
  95. Point Icon
  96. ``````````
  97. An icon different from the default can be specified with the ``ExternalGraphic`` element:
  98. .. code-block:: xml
  99. <PointSymbolizer>
  100. <Graphic>
  101. <ExternalGraphic>
  102. <OnlineResource xlink:type="simple"
  103. xlink:href="http://maps.google.com/mapfiles/kml/pal3/icon55.png"/>
  104. <Format>image/png</Format>
  105. </ExternalGraphic>
  106. </Graphic>
  107. </PointSymbolizer>
  108. .. figure:: pointCustomIcon.png
  109. :align: center
  110. *Figure 7: A custom icon for points*
  111. In Figure 7, the custom icon is specified as a remote URL. It is also possible to place the graphic in the GeoServer ``styles`` directory, and then specify the filename only:
  112. .. code-block:: xml
  113. <PointSymbolizer>
  114. <Graphic>
  115. <ExternalGraphic>
  116. <OnlineResource xlink:type="simple" xlink:href="icon55.png"/>
  117. <Format>image/png</Format>
  118. </ExternalGraphic>
  119. </Graphic>
  120. </PointSymbolizer>
  121. *Figure 8: Specifying a local file for a graphic point*
  122. .. _kml-styling-lines:
  123. Lines
  124. -----
  125. Styles for lines are specified via a ``LineSymbolizer``. An empty ``LineSymbolizer`` element will result in a default KML style:
  126. .. code-block:: xml
  127. <LineSymbolizer>
  128. </LineSymbolizer>
  129. .. figure:: lineDefault.png
  130. :align: center
  131. *Figure 9: Default line*
  132. The aspects of the resulting line which can be specified via a ``LineSymbolizer`` are *color*, *width*, and *opacity*.
  133. Line Color
  134. ``````````
  135. The color of a line is specified with a ``CssParameter`` element and a ``stroke`` attribute. The color is specified as a six digit hexadecimal code.
  136. .. code-block:: xml
  137. <LineSymbolizer>
  138. <Stroke>
  139. <CssParameter name="stroke">#ff0000</CssParameter>
  140. </Stroke>
  141. </LineSymbolizer>
  142. .. figure:: lineColor.png
  143. :align: center
  144. *Figure 10: Line rendered with color #ff0000 (100% red)*
  145. Line Width
  146. ``````````
  147. The width of a line is specified with a ``CssParameter`` element and a ``stroke-width`` attribute. The width is specified as an integer (in pixels):
  148. .. code-block:: xml
  149. <LineSymbolizer>
  150. <Stroke>
  151. <CssParameter name="stroke-width">5</CssParameter>
  152. </Stroke>
  153. </LineSymbolizer>
  154. .. figure:: lineWidth.png
  155. :align: center
  156. *Figure 11: Line rendered with a width of five (5) pixels*
  157. Line Opacity
  158. ````````````
  159. The opacity of a line is specified with a ``CssParameter`` element and a ``fill-opacity`` attribute. The opacity is specified as a floating point number between **0** and **1**, with 0 being completely transparent, and 1 being completely opaque.
  160. .. code-block:: xml
  161. <LineSymbolizer>
  162. <Stroke>
  163. <CssParameter name="stroke-opacity">0.5</CssParameter>
  164. </Stroke>
  165. </LineSymbolizer>
  166. .. figure:: lineOpacity.png
  167. :align: center
  168. *Figure 12: A line rendered with 50% opacity*
  169. .. _kml-styling-polygons:
  170. Polygons
  171. --------
  172. Styles for polygons are specified via a ``PolygonSymbolizer``. An empty ``PolygonSymbolizer`` element will result in a default KML style:
  173. .. code-block:: xml
  174. <PolygonSymbolizer>
  175. </PolygonSymbolizer>
  176. Polygons have more options for styling than points and lines, as polygons have both an inside ("fill") and an outline ("stroke"). The aspects of polygons that can be specified via a ``PolygonSymbolizer`` are *stroke color*, *stroke width*, *stroke opacity*, *fill color*, and *fill opacity*.
  177. Polygon Stroke Color
  178. ````````````````````
  179. The outline color of a polygon is specified with a ``CssParameter`` element and ``stroke`` attribute inside of a ``Stroke`` element. The color is specified as a 6 digit hexadecimal code:
  180. .. code-block:: xml
  181. <PolygonSymbolizer>
  182. <Stroke>
  183. <CssParameter name="stroke">#0000FF</CssParameter>
  184. </Stroke>
  185. </PolygonSymbolizer>
  186. .. figure:: polygonOutlineColor.png
  187. :align: center
  188. *Figure 13: Outline of a polygon (#0000FF or 100% blue)*
  189. Polygon Stroke Width
  190. ````````````````````
  191. The outline width of a polygon is specified with a ``CssParameter`` element and ``stroke-width`` attribute inside of a ``Stroke`` element. The width is specified as an integer.
  192. .. code-block:: xml
  193. <PolygonSymbolizer>
  194. <Stroke>
  195. <CssParameter name="stroke-width">5</CssParameter>
  196. </Stroke>
  197. </PolygonSymbolizer>
  198. .. figure:: polygonOutlineWidth.png
  199. :align: center
  200. *Figure 14: Polygon with stroke width of five (5) pixels*
  201. Polygon Stroke Opacity
  202. ``````````````````````
  203. The stroke opacity of a polygon is specified with a ``CssParameter`` element and ``stroke`` attribute inside of a ``Stroke`` element. The opacity is specified as a floating point number between **0** and **1**, with 0 being completely transparent, and 1 being completely opaque.
  204. .. code-block:: xml
  205. <PolygonSymbolizer>
  206. <Stroke>
  207. <CssParameter name="stroke-opacity">0.5</CssParameter>
  208. </Stroke>
  209. </PolygonSymbolizer>
  210. .. figure:: polygonOutlineOpacity.png
  211. :align: center
  212. *Figure 15: Polygon stroke opacity of 0.5 (50% opaque)*
  213. Polygon Fill Color
  214. ``````````````````
  215. The fill color of a polygon is specified with a ``CssParameter`` element and ``fill`` attribute inside of a ``Fill`` element. The color is specified as a six digit hexadecimal code:
  216. .. code-block:: xml
  217. <PolygonSymbolizer>
  218. <Fill>
  219. <CssParameter name="fill">#0000FF</CssParameter>
  220. </Fill>
  221. </PolygonSymbolizer>
  222. .. figure:: polygonFillColor.png
  223. :align: center
  224. *Figure 16: Polygon fill color of #0000FF (100% blue)*
  225. Polygon Fill Opacity
  226. ````````````````````
  227. The fill opacity of a polygon is specified with a ``CssParameter`` element and ``fill-opacity`` attribute inside of a ``Fill`` element. The opacity is specified as a floating point number between **0** and **1**, with 0 being completely transparent, and 1 being completely opaque.
  228. .. code-block:: xml
  229. <PolygonSymbolizer>
  230. <Fill>
  231. <CssParameter name="fill-opacity">0.5</CssParameter>
  232. </Fill>
  233. </PolygonSymbolizer>
  234. .. figure:: polygonFillOpacity.png
  235. :align: center
  236. *Figure 17: Polygon fill opacity of 0.5 (50% opaque)*
  237. .. _kml-styling-text-labels:
  238. Text Labels
  239. -----------
  240. There are two ways to specify a label for a feature in Google Earth. The first is with Freemarker templates (LINK?), and the second is with a ``TextSymbolizer``. Templates take precedence over symbolizers.
  241. Freemarker Templates
  242. ````````````````````
  243. Specifying labels via a Freemarker template involves creating a special text file called ``title.ftl`` and placing it into the ``workspaces/<ws name>/<datastore name>/<feature type name>`` directory (inside the GeoServer data directory) for the dataset to be labeled. For example, to create a template to label the ``states`` dataset by state name one would create the file here: ``<data_dir>/workspaces/topp/states_shapefile/states/title.ftl``. The content of the file would be:
  244. .. code-block:: none
  245. ${STATE_NAME.value}
  246. .. figure:: labelTemplate.png
  247. :align: center
  248. *Figure 18: Using a Freemarker template to display the value of STATE_NAME*
  249. For more information on Placemark Templates, please see our full tutorial (LINK FORTHCOMING).
  250. TextSymbolizer
  251. ``````````````
  252. In SLD labels are specified with the Label element of a ``TextSymbolizer``. (Note the ``ogc:`` prefix on the ``PropertyName`` element.)
  253. .. code-block:: xml
  254. <TextSymbolizer>
  255. <Label>
  256. <ogc:PropertyName>STATE_NAME</ogc:PropertyName>
  257. </Label>
  258. </TextSymbolizer>
  259. .. figure:: labelSymbolizer.png
  260. :align: center
  261. *Figure 19: Using a TextSymbolizer to display the value of STATE_NAME*
  262. The aspects of the resulting label which can be specified via a ``TextSymbolizer`` are *color* and *opacity*.
  263. TextSymbolizer Color
  264. ````````````````````
  265. The color of a label is specified with a ``CssParameter`` element and ``fill`` attribute inside of a ``Fill`` element. The color is specified as a six digit hexadecimal code:
  266. .. code-block:: xml
  267. <TextSymbolizer>
  268. <Label>
  269. <ogc:PropertyName>STATE_NAME</ogc:PropertyName>
  270. </Label>
  271. <Fill>
  272. <CssParameter name="fill">#000000</CssParameter>
  273. </Fill>
  274. </TextSymbolizer>
  275. .. figure:: labelColor.png
  276. :align: center
  277. *Figure 20: TextSymbolizer with black text color (#000000)*
  278. TextSymbolizer Opacity
  279. ``````````````````````
  280. The opacity of a label is specified with a ``CssParameter`` element and ``fill-opacity`` attribute inside of a ``Fill`` element. The opacity is specified as a floating point number between **0** and **1**, with 0 being completely transparent, and 1 being completely opaque.
  281. .. code-block:: xml
  282. <TextSymbolizer>
  283. <Label>
  284. <ogc:PropertyName>STATE_NAME</ogc:PropertyName>
  285. </Label>
  286. <Fill>
  287. <CssParameter name="fill-opacity">0.5</CssParameter>
  288. </Fill>
  289. </TextSymbolizer>
  290. .. figure:: labelOpacity.png
  291. :align: center
  292. *Figure 21: TextSymbolizer with opacity 0.5 (50% opaque)*
  293. .. _kml-styling-descriptions:
  294. Descriptions
  295. ------------
  296. When working with KML, each feature is linked to a description, accessible when the feature is clicked on. By default, GeoServer creates a list of all the attributes and values for the particular feature.
  297. .. figure:: descriptionDefault.png
  298. :align: center
  299. *Figure 22: Default description for a feature*
  300. It is possible to modify this default behavior. Much like with featureType titles, which are edited by creating a ``title.ftl`` template, a custom description can be used by creating template called ``description.ftl`` and placing it into the feature type directory (inside the GeoServer data directory) for the dataset. For instance, to create a template to provide a description for the states dataset, one would create the file: ``<data_dir>/workspaces/topp/states_shapefile/states/description.ftl``. As an example, if the content of the description template is:
  301. .. code-block:: none
  302. This is the state of ${STATE_NAME.value}.
  303. The resultant description will look like this:
  304. .. figure:: descriptionTemplate.png
  305. :align: center
  306. *Figure 23: A custom description*
  307. It is also possible to create one description template for all featureTypes in a given namespace. To do this, create a ``description.ftl`` file as above, and save it as ``<data_dir>/templates/<workspace>/description.ftl``. Please note that if a description template is created for a specific featureType that also has an associated namespace description template, the featureType template (i.e. the most specific template) will take priority.
  308. One can also create more complex descriptions using a combination of HTML and the attributes of the data. A full tutorial on how to use templates to create descriptions is available in our page on KML Placemark Templates. (LINK?)
  309. :ref:`kml-styling-basic`
  310. :ref:`kml-styling-sld-structure`
  311. :ref:`kml-styling-points`
  312. :ref:`kml-styling-lines`
  313. :ref:`kml-styling-polygons`
  314. :ref:`kml-styling-text-labels`
  315. :ref:`kml-styling-descriptions`