Professional Documents
Culture Documents
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.
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
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:
[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
Choose your preferred style for the scalebar. Remember that it should be useful and clearly readable, for instance:
[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]
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.
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.
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
10
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.
11
Here are two examples with SEARCH function and its result page: 1 - Query String: ([POPULATION]>15000)
12
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
14