1. 程式人生 > >MapServer Tutorial——MapServer7.2.1教程學習——第一節用例實踐:Example1.2 Static Map with Two Layers

MapServer Tutorial——MapServer7.2.1教程學習——第一節用例實踐:Example1.2 Static Map with Two Layers

htm pla sel sam 又是 mage url參數 邊界線 start

MapServer Tutorial——MapServer7.2.1教程學習——第一節用例實踐:Example1.2 Static Map with Two Layers

一、前言

  上一篇博客《MapServer Tutorial——MapServer7.2.1教程學習——第一節用例實踐:Example1.1 A map with single layer》中介紹了單圖層的地圖加載顯示。下面根據官網的例子介紹兩個圖層的加載顯示。官網地址:https://www.mapserver.org/tutorial/example1-2.html#example1-2。

  翻譯對於我而言只是一個過程,主要是自己邊翻譯,邊實踐,邊記載。

二、實現 Static Map with Two Layers

  1.Static Map with Two Layers例子實踐前說明

    技術分享圖片

    上一個例子中,通過打開:http://localhost:8011/mapserv?map=../apps/Example1.1/example1_1.map&layer=states&mode=map實現地圖圖片的生成。這是本節中大多數例子工作方式。

    但是Static Map with Two Layers 這個例子的mapfile文件不一樣。

  2.準備創建站點

    老規矩,老規則。在E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps

文件夾下面創建Example1.2文件夾

    在Example1.2文件下面創建data、logs文件夾,以及 web.config、example1_2.map文件

    在cmd中輸入:cd /d E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps

    在cmd中輸入:md Example1.2

    在cmd中輸入:cd Example1.2

    在cmd中輸入:md data

    在cmd中輸入:md logs

    在cmd中輸入:cd.>web.config

    在cmd中輸入:cd.>example1_2.map

    web.config文件如下:

1 <?xml version="1.0" encoding="UTF-8"?>
2 <configuration>
3     <system.webServer>
4         <handlers>
5             <add name="MapServerFastCgi" 
            path
="*"
            verb
="*"
            type
="" modules="FastCgiModule" scriptProcessor="E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\bin\mapserv.exe"
            resourceType
="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="" /> 6 </handlers> 7 <caching enabled="true" enableKernelCache="true" /> 8 </system.webServer> 9 </configuration>

    example1_2.map格式如下:

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    255 0 0
      END
    END
  END # States line layer ends here
  # End of LAYER DEFINITIONS -------------------------------
  DEBUG 5 
  CONFIG "MS_ERRORFILE" "logs\ms.log"
END 

    打開cmd輸入:icacls "E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.1\logs" /grant "IIS AppPool\Example1.2\logs" :(OI)(CI)RW

    是MapServer對logs文件夾有讀寫權限

    在瀏覽器中輸入:http://localhost:8012/mapserv?map=../apps/Example1.2/example1_2.map&layer=states_poly&layer=states_line&mode=map

    技術分享圖片

  3.URL參數說明

    map=../apps/Example1.2/example1_2.map 表示 MapServer 與 mapfile(example1_2.map)的相對路徑

    layer=states_poly 表示打開 NAME 值為 states_poly 的 polygon(多邊形)layer層

    layer=states_line 表示打開 NAME 值為 states_line 的 line(線)layer層

    mode=map 表示告訴MapServer對mapfile文件的輸出格式,這裏是告訴MapServer直接將圖像投影到瀏覽器,無需先在服務器端創建零時的圖像。

    在 mapfile(example1_2.map)中 NAME 值為 states_line 的 line(線)layer層的 CLASS 對象的 COLOR 255 0 0 表示紅色,顧瀏覽器中的邊線為紅色。

三、Mapfile結構解釋說明

  其結構如下:

           MAP
      LAYER-|-LAYER
   CLASS-|     |-CLASS
STYLE-|           |-STYLE

  在這個mapfile裏面,我們將原始層分為兩層。第一層是多邊層(polygon)layer,但是第一層沒有邊線顏色(OUTLINECOLOR)設置。(其實通過OUTLINECOLOR設置第一個層也可以實現上圖紅色邊線的視覺效果,但是現在學習的是用兩個層來表示)。

  第二個層將TYPE對象值由POLYGON修改為LINE,然後設置了 COLOR 對象值為 255 0 0 表示紅色。

  那麽TYPE對象有幾種值,然後又是如何區分的呢?詳見:https://www.mapserver.org/mapfile/layer.html。

  其中對TYPE對象的定義如下:

    TYPE [chart|circle|line|point|polygon|raster|query](這個中括號裏面豎線分割的就是TYPE對象的各種值)
      Specifies how the data should be drawn. Need not be the same as the shapefile type. For example, a polygon shapefile may be drawn as a point layer, but a point shapefile may not be drawn as a polygon layer. Common sense rules.

      In order to differentiate between POLYGONs and POLYLINEs (which do not exist as a type), simply respectively use or omit the COLOR keyword when classifying.

      If you use it, it’s a polygon with a fill color, otherwise it’s a polyline with only an OUTLINECOLOR.

      A circle must be defined by a a minimum bounding rectangle. That is, two points that define the smallest square that can contain it. These two points are the two opposite corners of said box. The following is an exampl using inline points to draw a circle:

LAYER
  NAME ‘inline_circles‘
  TYPE CIRCLE
  STATUS ON
  FEATURE
    POINTS
      74.01 -53.8
      110.7 -22.16
    END
  END
  CLASS
    STYLE
      COLOR 0 0 255
    END
  END
END

    TYPE query means the layer can be queried but not drawn.

    Note TYPE annotation has been deprecated since version 6.2. Identical functionality can be obtained by adding LABEL level STYLE blocks, and do not require loading the datasets twice in two different layers as was the case with layers of TYPE annotation.

    See also The Dynamic Charting HowTo for TYPE chart.

    (後續我也肯定會寫各種TYPE值的實現,畢竟是自己學習的過程)

四、為什麽這麽做而不直接在第一個層中定義OUTLINECOLOR呢?

  如果我們繼續在狀態層的頂部添加層,那麽輪廓很可能會被這些其他層覆蓋。為了在添加這些其他層之後仍能看到狀態邊界,我們必須將狀態邊界線層與狀態多邊形層分離,並將其放在其他層的頂部。我們如何定義/添加層是有順序的。

五、後記

  這節案例中,其實學到的東西分為以下幾點:

    1.強化記憶了mapfile的應用

    2.對 LAYER 對象中 TYPE 對象有了更加明確的認識

    3.運用了多個層,知道為什麽要這麽用。目前還不明白MapServer中,層的順序是否是按照mapfile中定義的順序加載的。如:一個layer是最下面,然後一級一級的網上放。有待資料查詢和證實。

MapServer Tutorial——MapServer7.2.1教程學習——第一節用例實踐:Example1.2 Static Map with Two Layers