You are on page 1of 14

GIS Exercise - spring 2011

Maria Antonia Brovelli Laura Carcano, Sara Lucca, Marco Minghini, Luana Valentini

Editing a mapfile

References
In the following pages you will find examples involving just some of all the possible mapfile configurations, so remember to take a look also on the documentation to clear your doubts and understand how to do what is required. Also you can try out more complex solutions if you want to. An essential reference when working with MapServer is the documentation on its website: http://www.mapserver.org/documentation.html Among other things, here you can find everything is needed to understand how to write the aforementioned configuration file: http://www.mapserver.org/mapfile/index.html#mapfile As already mentioned, the mapfile is a peculiar text file with .map extension that contains the configuration of MapServer application. It has a hierarchical structure, which means that it contains objects that can (and must) be nested inside other objects; every object is in turn defined by a series of parameters with associated values. The definition of each object in the mapfile is enclosed within two words: the first one is variable because it defines the type of object, while the second one is always END (to close the object). Remember to always close objects, otherwise MapServer will not understand where they start and finish. Notes: Unless otherwise specified, the content of a mapfile is not case-sensitive. In this script the words that define objects and parameters are all written upper-case just to make the mapfile more easily readable, and also because it has become a de facto standard among MapServer users.
GIS Exercise Spring 2011 1

Strings that contain non-alphanumeric characters or a MapServer keyword must be quoted []. Anyway, it is a good habit to put all strings in double-quotes.

