Mapscript Demo (1)

May 26th, 2007

In our last post, we setup MapServer using the MS4W package. That tutorial enabled us to have a working web mapping environment on a desktop machine without much hassle. If you haven’t read that post, I encourage you to read it before continuing.

Today’s project will build off that setup by creating a small desktop application that uses MapServer’s ability to generate map images. Since MapServer is just a program (mapserv.exe) and some required DLLs, it is possible to create stand-alone applications that don’t have a web component.

Introducing Mapscript

In their own words

MapScript provides a scripting interface for MapServer for the construction of Web and stand-alone applications. MapScript is used independently of CGI MapServer, it is a loadable module that adds MapServer capability to your favorite scripting language. MapScript currently exists in Php, Perl, Python, Ruby, Tcl, Java, and C# flavors.

This means we can use Mapscript in Visual C# Express to access MapServer’s capabilities to draw and query maps. The maps generated are just images so we can easily integrate them into a standard Windows application. So let’s go ahead and create a desktop mapping application powered by MapServer.

Create a MapServer Stand-alone Viewer

Here are the steps to creating a MapServer application using Mapscript in C#. We will assume that a valid MapServer map file is already setup and ready to use. See our last post on how to download and setup a sample mapping application.

Step 1 – Create a Windows Application in Visual C# Express

Create a new Windows Application and add some basic controls to the form. I first added some buttons for zooming in, out and to return to the full extent of the map. A tab control was added on the side to contain both a list of the map layers and an image of the legend. Most importantly, I placed a large picture box control on the form to serve as the map display area.

Mapscript Demo Form

At this point, I went ahead and compiled the application so the bin\debug folder would be generated within our application’s directory. Step 2 explains why we need that folder right away.

Step 2 – Adding the Mapserver DLLs

Stand-alone Mapserver applications need a number of DLLs to be present in the folder where the executable (*.EXE) will be generated. Luckily, the MS4W package ships with everything we will need. Navigate to the C:\ms4w\Apache\cgi-bin folder and note all the DLL files that are present there. There are approximately 54 in total so just go ahead and copy them all and then paste them into the bin\debug folder of your application. Some DLLs are unnecessary or only needed in certain situations but it’s easier right now just to copy them all.

Next, look in the C:\ms4w\Apache\cgi-bin\mapscript\csharp folder and copy the two DLLs there – mapscript.dll and mapscript_csharp.dll. Paste both of these into the same debug folder as above. Finally, back in the C# application, add a reference to the mapscript_csharp.dll only using the Tools…Add Reference… menu option. All of the other DLLs just need to be present in the folder.

Now were are ready to code in the map functionality. Because we referenced the mapscript C# DLL, we have access to an API for interacting with Mapserver.

Step 3 – Coding with Mapscript

A valid Mapserver application (i.e. GIS data sets and a map file) is still required but we can use the free sample application we setup in the last post. Mapserver applications run on a configuration file referred to as a map file (*.map). So this is the first piece of information we need to add to our form.


const string MAP_FILE = @"C:\ms4w\apps\gmap\htdocs\";


This file path is the only variable that our application will need to know before running. All other code we add is independent of any specific map file or dataset. So if you want to use your own data, feel free to change the value in the source code below.

To Be Continued…

There is still a lot of coding to discuss in the next couple of posts but for those of you can’t wait, the complete source code of the application is available to download below along with a screenshot of the program running. The download includes the Mapserver DLLs so the program should compile and run right away assuming the MAP_FILE constant is pointed to a valid map file. Comments and questions are always welcome at james [at]

Download Source Code

Mapscript Demo Running

Continue to part 2