From our Blog

Mapserver and URL Substitution

We recently had a requirement to provide a layer control with label and colour customisation for a Openlayers based web solution with a Mapserver WMS. As part of this project we were pleasantly surprised as to exactly how customisable Mapserver is through the URL request to the mapserv CGI  (perhaps i shouldn't be surprised!).

image image

What we first did is create a database driven layer control, enabling the user to turn layers and labels on or off. The url is then passed to the server with the appropriate layers defined

i.e. - &LAYERS = Imagery, Conservation, Compartments, Rivers

This handled the display of layers and labels.

The next step was to provide tools for changing the label field, label size, outline colour, colour and layer transparency (as seen in the second diagram. This all done by URL substitution. The basics of this can be read at http://mapserver.org/cgi/controls.html.

Layer Colour and Outline Colour

From the above link we were easily able to work out how to change the colour and outline colour which is simply a matter of adding the following to your url:

&map.layer[Compartments].class[0].style[0]=COLOR+R+G+B
or
&map.layer[Compartments].class[0].style[0]=OUTLINECOLOR+R+G+B

where R=Red, G=Green and B=Blue, and the [0] represents the first style in the first class

Opacity

Transparency/opacity is a layer specific attribute in your map file, so this would be added to your url as:

&map.layer[Compartments]=OPACITY+64

Label Size and Label Field

Changing the Label items gave me a bit more trouble than the layer specific attributes, and eventually after consulting with the mailing list, I was able to find out how to do this. First off, you need Mapserver 5.4 or greater as a lot of the required functionality has been added only recently.

Changing the Label Size uses the LABEL+SIZE parameter and would be added like:

&map.layer[Compartments].class[0]=LABEL+SIZE+7

Changing the label field was a little more complicated and required us to make a modification to our map file first. at the "layer" level the "VALIDATION" item needs to be added - i.e.

VALIDATION 
      'labelitem' '^Compt_No|Age|Area_ha|Species|Pd_yy|Method$'
END
LABELITEM 'COMPT_NO'

The validation item provides a regular expression of all the available label fields that can be passed through the URL. These are then changed by simply adding the following to the URL

&map.layer[Compartments]=LABELITEM+Species

or Method or Area_ha etc.

Therefore, a final fully customised URL with modification to the layer properties of a particular layer may look something like:

http://server/cgi-bin/mappage.aspx?mf=mapfile.map&map.layer[Compartments].class[0].style[0]= COLOR+102+153+153&map.layer[Compartments].class[0].style[1]= OUTLINECOLOR+204+102+51&map.layer[Compartments]= OPACITY+75&map.layer[Compartments= LABELITEM+Species&map.layer[Compartments].class[0]= LABEL+SIZE+8+END&LAYERS= Compartments&TRANSPARENT= true&SERVICE= WMS
&VERSION= 1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS= application%2Fvnd.ogc.se_inimage&FORMAT= image%2Fpng&SRS=EPSG%3A2054&BBOX= -83995.61806901402, -3310657.473814449, -71185.38193098598, -3303694.526185551&WIDTH= 1452&HEIGHT= 789


0 comment(s) for “Mapserver and URL Substitution”

    Leave comment: