MapServer教程2


 

第二章 Tutorial 教程

MapServer Tutorial MapServer教程

  • Tutorial background 教程背景
  • Section 1: Static Maps and the MapFile 第一階段:靜態地圖Maps和地圖文件MapFile
  • Section 2: CGI variables and the User Interface 第二階段:CGI變量和用戶界面
  • Section 3: Query and more about HTML Templates 第三階段:查詢和HTML模板
  • Section 4: Advanced User Interfaces 第四階段:增強用戶界面

Tutorial background教程背景

  • Tutorial Timeframe 教程時間框架

雖然有些用戶可以在一天內完成本教程,但那些詳細研究每個示例的用戶可能期望在一周內完成本教程。

  • Tutorial Data 教程數據

本教程中使用的數據集取自美國內政部a zs美國國家地圖集(現在由data.gov托管)。數據集被裁剪到上五大湖地區(明尼蘇達州、密歇根州和威斯康星州),以減少存儲空間。額外的光柵圖像是由明尼蘇達大學的Terrasip項目提供的。使用本教程時,鼓勵您使用自己的數據集。

與MapServer本身一樣,本教程對任何人都是開放的和可自定義的。這樣做的目的是希望有人(或某些人)能夠幫助設計和進一步開發它。

在http://download.osgeo.org/mapserver/docs/mapserver-tutorial.zip下載本教程的數據(以及所有HTML文件)。

 

  • Before Using the Tutorial 在使用本教程之前

使用本教程有一些先決條件:
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&amp;layer=states&amp;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參數(變量及其值),每個參數由一個與(&amp;)分隔。因此,查看查詢字符串,第一個參數“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...
View Code

 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

 http://localhost/cgi-bin/mapserv.exe?map=/ms4w/apps/tutorial/htdocs/example1-2.map&layer=states_poly&layer=states_line&mode=map

與第一個示例一樣,此圖像是通過將<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...
View Code

按對象划分的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...
View Code

按對象划分的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...
View Code

按對象划分的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...
View Code

 按對象划分的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...
View Code

在我們的地圖文件中,您可能注意到的第一件事是原始范圍已被注釋掉,新的范圍值看起來與緯度和經度值不同。另外,我們在頂部附近和每個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
View Code

讓我們來看看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
View Code

我們的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>
View Code

每當用戶單擊地圖時,此塊執行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!
View Code

只有一件事被添加到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
View Code

與第1節中的映射一樣,我們通過調用mapserv(“/cgi-bin/mapserv.exe”)初始化應用程序。並將我們的mapfile路徑和其他參數(“map=/ms4w/apps/tutorial/htdocs/example2-1.map&amp;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
View Code

注意圖層的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
View Code

 

 


如果用戶要瀏覽地圖,則應提供參考地圖。
示例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&nbsp;
              <input type="checkbox" name="layer" value="hydro"
              [hydro_check]>Water Features&nbsp;
          <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>
View Code

這段代碼中有趣的部分是標簽,即[縮放\選擇]。當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&nbsp;
              <input type="checkbox" name="layer" value="hydro"
              [hydro_check]>
          Water Features&nbsp;
          <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>
View Code

這段代碼中有趣的部分是標簽,即[縮放\選擇]。當mapserv看到這樣的標記時,它將評估是否選擇了值(“1”)。如果選中,則MapServ會將此選項標記為“已選中”。再次,轉到示例2.1並“查看”代碼。“recenter”選項應該是“selected”(這樣當我們再次提交表單時,“zoom”的值將是“1”)。
最后一個標簽是“[img]”標簽。正如您將注意到的,它是表單輸入對象的一部分。“[img]”將替換為mapserv生成的映像的完整路徑和名稱。
一旦mapserver替換了所有標記,它將向瀏覽器發送一個合適的HTML表單。用戶將再次能夠進行更改。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM