HOWTO for Rasters in MapServer 4.2

Frank Warmerdam

DM Solutions Group Inc.

Abstract

This document describes the procedures for using raster data within MapServer applications.

Last Updated: $Date: 2004/07/15 20:17:58 $

Revision History


Table of Contents

Introduction
How are rasters added to a Map file?
Classifying Rasters
Supported Formats
Rasters and Tile Indexing
Raster Warping
24bit RGB Rendering
Special Processing Directives
Preprocessing Rasters
Raster Display Performance Tips
Georeference with World Files
About This Document
Copyright Information
Disclaimer
Feedback
Document Changes

Introduction

MapServer supports rendering a variety of raster file formats in maps. The following describes some of the supported formats, and what capabilities are supported with what formats.

This document assumes that you are already familiar with setting up MapServer map files, but does explain the raster specific aspects of map files.

How are rasters added to a Map file?

A simple raster layer declaration looks like this. The DATA file is interpreted relative to the SHAPEPATH, much like shapefiles.

LAYER
  NAME "JacksonvilleNC_CIB"
  DATA "Jacksonville.tif"
  TYPE RASTER
  STATUS ON
END

Though not shown rasters can have PROJECTION, METADATA, PROCESSING, MINSCALE, and MAXSCALE information. It cannot have labels, queries, CONNECTION, CONNECTIONTYPE, or FEATURE information.

Classifying Rasters

Rasters can be classified in a manner similar to vectors, with a few exceptions. The CLASSITEM is always the pixel value, which is indicated by the special string "[pixel]" (must be lower case, it was "[value]" in versions before 3.5). The following demonstrates a raster layer with a simple classification scheme applied to it.

LAYER
  NAME "JacksonvilleNC_CIB"
  DATA "Jacksonville.tif"
  TYPE RASTER
  STATUS ON
  CLASSITEM "[pixel]"
  CLASS
    EXPRESSION ([pixel] < 64)
    COLOR 0 0 0
  END
  CLASS
    EXPRESSION ([pixel] >= 64 AND [pixel] < 128)
    COLOR 255 0 0 
  END
  CLASS
    EXPRESSION ([pixel] >= 128 AND [pixel] < 196)
    COLOR 0 255 0 
  END
  CLASS
    EXPRESSION ([pixel] >= 196 AND [pixel] < 256)
    COLOR 0 0 255
  END
END

Some format drivers (ie. GDAL) cast the pixel values to the range 0 to 255 before applying the classification scheme to them, making it hard to use this classification mechanism to classify floating point, or other data not well expressed as unsigned byte data (0-255). This is a bug and should be corrected at some point in the future.

Note that only the COLOR, EXPRESSION and NAME parameters are utilized for raster classifications. The other rendering information is ignored.

Supported Formats

What raster formats are supported by MapServer is largely controlled by configuration time options. Some formats are considered to be built-in while the remainder are handled by the optional GDAL raster library.

More information on GDAL can be found at http://www.remotesensing.org/gdal , including the supported formats list. Some of the advanced MapServer raster features, such as resampling, RGB color cube generation and automatic projection capture only work with raster formats used through GDAL. GDAL is normally built and installed separately from MapServer, and then enabled during the build of MapServer using the --with-gdal configuration switch. GDAL formats not listed below include CEOs, DOQ, ECW, ESRI Labelled BIL, Arc/Info Binary Grid, OGDI and Erdas Imagine (.hfa).

To find out what is built into a particular MapServer executable, use the -v flags to discover what build options are enabled. To find out what gdal formats are build, the gdal-config command may be used. For example:

warmerda@gdal[103]% ./mapserv -v
MapServer version 3.5 (pre-alpha) OUTPUT=PNG OUTPUT=JPEG OUTPUT=WBMP 
SUPPORTS=PROJ SUPPORTS=TTF INPUT=EPPL7 INPUT=OGR INPUT=GDAL INPUT=SHAPEFILE
warmerda@gdal[104]% gdal-config --formats
gxf gtiff hfa aigrid aaigrid ceos ceos2 iso8211 sdts raw dted mem jdem 
envisat elas ogdi gif jpeg png fits grass

The following formats are potential builtins.

  • TIFF/GeoTIFF: If built with INPUT=TIFF MapServer will have builtin support for reading TIFF or GeoTIFF files. The builtin TIFF support has some limitations with regard to the organization of files that can be read (no tiled, 16bit, RGB, or odd color models). This driver supports world files, or simple builtin GeoTIFF coordinates for georeferencing. The AUTO projection declaration is not support.

    Full featured TIFF/GeoTIFF support is available through GDAL. Note that only GDAL supports tiled TIFF files and TIFF files with overviews. Tiled TIFF files with overviews pre-built are one of the highest performance ways of serving large raster images.

  • GIF: If GD is configured with GIF (OUTPUT=GIF) support, then MapServer will also be able to read GIF files for raster layers. The only way to georeference GIF files is with a world file.

    If GD is not configured with GIF support, it may still be available in GDAL, which does not support georeferencing via world files.

  • PNG: If GD is configured with PNG (OUTPUT=PNG) support, then MapServer will also be able to read PNG files for raster layers. The only way to georeference PNG files is with a world file.

    If GD is not configured with PNG support, it may still be available in GDAL, which also supports georeferencing via world files.

  • JPEG: If MapServer is built with JPEG (INPUT=JPEG) support then greyscale JPEG files may be rendered in raster layers. RGB files (the more common kind) will not be able to be displayed. Georeferencing is via world files.

    If MapServer is not built with native JPEG support, GDAL may still support the format. In this case RGB files are also supported (via the RGB color cube mechanism). Georeferencing is still via world file.

  • Erdas .LAN/.GIS: If configured with INPUT=EPPL7 (the default) MapServer will support one band eight bit Erdas LAN/GIS files. The .trl file is read for a colormap, and if not found the layer is treated as greyscale. Georeferencing is read from the file.

Rasters and Tile Indexing

When handling very large raster layers it is often convenient, and higher performance to split the raster image into a number of smaller images. Each file is a tile of the larger raster mosaic available for display. The list of files forming a layer can be stored in a shapefile with polygons representing the footprint of each file, and the name of the files. This is called a TILEINDEX and works similarly to the same feature in vector layers. The result can be represented in the MAP file as one layer, but MapServer will first scan the tile index, and ensure that only raster files overlapping the current display request will be opened.

The following example shows a simple example. No DATA statement is required because MapServer will fetch the filename of the raster files from the Location attribute column in the hp2.dbf file for records associated with polygons in hp2.shp that intersect the current display region. The polygons in hp2.shp should be rectangles representing the footprint of the corresponding file. Note that the files do not have to be all the same size, the formats can vary and they can even overlap (later files will be drawn over earlier ones); however, they must all be in the same coordinate system (projection) as the layer.

  LAYER
    NAME "hpool"
    STATUS ON
    TILEINDEX "hp2.shp"
    TILEITEM "Location"
    TYPE RASTER
  END

While there are many ways to produce TILEINDEX shapefiles for use with this command, one option is the gdaltindex program, part of the GDAL utility suite. The gdaltindex program will automatically generate a tile index shapefile from a list of GDAL supported raster files passed on the command line.

Usage: gdaltindex [-tileindex field_name] index_file [gdal_file]*

