Using MapServer
===============
Software Installation
---------------------
`MapServer `_ can be used to provide a number of OGC Web Services (OWS) types, such as the Web Map Service (WMS), Web Feature Service (WFS) and Web Coverage Service (WCS) standards which are the current focus of interest for the OneGeology portal. In the following sections we run through how to configure MapServer so it can provide any one of these three service types.
MapServer will work both on Windows and Linux operating systems (both 32-bit and 64-bit), and with a web server of your choosing, the permutations are many and we can not cover all of them, below we take you through some common installations, using Apache HTTP as the web server.
The simplest way to set up MapServer on a Windows server is to use the MapServer for Windows (MS4W) installer provided by Gateway Geomatics, this installs a 32-bit version of the Apache HTTP web server and the 32-bit version of MapServer as well as some demo applications. For those wishing to use a 64-bit version of Apache HTTP web server, we recommend Apache Lounge and GISInternals. For installation of MapServer on Linux, (with Apache HTTP web server) we recommend Ubuntu and the Personal Package Archives.
Installing MS4W
^^^^^^^^^^^^^^^
All versions of the MS4W software package, including the latest version are available from the `Gateway Geomatics MS4W `_ web site.
Two options are available, a downloadable zip of all the appropriate binaries or an installer. We will use the installer here as it the easiest option for those setting up a OneGeology service for the first time. The installer is available from the `Download Packages `_ page. Download the latest setup file, for us this was ms4w-3.2.2-setup.exe, to a temporary location on your server.
.. figure:: images/ms4w-setup.png
Run the setup.exe file as an Administrator. A small dialogue window will pop-up appear detailing the licensing agreement. Please take the time to read the license, and if you accept the conditions click on the *I Agree* button.
.. figure:: images/MS4W-agree-license.png
The next screen will offer a selection of installation options. To run a OneGeology service you only need to accept the default options, but you can choose other options if you wish, such as p.mapper (if you want to use PHP//MapScript), or MapBender, OpenLayers, or GeoMoose, if you want to develop your own client application.
.. figure:: images/ms4w-select-options.png
Once you have made your option selection select *Next* and choose an installation folder for the MS4W folder. The default location will be into your *C:* drive.
.. figure:: images/MS4W-default-path.png
**Note** You shouldn't specify the MS4W folder as this will be created as part of the installation.
Finally, you need to choose a port to run your web service on. You should use the default port of 80 if at all possible, otherwise other official web server ports are: 591, 8008, and 8080.
.. figure:: images/MS4W-default-port.png
When you have finished click *Install* the dialogue will show you the progress of the installation process. If the installer finds that you are missing the appropriate Microsoft Visual C++ distribution to run MapServer, then it will provide an additional window as part of the installation, providing a license agreement which you need to agree to and *Install*.
.. figure:: images/MS4W-installCpp.png
If the installation works correctly you will be taken to a page on your newly installed web server as below:
.. figure:: images/MS4W-localhost-post-install.png
Configuring the exemplar services
"""""""""""""""""""""""""""""""""
Obtain the OneGeology template application in the 20Mbytes approx. sized 1G\_WMS-exemplar-data-MS6-update.zip file from the `BGS FTP website `_.
If you are using a web browser clicking on this URL may take you directly to it without requesting a password. If you prefer to use the older DOS prompt style FTP user interface then as normal with such anonymous ftp services enter anonymous if prompted for a userid and type your email address as the password to allow the FTP manager to monitor who is using the service.
Unzip the OneGeology template application to the same drive and directory level as the MS4W location resulting from the MapServer installation e.g. if you installed MS4W on C:\\MS4W then point the unzip extract to C:. It should create a number of files inside the *ms4w* directory. The main part of the two example applications are inside a BGS\_Bedrock\_and\_Superficial\_Geology directory (for the shapefile based example) and BGS\_Bedrock\_Raster\_Map directory (for the image file based example) which will be created inside ms4w\\apps\\cookbookExemplars.
The MapServer executables for the two applications are found in similarly named folders in ms4w\\apps\\exemplars, and the web server configuration files are found in ms4w\\httpd.d
After unzipping the exemplar files, restart your web service. Now when you browse to localhost/index.html you will get links to the two exemplar services.
Configuring your own services using the exemplars as examples
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
C:\\ms4w\\Apache\\cgi-bin\\
'''''''''''''''''''''''''''
Create a folder inside ms4w\\Apache\\cgi-bin with a name that follows the OneGeology profile.
It is not within the scope of OneGeology at this stage to address the problem of translating geological terms between different languages so the above service can be in the language you usually use for your data. However, if you already have your data in other languages, in particular English if that is not your default language, then we would like to encourage you to provide services in these other languages as well. These should be served from separate services with different URLs. In MapServer this means making another copy of the above directory and renaming it to use the appropriate language in the directory name.
At this stage you should have a sub-folder structure within c:\\ms4w\\Apache\\cgi-bin like:
.. code-block:: text
\BGS_Bedrock_and_Superficial_Geology
Holds MapServer cgi libraries for the ‘BGS_Bedrock_and_Superficial_Geology’ shapefile-based exemplar service.
\BGS_Bedrock_Geology
Holds MapServer cgi libraries for the ‘BGS_Bedrock_Geology’ raster-based exemplar service.
\Your-Organization-code_Your-Service-Theme
EMPTY
\Your-Organization-code_Your-alternate-language(service)-code_Your-Service-Theme
EMPTY
.. todo::
need to add content about copying cgi-bin executables from ms4w install to the exemplar folders (or overwring that content ~ need to check zip). Also need to add section about renaming the mapserv.exe to ows or wms. And add to the section above about copying to a new folder when creating your own service.
C:\\ms4w\\apps\\cookbookExemplars
'''''''''''''''''''''''''''''''''
Inside the \\ms4w\\apps\\cookbookExemplars folder you will find two folders: "BGS\_Bedrock\_and\_Superficial\_Geology" and "BGS\_Bedrock\_Raster\_Map". These folders contain the exemplar data, and map configuration files.
We will assume you are basing your service on the BGS\_Bedrock\_and\_Superficial\_Geology example; substitute with BGS\_Bedrock\_Raster\_Map if that is closer to your requirements. When you have decided which exemplar service is most suitable for your needs, you should copy that exemplar folder to a new folder that will be your new service.
Note the names of these folders do not have to match the names of the service, but you would be advised to ensure that the folder name gives some hint as to its contents and purpose. For example we call one of our exemplar folders "BGS\_Bedrock\_Raster\_Map" to indicate that this service application holds a raster map as datasource, rather than a shapefile.
Make more copies with appropriate names if you are also making multiple language services.
Inside this folder there is a wwwroot\\index.html file. This has some example queries which will enable you to test your service when you have set it up. For these to work for your new service you will need to edit the file and change all occurrences of the string "BGS\_Bedrock\_and\_Superficial\_Geology" with the name you have created for your service.
C:\\ms4w\\httpd.d\\
'''''''''''''''''''
Now you will need to create an httpd conf file in the \\ms4w\\httpd.d\\ folder that has the same name as your service; for example, the "BGS\_Bedrock\_and\_Superficial\_Geology" exemplar service has a conf file called "httpd\_BGS\_Bedrock_and\_Superficial\_Geology\_Exemplar.conf" and the "BGS\_Bedrock\_Geology" exemplar service has a corresponding conf file called "httpd\_BGS\_Bedrock\_Geology.conf"
You need to copy one of the exemplar .conf files and rename it to match the name of your service, you will then need to change the paths in the file to match your service name and folder configuration.
Using the raster exemplar service (as shown below), you will need to change all references to "BGS\_Bedrock\_Geology" to match the name of your service, and all references to "BGS\_Bedrock\_Raster\_Map" to match the name of your app folder. Note, you do not need to add the drive letter.
.. code-block:: apacheconf
Alias /BGS_Bedrock_Geology/ "/ms4w/apps/cookbookExemplars/BGS_Bedrock_Raster_Map/www/"
AllowOverride None
Options Indexes FollowSymLinks Multiviews
Order allow,deny
Allow from all
SetEnvIf Request_URI "/cgi-bin/exemplars/BGS_Bedrock_Geology/ows" MS_Mapfile=/ms4w/apps/cookbookExemplars/BGS_Bedrock_Raster_Map/onegeology.map
SetEnvIf Request_URI "/fcgi-bin/exemplars/BGS_Bedrock_Geology/ows" MS_Mapfile=/ms4w/apps/cookbookExemplars/BGS_Bedrock_Raster_Map/onegeology.map
C:\\ms4w\\Apache\\htdocs\\
''''''''''''''''''''''''''
Now you should edit the index.html file in the Apache web root \\ms4w\\Apache\\htdocs\\ and add a link to your new service. Note, the link you use is the value of the Alias (line one of the httpd conf file).
Again make more copies if making multiple language services.
Installing and configuring Apache HTTP web server
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
MS4W, as described above, installs both MapServer and the Apache HTTP webserver software. Other installations of MapServer require configuring of the web server as a separate process. This section takes you through installing alternate Apache HTTP webserver software, and through the additional configuration you will need to do to create a OneGeology service that follows the same pattern as above.
64-bit Apache HTTP server
"""""""""""""""""""""""""
if tyou want to run a 64-bit version of MapServer on Windows such as provided by GISInternals, you will also need to in install a 64-bit version of Apache.
If instead you want to use the latest stable release of Apache-HTTP, that is the version 2.4.n releases (latest is currently 2.4.29), you must instead go to the Apache Lounge site: `http://www.apachelounge.com/download/ `_. There are several options here both in server architecture (32 bit and 64 bit), and server functionality, for you to choose from to fit your needs.
For the purposes of this example we have selected a 64-bit package from Apache Lounge and installed it to our C:\\ drive as C:\\Apache24\\.
httpd.d configuration files
'''''''''''''''''''''''''''
For this installation we will now create a httpd.d folder on our D:\\ drive, to hold our OneGeology service configuration files, as: D:\\WxS\\ms\\httpd.d\\ , and create an http\_ file (i.e. ‘httpd_BGS_Bedrock_and_Superficial_Geology_Exemplar.conf’) for our exemplar service as below.
.. code-block:: apacheconf
#===============================================================================================#
Alias /BGS_Bedrock_and_Superficial_Geology_Exemplar/ "D:/WxS/ms/apps/cookbookExemplars/BGS_Bedrock_and_Superficial_Geology/wwwroot/"
AllowOverride None
Options FollowSymLinks Multiviews
Require all granted
#===============================================================================================#
SetEnvIf Request_URI "/cgi-bin/exemplars/BGS_Bedrock_and_Superficial_Geology/wms" MS_Mapfile=D:/WxS/ms/apps/cookbookExemplars/BGS_Bedrock_and_Superficial_Geology/onegeology.map
Note, that there is a change in the way access permissions are handled between versions 2.2.n and 2.4.n of Apache, so if you are copying the existing MS4W httpd\_ conf files you will need to change your information; that is, you will need to replace the ‘*Order allow,deny*’ and ‘*Allow from all*’ directives with ‘*Require all granted*’
apache.conf
'''''''''''
Finally you will need to add some information to the Apache-HTTP server configuration file (C:\\Apache24\\conf\\httpd.conf) as per the below snippets.
.. code-block:: apacheconf
...
# Alias: Maps web paths into filesystem paths and is used to
# access content that does not live under the DocumentRoot.
Alias /ms_tmp "D:/WxS/ms/out/tmp"
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the target directory are treated as applications and
# run by the server when requested rather than as documents sent to the
# client.
ScriptAlias /cgi-bin/ "C:/Apache24/cgi-bin/"
...
...
AllowOverride None
Options None
Require all granted
...
# Parse our MapServer Apache conf files
Include D:/WxS/ms/httpd.d/httpd_*.conf
Create alias configuration files
''''''''''''''''''''''''''''''''
Next we need to create an alias to our data files and MapServer html templates. The way you do this varies considerably depending on your Linux version. In older versions of Ubuntu these aliases are created in the **alias** file located in the **/etc/apache2/conf.d/** directory. In recent versions you should add these aliases to the **httpd.conf** file in **/etc/apache2/**
We need to create information in the style of the contents of the .conf files (found in our unzipped contents ../ms4w/httpd.d/ directory). We will combine the contents of both .conf files (that deal with the html templates and data content) into our ‘alias’ configuration file.
You may choose any text editor, but probably the easiest to use is nano.
.. code-block:: sh
# cd /etc/apache2
# nano httpd.conf
.. code-block:: apacheconf
Alias /BGS_Bedrock_Geology /usr/local/src/ms4w/apps/cookbookExemplars/BGS_Bedrock_Raster_Map/wwwroot/
AllowOverride None
Options Indexes FollowSymLinks Multiviews
Order allow,deny
Allow from all
Alias /BGS_Bedrock_and_Superficial_Geology /usr/local/src/ms4w/apps/cookbookExemplars/BGS_Bedrock_and_Superficial_Geology/wwwroot/
AllowOverride None
Options Indexes FollowSymLinks Multiviews
Order allow,deny
Allow from all
^O (to save changes)
ENTER
^X (to exit)
You will probably need to restart the Apache web server at this point:
.. code-block:: sh
#/etc/init.d/apache2 restart
We can test these using the lynx browser
.. code-block:: sh
#lynx http://127.0.0.1/BGS_Bedrock_and_Superficial_Geology/index.html
or using wget:
.. code-block:: sh
#cd /tmp
#wget http://127.0.0.1/BGS_Bedrock_Geology/index.html
#less index.html
Installing GISInternals packages for Windows
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The most recent versions of the GISInternals GDAL and MapServer packages are available online at: `http://www.gisinternals.com `_
In most instances we would recommend using the MS4W packages to install Apache and MapServer to give yourself a Windows implementation of MapServer, but in some instances, for example if you want the latest version of MapServer or if you want to use 64-bit software, you can alternatively use one of the GISinternals packages for your MapServer service.
In this section we will assume you are familiar with configuring a MS4W service and just provide some notes to assist you configure this alternative MapServer on Windows service using Apache-HTTP as your web server.
Once you have a working web service installed, you now need to obtain the corresponding GISInternals binaries, for example in this case we downloaded the zip file **release-1600-x64-gdal-1-9-2-MapServer-6-2-0.zip**, and unzipped onto our C:\\ drive as C:\\apps\\gisinternals\\.
Now we must run the SDKShell.bat batch file to set up some environment variables, for example it adds the following locations to the system PATH:
.. code-block:: text
C:\apps\gisinternals\bin;
C:\apps\gisinternals\bin\gdal\python\osgeo;
C:\apps\gisinternals\bin\proj\apps;
C:\apps\gisinternals\bin\gdal\apps;
C:\apps\gisinternals\bin\ms\apps;
C:\apps\gisinternals\bin\gdal\csharp;
C:\apps\gisinternals\bin\ms\csharp;
C:\apps\gisinternals\bin\curl;
The MapServer executable file (mapserv.exe) is found in the C:\\apps\\gisinternals\\bin\\ms\\apps folder. As ever, you can check the version by using the -v option in a command window like:
.. code-block:: doscon
c:\apps\gisinternals\bin\ms\apps>mapserv.exe -v
MapServer version 7.1-dev OUTPUT=PNG OUTPUT=JPEG OUTPUT=KML SUPPORTS=PROJ SUPPORTS=AGG SUPPORTS=FREETYPE SUPPORTS=CAIRO SUPPORTS=SVG_SYMBOLS SUPPORTS=SVGCAIRO SUPPORTS=ICONV SUPPORTS=FRIBIDI SUPPORTS=WMS_SERVER SUPPORTS=WMS_CLIENT SUPPORTS=WFS_SERVER SUPPORTS=WFS_CLIENT SUPPORTS=WCS_SERVER SUPPORTS=SOS_SERVER SUPPORTS=FASTCGI SUPPORTS=THREADS SUPPORTS=GEOS INPUT=JPEG INPUT=POSTGIS INPUT=OGR INPUT=GDAL INPUT=SHAPEFILE
Data
""""
You may put your OneGeology data for your service (and the Mapfile etc) anywhere on your server, but here we will follow the same pattern as we have for used for the MS4W services. In this case we have extracted the exemplar shapefile data to a location on our D:\\ drive as:
* D:\\WxS\\ms\\apps\\cookbookExemplars\\BGS_Bedrock_and_Superficial_Geology
* data (folder)
* templates (folder)
* wwwroot (folder)
* onegeology.map (file)
* ICSClasses.txt (file)
You will need to make a few change to the Mapfile from the downloaded exemplar file. For example you will need to tell MapServer where to find the proj files so that you can reproject your data. You do this by adding a CONFIG statement at the top of the Mapfile like:
.. code-block:: mapfile
MAP
CONFIG "PROJ_LIB" "C:/apps/gisinternals/bin/proj/SHARE"
You will also need to change the IMAGEPATH statement to point at your chosen temporary file location (within the WEB section of the Mapfile) like:
.. code-block:: mapfile
#====================================================================#
# Start of WEB interface definition (including WMS enabling metadata)
#====================================================================#
WEB
HEADER "tmpl/query_header.html"
FOOTER "tmpl/query_footer.html"
IMAGEPATH "D:/WxS/ms/out/tmp/"
IMAGEURL "/ms_tmp/"
MapServer cgi-bin
"""""""""""""""""
For this installation we will now create some folders in the Apache-HTTP cgi-bin folder to hold our copy of the mapserv.exe executable (which we will rename as ‘wms’) as:
* C:\\Apache24\\cgi-bin (folder)
* exemplars (folder)
* BGS_Bedrock_and_Superficial_Geology (folder)
* wms (file)
At this stage you will have a working MapServer service such that a request like the below (where we also specify the ‘map’ variable explicitly) will give you a GetCapabilities reponse document.
.. code-block:: text
http://[your-server-name]/cgi-bin/exemplars/BGS_Bedrock_and_Superficial_Geology/wms?
service=WMS&
request=GetCapabilities&
map=D:/WxS/ms/apps/cookbookExemplars/BGS_Bedrock_and_Superficial_Geology/onegeology.map&
Installing MapServer on Linux using PPAs
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This installation guide will give you simple step-by-step instructions of installing MapServer onto a Linux server and assumes you have an Apache HTTP webserver already running.
Users of Ubuntu/Debian systems should find that they are able to get the latest version of MapServer by adding the following Personal Package Archives to their system’s software sources:
ppa:ubuntugis/ppa
Official stable UbuntuGIS packages
ppa:ubuntugis/ubuntugis-unstable
Unstable releases of Ubuntu GIS packages
For example, the Official stable version PPA can be added like:
.. code-block:: sh
sudo add-apt-repository ppa:ubuntugis/ppa
sudo apt-get update
And the MapServer files need to create a OneGeology service can be added like:
.. code-block:: sh
sudo apt install cgi-mapserver mapserver-bin tinyows
The MapServer executable file is called **mapserv** and in our installation is found at //usr//bin//mapserv
.. code-block:: sh
#cp mapserv /usr/lib/cgi-bin/mapserv
The mapserv binary created needs to have —rwxr-xr-x permissions to be able to execute
You can check permissions using:
.. code-block:: sh
#ls —l mapserv
If needed you can change permissions using:
.. code-block:: sh
#chmod 755 mapserv
To test you have compiled mapserv with all appropriate options you can check the version:
.. code-block:: sh
#./mapserv —v
You should get an output like:
.. code-block:: text
MapServer version 7.0.4 OUTPUT=PNG OUTPUT=JPEG OUTPUT=KML SUPPORTS=PROJ SUPPORTS=AGG SUPPORTS=FREETYPE SUPPORTS=CAIRO SUPPORTS=SVG_SYMBOLS SUPPORTS=RSVG SUPPORTS=ICONV SUPPORTS=FRIBIDI SUPPORTS=WMS_SERVER SUPPORTS=WMS_CLIENT SUPPORTS=WFS_SERVER SUPPORTS=WFS_CLIENT SUPPORTS=WCS_SERVER SUPPORTS=SOS_SERVER SUPPORTS=FASTCGI SUPPORTS=THREADS SUPPORTS=GEOS INPUT=JPEG INPUT=POSTGIS INPUT=OGR INPUT=GDAL INPUT=SHAPEFILE
To test you have mapserv accessible through your web server you can use the ‘lynx’ text browser package (available through apt-get):
.. code-block:: sh
#apt-get install lynx
#lynx http://127.0.0.1/cgi-bin/mapserv
Or you could simply use the wget program (which will retrieve the output as a text file):
.. code-block:: sh
#cd /tmp
#wget http://127.0.0.1/cgi-bin/mapserv
#less mapserv
You should get the message "No query information to decode. QUERY_STRING is set but empty"
Congratulations! You have now got MapServer installed and configured to run in your web server.
Configuring MapServer exemplar services on a LAMP server
""""""""""""""""""""""""""""""""""""""""""""""""""""""""
We shall now configure the two BGS exemplar services (a shapefile version and a raster version) available from the BGS FTP server.
.. code-block:: sh
#cd /usr/local/src
#wget ftp://ftp.bgs.ac.uk/pubload/OneGeology/1G_WMS-exemplar-data-MS6-update.zip
#unzip 1G_WMS-exemplar-data-MS6-update.zip
We now need to move the contents of the zip file to the correct locations on our server.
First we move our index pages to the root directory of the web server (/var/www/ on Ubuntu).
.. code-block:: sh
#mv ms4w/Apache/htdocs/* /var/www/
Create a ows shell script
'''''''''''''''''''''''''
Next we need to create a ‘ows’ shell script for each of our Map Services; which we need to place in an associated directory.
.. code-block:: sh
#cd /usr/lib/cgi-bin
#mkdir --parents exemplars/BGS_Bedrock_Geology
#nano exemplars/BGS_Bedrock_Geology/ows
.. code-block:: sh
#!/bin/sh
MS_Mapfile=/usr/local/src/ms4w/apps/cookbookExemplars/BGS_Bedrock_Raster_Map/onegeology.map
export MS_Mapfile
exec /usr/lib/cgi-bin/mapserv
exit 0
^O (to save changes)
ENTER
^X (to exit)
.. code-block:: sh
#chmod 755 exemplars/BGS_Bedrock_Geology_Raster/ows
and similarly for our shapefile based service
.. code-block:: sh
#mkdir --parents exemplars/BGS_Bedrock_and_Superficial_Geology
#nano exemplars/BGS_Bedrock_and_Superficial_Geology/ows
.. code-block:: sh
#!/bin/sh
MS_Mapfile=/usr/local/src/ms4w/apps/cookbookExemplars/BGS_Bedrock_and_Superficial_Geology/onegeology.map
export MS_Mapfile
exec /usr/lib/cgi-bin/mapserv
exit 0
^O (to save changes)
ENTER
^X (to exit)
.. code-block:: sh
#chmod 755 exemplars/BGS_Bedrock_and_Superficial_Geology/ows
Modify paths in the Mapfile
''''''''''''''''''''''''''''
The final step is to modify the WEB > IMAGEPATH path (to "/var/tmp/") and the WEB > IMAGEURL path (to "/tmp/") in each of our onegeology.map files
That’s it!
Alternative MapServer configurations
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
If you want to use other permutations and get stuck remember you can ask the OneGeology Helpdesk any MapServer configuration issues in relation to your OneGeology services, and we will endeavour to help you.
As well as the OneGeology Helpdesk there are a number of other resources to help guide you in your installation, including:
MS4W documentation
https://ms4w.com/documentation.html
MapServer documentation
http://mapserver.org/documentation.html
GIS StackExchange (MapServer questions)
https://gis.stackexchange.com/search?q=is%3Aquestion+%5BMapServer%5D
GIS StackExchange (MS4W questions)
https://gis.stackexchange.com/search?q=is%3Aquestion+%5Bms4w%5D
StackOverflow (Apache web server questions)
https://stackoverflow.com/search?q=is%3Aquestion+%5Bapache%5D
Mailing Lists
http://mapserver.org/community/lists.html#mapserver-users
https://lists.ms4w.com/mailman/listinfo/ms4w-users
MapServer and IIS
"""""""""""""""""
You may use the IIS web server instead of Apache to run the MapServer CGI. See the previous cookbook for details of how to do this with IIS version 6. We haven't been able to update the cookbook for the latest version of IIS, but the MapServer documentation (`IIS Setup for MapServer `_) gives a good guide for how to do this in general for IIS 7 and up.
Compiling MapServer on Linux
""""""""""""""""""""""""""""
You may wish to compile your own version of MapServer on a \*nix operating system of your own choosing. We haven't done this for a while and the guidance in our previous cookbook was very out of date. There is guidance on the MapServer site that takes you through the process (`Compiling on Unix `_)
General configuration
---------------------
MapServer services are configured through the use of Mapfile templates (\*.map). You can use a single Mapfile to configure a service, or you can use a master file that includes other files. The benefits of using multiple files include ease of maintenance across multiple similar services, and readability. Here we will use multiple files to show how the various parts of a MapServer (OGC) service need to be configured. You can configure multiple service types through a single configuration, if desired.
The start of a Mapfile begins with a **MAP** statement and ends with an **END** statement
Comments are shown by lines beginning with a **#** sign
The order of statements in the Mapfile doesn't matter, here we tend to group alphabetically for readability.
For further details on Mapfile configuration options available see http://mapserver.org/mapfile/map.html
.. code-block:: mapfile
MAP
# You must supply a NAME for your service, which in OGC services will be the Root Layer Name
NAME "BGS_EN_Bedrock_and_Superficial_Geology"
# You may supply some extra configuration details using one or more CONFIG statements
CONFIG "MS_ERRORFILE" "D:/logs/MapServer/Pub/TFL/ms_error.log"
CONFIG "PROJ_LIB" "C:/apps/gisinternals/bin/proj/SHARE"
CONFIG "PGEO_DRIVER_TEMPLATE" "DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=%s"
CONFIG "OGR_SKIP" "ODBC"
# You may configure the level of DEBUG detail you want from your service
DEBUG 0
# You must supply an EXTENT, which defines the extent of the map to be created
# The EXTENT is specified in min-x, min-y, max-x, max-y order
# The EXTENT is specified in units of the default service projection
EXTENT -8.6476 49.8639 1.76943 60.8622
# You may specify a set of fonts for your service
# The location of any included file is relative to the location of Mapfile
# See below section on alternate character set support
FONTSET "../DefaultMapIncludes/fontset.lst"
# You may supply information on the output formats you wish to support
# See http://mapserver.org/mapfile/outputformat.html#outputformat
INCLUDE "../DefaultMapIncludes/BGS-service-std-output-plus1.map"
# You must provide information on the styling of the legend
LEGEND
IMAGECOLOR 255 255 255
STATUS ON
KEYSIZE 18 12
LABEL
TYPE BITMAP
SIZE MEDIUM
COLOR 0 0 89
END
END
# You may specify the Maximum Size of the map image
MAXSIZE 3072
# You must supply the default PROJECTION for the service
# For OneGeology services this would normally be EPSG:4326
PROJECTION
"init=epsg:4326"
END
# You may specify the path to the folder holding your shapefile data
# The path is relative to the location of the mapfile
SHAPEPATH "data"
# You must specify the default SIZE of the map image
# SIZE is specified in Pixels
SIZE 600 800
# You must state whether the map is active or not.
STATUS ON
# You can specify the location of a Symbol set that defines symbols used in your styles
SYMBOLSET "../DefaultMapIncludes/symbols.sym"
# You must specify the UNITS of the map coordinates
UNITS dd
WEB
#==================================================================================#
# The WEB section includes all the general configuration for the WEB service
#==================================================================================#
# See below section on the WEB object
END
# LAYER specific configuration would follow
INCLUDE "BedrockLithology.map"
...
END
The **WEB** section of the Mapfile (extract shown below) sets general information for your web service including a general description, contact information, etc.
.. code-block:: mapfile
WEB
# The FOOTER template is applied after everything else is sent for a query result
FOOTER "templates/query_footer.html"
# The HEADER template is applied before anything else is sent for a query result
HEADER "templates/query_header.html"
# IMAGEPATH is the temporary directory for writing temporary files and images
IMAGEPATH "/ms4w/tmp/ms_tmp/"
# IMAGEURL Base URL for IMAGEPATH
IMAGEURL "/ms_tmp/"
METADATA
#==================================================================================#
# The METADATA section is used to populate information into the Web Service metadata response
# The METADATA detailed here is part of the information found in a GetCapabilities response
#==================================================================================#
# OWS_ metadata applies to all available services (WMS, WFS, WCS, SOS...)
# WCS_ metadata applies to WCS services only. Values will override an OWS setting
# WFS_ metadata applies to WFS services only. Values will override an OWS setting
# WMS_ metadata applies to WMS services only. Values will override an OWS setting
#==================================================================================#
# See below section on the WEB > METADATA object
END
END
In any **METADATA** section instead of the "WMS\_" prefix you may use "OWS\_" prefix. The "OWS\_" prefix is used by WMS, WFS, WCS, GML, and other services, so you only have to specify that metadata type once. If you have "OWS_ABSTRACT" and "WMS_ABSTRACT", the "OWS_ABSTRACT" will be used by any WFS / WCS service whilst the "WMS_ABSTRACT" will be used by the WMS.
.. code-block:: mapfile
METADATA
"OWS_ABSTRACT" "The 1:625k DiGMap data covering the whole of the United Kingdom is available in this OGC web service for all uses - including commercial use subject to the conditions in the Access Constraints section and is being served as a contribution to the OneGeology initiative (www.onegeology.org)."
"OWS_ACCESSCONSTRAINTS" "The 1:625k DiGMap data is available for free download for your personal, teaching, research, or non-commercial use (as described on http://www.bgs.ac.uk/about/copyright/non_commercial_use.html). Your use of any information provided by the British Geological Survey (BGS) is at your own risk. Neither BGS nor the Natural Environment Research Council (NERC) gives any warranty, condition, or representation as to the quality, accuracy, or completeness of the information or its suitability for any use or purpose. All implied conditions relating to the quality or suitability of the information, and all liabilities arising from the supply of the information (including any liability arising in negligence) are excluded to the fullest extent permitted by law."
"OWS_ADDRESS" "Environmental Science Centre"
"OWS_ADDRESSTYPE" "postal"
"OWS_CITY" "Keyworth"
"OWS_CONTACTELECTRONICMAILADDRESS" "enquiries@bgs.ac.uk"
"OWS_CONTACTFACSIMILETELEPHONE" "+44 (0)115 936 3200"
"OWS_CONTACTINSTRUCTIONS" ""
"OWS_CONTACTORGANIZATION" "British Geological Survey"
"OWS_CONTACTPERSON" "Garry Baker"
"OWS_CONTACTPOSITION" ""
"OWS_CONTACTVOICETELEPHONE" "+44 (0)115 936 3100"
"OWS_COUNTRY" "United Kingdom"
# The following statement enables all WMS, WFS, WCS operations on all layers in the service
# The statemant can be overridden by service specific statements, either here or in the LAYERS
"OWS_ENABLE_REQUEST" "*"
"OWS_FEES" "none"
"OWS_HOURSOFSERVICE" "Mon-Fri, 09:00-17:00"
#===========================================================================#
# OWS_KEYWORDLIST
# Put your organisation name and any other information you want to include.
# You MUST include "OneGeology" as one of the keywords.
# Do NOT use spaces after the commas in the keyword listing.
#===========================================================================#
"OWS_KEYWORDLIST" "OneGeology,geology,map,United Kingdom,bedrock,superficial,lithology,lithostratigraphy,age,MD_LANG@ENG,MD_DATE@2011-06-15"
#"OWS_ONLINERESOURCE" "http://another-service/or/some-different-path/ows?"
"OWS_POSTCODE" "NG12 5GG"
"OWS_ROLE" "PointOfContact"
"OWS_SERVICE_ONLINERESOURCE" "http://www.bgs.ac.uk/products/digitalmaps/digmapgb.html"
"OWS_SLD_ENABLED" "TRUE"
"OWS_STATEORPROVINCE" "Nottinghamshire"
#===========================================================================#
# "OWS_SRS" For WCS/WFS you need to list the default projection _FIRST_
#===========================================================================#
"OWS_SRS" "EPSG:27700 EPSG:3857 EPSG:4258 EPSG:4326"
"OWS_TITLE" "BGS Bedrock and Superficial geology"
"OWS_UPDATESEQUENCE" "2017-02-09T14:00:00Z"
"WFS_ABSTRACT" "The 1:625k DiGMap data covering the whole of the United Kingdom is available in this OGC WFS service for your personal, non-commercial use only and is being served as a contribution to the OneGeology initiative(www.onegeology.org). \
The contents of this WFS service are not intended for direct use but are transformed by a mediator layer into separate WFS services which provide data in GeoSciML. This process is described in Chapter 2 of the OneGeology WFS Cookbook available at www.onegeology.org."
"WMS_ABSTRACT" "The 1:625k DiGMap data covering the whole of the United Kingdom is available in this OGC WMS service for your personal, non-commercial use only and is being served as a contribution to the OneGeology initiative (www.onegeology.org).\
Separate bedrock geology and superficial deposits layers are available in this service. Layers available for bedrock are lithostratigraphy, age, and lithology. Layers available for superficial deposits layer are lithostratigraphy and lithology. \
For information about more of the British Geological Survey’s maps that are available digitally please visit http://www.bgs.ac.uk/products/digitalmaps/digmapgb.html"
# INSPIRE WMS must provide a bounding box for all supported projections
"WMS_BBOX_EXTENDED" "TRUE"
# You can specify which output formats you want per operation
"WMS_FEATURE_INFO_MIME_TYPE" "text/html"
"WMS_GETMAP_FORMATLIST" "image/png,image/jpeg"
"WMS_KEYWORDLIST_GEMET_ITEMS" "Bathymetry"
"WMS_KEYWORDLIST_ISO_ITEMS" "infoMapAccessService"
"WMS_KEYWORDLIST_VOCABULARY" "GEMET,ISO"
#"WMS_ONLINERESOURCE" "http://another-service/or/some-different-path/wms?"
"WMS_SRS" "EPSG:4326 EPSG:3857 EPSG:27700 EPSG:4258"
"WMS_ROOTLAYER_TITLE" "BGS Bedrock and Superficial Geology"
END
You may use the "WMS_ONLINERESOURCE" (and "OWS_ONLINERESOURCE" etc) metadata sections to change the service endpoint for your service. For example, you can do this to force users (clients) to always use the IP version of your service rather than the server name (or vice versa), or to force them to always use the cgi-bin version rather than the fcgi-bin version (or vice versa), or to get them to use a different server. That is, you can have an initial GetCapabilities response document that itself advertises a different service endpoint for the subsequent GetMap requests. There are several reasons why you might want to do this; one such reason is when you have an existing service that has multiple layers only some of some of which are conformant to OneGeology and in which the service metadata doesn't otherwise conform to the OneGeology WMS profile. In such an example, you can set up a service that has a GetCapabilities document that is conformant to the OneGeology WMS profile and which advertises only some of the layers of the other service through the use of the "WMS_ONLINERESOURCE" metadata.
The SRS specifies the coordinate system (spatial reference system) that the WMS can serve data in. These are commonly specified using EPSG codes **and must include** `EPSG:4326 `_ so that all services have at least one coordinate system in common. We would like if you could specify the Spherical Mercator projection (`EPSG:3857 `_) to allow your service to be used in Google Maps. You may specify other systems that are appropriate for your region if you wish; for example we would expect most European services to support either (or both of) `EPSG:4258 `_ and `EPSG:3034 `_ to ensure compliance with INSPIRE coordinate system requirements.
Adding alternate character set support
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
If you are serving a non-English language service, you may need or want to change the font and character sets.
To specify a font set you need to use the FONTSET keyword which references a file that contains the mappings from the font name aliases, which you will use in your Mapfile, to the actual font file names on your computer. See the MAP section above for an example of referencing a fontset file (fontset.lst)
.. code-block:: text
arial C:\Windows\Fonts\Arial.ttf
arialuni C:\Windows\Fonts\ARIALUNI.TTF
esricaves2 C:\Windows\Fonts\esri_376.ttf
fradm C:\Windows\Fonts\FRADM.TTF
khmer C:\Windows\Fonts\KhmerUI.ttf
msgothic C:\Windows\Fonts\MSGOTHIC.TTC
msmincho C:\Windows\Fonts\MSMINCHO.TTC
opensym C:\Windows\Fonts\opens___.ttf
sc C:\Windows\Fonts\DejaVuSansCondensed.ttf
scb C:\Windows\Fonts\DejaVuSansCondensed-Bold.ttf
sym C:\Windows\Fonts\symbol.ttf
verdana C:\Windows\Fonts\verdana.ttf
You only need one font specified in your Mapfile but you may list as many as you like in your fontset file.
The below example shows how you could modify the LABEL section of the LEGEND, to allow you to display Chinese characters.
.. code-block:: mapfile
LEGEND
OUTLINECOLOR 200 200 200
KEYSPACING 10 10
LABEL
TYPE truetype
FONT "msgothic"
SIZE 8
ENCODING "UTF-8"
END
END
The important parts to note in the above example are:
* TYPE truetype (the default is TYPE bitmap)
* FONT "msgothic" (the font alias we set up in our fontset.lst file)
* SIZE 8 (size should be specified in points, you can’t use words like “small” or “medium” which you do with bitmap fonts.)
* ENCODING "UTF-8" (You must also save your Mapfile in this character set encoding).
Creating your own symbology
^^^^^^^^^^^^^^^^^^^^^^^^^^^
symbols.sym, is a set of defined symbols that can be used to style your map layers.
.. code-block:: mapfile
SYMBOLSET
SYMBOL
NAME "circle"
TYPE ellipse
FILLED false
POINTS
1 1
END
ANCHORPOINT 0.5 0.5
END
SYMBOL
NAME "circlef"
TYPE ellipse
FILLED true
POINTS
10 10
END
ANCHORPOINT 0.5 0.5
END
SYMBOL
NAME "divides"
TYPE TRUETYPE
FONT "opensym"
CHARACTER '∣'
FILLED true
ANTIALIAS true
END
SYMBOL
NAME "v-line"
TYPE vector
FILLED false
POINTS
0 0
5 10
10 0
END
END
END
Example include files
^^^^^^^^^^^^^^^^^^^^^
.. code-block:: mapfile
# BGS-Attribution.map
# May be included at SERVICE and/or LAYER metadata level
"WMS_ATTRIBUTION_LOGOURL_FORMAT" "image/gif"
"WMS_ATTRIBUTION_LOGOURL_HREF" "http://ogc.bgs.ac.uk/img/bgs_c_t_275x60.gif"
"WMS_ATTRIBUTION_LOGOURL_HEIGHT" "60"
"WMS_ATTRIBUTION_LOGOURL_WIDTH" "275"
"WMS_ATTRIBUTION_ONLINERESOURCE" "http://www.bgs.ac.uk/"
"WMS_ATTRIBUTION_TITLE" "British Geological Survey (BGS)"
"WMS_AUTHORITYURL_NAME" "BritishGeologicalSurvey"
"WMS_AUTHORITYURL_HREF" "http://data.bgs.ac.uk/ref/BritishGeologicalSurvey"
.. code-block:: mapfile
# BGS-service-std-output-plus1.map
# Included at the map level to extend or modify the output formats available to the service
# For example we can use this to enable GeoJSON and zipped shapefie as formats for WFS
#IMAGECOLOR: Background color for the map canvas
IMAGECOLOR 255 255 255
IMAGETYPE png
OUTPUTFORMAT
NAME png
DRIVER "AGG/PNG"
MIMETYPE "image/png"
IMAGEMODE RGBA
EXTENSION "png"
TRANSPARENT ON
FORMATOPTION "INTERLACE=ON,TRANSPARENT=ON"
END
OUTPUTFORMAT
NAME "SHAPEZIP"
DRIVER "OGR/ESRI Shapefile"
MIMETYPE "application/shapefile"
FORMATOPTION "STORAGE=filesystem"
FORMATOPTION "FORM=zip"
FORMATOPTION "FILENAME=shape.zip"
END
OUTPUTFORMAT
NAME "SPATIALITEZIP"
DRIVER "OGR/SQLITE"
MIMETYPE "application/spatialite"
FORMATOPTION "DSCO:SPATIALITE=YES"
FORMATOPTION "STORAGE=memory"
FORMATOPTION "FORM=zip"
FORMATOPTION "FILENAME=result.db"
END
OUTPUTFORMAT
NAME "MIDMIF"
DRIVER "OGR/MapInfo File"
FORMATOPTION "STORAGE=filesystem"
FORMATOPTION "FORM=multipart"
FORMATOPTION "DSCO:FORMAT=MIF"
FORMATOPTION "FILENAME=result.mif"
END
OUTPUTFORMAT
NAME "MultiMIDMIF"
DRIVER "OGR/MapInfo File"
FORMATOPTION "STORAGE=filesystem"
FORMATOPTION "FORM=multipart"
FORMATOPTION "DSCO:FORMAT=MIF"
FORMATOPTION "FILENAME=result"
END
OUTPUTFORMAT
NAME "CSV"
DRIVER "OGR/CSV"
MIMETYPE "text/csv"
FORMATOPTION "LCO:GEOMETRY=AS_WKT"
FORMATOPTION "STORAGE=filesystem"
FORMATOPTION "FORM=simple"
FORMATOPTION "FILENAME=result.csv"
END
OUTPUTFORMAT
NAME "CSVSTREAM"
DRIVER "OGR/CSV"
MIMETYPE "text/csv; streamed"
FORMATOPTION "LCO:GEOMETRY=AS_WKT"
FORMATOPTION "STORAGE=stream"
FORMATOPTION "FORM=simple"
FORMATOPTION "FILENAME=result.csv"
END
OUTPUTFORMAT
NAME "OGRGML"
DRIVER "OGR/GML"
MIMETYPE "text/xml; subtype=gml/2.1.2; driver=ogr"
FORMATOPTION "STORAGE=memory"
FORMATOPTION "FORM=multipart"
FORMATOPTION "FILENAME=result.gml"
END
OUTPUTFORMAT
NAME "GeoJSON"
DRIVER "OGR/GEOJSON"
MIMETYPE "application/json; subtype=geojson"
FORMATOPTION "STORAGE=stream"
FORMATOPTION "FORM=SIMPLE"
END
Debugging common errors
^^^^^^^^^^^^^^^^^^^^^^^
This section is added to help you debug common errors in your Mapfile.
Symbol definition error
"""""""""""""""""""""""
getString(): Symbol definition error. Parsing error near (*matching text*):(line *line-number*)
This error may occur when your layer classes have a name which includes an apostrophe or other quotation mark that matches the quotation marks used to delimit the CLASS name. For example if your class name is delimited using single quotes such as below and your class name includes a word with a single quote (d'Irma), you will get this error.
.. code-block:: mapfile
NAME 'Formation d'Irma : calcaire, dolomie à tromatolites, argilite'
You can correct the error by swapping the file name delimiters to double quotes (as below), in the CLASS name causing the problem; you don’t need to change all the delimiters in all the CLASS names in the Mapfile, just the one(s) with the problem.
.. code-block:: mapfile
NAME "Formation d'Irma : calcaire, dolomie à stromatolites, argilite"
Unknown identifier
""""""""""""""""""
loadLayer(): Unknown identifier. Parsing error near (*matching text*):(line *line-number*)
This error can occur when you are missing an enclosing KEYWORD in the Mapfile. For example in the below example, the CLASS keyword has been commented out, leaving the STYLE section uncommented; STYLE is now found in an unexpected position in the Mapfile, resulting in an error.
.. code-block:: mapfile
#CLASS
STYLE
COLOR 161 8 0
MINSIZE 1
MAXSIZE 10
END #style
#END #class
Missing magic string
""""""""""""""""""""
When running a GetFeatureInfo request with the info\_format set as text/html, you will get an error like the below, if you do not include a magic string in each of your HTML template documents.
Content-type: text/xml isValidTemplate(): Web application error. Missing magic string, *template-file* doesn’t look like a MapServer template.
You need to add **** to the top of ALL templates.
Example the exemplar template query\_footer.html is:
.. code-block:: html