Creating Static Maps using MapServer and Quantum GIS

From CUOSGwiki
Jump to navigationJump to search

Disclaimer

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

Introduction

This tutorial will help guide you step-by-step to how we can create a static map using MapServer. Starting from how we can get our data, to the nitty-gritty details of the process.

Data

Suitable data for this tutorial can be found online from several locations that provide open geospatial data. Sample elevation data can be found in the Geospatial Data Extraction database. This is preferred because there are no restrictions and no cost on data. Data from any other place would also be fine as long as it follows the rules we will talk about here. The data needed for this tutorial needs to be a delimited text file consisting of X,Y coordinates with a Z height in meters. 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. Making sure all your data is in the same folders will ensure that you are organized and the working directory has access to all the data you need to create your map. 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\GEOM4008\WikiDemo”

Methods

Software Installation

The following is an explanation of the installation of required FOSS4G software for this tutorial. Make sure you have the correct bit version for your machine. Always make sure to set the download location that you can find in the future.

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 3.14 using the link for Windows and direct the installation into your program files. It is also recommended to set it so that the downloaded file appears on your desktop upon completion.

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 select"Export" then “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. Follow these steps to reach the interpolation tool in QGIS:

      •	Processing -> Processing Toolbox -> Interpolation -> IDW Interpolation

Fig1.jpg 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 by right-clicking the layer and going to properties and the symbology tab will allow you to change the visuals of the map as you would please.

The last step in data processing is to add any additional vector layers to QGIS. these could be vector data such as base maps, roads, or any other vector data that would make the map more readable.

      •	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.

Fig2.jpg Figure 2: The QGIS Python Plugin installer

The MapServer Export Plugin is accessible in the Web toolbar once installed. you can begin using the function of the plugin by finding it in the web tab and selecting the plugin just installed.

      •	Web -> MapServer Export

Creating the Mapfile

Keep this in mind when working with Mapfiles: "The Mapfile is the heart of MapServer. It defines the relationships between objects, points MapServer to where data are located, and defines how things are to be drawn."

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.

Fig3.jpgFigure 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. if possible copy those URLs or try to get an image of those.

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.