Creating Static Maps using MapServer and Quantum GIS

From CUOSGwiki
Jump to navigationJump to search

Disclaimer

This tutorial was developed for Micrsoft Windows platforms, visit the MapServer Organization website for alternatives to MS4W for Linux and OS platforms. This tutorial assumes working knowledge of GIS environments.

Introduction

This tutorial contains step-by-step instructions on how to create a static map using MapServer.

Data

Suitable data for this tutorial can be found online from several locations which provide open geospatial data. Sample elevation data can be found in the GeoGratis database. This is preferred because there are no restrictions and no cost on data accessed from GeoGratis. The data needed for this tutorial needs to be a delimited text file consisting of X,Y coordinates with a Z height in metres. Additional vector data can be used in this tutorial to overlay on the raster surface.

It is important to place all of the data files in one project folder on the hard drive. Keep the folder pathname simple and as short as possible, this pathname will be used by MapServer to access data to display a map.

      •	Ex.  “C:\user\mapdemo”

Methods

Software Installation

The following is an explanation of the installation of required FOSS4G software for this tutorial.

Installing QGIS

The latest standalone version of QGIS for Windows is sufficient for this tutorial. If the computer you are using does not have QGIS installed, please download QGIS for Windows and direct the installation into your program files.

Installing MS4W

Map Server for Windows (MS4W) is a base package for installing Map Server on Microsoft Windows platforms. The package was used in this tutorial because if provides a pre-configured package with all of the bits and pieces needed to run Map Server on a local Windows machine, such as Apache. The installation process is intuitive for novice users; visit the MapServer website for further tutorials and documentation.

Data Processing – Raster Creation

Once QGIS is installed, open the program and save a project file in the same folder as the data you collected for your map. Keep the project name simple.

      •	File -> Save Project As

Load the text file to display your elevation data. Select the appropriate delimiter used to separate the X,Y and Z values in the text file and select the appropriate coordinate system for the elevation data.

      •	Layer -> Add Delimited Text Layer

Export the Delimited Text layer by Right-Clicking the layer in the layers tab and selecting “Save As.” This will bring up a dialogue box, simply specify the file as an ESRI Shapefile with an appropriate coordinate system and save the file in your working folder. Be sure to select “Add saved file to map.” Delete the Delimited text file from your project.

Interpolate a raster surface using the new elevation point shapefile with the interpolation function built into QGIS. Specify the elevation point file as the input and the Z attribute as seen in Figure 1. Use the Inverse Distance Weighted (IDW) algorithm to create the Digital Elevation Model and specify an output file within your working folder. The defaults are acceptable for the rest of the Interpolation dialogue.

      •	Raster -> Interpolation -> Interpolation

Figure 1: The Interpolation dialogue to create a successful DEM in QGIS from Elevation data.

Using the newly created raster surface you can choose to use the terrain analysis function built into QGIS to further analyze the elevation of your dataset.

      •	Raster -> Terrain Analysis -> Slope; Aspect; Hillshade; Relief; Ruggedness Index

To visualize your raster data, change the symbolization by right clicking the layer and selecting properties. Symbolization can be found in the style tab in the layer properties.

The last step in data processing is to add any additional vector layers to QGIS.

      •	Layer -> Add Vector Layer

MapServer Export Plugin

MapServer Plugin Installation

The MapServer export plugin can be fetched from the QGIS Python Plugin Installer

      •	Plugins -> Fetch Python Plugins

Enter “MapServer Export” in the search filter as seen in Figure 2 and install the latest version of the Plugin.

Figure 2: The QGIS Python Plugin installer Fig2.jpg

The MapServer Export Plugin is accessible in the Web toolbar once installed.

      •	Web -> MapServer Export

Creating the Mapfile

The Mapfile is created using the MapServer Export Plugin (Figure 3). The mapfile must be saved in the same project file as the saved project file and the raster/vector layers. When the MapServer Export Plugin is opened, mapfile (*.map) pathname needs to be specified as well as the Map Name and Resolution, the remaining defaults are acceptable.

Figure 3: The MapServer Export Plugin


Pay attention to the following messages after the Plugin is executed. A URL will be displayed which is important for accessing the Mapfile VIA your internet browser.


