mapserver raster howto

翻译:wlx wlx@mygis.org

    导言

MapServer的地图中支持多种栅格格式文件。下面就给大家介绍一些支持的栅格格式,以及每种格式对应的能力。
本文假定读者已经知道如何创建MapServer的map文件,本文是从map文件中的栅格文件的特定角度来进行说明。

    栅格文件时如何加入到map文件中的?


一个简单的栅格图层可以象下面这样声明。和shape文件一样,DATA文件的路径也是相对于SHAPEPATH的。

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

栅格文件还是没有显示出来?那肯定是这个栅格文件有PROJECTION、 METADATA、 PROCESSING、 MINSCALE和 MAXSCALE 信息。栅格文件的定义中不能有以下信息:labels、 queries、 CONNECTION、CONNECTIONTYPE、 FEATURE。

    栅格的分类显示


栅格可以象矢量一样采用同样的定义来进行分类显示,只是有一小部分参数不同。在栅格图层的定义中,CLASSITEM表示像素的值,用一个特殊的字符串来表达: “[pixel]” (必须小写,在3.5版本前是用”[value]”表达的)。下面示范了一个应用简单分类表的栅格图层定义。

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

一些栅格格式的驱动程序(比如, GDAL)在应用分类表前从0到255重新计算像素值,这使分类机制在分类浮点值或其他用UINT8(0-255)不太好表达的栅格数据时变得困难。这是一个bug,将来会修正它。

注意:只有COLOR、EXPRESSION和NAME参数被用来进行栅格分类,其他的描写信息被忽略。

    支持的格式

MapServer 支持的栅格格式很大程度上是在配置时选项(译者:指在编译MAPSERVER时的选项)所决定的。一部分格式是内部支持的,而另外一些是由可选的GDAL栅格库所处理的。

关于GDAL的更多信息,请参考http://www.remotesensing.org/gdal,包括支持的格式列表。一些高级的MapServer栅格特性,比如 ,重采样、 RGB color cube generation、自动投影捕获,只能通过GDAL来工作。 GDAL的安装和编译可独立的进行,然后在编译MapServer的时候使用 --with-gdal配置开关激活GDAL选项, GDAL 支持的格式没有列在下面的包括:CEOs、 DOQ、 ECW、 ESRI Labelled BIL、Arc/Info Binary Grid、 OGDI and Erdas Imagine (.hfa)。

为找出你所编译的MapServer到底支持哪些格式,可以使用-v 参数。为找出GDAL到底支持哪些栅格格式,可以使用gdal-config 命令。 例如:

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

下列的格式有可能被内建支持。

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.

如果你想在你的WEB服务器LOG文件中调试MapServer输出信息,请检查msResampleGDALToMap消息是否有效。如果是,说明栅格图层被重采样了。如果你认为它不应该被重采样(译者:或重采样的效果不好?),请修改你的MAP文件以确保那个图层的投影能和整个地图匹配或那个图层没有定义投影信息。

使用World文件表达地理参考信息

World文件以一种简单的机制来关联栅格文件的地理参考(世界坐标)信息 。 ESRI 是第一个提倡使用world文件的公司,他们常用world文件关联TIFF图片来代替内嵌地理参考信息的TIFF文件。

World文件的内容如下所述。第一个系数代表X方向每象素的大小。第二个和第三个系数代表平移量和旋转角度(通常都为0,另原文好象2和3搞反了)。第四个系数是 Y方向象素的大小,通常负数说明Y方向是从上到下是减少的(即Y轴向上是正向)。最后两个值代表左上角的角点的中心位置的坐标。下面的例子描述了一个2m x 2m象素大小、左上角坐标为(356800E, 5767999N)的图象。

2
0.0000000000
0.0000000000
-2
356800.00
5767999.00

World文件名和其相关联的图象文件名相同。例如,aerial.tif的world文件应该是aerial.tfw。 Conventions vary for appropriate endings(?),但是对于 MapServer来讲,后缀 .wld总是可以使用的。


已发布

分类

来自

标签:

评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注