Adding grid layer

View example

In addition to supporting vector data (points, lines, polygons, and callouts), MapServer can also display raster data. With the use of the GDAL library, MapServer can enter and output a variety of raster formats. Today's MapServer supports RGB and multispectral images (multilayer). The version raster input before 4.0 is limited to single-layer, grayscale, or color image indexes. This example shows how to use multispectral data to select what layer to display. With RGB and multispectral images, there is a risk of an obvious performance loss.

Because MapServer 5.x uses the GD 2.0.x repository to generate image output, it supports RGB (24-bit true color) as well as output. Therefore, you can now use both the PNG24 (true color) output and the 8-bit (index color or grayscale) PNG. This example uses PNG24 IMAGETYPE. Just like RGB input, when using PNG24, the rendering results can be significantly improved.

MapServer can actually generate output images using GDAL, but that's another topic. If you want to know more about it, take a look at the specific usage of the OUTPUTFORMAT object in MapFile.

The following is the Mapfile ( used in this example:

The file changes are as follows:

01 MAP
02     IMAGETYPE "PNG24"
03     EXTENT -180 -90 180 90
04     SIZE 600 300
05     SHAPEPATH "/gdata"
06     IMAGECOLOR 255 255 255
07     FONTSET "../fonts/fonts.list"
08     SYMBOLSET "../symbols/symbols35.sym"
09     LAYER
10         NAME "modis"
11         DATA "land_shallow_topo_8192.tif"
12         STATUS OFF
13         TYPE RASTER
14         PROCESSING "BANDS=1,2,3"
15         OFFSITE 71 74 65
16     END
17     LAYER
18         NAME "states_line"
19         DATA "wcountry.shp"
20         STATUS OFF
21         TYPE LINE
22         CLASS
23             NAME "State Boundary"
24             STYLE
25                 SYMBOL "line5"
26                 COLOR 255 255 0
27                 SIZE 1
28             END
29         END
30     END
31 END

Annotated Mapfile

The structure of the mapfile, through the object, looks like this:

               LAYER #1-------------LAYER #2----|----LAYER #3--------LAYER #4
            (states_poly) (modis) (states_line) (states_label)
               | | |
  (land) CLASS-|-CLASS (water) |-CLASS |-CLASS
             | | | |

The changes are as follows:

3    EXTENT -180 -90 180 903    EXTENT -180 -90 180 90
7    FONTSET "../fonts/fonts.list"7    FONTSET "../fonts/fonts.list"
nn8    SYMBOLSET "../symbols/symbols35.sym"
8    LAYER9    LAYER
n9        NAME "states_poly"n10        NAME "modis"
10        DATA "wcountry.shp"11        DATA "land_shallow_topo_8192.tif"
11        STATUS OFF12        STATUS OFF
n12        TYPE POLYGONn13        TYPE RASTER
13        LABELITEM "NAME"14        PROCESSING "BANDS=1,2,3"
14        CLASS15        OFFSITE 71 74 65
15            NAME "States"
16            STYLE
17                COLOR 232 232 232
18            END
19            LABEL
20                COLOR 132 31 31
21                SHADOWCOLOR 218 218 218
22                SHADOWSIZE 2 2
23                TYPE TRUETYPE
24                FONT "arial-bold"
25                SIZE 8
26                ANTIALIAS TRUE
27                POSITION CC
28                PARTIALS FALSE
29                MINDISTANCE 300
30                BUFFER 4
31            END
32        END
33    END16    END
41            STYLE24            STYLE
tt25                SYMBOL "line5"
42                COLOR 132 132 3226                COLOR 255 255 0
27                SIZE 1
43            END28            END

When you are in MapFile, you will see that the new layer "Modis" is added after the polygon layer "States". MapServer displays layers in reverse order-backward first Out (LIFO), and the first layer is drawn in MapFile defined at the bottom of the map.

So the national polygon layer will be drawn at the bottom of the result. Since the grid layer is drawn on top of it, it will not be seen. This is why the first layer gets closed with the status value STATUS OFF. The line layer is defined below the raster layer, so it will be drawn above the result (you can see it). This is why the separation of the line layer from the polygon layer begins. Finally, the label is drawn on top of everything.

MapServer can automatically turn on or off the l layer automatically according to the state of other layers (the polygon layer will be closed when the raster layer is opened). This is done with the REQUIRES parameter. This feature should be used once you start creating your own MapServer application.

Parameter Description

Let's take a look at the new parameters introduced in MapFile:

IMAGETYPE, this is not new, but the value is PNG24. The PNG24 is a 24-bit true color version of the PNG format. Instead of being output images with a limited 256 color combination, the map server now has millions. By the way, try changing this value back to PNG. Note the time it takes to generate an image in a format that can be used. Choose between true color and index color, taking into account the time it takes to generate the image.

SYMBOLSET, the path of the file defined by the symbol in the minute. The symbols in this file are referenced by the SYMBOL object in the class object. This is not really needed at this point, but I think I will throw this here now. See the MapFile reference, and for more information on building and drawing symbols in MapServer.

DATA "raster/mod09a12003161_ugl_ll_8bit.tif", In the newly added layer object, the data parameter points to a GeoTIFF image. MapServer's vector dataset supports multiple raster file formats. This support is through the use of the GDAL library. For more information, different raster formats support MapServer and general discussion using raster map servers, please read how the raster data is at

TYPE RASTER, When using raster data (images), the value is RASTER instead of POLYGON, LINE, POINT, or whatever.

PROCESSING "BANDS=1,2,3", This layer of object parameter map server 4.x is new. The processed keyword has a lot of values, but in this case, it is used to select the band multispectral image display. The value string here will be passed to the GDAL library. The documentation for this is currently the smallest, but see MAPFILE for more examples of using processing keywords.

OFFSITE, This parameter tells MapServer that the pixel values ​​are rendered as background (or ignored). You can get pixel value processing or image processing programs (such as GIMP, Photoshop) that utilize images.

RGB and index images

To compare the speed of creating a map using RGB images and indexing color images, replace the following line in MapFile:

DATA "raster/mod09a12003161_ugl_ll_8bit.tif"
OFFSITE 71 74 65

Use the following code:

DATA "raster/mod09a12003161_ugl_ll_idxa.tif"
OFFSITE 70 74 66

Also, try changing IMAGETYPE from PNG24 to PNG.

Using preprocessing

Raster data, such as remote sensing images, is more than just a picture. In a computer, images are usually represented by three channels of RGB. In remote sensing images, channels are generally referred to as bands. Moreover, the remote sensing image has a band different from the RGB three channels of the image. It does not necessarily correspond to RGB three colors, and the number of bands is not necessarily three.

In MapServer, in addition to rendering the image by default, you can also define the processing method. If you use any of the three bands, use fake color, if you do color stretching and so on.

For example, for remote sensing images, use the following statement to indicate that the band order is reversed:


The results are as follows: