Annotation layers in GeoServer.

Posted by – Saturday 2012-07-21

Currently I am working in a project that requires switching from MapServer to GeoServer. As a newcomer to the latter, one of the things I miss is MapServer’s annotation layers. An annotation layer is one that just shows labels, like names of towns or countries. In this post we will see how annotation layers can be implemented using GeoServer.

1. The arena.

In our example there is already an existing WMS layer named ‘municipalities’, that contains the borders of some municipalities of Galicia and whose geometry type is multipolygon. The data source is a shapefile. The following screenshot – taken from GeoServer’s administration application – shows the attributes the municipalities have.

GeoServer screenshot

Our goal is showing the municipalities names in a different WMS layer, so an user of the WMS service can toggle the visibility of this annotation layer, that is, being able to show or hide, in every moment, the municipalities’ names. The names of the municipalities are stored in the attribute ‘NOMBRE’.

2. The procedure.

The wanted annotation layer can be gotten by issuing a WMS GetMap request with the appropiate styles parameter. That style must show only the municipalities names. Let’s see how to create it.

To create a style in GeoServer, in the web administration interface we can go to init > Styles > Add a new style and create a Styled Layer Descriptor (SLD) [note 1] file named ‘municipalities-names’ like the shown below.

<?xml version="1.0" encoding="UTF-8"?>

                DejaVu Sans

Essentially, what this SLD file tells the WMS server is to render only the labels. The rendered attribute of the shapefile is specified in the line <ogc:PropertyName>NOMBRE</ogc:PropertyName>.

The lines 16 – 21 set the font settings and are self-explanatory [note 2].

The placement of the labels – one for each municipality is set with the tag <LabelPlacement> (lines 22 – 29). For each municipality, its centroid is calculated and the rendering of the label starts at that point. With the tags <AnchorPointX> and <AnchorPointY> the label is shifted horizontally and vertically, so the center of the label lies on the centroid of the municipality.

As said before, to get the annotation layer the styles parameter of the WMS GetMap request must be set to ‘municipalities-names’.

The picture shown below is the response of the previous GetMap request.

municipalities annotation layer

And in the following picture, the municipalities layer with its annotation layer superimposed is shown.

muncipalities annotation layer on top municipalities layer


[note 1] Two good references about SLD styling in GeoServer are the GeoServer manual, section ‘Styling’ and GeoServer’s SLD Cookbook.

[note 2] In a Debian system you can get a list of the available fonts by using the command fc-list.

0 Comments on Annotation layers in GeoServer.