OpenGeo

OpenGeo Stack Workshop

Previous topic

Scale Dependent Rendering with SLD

Next topic

Styling with the GeoExt Styler

Labeling with SLDΒΆ

Labeling is an equality import aspect of cartography as scale dependence. In the last section the map preview was used to analyze attributes of the data set. This section covers the task of adding labels to a style generated from data set attributes.

  1. Return to the Style Editor and add the following XML after the closing </PointSymbolizer> element:

    <TextSymbolizer>
      <Label>
          <ogc:PropertyName>routes</ogc:PropertyName>
      </Label>
    </TextSymbolizer>
    

    Note

    The <TextSymbolizer> element is the SLD construct used for labeling. The above specifies that the label from be generated by the routes attribute.

    ../_images/sld_create13.jpg

    Adding a label

  2. Submit, Apply, Save, and refresh the Map Preview.

    ../_images/sld_create14.jpg

    Previewing the bay_area_bus_stops layer with labels

  3. The label specifies in the previous section overlaps with the bus icon and could be made to look nicer. After the closing </Label> element add the following XML:

    <Font>
      <CssParameter name="font-family">Arial</CssParameter>
      <CssParameter name="font-size">12</CssParameter>
    </Font>
    

    Note

    The <Font> is used to specify all aspects of the font used for a label.

  4. After the closing <Font> element add the following XML:

    <LabelPlacement>
     <PointPlacement>
       <Displacement>
        <DisplacementX>20</DisplacementX>
        <DisplacementY>0</DisplacementY>
       </Displacement>
      </PointPlacement>
    </LabelPlacement>
    

    Note

    The <LabelPlacement> element is used to position a label. The above offsets the label 20 pixels to the right.

    ../_images/sld_create15.jpg

    Specifying font and label placement

  5. Submit, Apply, Save, and refresh the Map Preview.

    ../_images/sld_create16.jpg

    Previewing the bay_area_bus_stops layer with adjusted labels

  6. Return to the Style Editor and add the following XML after the closing </LabelPlacement> element:

    <Halo>
      <Radius>1</Radius>
      <Fill>
        <CssParameter name="fill">#000000</CssParameter>
      </Fill>
    </Halo>
    

    Note

    The <Halo> element adds a hue around a label and can used to make a label stand out better with respect to its background. The above specifies a black halo.

  7. Add the following XML after the closing </Halo> element:

    <Fill>
      <CssParameter name="fill">#ffffff</CssParameter>
    </Fill>
    

    Note

    By default labels are black. Since the halo specified in the previous step is black the label color should be changed. The above changes the label color to white.

    ../_images/sld_create17.jpg

    Adding a halo to a label

  8. Refresh the Map Preview

    ../_images/sld_create18.jpg

    Previewing the bay_area_bus_stops layer with halo labels

At this point the SLD being built up is complete. In the next module an alternative to writing out raw XML will be covered.