Using MapServer (3) – CGI MapServer

December 9th, 2007

This post is the third in a series on using MapServer. First-time visitors are encouraged to read part 1 to learn how to download and install MapServer on their computer. Additionally, the data layers used in this tutorial are available to download in this post.

We’ve already introduced MapServer and showed how to create a mapfile using GIS data. We will now introduce MapServer’s CGI functionality as a way to generate a map image. Later we will build on what we’ve learned to create a map using OpenLayers, a web mapping interface.

Introducing Mapserv.exe

At the heart of MapServer is a single file referred to as “mapserv”. With the MS4W package, it is a single executable found at C:\ms4w\Apache\cgi-bin\mapserv.exe. This file is the map production engine that uses a mapfile to generate a map image.

Mapserv.exe is usually accessed internally by MapServer-based packages such as ka-map or Mapscript. But it can also be called directly from a web browser as long as some required parameters are included.

Mapping from the Web Browser Address Bar

In order to demonstrate Mapserv.exe, we already need to have some data and a mapfile ready to use. We will be using the data and mapfile from our World Map dataset.

Open a web browser and enter the following: http://localhost/cgi-bin/mapserv.exe. If MapServer is correctly configured, you should see the following warning message:

CGI MapServer

This means mapserv.exe is working correctly so we can next tell it that we want to draw a map. Add ?mode=map to the address so the full address is http://localhost/cgi-bin/mapserv.exe?mode=map. Pressing enter again returns another message:

CGI MapServer

This is expected because we have not provided a mapfile to display. The next variable to add is named “map” and it’s value is the file path to our mapfile. For example, it could be: http://localhost/cgi-bin/mapserv.exe?mode=map&map=C:\ If a valid mapfile is entered the browser will generate an image such as this:

CGI MapServer Map

A full list of CGI MapServer parameters can be found here. For example, to generate an image of a specific map extent, the MAPEXT parameter can be used. In our example, we added MAPEXT -20 -20 20 20 in the browser to get an address like this: http://localhost/cgi-bin/mapserv.exe?mode=map&
map=C:\James\GIS\Data\World\ -20 20 20
. The image returned reflects the new extent.

CGI MapServer Map


Understanding how MapServer uses mapserv.exe to generate images will become more useful in our next post where we will setup a basic web mapping site using OpenLayers that will need to directly access the CGI functionality of MapServer.