Creating an OGC Web Map Service
Using MapServer and PostGIS

Map labels and legends

Adding labels to layers

Of course it is also possible to label objects in WMS. Let's add name labels to the airports:

Insert a LABELITEM object before the existing CLASS definition of airports. Then put the LABEL code inside the existing CLASS object. It should end up looking the highlighted lines in the listing below.

With LABELITEM you define which attribute column in the data table has to be used for labelling. In the CLASS object you insert one (or more) LABEL objects to define how the text labels should be rendered:

  • COLOR to draw the text in.
  • TYPE of the font to use.
  • FONT alias as defined in the FONTSET to use for labelling.
  • MINSIZE tells which minimum font size to use when scaling the text
  • MAXSIZE tells which maximum font size to use when scaling the text
  • POSITION: Where to position the label text in relation to the label points. The value is a combination of vertical and horizontal positions. You have the following choices for vertical alignment: C for centre, U for upper, and L for lower. For horizontal alignment you have the following choices: C for centre, L for left, and R for right. So, to align the label text to the centre you'd use the value "CC" (centre--centre). Or if you'd like it to be on the lower left, you'd use "LL". Another way is to let MapServer decide the best position for your labels. For this you use the value AUTO.
  • PARTIALS: Tells MapServer whether to generate incomplete label texts or not, e.g., when the label would cross the edge of the map, or if it overlaps a label already placed. The default is not to generate fragments of a label text (PARTIALS FALSE).
  • BUFFER of 2 (pixels) means that no label will be drawn if another label already is within 2 pixels distance.

Adding a map legend

For a better understanding of your map it is necessary to add a legend in your map where the symbols you used are listed and named:

Create MapServer code for the legend as shown in the listing below, directly under the MAP object (in the hierarchy, so on the same level as the LAYERs and SYMBOLs). Check the results in your browser.

The LEGEND object defines the look and placement of the legend drawing:

  • KEYSIZE sets the size (in pixels) of the symbol key boxes
  • LABEL set the type, the size and the color of the font you want to use for labeling the symbol keys (see description of LABEL above).
  • STATUS EMBED means that the legend is embedded into the map image
  • POSITION sets the place within the map image. In our case on the lower-right side (LR, see POSITION described above).