|Home | Products | Issue Tracker | FAQ | Download|
|Contact:||jmckenna at gatewaygeomatics.com|
|Contact:||david.fawcett at moea.state.mn.us|
|Contact:||hobu.inc at gmail.com|
MapServer is a popular Open Source project whose purpose is to display dynamic spatial maps over the Internet. Some of its major features include:
In its most basic form, MapServer is a CGI program that sits inactive on your Web server. When a request is sent to MapServer, it uses information passed in the request URL and the Mapfile to create an image of the requested map. The request may also return images for legends, scale bars, reference maps, and values passed as CGI variables.
The Glossary contains an overview of many of the jargon terms in this document.
MapServer can be extended and customized through MapScript or templating. It can be built to support many different vector and raster input data formats, and it can generate a multitude of output formats. Most pre-compiled MapServer distributions contain most all of its features.
MapScript provides a scripting interface for MapServer for the construction of Web and stand-alone applications. MapScript can be used independently of CGI MapServer, and 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 .NET flavors.
This guide will not explicitly discuss MapScript, check out the MapScript Reference for more information.
A simple MapServer application consists of:
Map File - a structured text configuration file for your MapServer application. It defines the area of your map, tells the MapServer program where your data is and where to output images. It also defines your map layers, including their data source, projections, and symbology. It must have a .map extension or MapServer will not recognize it.
Geographic Data - MapServer can utilize many geographic data source types. The default format is the ESRI Shape format. Many other data formats can be supported, this is discussed further below in Adding data to your site.
HTML Pages - the interface between the user and MapServer . They normally sit in Web root. In it’s simplest form, MapServer can be called to place a static map image on a HTML page. To make the map interactive, the image is placed in an HTML form on a page.
CGI programs are ‘stateless’, every request they get is new and they don’t remember anything about the last time that they were hit by your application. For this reason, every time your application sends a request to MapServer, it needs to pass context information (what layers are on, where you are on the map, application mode, etc.) in hidden form variables or URL variables.
A simple MapServer CGI application may include two HTML pages:
Initialization File - uses a form with hidden variables to send an initial query to the web server and MapServer. This form could be placed on another page or be replaced by passing the initialization information as variables in a URL.
Template File - controls how the maps and legends output by MapServer will appear in the browser. By referencing MapServer CGI variables in the template HTML, you allow MapServer to populate them with values related to the current state of your application (e.g. map image name, reference image name, map extent, etc.) as it creates the HTML page for the browser to read. The template also determines how the user can interact with the MapServer application (browse, zoom, pan, query).
MapServer CGI - The binary or executable file that receives requests and returns images, data, etc. It sits in the cgi-bin or scripts directory of the web server. The Web server user must have execute rights for the directory that it sits in, and for security reasons, it should not be in the web root. By default, this program is called mapserv
Web/HTTP Server - serves up the HTML pages when hit by the user’s browser. You need a working Web (HTTP) server, such as Apache or Microsoft Internet Information Server, on the machine on which you are installing MapServer.
Pre-compiled binaries for MapServer are available from a variety of sources, refer to the Windows section of the Downloads page.
OSGeo4W is a new Windows installer that downloads and/or updates MapServer, add-on applications, and also other Open Source geospatial software. The following steps illustrate how to use OSGeo4W:
Download OSGeo4W http://download.osgeo.org/osgeo4w/osgeo4w-setup.exe
Execute (double-click) the .exe
Choose “Advanced” install type
Express contains options for higher-level packages such as MapServer, GRASS, and uDig. Advanced gives you full access to choosing commandline tools and applications for MapServer that are not included in the Express install
Select packages to install
Click on the “Default” text beside the higher-level packages (such as Web) to install all of Web’s sub-packages, or click on the “Skip” text beside the sub-package (such as MapServer) to install that package and all of its dependencies.
Let the installer fetch the packages.
Run the apache-install.bat script to install the Apache Service.
You must run this script under the “OSGeo4W Shell”. This is usually available as a shortcut on your desktop
An apache-uninstall.bat script is also available to remove the Apache service installation.
Start Apache from the OSGeo4W shell and navigate to http://127.0.0.1
Verify that MapServer is working
MapServer runs on Linux, Windows, Mac OS X, Solaris, and more. To compile or install some of the required programs, you may need administrative rights to the machine. People commonly ask questions about minimum hardware specifications for MapServer applications, but the answers are really specific to the individual application. For development and learning purposes, a very minimal machine will work fine. For deployment, you will want to investigate Optimization of everything from your data to server configuration.
You need a working and properly configured Web (HTTP) server, such as Apache or Microsoft Internet Information Server, on the machine on which you are installing MapServer. OSGeo4W contains Apache already, but you can reconfigure things to use IIS if you need to. Alternatively, MS4W can be used to install MapServer on Windows.
If you are on a Windows machine, and you don’t have a web server installed, you may want to check out MS4W, which will install a pre-configured web server, MapServer, and more. The FGS Linux Installer provides similar functionality for several Linux distributions.
This introduction will assume you are using pre-compiled OSGeo4W Windows binaries to follow along. Obtaining MapServer or Linux or Mac OS X should be straightforward. Visit Download for installing pre-compiled MapServer builds on Mac OS X and Linux.
You will also need a Web browser, and a text editor (vi, emacs, notepad, homesite) to modify your HTML and mapfiles.
In addition to learning how the different components of a MapServer application work together and learning Map File syntax, building a basic application requires some conceptual understanding and proficiency in several skill areas.
The .map file is the basic configuration file for data access and styling for MapServer. The file is an ASCII text file, and is made up of different objects. Each object has a variety of parameters available for it. All .map file (or mapfile) parameters are documented in the mapfile reference. A simple mapfile example displaying only one layer follows, as well as the map image output:
MAP NAME "sample" STATUS ON SIZE 600 400 SYMBOLSET "../etc/symbols.txt" EXTENT -180 -90 180 90 UNITS DD SHAPEPATH "../data" IMAGECOLOR 255 255 255 FONTSET "../etc/fonts.txt" # # Start of web interface definition # WEB IMAGEPATH "/ms4w/tmp/ms_tmp/" IMAGEURL "/ms_tmp/" END # WEB # # Start of layer definitions # LAYER NAME 'global-raster TYPE RASTER STATUS DEFAULT DATA bluemarble.gif END # LAYER END # MAP
MAP NAME "sample" EXTENT -180 -90 180 90 # Geographic SIZE 800 400 IMAGECOLOR 128 128 255 END # MAP
MapServer currently uses a pixel-center based extent model which is a bit different from what GDAL or WMS use.
LAYER NAME "bathymetry" TYPE RASTER STATUS DEFAULT DATA "bath_mapserver.tif" END # LAYER
Vector layers of TYPE point, line, or polygon can be displayed. The following example shows how to display only lines from a TYPE polygon layer, using the OUTLINECOLOR parameter:
LAYER NAME "world_poly" DATA 'shapefile/countries_area.shp' STATUS ON TYPE POLYGON CLASS NAME 'The World' STYLE OUTLINECOLOR 0 0 0 END # STYLE END # CLASS END # LAYER
CLASS NAME "Primary Roads" STYLE SYMBOL "circle" COLOR 178 114 1 SIZE 15 END # STYLE STYLE SYMBOL "circle" COLOR 254 161 0 SIZE 7 END # STYLE END # CLASS
MAP NAME "sample" EXTENT -180 -90 180 90 # Geographic SIZE 800 400 IMAGECOLOR 128 128 255 SYMBOLSET "../etc/symbols.txt" END # MAP
where symbols.txt might contain:
SYMBOL NAME "ski" TYPE PIXMAP IMAGE "ski.png" END # SYMBOL
and the mapfile would contain:
LAYER ... CLASS NAME "Ski Area" STYLE SYMBOL "ski" END # STYLE END # CLASS END # LAYER
An example LABEL object that references one of the above fonts might look like:
LABEL FONT "sans-bold" TYPE truetype ENCODING "UTF-8" SIZE 10 POSITION LC PARTIALS FALSE COLOR 100 100 100 OUTLINECOLOR 242 236 230 END # LABEL
EXPRESSION ([POPULATION] > 50000 AND '[LANGUAGE]' eq 'FRENCH')
Logical expressions should be avoided wherever possible as they are very costly in terms of drawing time.
Added to MapServer 4.10, any part of the mapfile can now be stored in a separate file and added to the main mapfile using the INCLUDE parameter. The filename to be included can have any extension, and it is always relative to the main .map file. Here are some potential uses:
The following is an example of using mapfile includes to include a layer definition in a separate file:
If ‘shadedrelief.lay’ contains:
LAYER NAME 'shadedrelief' STATUS ON TYPE RASTER DATA 'GLOBALeb3colshade.jpg' END # LAYER
therefore the main mapfile would contain:
MAP ... INCLUDE "shadedrelief.lay" ... END # MAP
MAP NAME "base" # # include reference objects # INCLUDE "../templates/template.ref" # # Start of layer definitions # INCLUDE "../layers/usa/usa_outline.lay" INCLUDE "../layers/canada/base/1m/provinces.lay" INCLUDE "../layers/canada/base/1m/roads_atlas_of_canada_1m.lay" INCLUDE "../layers/canada/base/1m/roads_atlas_of_canada_1m_shields.lay" INCLUDE "../layers/canada/base/1m/populated_places.lay" END # MAP
Mapfiles must have the .map extension or MapServer will not recognize them. Include files can have any extension you want, however.
You can test if MapServer is working by running the MapServer executable (mapserv) with the -v parameter on the command line (./mapserv -v). Depending on your configuration, the output could be something like this:
MapServer version 6.0.1 OUTPUT=GIF OUTPUT=PNG OUTPUT=JPEG SUPPORTS=PROJ SUPPORTS=AGG SUPPORTS=FREETYPE SUPPORTS=ICONV SUPPORTS=WMS_SERVER INPUT=SHAPEFILE
You can also send a HTTP request directly to the MapServer CGI program without passing any configuration variables (e.g. http://your.domain.name/cgi-bin/ms4/mapserv.exe). If you receive the message, ‘No query information to decode. QUERY_STRING not set.’, your installation is working.
Download the MapServer Demo. UnZip it and follow the directions in ReadMe.txt. You will need to move the demo files to their appropriate locations on your web server, and modify the Map File and HTML pages to reflect the paths and URLs of your server. Next, point your browser to init.html and hit the ‘initialize button’. If you get errors, verify that you have correctly modified the demo files.
Now that you have a working MapServer demo, you can use the demo to display your own data. Add new LAYERs to your Map file that refer to your own geographic data layers (you will probably want to delete the existing layers or set their status to OFF).
Unless you are adding layers that fall within the same geographic area as the demo, modify MAP EXTENT to match the extent of your data. To determine the extent of your data, you can use ogrinfo. If you have access to a GIS, you could use that as well. The MAP EXTENT needs to be in the units of your output projection.
If you add geographic data layers with different geographical reference systems, you will need to modify your Map File to add a PROJECTION block to the MAP (defines the output projection / geographical reference system) and each of the LAYERs (defines the geographical reference system of the layer dataset).
Vector data includes features made up of points, lines, and polygons. MapServer support the ESRI Shape format by default, but it can be compiled to support spatially enabled databases such as PostgreSQL-PostGIS, and file formats such as Geography Markup Language (GML), MapInfo, delimited text files, and more formats with OGR.
See the Vector Data reference for examples on how to add different geographic data sources to your MapServer project.
Since version 6.2 Mapserver relies on GDAL for all raster access.
Because the earth is round and your monitor (or paper map) is flat, distortions will occur when you display geographic data in a two-dimensional image. Projections allow you to represent geographic data on a flat surface. In doing so, some of the original properties (e.g. area, direction, distance, scale or conformity) of the data will be distorted. Different projections excel at accurately portraying different properties. A good primer on map projections can be found at the University of Colorado.
With MapServer, if you keep all of your spatial data sets in the same projection (or unprojected Latitude and Longitude), you do not need to include any projection info in your Map File. In building your first MapServer application, this simplification is recommended.
There are two primary ways to query spatial data. Both methods return data through the use of templates and CGI variable replacement. A QUERYMAP can be used to map the results of the query.
To be queryable, each mapfile LAYER must have a TEMPLATE defined, or each CLASS within the LAYER must have a TEMPLATE defined. More information about the CGI variables used to define queries can be found in the MapServer CGI Reference.
The user selects features based on data associated with that feature. ‘Show me all of the lakes where depth is greater than 100 feet’, with ‘depth’ being a field in the Shape dataset or the spatial database. Attribute queries are accomplished by passing query definition information to MapServer in the URL (or form post). Mode=itemquery returns a single result, and mode=itemnquery returns multiple result sets.
The request must also include a QLAYER, which identifies the layer to be queried, and a QSTRING which contains the query string. Optionally, QITEM, can be used in conjunction with QSTRING to define the field to be queried. Attribute queries only apply within the EXTENT set in the map file.
The user selects features based on a click on the map or a user-defined selection box. Again the request is passed through a URL or form post. By setting mode=QUERY, a user click will return the one closest feature. In mode=NQUERY, all features found by a map click or user-defined selection box are returned. Additional query options can be found in the CGI documentation.
Data organization is at least as important as hardware configuration in optimizing a MapServer application for performance. MapServer is quite efficient at what it does, but by reducing the amount of processing that it needs to do at the time of a user request, you can greatly increase performance. Here are a few rules:
Register and post questions to the MapServer Users mailing list. Questions to the list are usually answered quickly and often by the developers themselves. A few things to remember:
MapServer users and developers can be found on Internet Relay Chat. The channel is #mapserver on irc.freenode.net.
Web Mapping Illustrated, a book by Tyler Mitchell that describes well and provides real-world examples for the use of Web mapping concepts, Open Source GIS software, MapServer, Web services, and PostGIS.
Mapping Hacks, by Schuyler Erle, Rich Gibson, and Jo Walsh, creatively demonstrates digital mapping tools and concepts. MapServer only appears in a handful of the 100 hacks, but many more are useful for concepts and inspiration.
Beginning MapServer: Opensource GIS Development, by Bill Kropla. According to the publisher, it covers installation and configuration, basic MapServer topics and features, incorporation of dynamic data, advanced topics, MapScript, and the creation of an actual application.