3.4. MapServer interaction: layer control#

  • Author: bukun

  • Update: 2022-10-7

3.4.1. How to control layers#

Being able to turn map layers on and off is a standard feature of web mapping applications. There are many ways to accomplish this using form objects as control. You can use the drop box/menu, check boxes, and/or radio buttons. In this example you’ll see how to implement layer selection using check boxes and drop boxes.

Here is the corresponding Mapfile:

 1MAP
 2    IMAGETYPE "PNG"
 3    EXTENT -180 -90 180 90
 4    SIZE 600 300
 5    SHAPEPATH "/gdata"
 6    IMAGECOLOR 255 255 255
 7    FONTSET "../fonts/fonts.list"
 8    PROJECTION
 9        "init=epsg:4326"
10    END
11    TEMPLATEPATTERN "tmpl-*"
12    WEB
13        TEMPLATE "to be replaced by map_web_template variable in section2.html"
14        IMAGEPATH "/owg/ms_tmp/"
15        IMAGEURL "/ms_tmp/"
16        METADATA
17            "wms_title" "WMS Demo Server"
18            "wms_onlineresource" "http://192.168.4.211/cgi-bin/mapserv?map=/mstu/htdocs/example2.map&"
19            "wms_srs" "EPSG:3857 EPSG:4326"
20        END
21    END
22    LAYER
23        NAME "land"
24        DATA "land_shallow_topo_8192.tif"
25        STATUS OFF
26        TYPE RASTER
27    END
28    LAYER
29        NAME "topo"
30        TYPE RASTER
31        CONNECTIONTYPE WMS
32        CONNECTION "http://wcs.osgeo.cn:8088/service?"
33        METADATA
34            "wms_srs" "EPSG:4326"
35            "wms_name" "maplet_i887"
36            "wms_server_version" "1.1.1"
37            "wms_format" "image/jpeg"
38        END
39        PROJECTION
40            "init=epsg:4326"
41        END
42    END
43    LAYER
44        NAME "states_poly"
45        DATA "wcountry.shp"
46        STATUS OFF
47        TYPE POLYGON
48        LABELITEM "NAME"
49        CLASS
50            NAME "States"
51            STYLE
52                COLOR 232 232 232
53            END
54        END
55    END
56    LAYER
57        NAME "states_line"
58        DATA "wcountry.shp"
59        STATUS OFF
60        TYPE LINE
61        CLASS
62            NAME "State Boundary"
63            STYLE
64                COLOR 132 132 32
65            END
66        END
67    END
68    LAYER
69        NAME "wriver"
70        DATA "wriver.shp"
71        STATUS OFF
72        TYPE LINE
73        CLASS
74            NAME "World River"
75            STYLE
76                COLOR 0 0 255
77            END
78        END
79    END
80    LAYER
81        NAME "wroads"
82        DATA "wroads.shp"
83        STATUS OFF
84        TYPE LINE
85        CLASS
86            NAME "World Road"
87            STYLE
88                COLOR 100 200 100
89            END
90        END
91    END
92END

Notice how the layer STATUS has been changed to OFF except for the “States” polygon background. The states background was left as default so our map will always have something when drawn without any layers turned on. The users of our application should have control of which layers to turn on or leave off.

You’ll understand how the layers are turned on/off by MapServ if you look at the:

<form method="get" action="/cgi-bin/mapserv" role="form">
    <fieldset>
        <legend>Layer control form</legend>
        <br>
        <!-- The following two variables are user defined variables.
             MapServer will pass its value to the HTML template if the
             proper tags are found, in square brackets "[]"  -->
        <input type="hidden" name="root" value="/owg">
        <input type="hidden" name="program" value="/cgi-bin/mapserv">
        <!-- The map and layer variables are internal MapServer variables.
             They are required by the mapping application. -->
        <input type="hidden" name="map" value="/owg/mfu3.map">
        <input type="hidden" name="layer" value="states_poly">
        <input type="hidden" name="zoom" value="0">
        <!-- The map_web_template variable will replace the TEMPLATE
             parameter in the WEB object of the MAP file... -->
        <div class="col-sm-12">
            <div class="col-sm-3">
                <select name="map_web" class="form-control">
                    <option value="template tmpl-example-u3.html">Layer control</option>
                </select>
            </div>
            <div class="col-sm-2">
                <input type="submit" name="submit" value="Open the tutorial" class="btn btn-primary">
            </div>
        </div>
    </fieldset>
</form>

3.4.2. Open and close map layers#

The demo could be opened directly via the following link:

<a href="/cgi-bin/mapserv?map=/owg/mfu3.map&layer=states_line&zoom=0&mode=browse&root=/mstuto&program=/cgi-bin/mapserv&map_web=template+tmpl-example-u3.html"
           target="_blank">Open layer control page</a>
<hr/>

The following shows a form submission, and various parameters are passed through the hidden input control. The effect is the same as above:

<form method="get" action="/cgi-bin/mapserv" role="form">
    <fieldset>
        <legend>Layer control form</legend>
        <br>
        <!-- The following two variables are user defined variables.
             MapServer will pass its value to the HTML template if the
             proper tags are found, in square brackets "[]"  -->
        <input type="hidden" name="root" value="/owg">
        <input type="hidden" name="program" value="/cgi-bin/mapserv">
        <!-- The map and layer variables are internal MapServer variables.
             They are required by the mapping application. -->
        <input type="hidden" name="map" value="/owg/mfu3.map">
        <input type="hidden" name="layer" value="states_poly">
        <input type="hidden" name="zoom" value="0">
        <!-- The map_web_template variable will replace the TEMPLATE
             parameter in the WEB object of the MAP file... -->
        <div class="col-sm-12">
            <div class="col-sm-3">
                <select name="map_web" class="form-control">
                    <option value="template tmpl-example-u3.html">Layer control</option>
                </select>
            </div>
            <div class="col-sm-2">
                <input type="submit" name="submit" value="Open the tutorial" class="btn btn-primary">
            </div>
        </div>
    </fieldset>
</form>