示例1.5
添加栅格图层
(http://www.mapserver.org/tutorial/example1-5.html#example1-5)
除了支持矢量数据(点,线,多边形和注记)外,MapServer也能显示栅格数据。通过使用GDAL库,MapServer支持多种栅格格式的输入输出。然而4.x版本以下的栅格输入只限定于单图层,灰度或索引图。当前MapServer版本支持RGB和多光谱(多图层)影像。这个例子展示了采用多光谱数据时如何选择层去显示。
注意:当使用RGB和多光谱数据时会有显示的性能损失。
由于MapServer 5.x使用AGG或GD 2.0.x生成输出图像,它也支持RGB(24位或真彩色)输出。所以,在支持8位(索引色或灰度)PNG8,你也可以使用PNG(真彩色)输出。此例使用的是PNG作为IMAGETYPE。
注意:当使用RGB输入时,使用PNG相比PNG8输出的时候性能会有显著下降
MapServer实际上是可以使用GDAL输出图你的,但那是另一个话题。如果你想了解更多,请看mapfile参考手册里面的OUTPUTFORMAT对象。
MapFile结构
下面是示例1-5的mapfile文件:
#The annotated map file (sort of)
#Created by Pericles S. Nacionales for the MapServer tutorial
#20050408
#
#MapServer map file uses the pound sign (#) to denote the start of a line
#comment--each line that needs to be commented has to be prepended with a"#".
#
#Map files begin with MAP keyword to signify the start of the map object.
#Well, the entire map file is THE map object. Enclosed between MAP and END
#at the very bottom of this map file, are keyword/value pairs and other
#objects.
MAP
IMAGETYPE PNG
EXTENT -97.238976 41.619778 -82.122902 49.385620
SIZE 400 300
SHAPEPATH "../data"
IMAGECOLOR 255 255 255
FONTSET "../fonts/fonts.list"
SYMBOLSET "../symbols/symbols35.sym"
# Layer objects are defined beneath the mapobject. You need at least one
# layer defined in your map filebefore you can display a map... You can
# define as many layers as you'd likealthough a limit is typically hard-coded
# in map.h in the MapServer source. The default limit is set at 100. You'd
# have to have a very specialized applicationto need more than 100 layers in
# your application.
#
# Start of LAYER DEFINITIONS---------------------------------------------
LAYER # States polygon layer begins here
NAME states
DATA states_ugl
STATUS OFF
TYPE POLYGON
# CLASSITEM defines the non-spatialattribute that you will be using to
# separate a layer into classes. This attribute will be in the DBF file
# of your shapefile (it will be different foreach data format). In this
# example the shapefile states_ugl has anassociated database
# (states_ugl.dbf) that contains anattribute called "CLASS". Youwill be
# using two values in the CLASS attributeto separate the classes (also
#called themes) used in this layer--land and water. CLASSITEM is used in
# association with the EXPRESSION parameterin the CLASS object. See below.
CLASSITEM "CLASS"
# The class object is defined within thelayer object. You can define as
# many classes as you need (well, there arelimits as with layers, but it's
# senseless to define more than ten on a"normal" layer. There are
# situations, however, where you might haveto do it.)
CLASS
NAME 'States'
EXPRESSION 'land'
# There are styles in a class, just likethere are classes in a layer,
# just like there are layers in amap. You can define multiple styles in
# a class just as you can define multipleclasses in a layer and multiple
# layers in a map.
STYLE
COLOR 232 232 232
END
END
END # States polygon layer ends here
# In addition to vector data (shapefiles arevector data), MapServer supports
# a host of raster formats. In GIS world, one of the most common raster
# formats is GeoTIFF, a TIFF image withgeospatial headers. MapServer also
# supports JPEG, PNG, GIF, and other commonformats. Other raster formats
# supported by MapServer include ESRIArc/Info grid, HDF and HDF-EOS, NetCDF,
# Generic raster binaries, OGC Web MapService (WMS) layers, etc. Pretty much
# any raster format you can think of isprobably supported, thanks to the
# impressive Geospatial Data AbstractionLibrary (GDAL, pronounced "GOODALL"
# or GOODLE?). More information on GDAL is available athttp://www.gdal.org.
#
# MapServer 4.x can read and displaybitmapped (like GIFs), RGB/A (true
# color), and multispectral (images with morethan 3 bands, like raw LandSat
# images) rasters.
LAYER # MODIS raster layer begins here
NAME modis
DATA "raster/mod09a12003161_ugl_ll_8bit.tif"
STATUS OFF
TYPE RASTER
PROCESSING "BANDS=1,2,3"
OFFSITE 71 74 65
END # MODIS raster layer ends here
LAYER # States line layer begins here
NAME states_line
DATA states_ugl
STATUS OFF
TYPE LINE
CLASSITEM "CLASS"
CLASS
NAME 'State Boundary'
EXPRESSION 'land'
STYLE
SYMBOL 'line5'
COLOR 64 64 64
SIZE 1
END
END
END # States line layer ends here
# Labels can be defined in its ownlayer. This is useful if, say, you want
# to label a polygon layer that's covered byanother layer. By keeping the
# label separate from the polygon and placingit near the bottom of the map
# file (so its drawn on, or near the, top),you can still see the label even
# though you might not be able to see thepolygon. It is also a good
# alternate to point symbols.
#
# A label layer is actually defined withANNOTATION type (This is derived from
# points, Node IDs for lines, or polygonIDs).
LAYER # States label layer begins here
NAME states_label
DATA states_ugl
STATUS OFF
TYPE ANNOTATION
CLASSITEM "CLASS"
# Just like CLASSITEM, LABELITEM definesthe database attribute that you
# will be using to draw labels. In this case, the values of the attribute
# "STATE" will be used to labelthe states polygons.
LABELITEM "STATE"
CLASS
EXPRESSION 'land'
STYLE
COLOR -1 -1 -1
END
# There can be labels in a class, justlike there are classes in a layer,
# just like there are layers in amap. You can define multiple labels in
# a class just as you can define multipleclasses in a layer and multiple
# layers in a map.
# MapServer has a very flexible labelingsystem. With that flexibility
# comes complexity, specially when usingtruetype fonts. Please read
# through the LABEL section of theMapServer map file documentation at
# http://www.mapserver.org/mapfile formore information.
LABEL
COLOR 132 31 31
#SHADOWCOLOR 218 218 218 # deprecated, useSTYLE and GEOMTRANSFORM
#SHADOWSIZE 2 2 # deprecated, use STYLE andGEOMTRANSFORM
TYPE TRUETYPE
FONT arial-bold
SIZE 12
ANTIALIAS TRUE
POSITION CL
PARTIALS FALSE
MINDISTANCE 300
BUFFER 4
END # end of label
END # end of class
END # States label layer ends here
# End of LAYER DEFINITIONS-------------------------------
END# All map files must come to an end just as all other things must come to.
Mapfile的对象结构如下:
MAP
LAYER#1-------------LAYER #2----|----LAYER #3--------LAYER #4
(states_poly) (modis) (states_line) (states_label)
| | |
(land)CLASS-|-CLASS (water) |-CLASS |-CLASS
| | | |
STYLE-| |-STYLE |-STYLE STYLE-|-LABEL
当你看到这个mapfile文件,你会发现在多边形州图层后面(下面)添加了一个新的LAYER对象。为什么?MapServer是按反序显示图层的:后进先出(LIFO).mapfile文件中定义的第一个图层会在地图最底层绘制。
所以,如果我们绘制了多边形州图层,它将位于最低层。如果我们在它上面绘制栅格图层,我们将看不到它。这就是为什么第一个图层的STATUS值要设置为OFF。LINE图层定义在栅格图层后面,所以它绘制在栅格图层上面(你可以看到它)。这也是为什么我们将线形州图层从多边形州图层分离的原因。当然标注是绘制在所有图层之上。
MapServer能够基于其他图层的状态自动打开或关闭某一个图层(比如当栅格图层打开的时候你想关闭多边形州图层)。这个是通过使用REQUIRES参数来实现的。一旦开始创建你自己的MapServer应用的时候你要记住这一点。
参数
让我们看看这个mapfile文件中的新参数。
IMAGETYPE
PNG是一种24位或真颜色版本的PNG格式。你也可以试着将这个值改为PNG8。无论使用何种格式注意生成图像的时间。当在真彩色和索引色之间选择时,需要考虑生成图像的时间。
SYMBOLSET
指向符号定义文件路径。文件中的符号被CLASS对象内的SYMBOL参数引用。其实此刻并不是真的需要但是我想在这里抛出空上。请参考Cartographic Symbol Reference 获取更多信息。
DATA raster/mod09a12003161_ugl_ll_8bit.tif
在新添加的LAYER对象中,DATA参数指向一幅GeoTIFF图像。正如支持多格式矢量数据,MapServer也支持多种栅格文件格式。这种支持是通过使用GDAL库来获取的。请阅读Raster Data Reference.获取MapServer支持的不同栅格格式信息和MapServer中栅格用法的一般讨论。
TYPE RASTER
矢量数据TYPT值为POLYGON,LINE和POINT,而用ANNOTATION来表示标注。相反,使用栅格数据(图像)时,我们使用RASTER给参数TYPE赋值。
PROCESSING “BANDS=1,2,3”
LAYER对象参数是在MapServer4.x中添加的。PROCESSING关键字有很多值,但是这里我们用它来指定多光谱数据中哪些波段显示。这个字符串类型的值会传给GDAL库。
OFFSITE
这个参数告诉MapServer哪个像素值需要作为背景(或无效值)去渲染。你可以用图像处理或图像操作程序来获取这个像素值(如Imagine,Photoshop,Gimp).
RGB vs 索引图
为了对比用RGB图像和索引图生成地图的速度,需要将示例的mapfile中的以下几行:
DATA"raster/mod09a12003161_ugl_ll_8bit.tif"
STATUSDEFAULT
TYPERASTER
PROCESSING"BANDS=1,2,3"
OFFSITE71 74 65
替换为
DATA"raster/mod09a12003161_ugl_ll_idxa.tif"
STATUSDEFAULT
TYPERASTER
OFFSITE70 74 66
再一次,将IMAGETYPE从PNG改变为PNG8。
PS:中文版权为asswclw所有,请尊重劳动成果,转载将注明出处。