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.
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"?>
municipalities-names municipalities-names DejaVu Sans 14 normal bold 0.5 0.5
Essentially, what this SLD file does is telling the WMS server 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’.
http://my-sdi.com/wms?service=WMS &version=1.1.1 &layers=municipalities &transparent=true &format=image/png &request=GetMap &srs=EPSG:23029 &bbox=523912.5,4699175,703912.5,4834175 &width=615 &height=375 &styles=municipalities-names
The picture shown below is the response of the previous GetMap request.
And in the following picture, the municipalities layer with its annotation layer superimposed is shown.
[note 2] In a Debian system you can get a list of the available fonts by using the command fc-list.