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.
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.
Adding a label
Submit, Apply, Save, and refresh the Map Preview.
Previewing the bay_area_bus_stops layer with labels
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.
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.
Specifying font and label placement
Submit, Apply, Save, and refresh the Map Preview.
Previewing the bay_area_bus_stops layer with adjusted labels
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.
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.
Adding a halo to a label
Refresh the Map Preview
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.