Editing the Mapfile

The defaults used to create the Mapfile using QGIS will not work without editing several things. The Mapfile can be opened with a simple text editor such as Notepad. A demo script below shows how a Mapfile works with MapServer to access certain shapefiles in the project file to display them as a static map on a web browser. A Mapfile will always begin with a “MAP” command. Two lines will have to be commented out so that MapServer ignores them. The FONTSET and SYMBOLSET commands can be commented out with a # symbol at the beginning of the command. This can be done in Notepad.


Demo Mapfile

# Map file created from QGIS project file C:/demo/demo.qgs
# Edit this file to customize for your map interface
# (Created with PyQgis MapServer Export plugin)
MAP
 NAME "DEMO MAP"
# Map image size
 SIZE 100 100
 UNITS meters
 EXTENT 342934.134461 5043972.900271 345065.845539 5045027.089729 
  #FONTSET './fonts/fonts.txt' – Comment out
 #SYMBOLSET './symbols/symbols.txt' – Comment out
 PROJECTION    'proj=longlat'    'datum=WGS84'    'no_defs'
 END
# Background color for the map canvas -- change as desired
 IMAGECOLOR 255 255 255
 IMAGEQUALITY 95
 IMAGETYPE agg
 OUTPUTFORMAT
   NAME agg
   DRIVER AGG/PNG
   IMAGEMODE RGB
 END 
# Legend
 LEGEND
     IMAGECOLOR 255 255 255
   STATUS ON
   KEYSIZE 18 12
   LABEL
     TYPE BITMAP
     SIZE MEDIUM
     COLOR 0 0 89
   END
 END
# Web interface definition. Only the template parameter
# is required to display a map. See MapServer documentation
 WEB
# Set IMAGEPATH to the path where MapServer should
# write its output.
   IMAGEPATH '/tmp/'
# Set IMAGEURL to the url that points to IMAGEPATH
# as defined in your web server configuration
   IMAGEURL '/tmp/'
# WMS server settings
   METADATA
     'ows_title'
          'DEMO MAP'
     'ows_onlineresource'
 'http://my.host.com/cgi-bin/mapserv.exe?map=C:/demo/demo.map'
     'ows_srs'             'EPSG:4326'
   END
# Scale range at which web interface will operate
# Template and header/footer settings
# Only the template parameter is required to display a map. See MapServer documentation
TEMPLATE 'fooOnlyForWMSGetFeatureInfo
 END
 LAYER
   NAME 'gp_dem'
   TYPE RASTER
   DUMP true
   TEMPLATE fooOnlyForWMSGetFeatureInfo
 EXTENT 342934.134461 5043972.900271 345065.845539 5045027.089729
   DATA './gp_dem.tif'
   METADATA
     'ows_title' 'gp_dem'
   END
   STATUS OFF
   TRANSPARENCY 100
   PROJECTION
   'proj=longlat'
   'datum=WGS84'
   'no_defs'
   END
 END
 LAYER
   NAME 'GP_Trails'
   TYPE LINE
   DUMP true
   TEMPLATE fooOnlyForWMSGetFeatureInfo
 EXTENT 342934.134461 5043972.900271 345065.845539 5045027.089729
   DATA './Luskville Falls Trail.shp'
   METADATA
     'ows_title' 'GP_Trails'
   END
   STATUS OFF
   TRANSPARENCY 100
   PROJECTION    'proj=longlat'    'datum=WGS84'    'no_defs'
   END
   CLASS
      NAME ‘GP_Trails’
      STYLE
        WIDTH 5.25
        COLOR 255 0 0
      END
   END
 END
END

Executing the Mapfile

The edited Mapfile can be accessed using the URL which was shown earlier in the QGIS Dialogue box. The URL is a combination of your local IP address and the project file pathname. If error messages show up in the browser instead of an image, follow the directions and make adjustments to the map file. Symbology definitions can also be changed in the Mapfile. The image displayed in the browser is a static map of the data in the project file, the order expressed in the map file is the order displayed on the browser.

The Mapfile can also be edited to make a more dynamic map with zoom features a legend and other map elements.