第二章 Tutorial 教程css
MapServer Tutorial MapServer教程html
Tutorial background教程背景web
虽然有些用户能够在一天内完成本教程,但那些详细研究每一个示例的用户可能指望在一周内完成本教程。正则表达式
本教程中使用的数据集取自美国内政部a zs美国国家地图集(如今由data.gov托管)。数据集被裁剪到上五大湖地区(明尼苏达州、密歇根州和威斯康星州),以减小存储空间。额外的光栅图像是由明尼苏达大学的Terrasip项目提供的。使用本教程时,鼓励您使用本身的数据集。
数据库
与MapServer自己同样,本教程对任何人都是开放的和可自定义的。这样作的目的是但愿有人(或某些人)可以帮助设计和进一步开发它。express
在http://download.osgeo.org/mapserver/docs/mapserver-tutorial.zip下载本教程的数据(以及全部HTML文件)。apache
使用本教程有一些先决条件:
1。用户须要在其计算机上安装并运行Web服务器。此Web服务器必须支持通用网关接口(CGI)程序。
2。用户应该对Web服务器和Internet安全有基本的了解。配置不正确的Web服务器很容易被恶意的人攻击。你的软件安装会失败你zll会损失数小时的工做效率,最坏的状况是你的电脑能够用来攻击互联网上的其余电脑。
3。建议本教程的用户在继续此操做以前先阅读MapServer简介。
4。要使用本教程,用户须要在他们的系统。MapServer源代码可在此处下载。关于如何编译的文档已经存在并安装MapServer:
•对于Unix用户,请阅读MapServer Unix编译和安装指南。
•Windows用户应阅读MapServer Win32编译和安装指南
此外,许多平台都存在预编译的二进制文件(请参见下载页)。浏览器
Windows, UNIX/Linux Issues Windows、Unix/Linux问题安全
Paths服务器
路径
本教程是在Linux/Unix上建立的,可是应该在Windows平台上进行最小的更改。主要区别在于map文件中的路径。Windows用户须要指定其教程文件所在硬盘的驱动器号。这里是一个例子:
Unix地图文件可能包含以下参数:
在Windows中,相同的参数可能以下所示:
或者:
请注意,斜杠或反斜杠在Windows中都有效。若是您想区分映射文件中的虚拟路径(如URL或Web地址)和本地路径,一般的反斜杠可能对您颇有用。可是,若是您计划在某个时间点将应用程序移动到Unix,那么您的zll有一项单调的任务,即将全部反斜杠切换为斜杠。
当咱们讨论路径的主题时,请记住,map文件中的路径一般与系统的根目录相关:UNIX中的斜杠(_aij/_ a i)或Windows中的某些驱动器号(_aijc:_ a i)。这是正确的,除非特别要求输入URL或引用URL。使用HTML模板文件时,路径是相对于Web服务器的根目录。也就是说,“/tutorial/“是相对于“https://demo.mapserver.org/“的。请阅读http://www.alistaparate.com/articles/slashforward/了解有关URL的一些细节。
Executable
可执行文件
另外一个问题是,Unix可执行文件不须要.exe或.com扩展名,但在Windows中须要。若是您使用的是Windows,请将.exe附加到全部实例中的”/cgi bin/mapserv“或”/cgi-bin/mapserv“改成"cgi-bin/mapserv.exe"或"/cgi-bin/mapserv50.exe "。
Other Resources 其余资源
还有其余文档可让您更好地了解MapServer提供的许多定制。请访问MapServer文档页面:http://www.mapserver.org/documentation.html。在这里,您能够找到几个howto文档,从入门到使用mapscript,这是一个针对mapserver的脚本接口。
Section 1: Static Maps and the MapFile
• 拿个shapefile,任何人咱们能够用鼠标在一个浏览器上显示该shapefile形状文件。看:
– Example 1.1 - A map with a single layer:http://localhost/tutorial/example1-1.html
mapserver能够建立一个地图并将其转储到本地目录,或者直接发送到请求的Web浏览器,如本例所示。您能够在不须要HTML页面的状况下查看它,只需输入如下URL:http://<insert hostname or ip address here>/cgi bin/mapserv.exe?map=/ms4w/apps/tutorial/htdocs/example1-1.map&layer=states&mode=map(记住将“<insert hostname or ip address here>”替换为Web服务器的名称,例如“localhost”,或其IP地址,例如“127.0.0.1”)。
这个URL能够分为三部分:第一部分,http://<insert hostname or ip address here>/cgi bin/mapserv.exe?,调用mapserver cgi程序。若是您按原样调用它,您将获得这个熟悉的消息:
No query information to decode. QUERY_STRING is set, but empty.
接下来的三个部分是组成查询字符串的部分。查询字符串包含CGI参数(变量及其值),每一个参数由一个与(&)分隔。所以,查看查询字符串,第一个参数“map”有一个值“/ms4w/apps/tutorial/htdocs/example1-1.map”--这告诉mapserver cgi程序(mapserv或mapserv.exe)要处理/解析什么mapfile。下一个参数“layer=states”告诉mapserv.exe“打开”状态层——回想一下,咱们将层对象命名为“states”。最后一个参数“mode=map”告诉mapserv.exe如何处理来自mapfile的输出。在本例中,它告诉mapserv.exe直接将映像转储到Web浏览器(客户端),而不首先在服务器上建立临时映像。mapserver“模式”cgi变量能够采用“map”之外的值。例如,若是使用“mode=browse”,mapserver将把映像转储到服务器上的临时目录。浏览模式如今不起做用,但稍后咱们将再次使用。
这是map文件的外观(例如1-1.map):
# 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 # Layer objects are defined beneath the map object. You need at least one # layer defined in your map file before you can display a map... You can # define as many layers as you'd like although 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 application to 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 # The class object is defined within the layer object. You can define as # many classes as you need (well, there are limits as with layers, but it's # senseless to define more than ten on a "normal" layer. There are # situations, however, where you might have to do it.) CLASS NAME "The Upper Great Lakes States" # There are styles in a class, just like there are classes in a layer, # just like there are layers in a map. You can define multiple styles in # a class just as you can define multiple classes in a layer and multiple # layers in a map. STYLE COLOR 232 232 232 OUTLINECOLOR 32 32 32 END END END # States polygon 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是mapserver的基本配置机制。它由“对象”组成,每一个对象能够有关键字或其余对象。它有一个层次结构,一些对象落在其余对象之下…在这个层次结构的顶部是映射对象,全部其余对象都属于它。这个例子显示了对象的一个很是直接的继承关系。当您经过每一个示例时,这些层次树的复杂性将增长。
关于mapfiles的一些简短说明:咱们用对象名定义mapfile中的每一个对象,并用“end”关闭它,在注释前面加上一个磅()符号。
让咱们按对象分解“example1-1.map”。其结构以下:
让咱们看下Map文件中的关键字:
MAP地图
每一个MAP地图文件都以MAP开始——整个地图文件就是MAP地图对象。
IMAGETYPE图像类型
关键字imagetype用于定义mapserver cgi程序应用于输出的图像格式。在本例中,咱们使用索引颜色png(相似于gif)。若是咱们使用gif支持、wbmp或jpeg编译gd库,这多是gif。咱们还能够指定其余输出选项(pdf、swf、geotiff),前提是咱们编译了对它们的支持并使用outputformat对象指定它们。输出格式超出了本教程的范围,但您能够经过阅读MapServer网站中的文档了解更多信息。
EXTENT范围
此参数指定地图的输出范围——初始地图的边界框。范围值按如下格式给出:<lower left x><lower left y><upper right x><upper right y>,每一个值之间用空格分隔。这须要与数据使用相同的单位,或者,若是指定了不一样的输出投影,则须要与输出投影使用相同的单位。
在这个例子中,咱们的数据是地理投影的,因此单位是十进制的。您可使用做为gdal/ogr库包一部分的实用程序“ogrinfo”来获取特定形状文件(或其余支持的矢量格式)的范围。下面是我用于获取此示例范围的命令:
ogrinfo -al -so states_ugl.shp
这将返回如下输出:
INFO: Open of `states_ugl.shp' using driver `ESRI Shapefile' successful. Layer name: states_ugl Geometry: Polygon Feature Count: 204 Extent: (-97.238976, 41.619778) - (-82.122902, 49.385620) Layer SRS WKT: (unknown) AREA: Real (12.3) PERIMETER: Real (12.3) STATESP020: Real (11.0) STATE: String (20.0) STATE_FIPS: String (2.0) CLASS: String (5.0)
您还可使用ArcView或其余开源的GIS包——Quantum GIS、Thuban等。
请随意更改范围的值,以便更好地了解它如何更改您的地图。
SIZE 尺寸
这是MapServer将生成的图像(地图)的大小(像素)。因此咱们的地图是400像素宽,300像素高。再次,把它改成你喜欢的内容,看看它是如何影响你的地图的。
SHAPEPATH 形状路径
这是到数据层的路径。您能够提供绝对路径(即“/ms4w/apps/tutorial/data”或“c:/ms4w/apps/tutorial/data”)或相对于map地图文件位置的路径(在本例中,您将使用“../data”)。此路径没必要是可访问Web的,除非您但愿任何人下载您的原始数据,不然可能不该该是。它与Web没有直接关系,所以在这里甚至不要考虑URL——只需确保运行Web服务器的用户(一般是*nix世界中的“nobody”或“apache”)能够读取shapepath中的数据。
IMAGECOLOR 图像颜色
这是地图的背景色。这些值是RGB值,所以255红色、255绿色和255b会产生白色背景。继续玩这个value。
如今让咱们看看图层对象参数:
Layer图层
标记地图对象中图层的开始。尽管默认状况下限制为100,但能够指定任意多个层。要更改此限制,必须编辑map.h头文件(在soure树中)并从新编译mapserver。
NAME名字
这是层标识符。MapServer使用此名称打开和关闭图层。在本例中,它不起做用,由于咱们将图层状态STATUS属性设置为默认。在后面的例子中咱们将回到这个问题。
DATA数据
数据的名称(本例中为shapefile)。请阅读MapServer矢量数据指南以了解有关如何访问MapServer中的矢量数据的更多信息。
MAPServer经过使用ogr库(gdal软件包的一部分)支持ESRI的shapefile之外的矢量数据格式。请访问gdal项目网站http://www.gdal.org/并阅读http://www.gdal.org/ogr/ogr_formats.html,了解有关各类支持的ogr格式的更多信息。
TYPE类型
它是什么类型的数据?若是是矢量数据,则能够指定它是多边形、直线(即便数据在技术上是多段线,也可使用直线)仍是点。也能够指定光栅或注释数据。这里咱们要显示多边形。
STATUS状态
层根据其状态打开或关闭。DEFAULT默认值始终为“开”。当层名称做为查询字符串的一部分传递时,打开或关闭工做。
让咱们看看CLASS类对象参数:
CLASS类
标记Layer层对象中CLASS类对象的开头。您能够在一个层中指定任意多个类,尽管默认状况下限制为50个类。您必须从新编译mapserver才能更改此默认值。
NAME名字
此类的描述性标识符。图层对象能够有多个类,就像地图对象能够有多个图层同样。MapServer将类名用做图例的标签,所以在命名类时请确保使用适当的描述性名称。咱们稍后将在本教程中讨论图例。
最后,咱们来看一下STYLE样式对象参数:
STYLE风格
标记样式对象的开始。您能够在一个类中定义多个样式——当您想将一个样式覆盖到另外一个样式上时,这颇有用。
COLOR颜色
这是多边形的填充颜色。若是类型是line,这是line颜色。这些值采用RGB格式。
OUTLINECOLOR轮廓颜色
这是多边形的轮廓颜色。这些值采用RGB格式。默认状况下,MapServer不绘制多边形轮廓,所以若是要查看多边形边界,则须要定义一个大纲颜色。
这结束了本教程中的第一个示例。建议您更改映射文件中关键字的值。它将帮助您理解这些关键字的做用。
Example 1.2: Static Map with Two Layers
• 咱们能够重复显示相同的shapefile数据集。咱们能够在一个图层中显示多边形属性,在另外一个图层中显示线条属性。˘
– Example 1.2 - A map with two layers
与第一个示例同样,此图像是经过将<img>标记的源连接到此URL生成的。这就是本节中大多数示例的工做方式。
不管如何,您会注意到这里的地图与第一个示例相同。是的,但地图文件不一样。看看。
这是映射文件的外观(example 1-2.map):
# 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 # Layer objects are defined beneath the map object. You need at least one # layer defined in your map file before you can display a map... You can # define as many layers as you'd like although 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 application to need more than 100 layers in # your application. # Start of LAYER DEFINITIONS --------------------------------------------- LAYER # States polygon layer begins here NAME states_poly DATA states_ugl STATUS OFF TYPE POLYGON # The class object is defined within the layer object. You can define as # many classes as you need (well, there are limits as with layers, but it's # senseless to define more than ten on a "normal" layer. There are # situations, however, where you might have to do it.) CLASS NAME "States" # There are styles in a class, just like there are classes in a layer, # just like there are layers in a map. You can define multiple styles in # a class just as you can define multiple classes in a layer and multiple # layers in a map. STYLE COLOR 232 232 232 END END END # States polygon layer ends here LAYER # States line layer begins here NAME states_line DATA states_ugl STATUS OFF TYPE LINE CLASS NAME "State Boundary" STYLE COLOR 32 32 32 END END END # States line layer ends here # End of LAYER DEFINITIONS ------------------------------- END # All map files must come to an end just as all other things must come to...
按对象划分的map文件结构以下所示:
在这里,咱们将原始层分为两层。第一层仍然是多边形层,但样式再也不具备outline颜色。第二层与第一层相似,只是类型更改成线条,样式中的颜色更改成与第一个示例中的outline轮廓颜色相同的颜色。这将生成与第一个图像相同的图像…那么,为何要这样作?若是咱们继续在状态层的顶部添加层,那么轮廓极可能会被其余层覆盖。为了在添加这些其余层以后仍能看到状态边界,咱们必须将状态边界线层与状态多边形层分离,并将其放在其余层的顶部。咱们如何定义/添加层是有顺序的,在本节中,您将清楚地看到它。
Example 1.3: Displaying Classes in a Layer
• 咱们能够选择要显示的形状文件的哪些部分。咱们使用类对象来完成这项工做…
– 示例1.3-使用类制做“有用”地图
这是map地图文件的外观(Example 1-3.map):
# 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 # Layer objects are defined beneath the map object. You need at least one # layer defined in your map file before you can display a map... You can # define as many layers as you'd like although 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 application to need more than 100 layers in # your application. # Start of LAYER DEFINITIONS --------------------------------------------- LAYER # States polygon layer begins here NAME states_poly DATA states_ugl STATUS OFF TYPE POLYGON # CLASSITEM defines the non-spatial attribute 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 for each data format). In this # example the shapefile states_ugl has an associated database # (states_ugl.dbf) that contains an attribute called "CLASS". You will be # using two values in the CLASS attribute to separate the classes (also # called themes) used in this layer--land and water. CLASSITEM is used in # association with the EXPRESSION parameter in the CLASS object. See below. CLASSITEM "CLASS" # The class object is defined within the layer object. You can define as # many classes as you need (well, there are limits as with layers, but it's # senseless to define more than ten on a "normal" layer. There are # situations, however, where you might have to do it.) CLASS NAME 'States' EXPRESSION 'land' # Only polygons where "CLASS" = 'land' will be drawn. # There are styles in a class, just like there are classes in a layer, # just like there are layers in a map. You can define multiple styles in # a class just as you can define multiple classes in a layer and multiple # layers in a map. STYLE COLOR 232 232 232 END END CLASS NAME 'Water' EXPRESSION 'water' # Only polygons where "CLASS" = 'water' will be drawn. STYLE COLOR 198 198 255 END END END # States polygon 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 COLOR 32 32 32 END END END # States line layer ends here # End of LAYER DEFINITIONS ------------------------------- END # All map files must come to an end just as all other things must come to...
按对象划分的map文件结构以下所示:
咱们的地图文件仍然只有两个图层,可是多边形图层被分解为两个类。让咱们看看其余参数:
CLASSITEM
此关键字用于指定用于分隔类对象的属性。在这个例子中,属性是“class”。若是打开与该层的shapefile关联的数据库文件,您将看到有一个名为“class”的列(属性)。
咱们如何知道要使用哪一个属性?好吧,shapefiles中的数据库记录存储在dbf文件中。您能够在电子表格程序(如openoffice.org的calc)或桌面地理信息系统软件(如qgis、thuban或arcview)中打开它。若是您的数据带有元数据(应该是!),您能够浏览该元数据文件以获取属性信息。您还可使用“ogrinfo”在shapefiles中显示基本属性信息——回顾示例1.1(在“layer srs wkt:”后面的最后几行显示属性名称和类型)。
EXPRESSION
对于每一个类,咱们指定要使用的属性值。这是最简单的表达形式。表达式可能比这更复杂,容许计算正则表达式或逻辑表达式。请查看mapfile引用页,了解如何使用表达式。
Example 1.4: Labeling the Map
咱们也能够在地图上添加标签…
MapServer有一个很是灵活的标签引擎。它支持本机位图和TrueType字体。TrueType支持字体缩放。标签的角度和位置能够定制…若是您花时间学习建立好标签所涉及的许多参数,您将得到信息丰富、美观的地图。
这是map文件的外观(例如1-4.map):
# 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" # Layer objects are defined beneath the map object. You need at least one # layer defined in your map file before you can display a map... You can # define as many layers as you'd like although 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 application to need more than 100 layers in # your application. # Start of LAYER DEFINITIONS --------------------------------------------- LAYER # States polygon layer begins here NAME states_poly DATA states_ugl STATUS OFF TYPE POLYGON # CLASSITEM defines the non-spatial attribute 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 for each data format). In this # example the shapefile states_ugl has an associated database # (states_ugl.dbf) that contains an attribute called "CLASS". You will be # using two values in the CLASS attribute to separate the classes (also # called themes) used in this layer--land and water. CLASSITEM is used in # association with the EXPRESSION parameter in the CLASS object. See below. CLASSITEM "CLASS" # Just like CLASSITEM, LABELITEM defines the database attribute that you # will be using to draw labels. In this case, the values of the attribute # "STATE" will be used to label the states polygons. LABELITEM "STATE" # The class object is defined within the layer object. You can define as # many classes as you need (well, there are limits as with layers, but it's # senseless to define more than ten on a "normal" layer. There are # situations, however, where you might have to do it.) CLASS NAME 'States' EXPRESSION 'land' # There are styles in a class, just like there are classes in a layer, # just like there are layers in a map. You can define multiple styles in # a class just as you can define multiple classes in a layer and multiple # layers in a map. STYLE COLOR 232 232 232 END # There can be labels in a class, just like there are classes in a layer, # just like there are layers in a map. You can define multiple labels in # a class just as you can define multiple classes in a layer and multiple # layers in a map. # MapServer has a very flexible labeling system. With that flexibility # comes complexity, specially when using truetype fonts. Please read # through the LABEL section of the MapServer map file documentation at # http://www.mapserver.org/mapfile for more information. LABEL COLOR 132 31 31 SHADOWCOLOR 218 218 218 SHADOWSIZE 2 2 TYPE TRUETYPE FONT arial-bold SIZE 12 ANTIALIAS TRUE POSITION CL PARTIALS FALSE MINDISTANCE 300 BUFFER 4 END # end of label END CLASS NAME 'Water' EXPRESSION 'water' STYLE COLOR 198 198 255 END END END # States polygon 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 COLOR 64 64 64 END END END # States line layer ends here # End of LAYER DEFINITIONS ------------------------------- END # All map files must come to an end just as all other things must come to...
按对象划分的map文件结构以下所示:
这里咱们介绍LABEL对象的几个参数:
FONTSET字体集
这里咱们指定TrueType字体列表(或字体集)文件的完整路径。此文件列出了每一个可用字体。有关详细信息,请参见文件自己和mapfile引用。fontset是地图对象的参数。
LABELITEM
这将指定要用于标记的数据属性,在本例中为“STATE”。labelitem是层对象的参数。
LABEL标签
标记标签对象的开头。标签对象可用于其余对象(即scaleBar对象)下。
COLOR颜色
在标签对象中,颜色指定标签文本的颜色。
SHADOWCOLOR阴影颜色
指定标签文本的阴影颜色。
SHADOWSIZE阴影大小
指定阴影大小。该值对应于x和y以像素为单位移动。因此,“2 2”是指两个像素宽两个像素高。
TYPE类型
在Label对象中,TYPE指定要使用的字体类型。咱们能够选择TrueType或BitMap(内置字体)。咱们选择TrueType。
FONT字体
若是将TYPE指定为TrueType,则须要指定要使用的字体。这里的值是字体列表文件中的“别名”。
SIZE尺寸
若是使用TrueType字体,则值为像素大小。若是是位图,能够说“小”或“大”。
ANTIALIAS
这将打开或关闭TrueType抗锯齿。请记住,值不是开或关,而是真或假。
POSITION位置
标签文本相对于标签点的位置。该值是垂直和水平位置的组合。垂直对齐有如下选项:C表明中心,U表明上部,L表明下部。对于水平对齐,您有如下选项:C表明中心,L表明左侧,R表明右侧。所以,要将标签文本与标签ID的中心对齐,须要使用值“cc”(中心)。或者若是你想把它放在身份证的左下角,你能够用LL。另外一种方法是让MapServer决定标签的最佳位置。为此,您将使用值“auto”。
PARTIALS部分
告诉MapServer是否生成不完整的标签文本。这里的默认设置是不生成标签文本的片断。该值为真或假。
MINDISTANCE最小距离
这是重复标签之间的最小距离(像素)。看看若是增长或减小这个值会发生什么。
BUFFER缓冲器
每一个标签的填充(像素)。这用于加强可读性。4个像素的缓冲区意味着在4个像素的范围内不会绘制任何标签。再次,改变看看它是如何工做的。
还能够建立与多边形层分离的标签。对ANNOTATION数据类型执行此操做。查看下一个示例的映射文件,了解如何实现这种标记。您将注意到“label”层中的类对象的颜色参数值为“-1-1-1”。负数告诉MapServer给这个类一个透明的颜色(标签ID不显示)。再次,使用这些值了解它如何影响地图。
Example 1.5: Adding a Raster Layer
除了支持矢量数据(点、线、多边形和注释),MapServer还能够显示光栅数据。经过使用gdal库,mapserver能够输入和输出多种光栅格式。在4.x以前的版本中,光栅输入仅限于单层、灰度或索引彩色图像,而MapServer如今支持RGB和多光谱(多层)图像。此示例演示如何选择使用多光谱数据时要显示的图层。在使用RGB和多光谱图像时,可能会出现明显的性能问题。
由于MapServer5.x使用gd版本2.0.x库来生成输出图像,因此它也支持RGB(24位或真彩色)输出。所以,除了8位(索引颜色或灰度)PNG,您如今还可使用PNG24(真颜色)进行输出。此示例使用PNG24做为ImageType。与RGB输入同样,使用PNG24时可能会出现明显的性能问题。
mapserver实际上也可使用gdal生成输出图像,但这是另外一个主题。若是您想了解更多信息,请查看mapfile引用中的outputformat对象。
这是映射文件的外观(例如1-5.map):
# 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 PNG24 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 map object. You need at least one # layer defined in your map file before you can display a map... You can # define as many layers as you'd like although 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 application to 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-spatial attribute 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 for each data format). In this # example the shapefile states_ugl has an associated database # (states_ugl.dbf) that contains an attribute called "CLASS". You will be # using two values in the CLASS attribute to separate the classes (also # called themes) used in this layer--land and water. CLASSITEM is used in # association with the EXPRESSION parameter in the CLASS object. See below. CLASSITEM "CLASS" # The class object is defined within the layer object. You can define as # many classes as you need (well, there are limits as with layers, but it's # senseless to define more than ten on a "normal" layer. There are # situations, however, where you might have to do it.) CLASS NAME 'States' EXPRESSION 'land' # There are styles in a class, just like there are classes in a layer, # just like there are layers in a map. You can define multiple styles in # a class just as you can define multiple classes 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 are vector data), MapServer supports # a host of raster formats. In GIS world, one of the most common raster # formats is GeoTIFF, a TIFF image with geospatial headers. MapServer also # supports JPEG, PNG, GIF, and other common formats. Other raster formats # supported by MapServer include ESRI Arc/Info grid, HDF and HDF-EOS, NetCDF, # Generic raster binaries, OGC Web Map Service (WMS) layers, etc. Pretty much # any raster format you can think of is probably supported, thanks to the # impressive Geospatial Data Abstraction Library (GDAL, pronounced "GOODALL" # or GOODLE?). More information on GDAL is available at http://www.gdal.org. # # MapServer 4.x can read and display bitmapped (like GIFs), RGB/A (true # color), and multispectral (images with more than 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 own layer. This is useful if, say, you want # to label a polygon layer that's covered by another layer. By keeping the # label separate from the polygon and placing it 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 the polygon. It is also a good # alternate to point symbols. # # A label layer is actually defined with ANNOTATION type (This is derived from # points, Node IDs for lines, or polygon IDs). LAYER # States label layer begins here NAME states_label DATA states_ugl STATUS OFF TYPE ANNOTATION CLASSITEM "CLASS" # Just like CLASSITEM, LABELITEM defines the database attribute that you # will be using to draw labels. In this case, the values of the attribute # "STATE" will be used to label the states polygons. LABELITEM "STATE" CLASS EXPRESSION 'land' STYLE COLOR -1 -1 -1 END # There can be labels in a class, just like there are classes in a layer, # just like there are layers in a map. You can define multiple labels in # a class just as you can define multiple classes in a layer and multiple # layers in a map. # MapServer has a very flexible labeling system. With that flexibility # comes complexity, specially when using truetype fonts. Please read # through the LABEL section of the MapServer map file documentation at # http://www.mapserver.org/mapfile for more information. LABEL COLOR 132 31 31 SHADOWCOLOR 218 218 218 SHADOWSIZE 2 2 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...
按对象划分的map地图文件结构以下所示:
当您查看map地图文件时,您将看到新的图层对象被添加到区域polygon多边形图层的下面(后面)。为何?mapserver以相反的顺序显示层——后进先出(lifo)。地图文件中定义的第一个图层绘制在地图的底部。
因此,若是咱们画了state多边形层,它会在底部。由于光栅图层被绘制在上面,因此咱们看不到它。这就是为何第一层的状态值为off的缘由。状态线图层定义在光栅图层的下方,以便在顶部绘制(您能够看到)。这就是咱们将状态线层与状态多边形层分离的缘由。固然,标签是在最上面。
MapServer能够根据其余图层的状态自动打开或关闭图层——好比说,当光栅图层打开时,但愿关闭状态多边形图层。这是经过使用Requires参数来完成的。记住这一点,由于一旦开始建立本身的MapServer应用程序,您可能会想要使用它。
让咱们看看mapfile中引入的新参数:
IMAGETYPE 图像类型
这不是新的,但值“png24”是。PNG24是PNG格式的24位或真彩色版本。mapserver如今拥有数百万个颜色组合,而不是仅限于输出图像的256种颜色组合。顺便说一下,试着把这个值改回png。请注意,使用这两种格式生成图像所需的时间。在选择真彩色和索引颜色时,要考虑生成图像所需的时间。
SYMBOLSET符号集
指向符号定义文件的路径。此文件中的符号由类对象中的符号参数引用。如今不须要,但我想我如今就把它扔到这里。更多信息,请参考地图文件参考和地图服务器的地图符号构造。
DATA数据“光栅/mod09a1203161_ugl_ll_8bit.tif”
在新添加的图层对象中,数据参数指向geotiff图像。与矢量数据集同样,MapServer支持多种光栅文件格式。这种支持是经过使用gdal库来实现的。有关MapServer支持的不一样光栅格式的详细信息,以及有关在MapServer中使用光栅的通常性讨论,请阅读http://www.mapserver.org/input/graster.html上的光栅数据操做方法。
TYPE RASTER类型光栅
当使用光栅数据(图像)时,咱们使用值光栅做为参数类型,而不是矢量数据的多边形、直线和点值以及标签ID的注释。
PROCESSING "BANDS=1,2,3"处理“波段=1,2,3”
在MapServer 4.x中,这个层对象参数是新的。处理关键字有不少值,但在本例中,咱们使用它来选择要显示多光谱图像中的波段。这里的值是将传递到gdal库的字符串。有关这一点的文档目前是最少的,但有关使用processing关键字的更多示例,请参阅mapfile参考。
OFFSITE
此参数告诉MapServer要渲染为背景(或忽略)的像素值。您可使用图像处理或图像操做程序(例如,Imagine、Photoshop、Gimp)获取像素值。
要在使用RGB图像而不是索引颜色图像时比较地图建立速度,请替换地图文件中的如下行:
DATA "raster/mod09a12003161_ugl_ll_8bit.tif" STATUS DEFAULT TYPE RASTER PROCESSING "BANDS=1,2,3" OFFSITE 71 74 65
为:
DATA "raster/mod09a12003161_ugl_ll_idxa.tif" STATUS DEFAULT TYPE RASTER OFFSITE 70 74 66
另外,尝试将imagetype从png24更改成png。
Example 1.6: Defining Projections and Extents
咱们能够将咱们的数据从几乎任何投影从新投影到几乎任何…是的,看看!
哇!这里发生了什么?
你刚刚经历了一次on-the-fly的再投射。在map文件中定义投影对象时,MapServer将图层和投影信息传递给执行重投影的proj.4库。有关proj.4库的更多信息,请访问http://trac.osgeo.org/proj/。
这个例子试图为MapServer中的投影支持提供一些帮助。
这是map文件的外观(例如1-6.map):
# 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 PNG24 # EXTENT 199949.651166 -371954.772084 1472121.6862 632767.19157 EXTENT 201621.496941 -294488.285333 1425518.020722 498254.511514 # LAEA #EXTENT -97.5 41.619778 -82.122902 49.38562 # Geographic SIZE 400 300 SHAPEPATH "../data" SYMBOLSET "../symbols/symbols35.sym" FONTSET "../fonts/fonts.list" # The projection object is typically used within the map and the layer # objects. You only define it once within the map object and this definition # becomes your output projection--MapServer will render your maps in this # projection. You also use the projection object within the layer object to # define your input projection. Your layers can be in different # projections--MapServer will reproject them into your output projection. # If no projection is defined within the layer object, MapServer assumes # your input projection is the same as your output projection. This is not # a required object unless you're creating a map file that supports one of # the OGC interoperability web services specifications (WMS/WFS/WCS). # # This is the output PROJECTION definition ------ PROJECTION # Projection parameters can be defined in two ways... # This is the traditional Proj.4 definition of Lambert Azimuthal Equal-Area # projection for the Continental U.S. "proj=laea" "ellps=clrk66" "lat_0=45" "lon_0=-100" # Alternatively, you can specify an EPSG code. # This is the EPSG code for Lambert Azimuthal Equal-Area # projection for the U.S. # # "init=epsg:2163" END # End of the output Projection definition --- # Layer objects are defined beneath the map object. You need at least one # layer defined in your map file before you can display a map... You can # define as many layers as you'd like although 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 application to 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 # Here's an example of the input projection definition. # EPSG:4326 is code for geographic (latlong) projection # using the WGS84 datum. # # PROJECTION objects within the LAYER object define the input # projection--this is the native projection of your data. PROJECTION "init=epsg:4326" END # CLASSITEM defines the non-spatial attribute 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 for each data format). In this # example the shapefile states_ugl has an associated database # (states_ugl.dbf) that contains an attribute called "CLASS". You will be # using two values in the CLASS attribute to separate the classes (also # called themes) used in this layer--land and water. CLASSITEM is used in # association with the EXPRESSION parameter in the CLASS object. See below. CLASSITEM "CLASS" # The class object is defined within the layer object. You can define as # many classes as you need (well, there are limits as with layers, but it's # senseless to define more than ten on a "normal" layer. There are # situations, however, where you might have to do it.) CLASS NAME 'States' EXPRESSION 'land' # There are styles in a class, just like there are classes in a layer, # just like there are layers in a map. You can define multiple styles in # a class just as you can define multiple classes 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 are vector data), MapServer supports # a host of raster formats. In GIS world, one of the most common raster # formats is GeoTIFF, a TIFF image with geospatial headers. MapServer also # supports JPEG, PNG, GIF, and other common formats. Other raster formats # supported by MapServer include ESRI Arc/Info grid, HDF and HDF-EOS, NetCDF, # Generic raster binaries, OGC Web Map Service (WMS) layers, etc. Pretty much # any raster format you can think of is probably supported, thanks to the # impressive Geospatial Data Abstraction Library (GDAL, pronounced "GOODALL" # or GOODLE?). More information on GDAL is available at http://www.gdal.org. # # MapServer 4.x can read and display bitmapped (like GIFs), RGB/A (true # color), and multispectral (images with more than 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 PROJECTION "init=epsg:4326" END END # MODIS raster layer ends here LAYER # States line layer begins here NAME states DATA states_ugl STATUS OFF TYPE LINE PROJECTION "init=epsg:4326" END 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 LAYER # States label layer begins here NAME states_label DATA states_ugl STATUS OFF TYPE ANNOTATION PROJECTION "init=epsg:4326" END CLASSITEM "CLASS" # Just like CLASSITEM, LABELITEM defines the database attribute that you # will be using to draw labels. In this case, the values of the attribute # "STATE" will be used to label the states polygons. LABELITEM "STATE" CLASS EXPRESSION 'land' STYLE COLOR -1 -1 -1 END # There can be labels in a class, just like there are classes in a layer, # just like there are layers in a map. You can define multiple labels in # a class just as you can define multiple classes in a layer and multiple # layers in a map. # MapServer has a very flexible labeling system. With that flexibility # comes complexity, specially when using truetype fonts. Please read # through the LABEL section of the MapServer map file documentation at # http://www.mapserver.org/mapfile for more information. LABEL COLOR 132 31 31 SHADOWCOLOR 218 218 218 SHADOWSIZE 2 2 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...
在咱们的地图文件中,您可能注意到的第一件事是原始范围已被注释掉,新的范围值看起来与纬度和经度值不一样。另外,咱们在顶部附近和每一个Layer层中添加了一个投影对象。
让咱们看看新的对象和参数:
EXTENT 201621.496941 -294488.285333 1425518.020722 498254.511514
咱们提供给MapServer的范围须要与输出投影使用相同的单位。因为兰伯特方位角的等面积单位是米,咱们必须给新的范围以米。咱们可使用arcview或其余一些gis包计算新的范围,也可使用proj.4的cs2cs实用程序。下面的命令可用于从新投影原始范围值:
cs2cs +proj=latlong +datum=WGS84 +to +proj=laea +ellps=clrk66 +lat_0=45 +lon_0=-100
键入命令后,输入西南坐标对(左下角坐标),用空格分隔:-97.5 41.619778
“cs2cs”实用程序给出的值:
208398.01 -372335.44 0.000
而后键入东北坐标对(右上角坐标),再次用空格分隔:-82.122902 49.38562,并返回如下值:
1285308.08 632638.93 0.000
您会注意到“cs2cs”返回一组三个值。您能够忽略第三个值0.000,由于它表示高度(咱们不使用)。不管如何,咱们不能将范围表示为:
EXTENT 208398.01 -372335.44 1285308.08 632638.93
但这与上面的范围不符,你说。好吧,这就是你回绝的时候一般会发生的事情——地图不必定像你想象的那样居中。你能够绕着它转,向左走几公里,向右再加几米。或者,您可使用图形化的地理信息系统包来提供范围。如下是使用ArcView获取范围的说明。
MapServer能够经过两种方式获取投影定义。第一个显示在映射文件的输出投影对象(第一个投影块)中。这是将参数传递到proj.4库的传统方法。另外一种方法是使用EPSG代码。这些代码是欧洲石油调查集团(EPSG)定义的标准投影代码(或空间参考标识符)。在咱们的兰伯特方位角等面积投影,它有一个代码“2163”。若是在投影后注释掉这四行,并取消对行“init=epsg:2163”的注释,这将向项目4提供相同的信息。若是您想了解EPSG代码的更多信息,请查看“/usr/local/share/proj/epsg”或“c:/proj/nad/epsg”(MS4W中的“/ms4W/proj/nad”)。此外,请访问EPSG网站http://www.epsg.org。
若是您须要了解有关投影的更多信息,请查看如下连接:
http://www.colorado.edu/geography/gcraft/notes/mapproj/mapproj_f.html
http://www.geography.hunter.cuny.edu/mp/
http://www.nationalatlas.gov/articles/mapping/a_projections.html
http://en.wikipedia.org/wiki/Map_projection
http://erg.usgs.gov/isb/pubs/MapProjections/projections.html
你能够经过在网上搜索“地图投影”找到更多信息。
Example 1.7: Adding OGC WMS Layers
咱们可使用来自互联网上其余地图服务器的层(只要它们是WMS服务器)。
MapServer的另外一个使人兴奋的特性是它可以使用来自其余地图服务器的层。在这种状况下,MapServer应用程序将成为WMS(或WFS)客户机。mapserver还能够将map文件中的层共享(或服务于其余地图服务器)。这使应用程序成为一个WMS(或WFS)服务器。什么是WMS或WFS?这些是由开放地理空间联盟(OGC)发布的“基于Web的互操做性服务”规范。
wms表明web map服务,wfs表明web feature服务。简单来讲,这两种规范的区别在于,WMS使用Web光栅格式(PNG、GIF、JPEG)共享层,而WFS使用地理标记语言GML。第三个OGC互操做性规范是Web Coverage Services规范或WCS——MapServer仅在服务器级别支持此规范。要查找有关WMS、WFS和WCS的更多信息,请访问OGC的网站,并查找OGC实现规范或OGC抽象规范。MapServer网站还提供了这些规范的文档。
此示例演示如何在map地图文件中添加WMS层。
这是map文件的外观(示例1-7.map):
# 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 PNG24 EXTENT 201621.496941 -294488.285333 1425518.020722 498254.511514 # LAEA #EXTENT -97.5 41.619778 -82.122902 49.38562 # Geographic SIZE 400 300 SHAPEPATH "../data" SYMBOLSET "../symbols/symbols35.sym" FONTSET "../fonts/fonts.list" # The projection object is typically used within the map and the layer # objects. You only define it once within the map object and this definition # becomes your output projection--MapServer will render your maps in this # projection. You also use the projection object within the layer object to # define your input projection. Your layers can be in different # projections--MapServer will reproject them into your output projection. # If no projection is defined within the layer object, MapServer assumes # your input projection is the same as your output projection. This is not # a required object unless you're creating a map file that supports one of # the OGC interoperability web services specifications (WMS/WFS/WCS). # # This is the output PROJECTION definition ------ PROJECTION # Projection parameters can be defined in two ways... # This is the traditional Proj.4 definition of Lambert Azimuthal Equal-Area # projection for the Continental U.S. # "proj=laea" # "ellps=clrk66" # "lat_0=45" # "lon_0=-100" # # Alternatively, you can specify an EPSG code. # This is the EPSG code for Lambert Azimuthal Equal-Area # projection for the U.S. "init=epsg:2163" END # The web object is defined at the level below the map object. All # web-related parameters (I interchange "parameters" and "keyword/value # pairs" quite frequently, sorry about that) are defined in this object. WEB IMAGEPATH "/ms4w/tmp/" IMAGEURL "/tmp/" END # Layer objects are defined beneath the map object. You need at least one # layer defined in your map file before you can display a map... You can # define as many layers as you'd like although 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 application to 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 # Here's an example of the input projection definition. # EPSG:4326 is code for geographic (latlong) projection # using the WGS84 datum. # # PROJECTION objects within the LAYER object define the input # projection--this is the native projection of your data. PROJECTION "init=epsg:4326" END # CLASSITEM defines the non-spatial attribute 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 for each data format). In this # example the shapefile states_ugl has an associated database # (states_ugl.dbf) that contains an attribute called "CLASS". You will be # using two values in the CLASS attribute to separate the classes (also # called themes) used in this layer--land and water. CLASSITEM is used in # association with the EXPRESSION parameter in the CLASS object. See below. CLASSITEM "CLASS" # The class object is defined within the layer object. You can define as # many classes as you need (well, there are limits as with layers, but it's # senseless to define more than ten on a "normal" layer. There are # situations, however, where you might have to do it.) CLASS NAME 'States' EXPRESSION 'land' # There are styles in a class, just like there are classes in a layer, # just like there are layers in a map. You can define multiple styles in # a class just as you can define multiple classes 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 are vector data), MapServer supports # a host of raster formats. In GIS world, one of the most common raster # formats is GeoTIFF, a TIFF image with geospatial headers. MapServer also # supports JPEG, PNG, GIF, and other common formats. Other raster formats # supported by MapServer include ESRI Arc/Info grid, HDF and HDF-EOS, NetCDF, # Generic raster binaries, OGC Web Map Service (WMS) layers, etc. Pretty much # any raster format you can think of is probably supported, thanks to the # impressive Geospatial Data Abstraction Library (GDAL, pronounced "GOODALL" # or GOODLE?). More information on GDAL is available at http://www.gdal.org. # # MapServer 4.x can read and display bitmapped (like GIFs), RGB/A (true # color), and multispectral (images with more than 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 PROJECTION "init=epsg:4326" END END # MODIS raster layer ends here # MapServer can consume (in ESRI parlance) layers from other map servers as # long as those servers are Web Mapping Service (WMS) providers. WMS is a # web service specification from Open Geospatial Consortium (OGC) and is # intended to be an interoperability standard for web mapping applications. # This allows us to display layers we don't usually have (or can't store in # our computers due to space limitations). The downside is that we have to # depend on some other server to display our layer, and that server can be # down when you really need it. The cool thing is that JPL has a WMS server # that serves out MODIS and LandSat maps for the whole world--try storing # those datasets on your computer! LAYER # MODIS WMS map from JPL NAME modis_jpl TYPE RASTER OFFSITE 0 0 0 STATUS OFF CONNECTIONTYPE WMS CONNECTION "http://wms.jpl.nasa.gov/wms.cgi?" METADATA "wms_srs" "EPSG:4326" "wms_name" "modis" "wms_server_version" "1.1.1" "wms_format" "image/jpeg" END PROJECTION "init=epsg:4326" END END # Modis WMS image ends here LAYER # States line layer begins here NAME states DATA states_ugl STATUS OFF TYPE LINE PROJECTION "init=epsg:4326" END CLASSITEM "CLASS" CLASS NAME 'State Boundary' EXPRESSION 'land' STYLE SYMBOL 'line5' COLOR 32 32 32 SIZE 1 END END END # States line layer ends here LAYER # States label layer begins here NAME states_label DATA states_ugl STATUS OFF TYPE ANNOTATION PROJECTION "init=epsg:4326" END CLASSITEM "CLASS" # Just like CLASSITEM, LABELITEM defines the database attribute that you # will be using to draw labels. In this case, the values of the attribute # "STATE" will be used to label the states polygons. LABELITEM "STATE" CLASS EXPRESSION 'land' STYLE COLOR -1 -1 -1 END # There can be labels in a class, just like there are classes in a layer, # just like there are layers in a map. You can define multiple labels in # a class just as you can define multiple classes in a layer and multiple # layers in a map. # MapServer has a very flexible labeling system. With that flexibility # comes complexity, specially when using truetype fonts. Please read # through the LABEL section of the MapServer map file documentation at # http://www.mapserver.org/mapfile for more information. LABEL COLOR 132 31 31 SHADOWCOLOR 218 218 218 SHADOWSIZE 1 1 TYPE TRUETYPE FONT arial-bold SIZE 12 ANTIALIAS TRUE POSITION CL PARTIALS FALSE MINDISTANCE 200 BUFFER 4 END # end of label END # end of class END # States label layer ends here # End of LAYER DEFINITIONS ------------------------------- END # end of map file
让咱们来看看WMS层:
LAYER #来自JPL的MODIS WMS地图
标记WMS Layer图层对象的开始。
NAME modis_jpl
The LAYER 标识符.
TYPE RASTER 类型光栅
由于这个WMS层是一个图像,因此咱们使用光栅做为层类型。
OFFSITE 0 0 0
Ignore the black background color忽略黑背景色
STATUS OFF
Turn this layer off by default.默认关掉图层
CONNECTIONTYPE WMS WMS链接类型
要使用的层链接类型。默认值是本地的——若是咱们想显式定义,咱们将向地图文件中的全部其余向量和光栅层添加CONNECTIONTYPE LOCAL 。相反,咱们只定义外部的链接类型。WMS是一个外部数据层,来自另外一个地图服务器。
链接“http://mapus.jpl.nasa.gov/wms.cgi?”
容许咱们从另外一个服务器“获取”数据的链接字符串。对于WMS链接,这是一个URL。若是咱们使用PostGIS数据库,它将是一个SQL语句。请注意,字符串必须在映射文件中的一行中。
METADATA 元数据
标记WMS层的元数据对象的开头。MapServer使用此对象中定义的参数以及上面的链接参数来造成对WMS服务器的有效WMS请求。
"wms_srs" "epsg:4326"
WMS投影。有时,WMS服务器支持多个投影。若是是这种状况,您可能但愿在输出投影中请求映射。不幸的是,JPL服务器不支持这种投影。
"wms_name" "modis"
WMS层名称。这就像添加参数“layers=modis”。
"wms_server_version" "1.1.1"
服务器符合的WMS版本。MapServer遵循1.1.1版。
"wms_format" "image/jpeg"
T咱们指望从WMS服务器接收的图像格式。您能够尝试将该值替换为“image/png”或其余内容。
END
标记METADATA元数据对象的结尾
要了解有关向应用程序添加WMS层的更多信息,请访问WMS客户端操做说明文档。
除了添加WMS图层对象,地图下还有一个新对象。这是Web对象。Web对象中有两个参数:
imagepath'/data/tmp/'
指向Web可访问临时目录的绝对本地路径。运行Web服务器进程的用户应该可以写入此目录。确保路径末尾包含斜线(/)。(您的IMAGEPATH可能以下所示:“/home/apache/htdocs/tmp/”或“c:/inetpub/wwwroot/tmp/”。
IMAGEURL '/tmp/'
这是相对于Web服务器的根目录显示ImagePath的方式。若是必须为此键入完整的URL,它将是“http://terrasip.gis.umn.edu/tmp/”。确保路径末尾包含斜线(/)。
最后,在MAP对象中,我添加了一个新参数:NAME。这是MAP对象的标识符。MapServer将此做为它建立的全部图像的前缀,并将其转储到“tmp”目录。在这一点上不须要它,可是拥有它也不会带来伤害。
若是要将数据层共享到其余地图服务器,则须要在地图对象内以及要共享的每一个图层对象内添加元数据对象。从另外一台服务器添加的全部WMS层将自动层叠,而且也将对其余服务器可用。要了解有关如何使MapServer应用程序成为WMS服务器的更多信息,请阅读WMS服务器操做说明文档。还存在用于配置MapServer应用程序以支持WFS标准的文档,不管是做为WFS服务器仍是做为WFS客户机。
Example 1.8: Changing the Map's Output Format
MapServer能够输出各类格式,如PDF和geotiff。
根据您选择的格式,图像可能不会显示在浏览器上。若是连接没法显示在浏览器上,请右键单击上面的图形,并将连接保存为您在地图文件中指定的任何格式。
# 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 NAME EX1.8_ EXTENT 201621.496941 -294488.285333 1425518.020722 498254.511514 # LAEA #EXTENT -97.5 41.619778 -82.122902 49.38562 # Geographic SIZE 400 300 IMAGECOLOR 255 255 255 SHAPEPATH "../data" SYMBOLSET "../symbols/symbols35.sym" FONTSET "../fonts/fonts.list" IMAGETYPE PNG24 OUTPUTFORMAT NAME png DRIVER "GD/PNG" MIMETYPE "image/png" IMAGEMODE PC256 EXTENSION "png" END OUTPUTFORMAT NAME png24 DRIVER "GD/PNG" MIMETYPE "image/png" IMAGEMODE RGBA EXTENSION "png" END OUTPUTFORMAT NAME jpeg DRIVER "GD/JPEG" FORMATOPTION "QUALITY=75" MIMETYPE "image/jpeg" IMAGEMODE RGB EXTENSION "jpg" END OUTPUTFORMAT NAME GTiff DRIVER "GDAL/GTiff" MIMETYPE "image/tiff" IMAGEMODE RGB EXTENSION "tif" END #OUTPUTFORMAT # NAME pdf # MIMETYPE "application/x-pdf" # DRIVER pdf # #FORMATOPTION "OUTPUT_TYPE=RASTER" # not mandatory but needed for WMS layer #END OUTPUTFORMAT NAME AGG DRIVER "AGG/PNG" IMAGEMODE RGB END OUTPUTFORMAT NAME AGGA DRIVER "AGG/PNG" IMAGEMODE RGBA END OUTPUTFORMAT NAME AGGJ DRIVER "AGG/JPEG" IMAGEMODE RGB END # The web object is defined at the level below the map object. All # web-related parameters (I interchange "parameters" and "keyword/value # pairs" quite frequently, sorry about that) are defined in this object. WEB IMAGEPATH '/ms4w/tmp/ms_tmp/' IMAGEURL '/ms_tmp/' END # The projection object is typically used within the map and the layer # objects. You only define it once within the map object and this definition # becomes your output projection--MapServer will render your maps in this # projection. You also use the projection object within the layer object to # define your input projection. Your layers can be in different # projections--MapServer will reproject them into your output projection. # If no projection is defined within the layer object, MapServer assumes # your input projection is the same as your output projection. This is not # a required object unless you're creating a map file that supports one of # the OGC interoperability web services specifications (WMS/WFS/WCS). # # This is the output PROJECTION definition ------ PROJECTION # Projection parameters can be defined in two ways... # This is the traditional Proj.4 definition of Lambert Azimuthal Equal-Area # projection for the Continental U.S. # "proj=laea" # "ellps=clrk66" # "lat_0=45" # "lon_0=-100" # # Alternatively, you can specify an EPSG code. # This is the EPSG code for Lambert Azimuthal Equal-Area # projection for the U.S. "init=epsg:2163" END # Layer objects are defined beneath the map object. You need at least one # layer defined in your map file before you can display a map... You can # define as many layers as you'd like although 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 application to 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 # Here's an example of the input projection definition. # EPSG:4326 is code for geographic (latlong) projection # using the WGS84 datum. # # PROJECTION objects within the LAYER object define the input # projection--this is the native projection of your data. PROJECTION "init=epsg:4326" END # CLASSITEM defines the non-spatial attribute 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 for each data format). In this # example the shapefile states_ugl has an associated database # (states_ugl.dbf) that contains an attribute called "CLASS". You will be # using two values in the CLASS attribute to separate the classes (also # called themes) used in this layer--land and water. CLASSITEM is used in # association with the EXPRESSION parameter in the CLASS object. See below. CLASSITEM "CLASS" # The class object is defined within the layer object. You can define as # many classes as you need but it is good cartographic practice to limit # classes to 8 to 10 per layer. (There are also limits as with layers and # it's senseless to define more than ten on a "normal" layer. There are # situations, however, where you might have to do it.) CLASS EXPRESSION 'land' STYLE SYMBOL 0 COLOR 232 232 232 END END END # States polygon layer ends here # In addition to vector data (shapefiles are vector data), MapServer supports # a host of raster formats. In GIS world, one of the most common raster # formats is GeoTIFF, a TIFF image with geospatial headers. MapServer also # supports JPEG, PNG, GIF, and other common formats. Other raster formats # supported by MapServer include ESRI Arc/Info grid, HDF and HDF-EOS, NetCDF, # Generic raster binaries, OGC Web Map Service (WMS) layers, etc. Pretty much # any raster format you can think of is probably supported, thanks to the # impressive Geospatial Data Abstraction Library (GDAL, pronounced "GOODALL" # or GOODLE?). More information on GDAL is available at http://www.gdal.org. # # MapServer 4.x can read and display bitmapped (like GIFs), RGB/A (true # color), and multispectral (images with more than 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 PROJECTION "init=epsg:4326" END END # MODIS raster layer ends here LAYER # MODIS WMS map from JPL NAME modis_jpl TYPE RASTER OFFSITE 0 0 0 STATUS OFF CONNECTIONTYPE WMS CONNECTION "http://wms.jpl.nasa.gov/wms.cgi?" METADATA "wms_srs" "EPSG:4326" "wms_name" "modis" "wms_server_version" "1.1.1" "wms_format" "image/jpeg" END PROJECTION "init=epsg:4326" END END # Modis WMS image ends here LAYER # States line layer begins here NAME states DATA states_ugl STATUS OFF TYPE LINE PROJECTION "init=epsg:4326" END CLASSITEM "CLASS" CLASS EXPRESSION 'land' STYLE #SYMBOL 'line1' COLOR 32 32 32 #SIZE 1 END END END # States line layer ends here LAYER # States label layer begins here NAME states DATA states_ugl STATUS OFF TYPE ANNOTATION PROJECTION "init=epsg:4326" END CLASSITEM "CLASS" LABELITEM "STATE" CLASS EXPRESSION 'land' STYLE COLOR -1 -1 -1 END LABEL COLOR 255 255 255 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 # end of map file
咱们的mapfile如今包含一个新的对象outputformat。此对象在map对象内定义,并与imagetype关键字一块儿使用。根据您使用MapServer编译的库,您能够有几个输出格式选择——gd库提供PNG(8位和24位)、GIF、JPEG和WBMP;gdal库是许多MapServer输入格式的源,也能够向PNG、JPEG、TIFF/GEOTIFF和其余RASTE提供输出。R格式;pdflib库提供PDF输出;ming库提供闪存输出。查看outputformat对象并经过更改mapfile上的imagetype关键字进行实验。输出格式的名称应用做imagetype值(例如:imagetype png或imagetype png24)。
请在如下位置查阅outputformat对象引用:
http://www.mapserver.org/mapfile/outputformat.html outputformat
Example 1.9: Interactive Maps and the Browse Mode
上面的地图是在“地图”模式下建立的。这是一个静态地图(单击它不会改变)。
这个动态地图是在“浏览”模式下生成的。点击地图上的任何一点,看看会发生什么。
这两个地图都使用相同的mapfiile文件定义。区别在于第二个地图(动态建立的地图)依赖于HTML表单进行交互。若是你看一下这个页面是如何与前一个页面连接的,你会发现它与前一个例子不同。用mapserver的术语来讲,这个页面就是一个HTML模板。您将在第2部分中了解有关HTML模板的更多信息。
查看此页面中的“表单”块(右键单击浏览器并选择“查看源代码”或相似内容):
<!-- START OF MAPSERVER FORM --> <form name="mapserv" method="GET" action="/cgi-bin/mapserv.exe"> <!-- HIDDEN MAPSERVER CGI VARIABLES --> <input type="hidden" name="map" value="[map]"> <input type="hidden" name="imgext" value="[mapext]"> <input type="hidden" name="imgxy" value="199.5 149.5"> <input type="hidden" name="zoom" value="1"> <input type="hidden" name="mode" value="browse"> <div align="center"> <table border="1" cellpadding="0" cellspacing="0"> <tr> <td> <!-- THE INTERACTIVE, DYNAMICALLY CREATED MAP -- <input type="image" name="img" src="[img]" width="400" height="300"> </td> </tr> </table> </div> </form>
每当用户单击地图时,此块执行mapserver cgi程序(“/cgi-bin/mapserv.exe”)。地图其实是另外一种形式的“输入”,在这里用行表示:
<input type="image" name="img" src="[img]" width="400" height="300">
方括号([map]、[mapext]和[img])中的项称为mapserver标记——这些是mapserver cgi变量,在从新加载时会被mapserver cgi程序替换。标记[map]是mapfile路径的一个占位符,所以在运行mapserver时,它被替换为“/ms4w/apps/tutorial/htdocs/example1-9.map”。标记[mapext]替换为当前地图范围“91734.994981-533247.003346 1432483.085284 471473.996656”,而且[img]标记替换为mapserver cgi程序建立的图像路径,“/ms_tmp/ex1.9_156222312833540.png”。继续检查映像路径(/ms4w/tmp/)是否存在此映像。(这些参数是在.map文件里获取的吗?:)
值为“browse”的隐藏变量“mode”告诉CGI程序须要在“tmp”目录中建立和转储图像。而后,这个图像被引用为[img],这就是您在浏览器上看到的。
如今,看看地图文件:
# 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 NAME EX1.9_ IMAGETYPE PNG24 EXTENT 166221 -371954 1505849 632767 # LAEA #EXTENT -97.5 41.619778 -82.122902 49.38562 # Geographic SIZE 400 300 SHAPEPATH "../data" SYMBOLSET "../symbols/symbols35.sym" FONTSET "../fonts/fonts.list" # The web object is defined at the level below the map object. All # web-related parameters (I interchange "parameters" and "keyword/value # pairs" quite frequently, sorry about that) are defined in this object. WEB TEMPLATE 'example1-9.html' IMAGEPATH '/ms4w/tmp/ms_tmp/' IMAGEURL '/ms_tmp/' END # The projection object is typically used within the map and the layer # objects. You only define it once within the map object and this definition # becomes your output projection--MapServer will render your maps in this # projection. You also use the projection object within the layer object to # define your input projection. Your layers can be in different # projections--MapServer will reproject them into your output projection. # If no projection is defined within the layer object, MapServer assumes # your input projection is the same as your output projection. This is not # a required object unless you're creating a map file that supports one of # the OGC interoperability web services specifications (WMS/WFS/WCS). # # This is the output PROJECTION definition ------ PROJECTION # Projection parameters can be defined in two ways... # This is the traditional Proj.4 definition of Lambert Azimuthal Equal-Area # projection for the Continental U.S. # "proj=laea" # "ellps=clrk66" # "lat_0=45" # "lon_0=-100" # # Alternatively, you can specify an EPSG code. # This is the EPSG code for Lambert Azimuthal Equal-Area # projection for the U.S. "init=epsg:2163" END # Layer objects are defined beneath the map object. You need at least one # layer defined in your map file before you can display a map... You can # define as many layers as you'd like although 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 application to 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 # Here's an example of the input projection definition. # EPSG:4326 is code for geographic (latlong) projection # using the WGS84 datum. # # PROJECTION objects within the LAYER object define the input # projection--this is the native projection of your data. PROJECTION "init=epsg:4326" END # CLASSITEM defines the non-spatial attribute 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 for each data format). In this # example the shapefile states_ugl has an associated database # (states_ugl.dbf) that contains an attribute called "CLASS". You will be # using two values in the CLASS attribute to separate the classes (also # called themes) used in this layer--land and water. CLASSITEM is used in # association with the EXPRESSION parameter in the CLASS object. See below. CLASSITEM "CLASS" CLASS EXPRESSION 'land' STYLE SYMBOL 0 COLOR 232 232 232 END END END # States polygon layer ends here # In addition to vector data (shapefiles are vector data), MapServer supports # a host of raster formats. In GIS world, one of the most common raster # formats is GeoTIFF, a TIFF image with geospatial headers. MapServer also # supports JPEG, PNG, GIF, and other common formats. Other raster formats # supported by MapServer include ESRI Arc/Info grid, HDF and HDF-EOS, NetCDF, # Generic raster binaries, OGC Web Map Service (WMS) layers, etc. Pretty much # any raster format you can think of is probably supported, thanks to the # impressive Geospatial Data Abstraction Library (GDAL, pronounced "GOODALL" # or GOODLE?). More information on GDAL is available at http://www.gdal.org. # # MapServer 4.x can read and display bitmapped (like GIFs), RGB/A (true # color), and multispectral (images with more than 3 bands, like raw LandSat # images) rasters. LAYER # MODIS raster layer begins here NAME modis DATA "raster/mod09a12003161_ugl_ll_8bit.tif" STATUS OFF #DEFAULT TYPE RASTER PROCESSING "BANDS=1,2,3" OFFSITE 71 74 65 PROJECTION "init=epsg:4326" END END # MODIS raster layer ends here LAYER # MODIS WMS map from JPL (or from USGS) NAME modis_jpl TYPE RASTER OFFSITE 0 0 0 STATUS OFF #OFF CONNECTIONTYPE WMS CONNECTION "http://wms.jpl.nasa.gov/wms.cgi?" METADATA "wms_srs" "EPSG:4326" "wms_name" "daily_terra" #"global_mosaic" "daily_aqua" "wms_server_version" "1.1.1" "wms_format" "image/jpeg" END PROJECTION "init=epsg:4326" END END # MODIS WMS image ends here LAYER # States line layer begins here NAME states DATA states_ugl STATUS OFF TYPE LINE PROJECTION "init=epsg:4326" END CLASSITEM "CLASS" CLASS EXPRESSION 'land' STYLE SYMBOL 'line5' COLOR 32 32 32 SIZE 1 END END END # States line layer ends here LAYER # States label layer begins here NAME states DATA states_ugl STATUS OFF TYPE POLYGON PROJECTION "init=epsg:4326" END CLASSITEM "CLASS" LABELITEM "STATE" CLASS EXPRESSION 'land' STYLE COLOR -1 -1 -1 END LABEL COLOR 132 31 31 OUTLINECOLOR 128 128 128 SHADOWCOLOR 218 218 218 SHADOWSIZE 1 1 TYPE TRUETYPE FONT arial-bold SIZE 12 ANTIALIAS TRUE POSITION CL PARTIALS FALSE MINDISTANCE 200 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... # End of section 1, dude!
只有一件事被添加到map文件中:
TEMPLATE 'example1-9.html'
这将告诉MapServer使用页面“example1-9.html”做为模板文件。MapServer将处理此文件并替换它遇到的标记,而后将其发送到Web浏览器。本教程接下来的两个部分就是这样工做的。
这标志着第1节的结束。我但愿您在本节中留下足够的知识,了解如何在MapServer映射文件中设置内容。在建立本身的映射文件和应用程序时,我不能过度强调保持MapServer映射文件引用打开的重要性。没有它,我就不会在这个教程中走得太远。
Section 2: CGI variables and the User Interface
到目前为止,咱们只在建立地图时查看了地图文件。在建立Web地图应用程序时,咱们一般打算制做用户(应用程序的用户)能够交互更改的映射。也就是说,用户应该可以更改地图的内容(或其中的信息)。为了实现这种交互性,咱们使用mapserver HTML模板。
MapServer HTML模板本质上是一个HTML文件,带有一些特定于MapServer的标记——这些标记是MapServer CGI变量带括在方括号“[]”中。当mapserver cgi程序处理一个应用程序时,它首先解析查询字符串和mapfile并生成必要的输出。其中一些输出将须要写入HTML模板文件,您还必须使用web template关键字(或单独的HTML初始化文件)在mapfile中指定该文件。CGI程序将用适当的值替换HTML模板中的全部变量,而后再将其发送回Web浏览器。若是要在Web浏览器上直接查看HTML模板,则不会呈现任何映射,而是会获得空白图像和其余垃圾。
MapServer为Web映射提供了几个变量——您在示例1.9中看到的“img”变量只是一个示例。最初做为绘图接口的一部分设计的核心CGI变量不多,但实际上全部mapfile参数均可以定义为变量。CGI变量的最终参考可在http://www.mapserver.org/cgi/index.html上找到。
咱们还能够定义本身的变量——mapserver将把它传递给咱们的应用程序。例如,咱们能够建立一个名为“root”的变量来表示本教程的根目录——“root”的值将是“/tutorial”。当mapserver cgi程序处理咱们的html模板时,它将用“/tutorial”替换他“[根]”标记的每一个实例。您将在下面的每一个示例中看到这一点。
因此,让咱们为咱们的应用程序构建一个交互式界面…
Example 2.1 - Pan and Zoom Controls
示例2.1-平移和缩放控件
Web地图应用程序的用户应该可以在地图上平移和缩放…
查看地图文件:
# The annotated map file (sort of) # Created by Pericles S. Nacionales for the MapServer tutorial # 20050623 # # Map files begin with map keyword to signify the start of the map object. # Well, the entire map file is THE map object. Enclose between MAP and END # at the very bottom of this map file, are keyword/value pairs and other # objects. MAP NAME EX2_ IMAGETYPE png24 EXTENT 166221 -371954 1505849 632767 # LAEA #EXTENT -97.5 41.619778 -82.122902 49.38562 # Geographic SIZE 400 300 SHAPEPATH "../data" SYMBOLSET "../symbols/symbols35.sym" FONTSET "../fonts/fonts.list" # When changing any of the mapfile parameters via the web interface, you # need to define a TEMPLATEPATTERN. This is required for security reasons. # Since the example filenames in section 2 begin with "example2" (as in # example2-1.html or example2-2.html), you can use it as the pattern. # The template pattern is a regular expression used by MapServer to match the # value of map_web_template variable against. TEMPLATEPATTERN 'example2*' # The web object is defined at the level below the map object. All # web-related parameters (I interchange "parameters" and "keyword/value # pairs" quite frequently, sorry about that) are defined in this object. WEB TEMPLATE 'to be replaced by map_web_template variable in section2.html' IMAGEPATH '/ms4w/tmp/ms_tmp/' IMAGEURL '/ms_tmp/' END # Every object in MapServer must have an END.;) # The projection object is typically used within the map and the layer # objects. You only define it once within the map object and this definition # becomes your output projection--MapServer will render your maps in this # projection. You also use the projection object within the layer object to # define your input projection. Your layers can be in different # projections--MapServer will reproject them into your output projection. # If no projection is defined within the layer object, MapServer assumes # your input projection is the same as your output projection. This is not # a required object unless you're creating a map file that supports one of # the OGC interoperability web services specifications (WMS/WFS/WCS). PROJECTION # Projection parameters can be defined in two ways... # This is the traditional Proj.4 definition of Lambert Azimuthal Equal-Area # projection for the Continental U.S. # "proj=laea" # "ellps=clrk66" # "lat_0=45" # "lon_0=-100" # # Alternatively, you can specify an EPSG code. # This is the EPSG code for Lambert Azimuthal Equal-Area # projection for the U.S. "init=epsg:2163" END # This is the ending of the output projection # # Start of legend # LEGEND KEYSIZE 12 12 LABEL TYPE BITMAP SIZE MEDIUM COLOR 0 0 89 END STATUS ON END # The reference object is used to define a reference map for your mapping # application. This typically involves defining a small image that covers # the entire area of the map and defining a box that represents the current # view on in relation to the entire area. REFERENCE IMAGE '../images/ugl_ref1.png' # The reference image SIZE 155 105 # The size of the reference image in pixels EXTENT 201621.496941 -294488.285333 1425518.020722 498254.511514 # The extent of the reference image in map units STATUS ON MINBOXSIZE 10 # How small can the reference box be before it gets drawn as a point, in pixels MAXBOXSIZE 150 # The maximum size of the reference box, in pixels COLOR -1 -1 -1 # The reference box fill color, negative numbers mean transparent OUTLINECOLOR 128 0 0 # The reference box outline color MARKERSIZE 8 # The size of the point marker MARKER 'star' # The marker symbol END # A scalebar object is defined one level below the map object. This object # controls how a scalebar is drawn by MapServer. Scalebars can be embedded # in the map itself or can be created as a separate image. It has an # associated MapServer CGI variable called "scalebar" (or [scalebar] when # used in the HTML template). SCALEBAR IMAGECOLOR 255 255 255 LABEL COLOR 0 0 0 SIZE TINY END STYLE 1 SIZE 100 2 COLOR 0 0 0 UNITS MILES INTERVALS 2 TRANSPARENT FALSE STATUS ON END # Scalebar object ends # Layer objects, too, are defined beneath the map object. Be mindful of the # order of your layer objects. MapServer "stacks them" in reverse # order--that is, the last layer you define (at the bottom of the map file) # will be drawn on top and the first layer you define (right after this # comment), will be drawn at the bottom. Here's my rule: rasters and # polygons are defined first, followed by the line layers. The point and # annotation layers are defined last. You can play around with the ordering # of your layers until you're satisfied. # Start of LAYER DEFINITIONS --------------------------------------------- LAYER # States polygon layer begins here NAME states DATA states_ugl STATUS DEFAULT TYPE POLYGON REQUIRES "![modis] OR ![modis_jpl]" # Here's an example of the input projection definition. # EPSG:4326 is code for geographic (latlong) projection # using the WGS84 datum PROJECTION "init=epsg:4326" END CLASSITEM "CLASS" # The class object is defined within the layer object. You can define as # many classes as you need (well, there are limits, but it's senseless to # define more than ten on a "normal" layer. There are situations, # however, where you might have to do it.) CLASS EXPRESSION 'land' # There are styles in a class, just like there are classes in a layer, # just like there are layers in a map. STYLE SYMBOL 0 COLOR 232 232 232 END # And they all must come to an end. END # End of this class. END # States polygon layer ends here LAYER # MODIS raster layer begins here NAME modis DATA "raster/mod09a12003161_ugl_ll_idxa.tif" STATUS OFF TYPE RASTER OFFSITE 70 74 66 #167 151 152 PROJECTION "init=epsg:4326" END END # MODIS raster layer ends here LAYER # MODIS WMS map from JPL (or from USGS) NAME modis_jpl TYPE RASTER OFFSITE 0 0 0 STATUS OFF CONNECTIONTYPE WMS CONNECTION "http://wms.jpl.nasa.gov/wms.cgi?" METADATA "wms_srs" "EPSG:4326" "wms_name" "daily_terra" #"global_mosaic" "daily_aqua" "wms_server_version" "1.1.1" "wms_format" "image/jpeg" END PROJECTION "init=epsg:4326" END END # MODIS WMS image ends here LAYER # Hydrography layer begins here NAME hydro TYPE POLYGON STATUS OFF DATA hydrop_ugl PROJECTION "init=epsg:4326" END CLASSITEM 'FEATURE' CLASS NAME 'Lakes' EXPRESSION /(^B|^C|^L|^R)./ STYLE COLOR 72 64 254 END END # CLASS CLASS NAME 'Rivers' EXPRESSION 'Stream' STYLE COLOR 136 128 255 END END # CLASS CLASS NAME 'Wetlands' EXPRESSION 'Swamp or Marsh' STYLE COLOR 195 252 255 OUTLINECOLOR 195 252 255 SYMBOL 'circle' SIZE 0 END END # CLASS END # LAYER LAYER # ROADS LAYER NAME cty_roads GROUP roads TYPE LINE STATUS OFF DATA roads_ugl MAXSCALE 750000 PROJECTION "init=epsg:4326" END CLASSITEM 'CLASS1' CLASS NAME 'Minor Arterial Roads' EXPRESSION '3' STYLE COLOR 165 165 165 END END # CLASS CLASS EXPRESSION '4' STYLE COLOR 210 210 210 END END # CLASS END # ROADS LAYER LAYER # state highways begin here NAME state_hwy GROUP roads MAXSCALE 1500000 STATUS OFF DATA roads_ugl TYPE LINE FILTERITEM 'CLASS1' FILTER '2' CLASS NAME 'Principal Arterial Roads' STYLE COLOR 255 0 0 SIZE 1 SYMBOL 'circle' END END PROJECTION "init=epsg:4326" END END # highways LAYER # interstate highways begin here NAME interstate GROUP roads # MAXSCALE 22500000 STATUS OFF DATA roads_ugl TYPE LINE FILTERITEM 'CLASS1' FILTER '1' CLASS NAME "Interstate Hwy." STYLE COLOR 128 0 0 END END PROJECTION "init=epsg:4326" END END # highways LAYER # States line layer begins here NAME states DATA states_ugl STATUS OFF TYPE LINE PROJECTION "init=epsg:4326" END CLASSITEM "CLASS" CLASS NAME 'State Boundary' EXPRESSION 'land' STYLE SYMBOL 'line5' COLOR 32 32 32 SIZE 1 END END END # States line layer ends here LAYER NAME roads_anno GROUP roads MAXSCALE 750000 STATUS OFF DATA roads_ugl TYPE POLYGON LABELITEM "SIGN" CLASSITEM "CLASS1" CLASS EXPRESSION "3" STYLE COLOR 0 0 0 # dummy color SYMBOL '../symbols/sthwy.png' END LABEL MINFEATURESIZE 50 MINDISTANCE 150 POSITION CC SIZE TINY COLOR 0 0 0 END END CLASS EXPRESSION "2" STYLE COLOR 0 0 0 # dummy color SYMBOL '../symbols/ushwy.png' END LABEL MINFEATURESIZE 50 MINDISTANCE 150 POSITION CC SIZE TINY COLOR 0 0 0 END END PROJECTION "init=epsg:4326" END END # highway annotation LAYER NAME roads_anno1 GROUP roads STATUS OFF DATA roads_ugl TYPE POLYGON LABELITEM "SIGN" CLASSITEM "CLASS1" CLASS EXPRESSION "1" STYLE COLOR 0 0 0 # dummy color SYMBOL '../symbols/interstate.png' END LABEL MINFEATURESIZE 20 MINDISTANCE 150 POSITION CC SIZE TINY COLOR 255 255 255 END END PROJECTION "init=epsg:4326" END END LAYER # States label layer begins here NAME states DATA states_ugl STATUS OFF TYPE POLYGON PROJECTION "init=epsg:4326" END CLASSITEM "CLASS" LABELITEM "STATE" CLASS EXPRESSION 'land' STYLE COLOR -1 -1 -1 END LABEL COLOR 132 31 31 OUTLINECOLOR 128 128 128 SHADOWCOLOR 218 218 218 SHADOWSIZE 1 1 TYPE TRUETYPE FONT arial-bold SIZE 12 ANTIALIAS TRUE POSITION CL PARTIALS FALSE MINDISTANCE 200 BUFFER 4 END # end of label END # end of class END # States label layer ends here # End of LAYER DEFINITIONS ------------------------------- END # end of map file
与第1节中的映射同样,咱们经过调用mapserv(“/cgi-bin/mapserv.exe”)初始化应用程序。并将咱们的mapfile路径和其余参数(“map=/ms4w/apps/tutorial/htdocs/example2-1.map&mode=browse”)传递给它。因此这个页面有一个来自第2节页面的连接,以下所示:
<a href="/cgi-bin/mapserv.exe? map=/ms4w/apps/tutorial/htdocs/example2-1.map &mode=browse&root=/tutorial&program=/cgi-bin/mapserv.exe &map_web=template+example2-1.html"> Proceed to Example 2.1</a>
此次咱们使用“浏览”模式而不是“地图”。浏览模式告诉mapserv在咱们的“/tmp/”目录上建立一个映射(一个图像)。图像路径和名称由名称为“img”的mapserv引用。所以,当mapserv解析咱们的HTML模板时,它将用正确的图像路径替换“[img]”/ms_tmp/ex2_156231592315176.png。
你能够用这张地图作一些事情。首先,您能够单击图像的任何部分,地图将刷新并使您单击的点居中。这是平移。若是单击“地图控件”下拉框,能够选择“放大”或“缩小”值。若是将其设置为“放大2倍”,而后单击地图的任何部分,地图将刷新、放大,并以单击的点为中心。反之,若是你缩小。当选择“放大”或“缩小”值并单击“刷新”按钮时,地图将刷新并放大或缩小上一个地图的中心。您能够随时使用“刷新”按钮刷新地图。
缩放/平移控件使用内部mapserver cgi变量。此示例演示如何使用“zoom”变量。缩放值决定放大或缩小的距离。若是值为“0”,则MapServer将根据用户的鼠标单击从新输入图像。若是该值大于“0”,则CGI程序放大(地图的当前比例乘以缩放值)。若是是负数,那么CGI程序会缩小(将当前比例除以缩放值)。其余可用于控制缩放和平移的变量有“zoomdir”和“zoomsize”。这两个变量的做用与“缩放”差很少——“zoomdir”控制缩放方向,“zoomsize”控制放大或缩小的距离。请查看osgeo库或mapserver wiki上的其余示例,以获取其余缩放/平移控制选项。
尝试将“地图模式”从“浏览”更改成“地图”。单击刷新时会发生什么?记住,当咱们使用“模式=映射”时,mapserver会返回一个静态映射…它忽略HTML模板,只将映射图像直接流式传输到浏览器。
尽管添加了更多的数据层,但它仍然与第一节中的映射文件类似。我没有什么新的东西能够解释这个地图文件,可是若是你对一些新的关键字感兴趣,请查阅地图文件参考页。
如今,看看HTML模板文件。您会注意到这其实是一个调用mapserver cgi程序的HTML表单。由于它是一个表单,因此您可使用单选按钮和复选框以及下拉框来实现您的界面。不要以为受这个例子的限制——有创造力是件好事。;)有关详细信息,请查看HTML模板引用页。
……他们还应该可以打开和关闭地图上的图层。
示例2.2-层控制
可以打开和关闭地图层是Web映射应用程序的标准功能。使用表单对象做为控件有不少方法能够实现这一点。您可使用下拉框/菜单、复选框和/或单选按钮。在本例中,您将看到如何使用复选框和放置框实现层选择。
这里是mapfile:
# The annotated map file (sort of) # Created by Pericles S. Nacionales for the MapServer tutorial # 20050623 # # Map files begin with map keyword to signify the start of the map object. # Well, the entire map file is THE map object. Enclose between MAP and END # at the very bottom of this map file, are keyword/value pairs and other # objects. MAP NAME EX2_ IMAGETYPE png24 EXTENT 166221 -371954 1505849 632767 # LAEA #EXTENT -97.5 41.619778 -82.122902 49.38562 # Geographic SIZE 400 300 SHAPEPATH "../data" SYMBOLSET "../symbols/symbols35.sym" FONTSET "../fonts/fonts.list" # When changing any of the mapfile parameters via the web interface, you # need to define a TEMPLATEPATTERN. This is required for security reasons. # Since the example filenames in section 2 begin with "example2" (as in # example2-1.html or example2-2.html), you can use it as the pattern. # The template pattern is a regular expression used by MapServer to match the # value of map_web_template variable against. TEMPLATEPATTERN 'example2*' # The web object is defined at the level below the map object. All # web-related parameters (I interchange "parameters" and "keyword/value # pairs" quite frequently, sorry about that) are defined in this object. WEB TEMPLATE 'to be replaced by map_web_template variable in section2.html' IMAGEPATH '/ms4w/tmp/ms_tmp/' IMAGEURL '/ms_tmp/' END # Every object in MapServer must have an END.;) # The projection object is typically used within the map and the layer # objects. You only define it once within the map object and this definition # becomes your output projection--MapServer will render your maps in this # projection. You also use the projection object within the layer object to # define your input projection. Your layers can be in different # projections--MapServer will reproject them into your output projection. # If no projection is defined within the layer object, MapServer assumes # your input projection is the same as your output projection. This is not # a required object unless you're creating a map file that supports one of # the OGC interoperability web services specifications (WMS/WFS/WCS). PROJECTION # Projection parameters can be defined in two ways... # This is the traditional Proj.4 definition of Lambert Azimuthal Equal-Area # projection for the Continental U.S. # "proj=laea" # "ellps=clrk66" # "lat_0=45" # "lon_0=-100" # # Alternatively, you can specify an EPSG code. # This is the EPSG code for Lambert Azimuthal Equal-Area # projection for the U.S. "init=epsg:2163" END # This is the ending of the output projection # # Start of legend # LEGEND KEYSIZE 12 12 LABEL TYPE BITMAP SIZE MEDIUM COLOR 0 0 89 END STATUS ON END # The reference object is used to define a reference map for your mapping # application. This typically involves defining a small image that covers # the entire area of the map and defining a box that represents the current # view on in relation to the entire area. REFERENCE IMAGE '../images/ugl_ref1.png' # The reference image SIZE 155 105 # The size of the reference image in pixels EXTENT 201621.496941 -294488.285333 1425518.020722 498254.511514 # The extent of the reference image in map units STATUS ON MINBOXSIZE 10 # How small can the reference box be before it gets drawn as a point, in pixels MAXBOXSIZE 150 # The maximum size of the reference box, in pixels COLOR -1 -1 -1 # The reference box fill color, negative numbers mean transparent OUTLINECOLOR 128 0 0 # The reference box outline color MARKERSIZE 8 # The size of the point marker MARKER 'star' # The marker symbol END # A scalebar object is defined one level below the map object. This object # controls how a scalebar is drawn by MapServer. Scalebars can be embedded # in the map itself or can be created as a separate image. It has an # associated MapServer CGI variable called "scalebar" (or [scalebar] when # used in the HTML template). SCALEBAR IMAGECOLOR 255 255 255 LABEL COLOR 0 0 0 SIZE TINY END STYLE 1 SIZE 100 2 COLOR 0 0 0 UNITS MILES INTERVALS 2 TRANSPARENT FALSE STATUS ON END # Scalebar object ends # Layer objects, too, are defined beneath the map object. Be mindful of the # order of your layer objects. MapServer "stacks them" in reverse # order--that is, the last layer you define (at the bottom of the map file) # will be drawn on top and the first layer you define (right after this # comment), will be drawn at the bottom. Here's my rule: rasters and # polygons are defined first, followed by the line layers. The point and # annotation layers are defined last. You can play around with the ordering # of your layers until you're satisfied. # Start of LAYER DEFINITIONS --------------------------------------------- LAYER # States polygon layer begins here NAME states DATA states_ugl STATUS DEFAULT TYPE POLYGON REQUIRES "![modis] OR ![modis_jpl]" # Here's an example of the input projection definition. # EPSG:4326 is code for geographic (latlong) projection # using the WGS84 datum PROJECTION "init=epsg:4326" END CLASSITEM "CLASS" # The class object is defined within the layer object. You can define as # many classes as you need (well, there are limits, but it's senseless to # define more than ten on a "normal" layer. There are situations, # however, where you might have to do it.) CLASS EXPRESSION 'land' # There are styles in a class, just like there are classes in a layer, # just like there are layers in a map. STYLE SYMBOL 0 COLOR 232 232 232 END # And they all must come to an end. END # End of this class. END # States polygon layer ends here LAYER # MODIS raster layer begins here NAME modis DATA "raster/mod09a12003161_ugl_ll_idxa.tif" STATUS OFF TYPE RASTER OFFSITE 70 74 66 #167 151 152 PROJECTION "init=epsg:4326" END END # MODIS raster layer ends here LAYER # MODIS WMS map from JPL (or from USGS) NAME modis_jpl TYPE RASTER OFFSITE 0 0 0 STATUS OFF CONNECTIONTYPE WMS CONNECTION "http://wms.jpl.nasa.gov/wms.cgi?" METADATA "wms_srs" "EPSG:4326" "wms_name" "daily_terra" #"global_mosaic" "daily_aqua" "wms_server_version" "1.1.1" "wms_format" "image/jpeg" END PROJECTION "init=epsg:4326" END END # MODIS WMS image ends here LAYER # Hydrography layer begins here NAME hydro TYPE POLYGON STATUS OFF DATA hydrop_ugl PROJECTION "init=epsg:4326" END CLASSITEM 'FEATURE' CLASS NAME 'Lakes' EXPRESSION /(^B|^C|^L|^R)./ STYLE COLOR 72 64 254 END END # CLASS CLASS NAME 'Rivers' EXPRESSION 'Stream' STYLE COLOR 136 128 255 END END # CLASS CLASS NAME 'Wetlands' EXPRESSION 'Swamp or Marsh' STYLE COLOR 195 252 255 OUTLINECOLOR 195 252 255 SYMBOL 'circle' SIZE 0 END END # CLASS END # LAYER LAYER # ROADS LAYER NAME cty_roads GROUP roads TYPE LINE STATUS OFF DATA roads_ugl MAXSCALE 750000 PROJECTION "init=epsg:4326" END CLASSITEM 'CLASS1' CLASS NAME 'Minor Arterial Roads' EXPRESSION '3' STYLE COLOR 165 165 165 END END # CLASS CLASS EXPRESSION '4' STYLE COLOR 210 210 210 END END # CLASS END # ROADS LAYER LAYER # state highways begin here NAME state_hwy GROUP roads MAXSCALE 1500000 STATUS OFF DATA roads_ugl TYPE LINE FILTERITEM 'CLASS1' FILTER '2' CLASS NAME 'Principal Arterial Roads' STYLE COLOR 255 0 0 SIZE 1 SYMBOL 'circle' END END PROJECTION "init=epsg:4326" END END # highways LAYER # interstate highways begin here NAME interstate GROUP roads # MAXSCALE 22500000 STATUS OFF DATA roads_ugl TYPE LINE FILTERITEM 'CLASS1' FILTER '1' CLASS NAME "Interstate Hwy." STYLE COLOR 128 0 0 END END PROJECTION "init=epsg:4326" END END # highways LAYER # States line layer begins here NAME states DATA states_ugl STATUS OFF TYPE LINE PROJECTION "init=epsg:4326" END CLASSITEM "CLASS" CLASS NAME 'State Boundary' EXPRESSION 'land' STYLE SYMBOL 'line5' COLOR 32 32 32 SIZE 1 END END END # States line layer ends here LAYER NAME roads_anno GROUP roads MAXSCALE 750000 STATUS OFF DATA roads_ugl TYPE POLYGON LABELITEM "SIGN" CLASSITEM "CLASS1" CLASS EXPRESSION "3" STYLE COLOR 0 0 0 # dummy color SYMBOL '../symbols/sthwy.png' END LABEL MINFEATURESIZE 50 MINDISTANCE 150 POSITION CC SIZE TINY COLOR 0 0 0 END END CLASS EXPRESSION "2" STYLE COLOR 0 0 0 # dummy color SYMBOL '../symbols/ushwy.png' END LABEL MINFEATURESIZE 50 MINDISTANCE 150 POSITION CC SIZE TINY COLOR 0 0 0 END END PROJECTION "init=epsg:4326" END END # highway annotation LAYER NAME roads_anno1 GROUP roads STATUS OFF DATA roads_ugl TYPE POLYGON LABELITEM "SIGN" CLASSITEM "CLASS1" CLASS EXPRESSION "1" STYLE COLOR 0 0 0 # dummy color SYMBOL '../symbols/interstate.png' END LABEL MINFEATURESIZE 20 MINDISTANCE 150 POSITION CC SIZE TINY COLOR 255 255 255 END END PROJECTION "init=epsg:4326" END END LAYER # States label layer begins here NAME states DATA states_ugl STATUS OFF TYPE POLYGON PROJECTION "init=epsg:4326" END CLASSITEM "CLASS" LABELITEM "STATE" CLASS EXPRESSION 'land' STYLE COLOR -1 -1 -1 END LABEL COLOR 132 31 31 OUTLINECOLOR 128 128 128 SHADOWCOLOR 218 218 218 SHADOWSIZE 1 1 TYPE TRUETYPE FONT arial-bold SIZE 12 ANTIALIAS TRUE POSITION CL PARTIALS FALSE MINDISTANCE 200 BUFFER 4 END # end of label END # end of class END # States label layer ends here # End of LAYER DEFINITIONS ------------------------------- END # end of map file
注意图层的STATUS已经被改成OFF,除了“States”多边形背景。
状态背景保留为默认设置,所以在绘制地图时,若是不打开任何图层,则始终会显示某些内容。应用程序的用户应该可以控制打开或关闭哪些层。
若是您查看HTML模板的源代码,您将了解mapserv如何打开/关闭层。
地图应始终包含一个scaleBar。
示例2.3-添加scaleBar
这里是mapfile文件:
# The annotated map file (sort of) # Created by Pericles S. Nacionales for the MapServer tutorial # 20050623 # # Map files begin with map keyword to signify the start of the map object. # Well, the entire map file is THE map object. Enclose between MAP and END # at the very bottom of this map file, are keyword/value pairs and other # objects. MAP NAME EX2_ IMAGETYPE png24 EXTENT 166221 -371954 1505849 632767 # LAEA #EXTENT -97.5 41.619778 -82.122902 49.38562 # Geographic SIZE 400 300 SHAPEPATH "../data" SYMBOLSET "../symbols/symbols35.sym" FONTSET "../fonts/fonts.list" # When changing any of the mapfile parameters via the web interface, you # need to define a TEMPLATEPATTERN. This is required for security reasons. # Since the example filenames in section 2 begin with "example2" (as in # example2-1.html or example2-2.html), you can use it as the pattern. # The template pattern is a regular expression used by MapServer to match the # value of map_web_template variable against. TEMPLATEPATTERN 'example2*' # The web object is defined at the level below the map object. All # web-related parameters (I interchange "parameters" and "keyword/value # pairs" quite frequently, sorry about that) are defined in this object. WEB TEMPLATE 'to be replaced by map_web_template variable in section2.html' IMAGEPATH '/ms4w/tmp/ms_tmp/' IMAGEURL '/ms_tmp/' END # Every object in MapServer must have an END.;) # The projection object is typically used within the map and the layer # objects. You only define it once within the map object and this definition # becomes your output projection--MapServer will render your maps in this # projection. You also use the projection object within the layer object to # define your input projection. Your layers can be in different # projections--MapServer will reproject them into your output projection. # If no projection is defined within the layer object, MapServer assumes # your input projection is the same as your output projection. This is not # a required object unless you're creating a map file that supports one of # the OGC interoperability web services specifications (WMS/WFS/WCS). PROJECTION # Projection parameters can be defined in two ways... # This is the traditional Proj.4 definition of Lambert Azimuthal Equal-Area # projection for the Continental U.S. # "proj=laea" # "ellps=clrk66" # "lat_0=45" # "lon_0=-100" # # Alternatively, you can specify an EPSG code. # This is the EPSG code for Lambert Azimuthal Equal-Area # projection for the U.S. "init=epsg:2163" END # This is the ending of the output projection # # Start of legend # LEGEND KEYSIZE 12 12 LABEL TYPE BITMAP SIZE MEDIUM COLOR 0 0 89 END STATUS ON END # The reference object is used to define a reference map for your mapping # application. This typically involves defining a small image that covers # the entire area of the map and defining a box that represents the current # view on in relation to the entire area. REFERENCE IMAGE '../images/ugl_ref1.png' # The reference image SIZE 155 105 # The size of the reference image in pixels EXTENT 201621.496941 -294488.285333 1425518.020722 498254.511514 # The extent of the reference image in map units STATUS ON MINBOXSIZE 10 # How small can the reference box be before it gets drawn as a point, in pixels MAXBOXSIZE 150 # The maximum size of the reference box, in pixels COLOR -1 -1 -1 # The reference box fill color, negative numbers mean transparent OUTLINECOLOR 128 0 0 # The reference box outline color MARKERSIZE 8 # The size of the point marker MARKER 'star' # The marker symbol END # A scalebar object is defined one level below the map object. This object # controls how a scalebar is drawn by MapServer. Scalebars can be embedded # in the map itself or can be created as a separate image. It has an # associated MapServer CGI variable called "scalebar" (or [scalebar] when # used in the HTML template). SCALEBAR IMAGECOLOR 255 255 255 LABEL COLOR 0 0 0 SIZE TINY END STYLE 1 SIZE 100 2 COLOR 0 0 0 UNITS MILES INTERVALS 2 TRANSPARENT FALSE STATUS ON END # Scalebar object ends # Layer objects, too, are defined beneath the map object. Be mindful of the # order of your layer objects. MapServer "stacks them" in reverse # order--that is, the last layer you define (at the bottom of the map file) # will be drawn on top and the first layer you define (right after this # comment), will be drawn at the bottom. Here's my rule: rasters and # polygons are defined first, followed by the line layers. The point and # annotation layers are defined last. You can play around with the ordering # of your layers until you're satisfied. # Start of LAYER DEFINITIONS --------------------------------------------- LAYER # States polygon layer begins here NAME states DATA states_ugl STATUS DEFAULT TYPE POLYGON REQUIRES "![modis] OR ![modis_jpl]" # Here's an example of the input projection definition. # EPSG:4326 is code for geographic (latlong) projection # using the WGS84 datum PROJECTION "init=epsg:4326" END CLASSITEM "CLASS" # The class object is defined within the layer object. You can define as # many classes as you need (well, there are limits, but it's senseless to # define more than ten on a "normal" layer. There are situations, # however, where you might have to do it.) CLASS EXPRESSION 'land' # There are styles in a class, just like there are classes in a layer, # just like there are layers in a map. STYLE SYMBOL 0 COLOR 232 232 232 END # And they all must come to an end. END # End of this class. END # States polygon layer ends here LAYER # MODIS raster layer begins here NAME modis DATA "raster/mod09a12003161_ugl_ll_idxa.tif" STATUS OFF TYPE RASTER OFFSITE 70 74 66 #167 151 152 PROJECTION "init=epsg:4326" END END # MODIS raster layer ends here LAYER # MODIS WMS map from JPL (or from USGS) NAME modis_jpl TYPE RASTER OFFSITE 0 0 0 STATUS OFF CONNECTIONTYPE WMS CONNECTION "http://wms.jpl.nasa.gov/wms.cgi?" METADATA "wms_srs" "EPSG:4326" "wms_name" "daily_terra" #"global_mosaic" "daily_aqua" "wms_server_version" "1.1.1" "wms_format" "image/jpeg" END PROJECTION "init=epsg:4326" END END # MODIS WMS image ends here LAYER # Hydrography layer begins here NAME hydro TYPE POLYGON STATUS OFF DATA hydrop_ugl PROJECTION "init=epsg:4326" END CLASSITEM 'FEATURE' CLASS NAME 'Lakes' EXPRESSION /(^B|^C|^L|^R)./ STYLE COLOR 72 64 254 END END # CLASS CLASS NAME 'Rivers' EXPRESSION 'Stream' STYLE COLOR 136 128 255 END END # CLASS CLASS NAME 'Wetlands' EXPRESSION 'Swamp or Marsh' STYLE COLOR 195 252 255 OUTLINECOLOR 195 252 255 SYMBOL 'circle' SIZE 0 END END # CLASS END # LAYER LAYER # ROADS LAYER NAME cty_roads GROUP roads TYPE LINE STATUS OFF DATA roads_ugl MAXSCALE 750000 PROJECTION "init=epsg:4326" END CLASSITEM 'CLASS1' CLASS NAME 'Minor Arterial Roads' EXPRESSION '3' STYLE COLOR 165 165 165 END END # CLASS CLASS EXPRESSION '4' STYLE COLOR 210 210 210 END END # CLASS END # ROADS LAYER LAYER # state highways begin here NAME state_hwy GROUP roads MAXSCALE 1500000 STATUS OFF DATA roads_ugl TYPE LINE FILTERITEM 'CLASS1' FILTER '2' CLASS NAME 'Principal Arterial Roads' STYLE COLOR 255 0 0 SIZE 1 SYMBOL 'circle' END END PROJECTION "init=epsg:4326" END END # highways LAYER # interstate highways begin here NAME interstate GROUP roads # MAXSCALE 22500000 STATUS OFF DATA roads_ugl TYPE LINE FILTERITEM 'CLASS1' FILTER '1' CLASS NAME "Interstate Hwy." STYLE COLOR 128 0 0 END END PROJECTION "init=epsg:4326" END END # highways LAYER # States line layer begins here NAME states DATA states_ugl STATUS OFF TYPE LINE PROJECTION "init=epsg:4326" END CLASSITEM "CLASS" CLASS NAME 'State Boundary' EXPRESSION 'land' STYLE SYMBOL 'line5' COLOR 32 32 32 SIZE 1 END END END # States line layer ends here LAYER NAME roads_anno GROUP roads MAXSCALE 750000 STATUS OFF DATA roads_ugl TYPE POLYGON LABELITEM "SIGN" CLASSITEM "CLASS1" CLASS EXPRESSION "3" STYLE COLOR 0 0 0 # dummy color SYMBOL '../symbols/sthwy.png' END LABEL MINFEATURESIZE 50 MINDISTANCE 150 POSITION CC SIZE TINY COLOR 0 0 0 END END CLASS EXPRESSION "2" STYLE COLOR 0 0 0 # dummy color SYMBOL '../symbols/ushwy.png' END LABEL MINFEATURESIZE 50 MINDISTANCE 150 POSITION CC SIZE TINY COLOR 0 0 0 END END PROJECTION "init=epsg:4326" END END # highway annotation LAYER NAME roads_anno1 GROUP roads STATUS OFF DATA roads_ugl TYPE POLYGON LABELITEM "SIGN" CLASSITEM "CLASS1" CLASS EXPRESSION "1" STYLE COLOR 0 0 0 # dummy color SYMBOL '../symbols/interstate.png' END LABEL MINFEATURESIZE 20 MINDISTANCE 150 POSITION CC SIZE TINY COLOR 255 255 255 END END PROJECTION "init=epsg:4326" END END LAYER # States label layer begins here NAME states DATA states_ugl STATUS OFF TYPE POLYGON PROJECTION "init=epsg:4326" END CLASSITEM "CLASS" LABELITEM "STATE" CLASS EXPRESSION 'land' STYLE COLOR -1 -1 -1 END LABEL COLOR 132 31 31 OUTLINECOLOR 128 128 128 SHADOWCOLOR 218 218 218 SHADOWSIZE 1 1 TYPE TRUETYPE FONT arial-bold SIZE 12 ANTIALIAS TRUE POSITION CL PARTIALS FALSE MINDISTANCE 200 BUFFER 4 END # end of label END # end of class END # States label layer ends here # End of LAYER DEFINITIONS ------------------------------- END # end of map file
若是用户要浏览地图,则应提供参考地图。
示例2.4-添加参考图
HTML Template:
<!-- MapServer Template --> <html> <head> <title>MapServer 5.x Tutorial</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link type="text/css" rel="stylesheet" href="/tutorial/ms35.css" /> </head> <body bgcolor="#FFFFFF" text="#000000"> <table align="center" border="0"><tr> <td> <h3 align="center">Example 2.4: Adding a Reference Map</h3> <!-- START OF MAPSERVER FORM --> <form name="mapserv" method="GET" action="/cgi-bin/mapserv.exe"> <!-- HIDDEN MAPSERVER CGI VARIABLES --> <input type="hidden" name="map" value="[map]"> <input type="hidden" name="imgext" value="[mapext]"> <input type="hidden" name="imgxy" value="199.5 149.5"> <table width="600" border="0" align="center"> <tr> <td> <table width="400" border="1" align="center"> <tr> <td colspan="4">Select Layers: <!-- SPECIFY VECTOR LAYERS --> <input type="checkbox" name="layer" value="states" [states_check] checked>State Boundaries <input type="checkbox" name="layer" value="hydro" [hydro_check]>Water Features <input type="checkbox" name="layer" value="roads" [roads_check]>Roads<br> <!-- SPECIFY RASTER LAYERS --> Select Background: <select name="layer"> <option value=" " [ _select]>No Background</option> <option value="modis_jpl" [modis_jpl_select]> Daily MODIS Image</option> <option value="modis" [modis_select]> MODIS Surface Reflectance</option> </select> </td> </tr> <tr> <td> <!-- SPECIFY MAP MODE --> <div align="center">Map Mode:<br> <select name="mode"> <option value="browse" [browse_select]>Browse</option> </select> </div> </td> <td> <!-- FORM SUBMIT BUTTON --> <div align="center"> <input type="submit" name="submit" value="Refresh"> </div> </td> <td> <!-- ZOOM/PAN CONTROLS --> <div align="center">Map Control: <br> <select name="zoom"> <option value="4" [zoom_4_select]>Zoom In 4x</option> <option value="3" [zoom_3_select]>Zoom In 3x</option> <option value="2" [zoom_2_select]>Zoom In 2x</option> <option value="1" [zoom_1_select]>Recenter</option> <option value="-2" [zoom_-2_select]>Zoom Out 2x</option> <option value="-3" [zoom_-3_select]>Zoom Out 3x</option> <option value="-4" [zoom_-4_select]>Zoom Out 4x</option> </select> </div> </td> <!-- REFERENCE COLUMN --> <td rowspan="2" valign="top"> <p>Reference:<br> <input type="image" name="ref" src="[ref]" border="0"></p> </td> </tr> <tr> <!-- DISPLAY THE MAPSERVER-CREATED MAP IMAGE --> <td colspan="3" align="center" valign="top"> <input type="image" name="img" src="[img]" width="400" height="300" border="0"> </td> </tr> </table> </td> </tr> </table> </form> </body> </html>
这段代码中有趣的部分是标签,即[缩放\选择]。当mapserv看到这样的标记时,它将评估是否选择了值(“1”)。若是选中,则MapServ会将此选项标记为“已选中”。再次,转到示例2.1并“查看”代码。“recenter”选项应该是“selected”(这样当咱们再次提交表单时,“zoom”的值将是“1”)。
最后一个标签是“[img]”标签。正如您将注意到的,它是表单输入对象的一部分。“[img]”将替换为mapserv生成的映像的完整路径和名称。
一旦mapserver替换了全部标记,它将向浏览器发送一个合适的HTML表单。用户将再次可以进行更改。
像任何好的地图同样,它应该包括一个传说。
示例2.5-添加图例
这里是HTML模板源代码:
<!-- MapServer Template --> <html> <head> <title>MapServer 5.x Tutorial</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link type="text/css" rel="stylesheet" href="/tutorial/ms35.css" /> </head> <body bgcolor="#FFFFFF" text="#000000"> <table align="center" border="0"><tr> <td> <h3 align="center">Example 2.5: Adding a Legend</h3> <!-- START OF MAPSERVER FORM --> <form name="mapserv" method="GET" action="/cgi-bin/mapserv.exe"> <!-- HIDDEN MAPSERVER CGI VARIABLES --> <input type="hidden" name="map" value="[map]"> <input type="hidden" name="imgext" value="[mapext]"> <input type="hidden" name="imgxy" value="199.5 149.5"> <table width="600" border="0" align="center"> <tr> <td> <table width="400" border="1" align="center"> <tr> <td colspan="4">Select Layers: <!-- SPECIFY VECTOR LAYERS --> <input type="checkbox" name="layer" value="states" [states_check] checked> State Boundaries <input type="checkbox" name="layer" value="hydro" [hydro_check]> Water Features <input type="checkbox" name="layer" value="roads" [roads_check]> Roads<br> <!-- SPECIFY RASTER LAYERS --> Select Background: <select name="layer"> <option value=" " [ _select]>No Background</option> <option value="modis_jpl" [modis_jpl_select]> Daily MODIS Image</option> <option value="modis" [modis_select]> MODIS Surface Reflectance</option> </select> </td> </tr> <tr> <td> <!-- SPECIFY MAP MODE --> <div align="center">Map Mode:<br> <select name="mode"> <option value="browse" [browse_select]>Browse</option> </select> </div> </td> <td> <!-- FORM SUBMIT BUTTON --> <div align="center"> <input type="submit" name="submit" value="Refresh"> </div> </td> <td> <!-- ZOOM/PAN CONTROLS --> <div align="center">Map Control: <br> <select name="zoom"> <option value="4" [zoom_4_select]>Zoom In 4x</option> <option value="3" [zoom_3_select]>Zoom In 3x</option> <option value="2" [zoom_2_select]>Zoom In 2x</option> <option value="1" [zoom_1_select]>Recenter</option> <option value="-2" [zoom_-2_select]>Zoom Out 2x</option> <option value="-3" [zoom_-3_select]>Zoom Out 3x</option> <option value="-4" [zoom_-4_select]>Zoom Out 4x</option> </select> </div> </td> <!-- REFERENCE AND LEGEND COLUMN --> <td rowspan="2" valign="top"> <p>Reference:<br> <img name="ref" src="[ref]"></p> <p>Legend:<br> <img name="legend" src="[legend]"></p> </td> </tr> <tr> <!-- DISPLAY THE MAPSERVER-CREATED MAP IMAGE --> <td colspan="3" align="center" valign="top"> <input type="image" name="img" src="[img]" width="400" height="300" border="0"> </td> </tr> </table> </td> </tr> </table> </form> </body> </html>
这段代码中有趣的部分是标签,即[缩放\选择]。当mapserv看到这样的标记时,它将评估是否选择了值(“1”)。若是选中,则MapServ会将此选项标记为“已选中”。再次,转到示例2.1并“查看”代码。“recenter”选项应该是“selected”(这样当咱们再次提交表单时,“zoom”的值将是“1”)。最后一个标签是“[img]”标签。正如您将注意到的,它是表单输入对象的一部分。“[img]”将替换为mapserv生成的映像的完整路径和名称。一旦mapserver替换了全部标记,它将向浏览器发送一个合适的HTML表单。用户将再次可以进行更改。