Query template

Mapfile

We will search the country and region, first create a Mapfile as follows:

01 MAP
02     IMAGETYPE "PNG"
03     EXTENT -180 -90 180 90
04     SIZE 600 300
05     SHAPEPATH "/gdata"
06     IMAGECOLOR 255 255 255
07     FONTSET "../fonts/fonts.list"
08     TEMPLATEPATTERN "tmpl-*"
09     LEGEND
10         KEYSIZE 12 12
11         LABEL
12             TYPE BITMAP
13             SIZE MEDIUM
14             COLOR 0 0 89
15         END
16         STATUS ON
17     END
18     SYMBOL
19         NAME "star"
20         TYPE VECTOR
21         FILLED TRUE
22         POINTS
23             0 0.375
24             0.35 0.375
25             0.5 0
26             0.65 0.375
27             1 0.375
28             0.75 0.625
29             0.875 1
30             0.5 0.75
31             0.125 1
32             0.25 0.625
33         END
34     END
35     REFERENCE
36         IMAGE "../images/worldref.png"
37         SIZE 300 150
38         EXTENT -180 -90 180 90
39         STATUS ON
40         MINBOXSIZE 30
41         MAXBOXSIZE 150
42         COLOR 255 255 0
43         OUTLINECOLOR 255 50 50
44         MARKERSIZE 30
45         MARKER "star"
46     END
47     QUERYMAP
48         SIZE 200 150
49         STATUS ON
50         STYLE HILITE
51         COLOR 255 0 0
52     END
53     SCALEBAR
54         IMAGECOLOR 255 255 255
55         LABEL
56             COLOR 0 0 0
57             SIZE TINY
58         END
59         STYLE 1
60         SIZE 160 2
61         COLOR 0 0 0
62         UNITS KILOMETERS
63         INTERVALS 2
64         TRANSPARENT FALSE
65         STATUS ON
66     END
67     WEB
68         HEADER "qtpl_header.html"
69         FOOTER "qtpl_footer.html"
70         TEMPLATE "to be replaced by map_web_template variable in section3.html"
71         IMAGEPATH "/owg/ms_tmp/"
72         IMAGEURL "/ms_tmp/"
73         METADATA
74             "wms_title" "WMS Demo Server"
75             "wms_onlineresource" "http://192.168.4.211/cgi-bin/mapserv?map=/mstu/htdocs/example2.map&"
76             "wms_srs" "EPSG:3857 EPSG:4326"
77         END
78     END
79     LAYER
80         NAME "land"
81         DATA "land_shallow_topo_8192.tif"
82         STATUS OFF
83         TYPE RASTER
84     END
85     LAYER
86         NAME "topo"
87         TYPE RASTER
88         CONNECTIONTYPE WMS
89         CONNECTION "http://wcs.osgeo.cn:8088/service?"
90         METADATA
91             "wms_srs" "EPSG:4326"
92             "wms_name" "maplet_i887"
93             "wms_server_version" "1.1.1"
94             "wms_format" "image/jpeg"
95         END
96         PROJECTION
97             "init=epsg:4326"
98         END
99     END
100     LAYER
101         NAME "states_poly"
102         GROUP "states"
103         DATA "wcountry.shp"
104         STATUS ON
105         TYPE POLYGON
106         LABELITEM "NAME"
107         TOLERANCE 3
108         CLASS
109             NAME "the States"
110             TEMPLATE "qtpl_country.html"
111             STYLE
112                 COLOR 232 232 232
113                 OUTLINECOLOR 132 132 32
114             END
115         END
116     END
117     LAYER
118         NAME "states_line"
119         DATA "wcountry.shp"
120         STATUS OFF
121         TYPE LINE
122         CLASS
123             NAME "State Boundary"
124             STYLE
125                 COLOR 132 132 32
126             END
127         END
128     END
129     LAYER
130         NAME "wriver"
131         DATA "wriver.shp"
132         STATUS ON
133         TYPE LINE
134         TOLERANCE 3
135         CLASS
136             NAME "World River"
137             TEMPLATE "qtpl_river.html"
138             STYLE
139                 COLOR 0 0 255
140             END
141         END
142     END
143     LAYER
144         NAME "wroads"
145         DATA "wroads.shp"
146         STATUS OFF
147         TYPE LINE
148         CLASS
149             NAME "World Road"
150             STYLE
151                 COLOR 100 200 100
152             END
153         END
154     END
155 END

As you can see, we only added two parameters to MapFile:

  1. Added TOLERANCE to the states_poly layer;
  2. Added TEMPLATE to the CLASS object of the layer states_poly.

Template file

The template file is used in the Mapfile above, and the file name is states_query.html, which is accessed using a relative path. The contents of this template file are as follows:


<tr>
  <td>[lrn]</td>
  <td>[NAME]</td>
  <td>[STATE]</td>
</tr>

Note that it looks like a fragment in an HTML table. The tags for MapServer [NAME] and [STATE] are actually properties of states_poly.dbf. This is a non-spatial database associated with the output shapefile. When MapServer defines the Shapefile property, We always use NAME (for Shapefile, this is true, but in all cases it is not correct). We also used this when we defined CLASSITEM and LABELITEM in Chapter 1. For other tags, [LRN] is another variable in MapServer that will display the result count in the layer.

If you use "Query Multiple Layers to query multiple layers", the MapServer CGI program will apply this query template to each record.

Open the example

Use the form to submit the various parameters required by the MapServer CGI program:

Simple MapServer query template
 

The map and layer variables are internal variables of MapServer and are required in the mapping application.