MapServer教程2

 

第二章 Tutorial 教程css

MapServer Tutorial MapServer教程html

  • 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教程背景web

  • Tutorial Timeframe 教程時間框架

雖然有些用戶能夠在一天內完成本教程,但那些詳細研究每一個示例的用戶可能指望在一週內完成本教程。正則表達式

  • Tutorial Data 教程數據

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

與MapServer自己同樣,本教程對任何人都是開放的和可自定義的。這樣作的目的是但願有人(或某些人)可以幫助設計和進一步開發它。express

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

 

  • 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表單。用戶將再次可以進行更改。

相關文章
相關標籤/搜索