.. _sld_reference_labeling: Labeling ======== This section discusses the details of controlling label placement via the standard SLD options. It also describes a number of GeoServer enhanced options for label placement that provide better cartographic output. LabelPlacement -------------- The SLD specification defines two alternative label placement strategies which can be used in the ```` element: * ```` places labels at a single point * ```` places labels along a line PointPlacement -------------- When ```` is used the geometry is labelled at a single **label point**. For lines, this point lies at the middle of the visible portion of the line. For polygons, the point is the centroid of the visible portion of the polygon. The position of the label relative to the label point can be controlled by the following sub-elements: .. list-table:: :widths: 30 70 * - **Element** - **Description** * - ```` - Determines the placement of the label relative to the label point. Values given as decimals between 0-1. * - ```` - Offsets the label from the anchor point. Values given in pixels. * - ```` - Rotates the label clockwise by a given number of degrees. The best way to explain these options is with examples. AnchorPoint ^^^^^^^^^^^ The anchor point determines where the label is placed relative to the label point. .. code-block:: xml 0.5 0.5 The anchor point values—listed here as (X, Y) ordered pairs—are specified relative to the bounding box of the label, with values from 0 to 1 inclusive. For example: * *(Default)* Bottom left of the box is (0, 0) * Top right is (1, 1) * Center is (0.5, 0.5) So to have the anchor location centered just below the label (label top-centered), use (0.5, 0): .. code-block:: xml 0.5 0 .. figure:: img/label_bbox.png The following examples show how changing the anchor point affects the position of labels: .. figure:: img/point_x0y0_5.png (0, 0.5) places the label to the right of the label point .. figure:: img/point_x0_5y0_5.png (0.5, 0.5) places the center of the label at the label point .. figure:: img/point_x15y0_5.png (1, 0.5) places the label to the left of the label point .. figure:: img/point_x0_5y0.png (0.5, 0) places the label horizontally centered above the label point Displacement ^^^^^^^^^^^^ Displacement allows fine control of the placement of the label. The displacement values offset the location of the label from the anchor point by a specified number of pixels. The element syntax is: .. code-block:: xml 10 0 Examples: .. figure:: img/point_x0y0_5_displacex10.png :align: center *Displacement of X=10 pixels (compare with default anchor point of (X=0, Y=0.5) shown above)* .. figure:: img/point_x0y1_displacey10.png :align: center *Displacement of Y=-10 pixels (compare with anchor point (X= 0.5, Y=1.0) - not shown)* Rotation ^^^^^^^^ The optional ```` element specifies that labels should be rotated clockwise by a given number of degrees .. code-block:: xml 45 The examples below show how the rotation interacts with anchor points and displacements. .. figure:: img/rot1.png *45 degree rotation* .. figure:: img/rot2.png *45 degree rotation with anchor point (X=0.5, Y=0.5)* .. figure:: img/rot3.png *45 degree rotation with 40-pixel X displacement* .. figure:: img/rot4.png *45 degree rotation with 40-pixel Y displacement with anchor point (X=0.5, Y=0.5)* LinePlacement ------------- To label linear features (such as a road or river), the ```` element can be specified. This indicates that the styler should determine the best placement and rotation for the labels along the lines. The standard SLD LinePlacement element provides one optional sub-element, ````. GeoServer provides much more control over line label placement via vendor-specific options; see below for details. PerpendicularOffset ^^^^^^^^^^^^^^^^^^^ The optional ```` element allows you to position a label above or below a line. (This is similiar to the ```` for label points described above.) The displacement value is specified in pixels. A positive value displaces upwards, a negative value downwards. .. code-block:: xml 10 Examples: .. figure:: img/lp_1.png *PerpendicularOffset = 0 (default)* .. figure:: img/lp_2.png *PerpendicularOffset = 10* Composing labels from multiple attributes ----------------------------------------- The ``