eg.
  % gdaltindex doq_index.shp doq/*.tif

NOTES:
  o The shapefile (index_file) will be created if it doesn't already exist.
  o The default tile index field is 'location'.
  o Raster filenames will be put in the file exactly as they are specified
    on the commandline.
  o Simple rectangular polygons are generated in the same
    coordinate system as the rasters.

The gdaltindex program can be built as part of GDAL (it isn't part of the default build), or a prebuilt copy for Windows can be downloaded from ftp//gdal.velocet.ca/pub/outgoing/gdaltindex.zip. You will also need supporting libraries ftp//ftp.remotesensing.org/gdal/gdal-1142-ntbin.zip.

Raster Warping

MapServer is able to resample GDAL rasters on the fly into new projections. Non-GDAL rasters may only be up or down sampled without any rotation or warping.

Raster warping kicks in if the projection appears to be different for a raster layer than for the map being generated. Warped raster layers are significantly more expensive to render than normal raster layers with rendering time being perhaps 2-4 times long than a normal layer. The projection and datum shifting transformation is computed only at selected points, and generally linearly interpolated along the scanlines (as long as the error appears to be less than 0.333 pixels.

In addition to reprojecting rasters, the raster warping ability can also apply rotation to GDAL rasters with rotational coefficients in their georeferencing information. Currently rotational coefficients won't trigger raster warping unless the map and layer have valid (though matching is fine) projection definitions.

24bit RGB Rendering

Traditionally MapServer has been used to produce 8 bit pseudo-colored map displays generated from 8bit greyscale or pseudocolored raster data. However, if the raster file to be rendered is actually 24bit (a red, green and blue band) then additional considerations come into play. Currently rendering of 24bit imagery is only supported via the GDAL renderer. The built-in PNG, JPEG and other drivers do not support 24bit input images.

If the output is still 8bit pseudo-colored (the IMAGEMODE is PC256 in the associated OUTPUT format declaration) then the full 24bit RGB colors for input pixels will be converted to a color in the colormap of the output image. By default a color cube is used. That is a fixed set of 175 colors providing 5 levels of red, 7 levels of green and 5 levels of blue is used, plus an additional 32 greyscale color entries. Colors in the input raster are mapped to the closest color in this color cube on the fly. This substantial degrades color quality, especially for smoothly changing images. It also fills up the colors table, limited to 256 colors, quite quickly.

A variation on this approach is to dither the image during rendering. Dithering selects a color for a pixel in a manner that "diffuses error" over pixels. In an area all one color in the source image, a variety of output pixel colors would be selected such that the average of the pixels would more closely approximate the desired color. Dithering also takes advantage of all currently allocated colors, not just those in the color cube. Dithering requires GDAL 1.1.9 or later, and is enabled by providing the PROCESSING "DITHER=YES" option in the mapfile. Dithering is more CPU intensive than using a simple color cube, and should be avoided if possible in performance sensitive situations.

The other new possibility for handling 24bit input imagery in MapServer 4.0 or later, is to produce 24bit output images. The default "IMAGETYPE png24" or "IMAGETYPE jpeg" declaration may be used to produce a 24bit PNG output file, instead of the more common 8bit pseudo-colored PNG file. The OUTPUTFORMAT declaration provides for detailed control of the output format. The IMAGEMODE RGB and IMAGEMODE RGBA options produce 24bit and 32bit (24bit plus 8bit alpha/transparency) for supported formats.

Special Processing Directives

In MapServer 4, the new PROCESSING keyword was added to the LAYER object. It is primariliy used to pass specialized raster processing options to the GDAL based raster renderer. The following processing options are supported in MapServer 4.

DITHER=YES

This turns on error diffusion mode, used to convert 24bit images to 8bit with error diffusion to get better color results.

Example:

PROCESSING "DITHER=YES"
      

BANDS=red_or_grey[,green,blue[,alpha]]

This directive allows a specific band or bands to be selected from a raster file. If one band is selected, it is treated as greyscale. If 3 are selected, they are treated as red, green and blue. If 4 are selected they are treated as red, green, blue and alpha (opacity).

Example:

PROCESSING "BANDS=4,2,1"
      

SCALE[_n]=AUTO or min,max

This directive instructs the GDAL reader to pre-scale the incoming raster data. It is primarily used to scale 16bit or floating point data to the range 0-255, but can also be used to constrast stretch 8bit data. If an explicit min/max are provided then the input data is stretch (or squished) such that the minimum value maps to zero, and the maximum to 255. If AUTO is used instead, a min/max is automatically computed. To control the scaling of individual input bands, use the SCALE_1, SCALE_2 and SCALE_3 keywords (for red, green and blue) instead of SCALE which applies to all bands.

Example:

PROCESSING "SCALE=AUTO"
  or
PROCESSING "SCALE_1=409,1203"
PROCESSING "SCALE_2=203,296"
PROCESSING "SCALE_3=339,1004"
      

COLOR_MATCH_THRESHOLD=n

Alter the precision with which colors need to match an entry in the color table to use it when producing 8bit colormapped output (IMAGEMODE PC256). Normally colors from a raster colormap (or greyscale values) need to match exactly. This relaxes the requirement to being within the specified color distance. So a COLOR_MATCH_THRESHOLD of 3 would mean that an existing color entry within 3 (sum of difference in red, green and blue) would be used instead of creating a new colormap entry. Especially with greyscale raster layers, which would normally use all 256 color entries if available, this can be a good way to avoid "stealing" your whole colormap for a raster layer. Normally values in the range 2-6 will give good results.

Example:

PROCESSING "COLOR_MATCH_THRESHOLD=3"
      

Preprocessing Rasters

Eventually I should add some general information on how to preprocess raster data for optimal use in MapServer, and give specific information on how to use GDAL's rgb2pct.py, gdaladdo and gdal_translate to converting stuff into pseudocolored, tiled GeoTIFF files with overviews.

Raster Display Performance Tips

  • Build overview levels for large rasters to ensure only a reasonable amount of data needs to be touched to display an overview of a large layer. Overviews can be implemented as a group of raster layers at different resolutions, using MINSCALE, and MAXSCALE to control which layers are displayed at different resolutions. Another, perhaps easier way, is to build overviews for GDAL supported formats using the gdaladdo utility.

  • Preprocess RGB images to eightbit with a colormap to reduce the amount of data that has to be read, and the amount of computation to do on the fly.

  • For large images use tiling to reduce the overhead for loading a view of a small area. This can be accomplished using the TILEINDEX mechanism of the mapfile, or by creating a tiled format file (ie. TIFF with GDAL).

  • Ensure that the image is kept on disk in the most commonly requested projection to avoid on-the-fly image warping which is fairly expensive.

  • If you are getting debug output from MapServer in your web server log file, check to see if the message msResampleGDALToMap in effect appears. If so, the raster layer is being resampled. If you don't think it should be resampled carefully review your map file to ensure that the layer projection exactly matches the map projection or that the layer has no projection definition.

Georeference with World Files

World files are a simple mechanism for associating georeferencing (world coordinates) information with raster files. ESRI was the first company to propagate the use of world files, and they often used with TIFF instead of embedding georeferencing information in the file itself.

The world file contents look like the following. The first coefficient is the X pixel size. The second and third are rotational/shear coefficients (and should normally be 0.0). The fourth is the Y pixel size, normally negative indicating that Y decreases as you move down from the top left origin. The final two values are the X and Y location of the center of the top left pixel. This example is for an image with a 2m x 2m pixel size, and a top left origin at (356800E, 5767999N).

2
0.0000000000
0.0000000000
-2
356800.00
5767999.00

The name of the world file is based on the file it relates to. For instance, the world file for aerial.tif might be aerial.tfw. Conventions vary for appropriate endings, but with MapServer the extension .wld is always OK for world files.

About This Document

Copyright Information

Copyright (c) 2001, Frank Warmerdam, DM Solutions Group Inc.

This documentation is covered by the same Open Source license as the MapServer software itself. See MapServer's License and Credits page for the complete text.

Disclaimer

No liability for the contents of this document can be accepted. Use the concepts, examples and other content at your own risk. As this is a new edition of this document, there may be errors and inaccuracies that may be damaging to your system. Although this is highly unlikely, the author(s) do not take any responsibility for that: proceed with caution.

Feedback

Send any comments or suggestions to the author.

Document Changes

Revision History
Revision : 1$Date: 2004/07/15 20:17:58 $Author: Frank Warmerdam
I added the Revision History section to this document.

Add a Comment