Comments can be inserted using a hash [#] before them.

The root object that contains all the other objects is MAP, so every mapfile must start with the word MAP and ends with END. Lets start by checking inside the mapfile ethex.map: at a matter of fact the whole file starts with MAP and ends with END. The MAP object contains application/map wide parameters. You are required to introduce the ones that follow (write them between MAP and END, using some indentation). Check on the mapfile reference here: http://mapserver.org/introduction.html#map-object

Important information
Host of MapServer: MapServer CGI: Web path of the webGIS:
http://webgis.como.polimi.it/GIScourse11 /cgi-bin/mapserv11; //utentexx/, where

xx is the number of the user folder (e.g.

utente01, utente02, etc); Map file:


http://webgis.como.polimi.it/GIScourse11/utentexx/ethex.map

Adding a layer
Each cartographic datum you use in MapServer represents a layer of your map. Layers are drawn on the map according to their order of definition in the mapfile: the last layer you define in the mapfile will be the last to be drawn, so it will stay above all the others.
GIS Exercise Spring 2011 2

Lets add a first layer (municipalities) to the webGIS, by adding a LAYER object just after the WEB object in the mapfile. Inside the object add the following parameters (remember to check the online mapfile documentation!):
NAME STATUS DATA TYPE [set this to municipalities] [this layer should be turned on permanently, so DEFAULT] [this should link to the municipalities shapefile via relative path, so municipalities"] polygon

Just after these parameters, add also a CLASS object, nested inside the LAYER object. The CLASS object is required to have just a nested STYLE object for the moment. The STYLE object must have the following parameters:

COLOR OUTLINECOLOR

(RGB) (RGB)

To summarize, you should have obtained a structure like the following one, filled in with proper values:

LAYER NAME STATUS DATA TYPE CLASS STYLE COLOR OUTLINECOLOR END END END

This is the most basic structure that is commonly used to define a layer.
GIS Exercise Spring 2011 3

Let us now add labels to them. Since there are many small polygons, we will set labels to be displayed only beyond a certain scale (otherwise some of them would not be readable). Add the following parameters to the LAYER object:
LABELITEM [check the field of the attribute table that contains the municipalities names, NAME] LABELMAXSCALEDENOM [set this to a value 100000]

Inside the CLASS object add then a LABEL object with the following parameters:
TYPE FONT SIZE POSITION COLOR [set this to truetype] [choose one of the fonts available, e.g. times] [e.g. 8] [auto] [choose a color easily readable over a blue background]

Reloading the webGIS and zooming in beyond the scale you have chosen, you should see labels displayed.

Add the stations shapefile (the one in Gauss Boaga coordinates) as a layer named stationsgb to the mapfile. Try out all the different symbols by displaying them (SYMBOL parameter in the STYLE object).

Adding a scalebar
In order to insert a scalebar to your map, define a SCALEBAR object before the two LAYER objects and after the WEB object. Use the following parameters:

SCALEBAR STATUS COLOR OUTLINECOLOR BACKGROUNDCOLOR IMAGECOLOR


GIS Exercise Spring 2011

[try out different colors] [try out different colors] [try out different colors] [try out different colors]
4

UNITS INTERVALS SIZE STYLE LABEL TYPE SIZE COLOR END END

[kilometers should be ok for this map]

[try out both available styles]

Choose your preferred style for the scalebar. Remember that it should be useful and clearly readable, for instance:

Adding a reference map


The reference map (or keymap) is a small representation of the full extent of the map. A rectangle is usually overlaid on it to let the user know where the current view of the map is located with respect to the full extent. MapServer needs an image file to insert a reference map. The best way to create it is to reduce temporarily the size of the main map (inside mapfile) to the dimension required for the reference map. In this way, once the webGIS is reloaded, you can save the created image (right click on it) and use it for the reference map. We here have already done this and have saved this image in the folder: http://webgis.como.polimi.it/GIScourse11/utentexx/graphics/refmap/ Take into account that the REFERENCE object needs the coordinates that match the extent represented in the reference map. Therefore take note of them, when you save the resized image (you can find them at the bottom of the webGIS interface). Now we add the REFERENCE object into the mapfile, before the SCALEBAR object, with the following parameters:
REFERENCE STATUS
GIS Exercise Spring 2011 5

IMAGE SIZE EXTENT COLOR OUTLINECOLOR END

[relative path with filename of the image file] [a width of 150 pixels would fit well in the page] [see the map, for example: 1486000 5056000 1525000 5088000]

[this color should be clearly visible]

Adding a legend
The users of our webGIS will need a legend to understand what the different colors and symbols stand for. Insert a LEGEND object after the REFERENCE object, with the following parameters:

LEGEND STATUS IMAGECOLOR KEYSIZE LABEL TYPE SIZE FONT COLOR END END [default = 20 x 10, here 18 12 is ok]

The MapServer legend displays the layer classes that have the parameter NAME set with a proper value. This value will be displayed in the legend. If you dont see anything after having inserted the LEGEND object, try to assign a name to the CLASS object of the municipalities layer. You should see it in the legend after having reloaded the webGIS.

Assigning different styles to different features in a layer


Lets try now to assign different styles to different features in a layer, according to specific values in the attribute table. For instance, we try to display Como municipalities for example as blue polygons with green borders, and Varese municipalities as green polygons
GIS Exercise Spring 2011 6

with red borders. In order to do that, you have to define two different classes for the municipalities layer, with different styles. Then you have to insert the CLASSITEM parameter in the LAYER object and an EXPRESSION parameter in each CLASS object to define which features should be displayed as belonging to Como and which ones as belonging to Varese. Remember to assign a NAME parameter to each class in order to let it be displayed in the legend. Reload the webGIS and check the result.

Try to do the same with the stationsgb layer. In this case you should have taken note of the different values of the COMP_CODE field. You can use different colors and/or different symbols for station points. Add also labels to stations, showing their name.

Assigning layers to different CRS


All the data you have added to our webGIS so far are in the same CRS (Roma40 Gauss Boaga, hereafter simply named Gauss Boaga). Now suppose you want to introduce data in a different CRS (WGS84 UTM 32N hereafter simply named UTM). First, we define the main CRS for the webGIS. This has been done using the PROJECTION object, nested directly inside the MAP object (add it after the WEB object):
PROJECTION +proj=tmerc +lat_0=0 +lon_0=9 +k=0.9996 +x_0=1500000 +y_0=0 +ellps=intl +units=m +no_defs no_defs END
GIS Exercise Spring 2011 7

This is the definition of the Italian National CRS Gauss-Boaga (West zone) code. Now MapServer knows that every layer without a specific definition of a CRS has to be considered in the Gauss Boaga one. Add a new layer containing the UTM stations shapefile, just before the stationsgb layer. Name the new LAYER object stations. Also in this case, use different styles and labels for the features having different values in the COMP_CODE field. In order to reproject the stations data from the UTM CRS to the Gauss Boaga CRS, you have to nest a PROJECTION object inside the LAYER object. For this you have to specify the correct EPSG code for the UTM CRS (WGS84 / UTM zone 32N). Reloading the webGIS and activate both stations layers, we will notice a difference between the stationsgb and the stations points (up to 200 m). This happens because we have set the two CRS without defining which projection parameters have to be used. In some cases this is not necessary, because there is just one set of parameters and these are already defined inside the EPSG file. Additional notes: On the contrary, in the case of the Italian Gauss Boaga CRS it is necessary, because there is more than one set of parameters to project to UTM. We can use the mean values, as we already did, but we will get this level of accuracy. If we want to improve the accuracy of the transformation we must use local parameters. But therefore MapServer needs to know which parameters have to be used. In this case you have to add the definition of the transformation within the main PROJECTION object (the one nested in the MAP object):
+proj=tmerc +lat_0=0 +lon_0=9 +k=0.9996 +x_0=1500000 +y_0=0 +ellps=intl +units=m +no_defs no_defs +towgs84=-104.1,-49.1,-9.9, 0.971,-2.917, 0.714,-11.68

These parameters, and also others that can be used for various countries throughout Europe, can be found at the CRSEU website: http://www.crs-geo.eu/nn_124226/crseu/EN/CRS__Description/crsnational__node.html?__nnn=true
GIS Exercise Spring 2011 8

The definition of the Italian National CRS Gauss Boaga and its projection to UTM could be sinthetized, with the same final effect, in the simple specification of the Gauss Boaga EPSG code inside the main PROJECTION object:
PROJECTION init=epsg:3003 END

MapServer is infact able to recognize the Gauss Boaga CRS from its EPSG code and to compute the right reprojection from UTM to it.

Trying to
Add a lakes layer to the mapfile after municipalities and before stations: - make it always visible - use the ellipse symbol with a radius of 2 2 with a cyan color for the layer style - add labels, trying out also the BACKGROUNDCOLOR parameter for them Add a railways line layer after lakes and before stations: - make them visible on demand - make them not visible when scale is > 100000 (use MAXSCALEDENOM) - edit the ellipse symbol (named line10-10) with a radius of (1 1), adding the following parameter to it to define a dashed line ( draw for 10 pixels, then draw for 10 pixels):
PATTERN 10 10 END

do

not

- combine the following three symbols to build the symbology for railways

(its

sufficient to define three subsequent STYLE objects inside the CLASS object, keeping in mind that they are drawn in the same order as they are defined here)
black point symbol, with SIZE 7 yellow point symbol, with SIZE 5 black line10-10 symbol, with SIZE 1
GIS Exercise Spring 2011 9

Adding raster layers


Add the four raster layers after municipalities and before lakes. You have to call the raster layers as NWraster, NEraster, SEraster and SWraster. Rasters are added to MapServer using the value raster for the TYPE parameter. Let them be visible only at scales > 2000 and < 25000 using the parameters MINSCALEDENOM and MAXSCALEDENOM.

Adding a MapInfo layer


The last layer is the MapInfo file containing alluvional cones. Since MapServer uses shapefiles as its default vector format, when you need to add data in different vector formats you have to specify the OGR value for the CONNECTIONTYPE parameter in the LAYER object. OGR is an open source library used for handling a large number of geographic vector formats. Other values for the CONNECTIONTYPE parameter allow you to add data coming from other sources (e.g. ArcSDE, PostGIS, Oracle Spatial, WMS, etc). Add an alluvional-cones line layer after railways and before stations. Let it be visible only when the scale is < 100000. For example of what alluvional cones should look like:

GIS Exercise Spring 2011

10

Enabling queries on the layers


Municipalities and both stations layers must be queriable by users. In order to enable queries on those layers, you have to instruct MapServer which HTML files it must use as template files for query results. This is done by adding the parameters HEADER, FOOTER and TEMPLATE to the layers and using the following filenames as values:
Municipalities Stations

header.html footer.html query.html

header_f.html footer.html query_f.html

NOTE: if the name of the layer is always written as given to you and the STATUS parameter is set to ON, then you can use the checkboxes on the left side of your map to turn the layers on and off and the query function is usable. To check if everything works correctly, it is sufficient to select the Query radio button (on the left of the scalebar) and click on a municipality or station. A query result page should appear. In order to enable also attribute queries on municipalities and stations, the following METADATA object has to be nested inside the LAYER objects:
METADATA qstring_validation_pattern '.' END

You can check if attribute queries are working correctly by selecting the Search radio button (right column), choosing Municipalities, copying and pasting one of the examples displayed at the bottom of the webGIS interface (Query String Syntax), selecting All data and clicking SEARCH.

GIS Exercise Spring 2011

11

Here are two examples with SEARCH function and its result page: 1 - Query String: ([POPULATION]>15000)

GIS Exercise Spring 2011

12

2 - Query String: ('[COMPANY]'= 'Ferrovie Nord Milano')

GIS Exercise Spring 2011

13

LICENSE
This document is realised under the following license:

Creative Commons , Attribution Noncommercial - Share Alike , 3.0 Creative Commons , Attribuzione - Non commerciale - Condividi allo stesso modo , 3.0

More information Use conditions http://creativecommons.org/licenses/by-nc-sa/3.0 http://creativecommons.org/licenses/by-nc-sa/3.0/deed.it Legal Code (the full license) http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode

GIS Exercise Spring 2011

14

You might also like