MapServer Tutorial——MapServer7.2.1教程學習——第一節用例實踐:Example1.3 Displaying Classes in a Layer

MapServer Tutorial——MapServer7.2.1教程學習——第一節用例實踐:Example1.3 Displaying Classes in a Layerhtml

1、前言web

   關於第一節的案例,分別介紹了一個基本的地圖站點應用程序建立和多圖層地圖站點 應用程序建立。這個案例 主要來介紹一下mapfile文件中 LAYER 對象裏面,CLASS對象的應用。數據庫

  同時還有如何根據CLASSITEM、EXPRESSION等配置去修改地圖的顯示方式。express

  最後還有一個很酷炫的方法一次性讀取shp文件中的數據信息。瀏覽器

  當前案例官網:https://www.mapserver.org/tutorial/example1-3.html#example1-3app

  附帶一點其餘的,當前系列博客的大綱博文《MapServer Tutorial——MapServer7.2.1教程學習(大綱)》,我會持續更新。less

  以及我所寫博文的應用環境 《MapServer Configuring with IIS》。學習

  但願可以給新手帶來幫助。ui

2、建立Example1.3 Displaying Classes in a Layer站點this

  老規矩,按照先前的建立站點規則。

  在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.3

  在cmd中輸入:cd Example1.3

  在cmd中輸入:md data

  在cmd中輸入:md logs

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

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

  紅色標記路徑部分,按照你站點建立位置填寫。

  修改web.config,內容以下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <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="" /> </handlers> <caching enabled="true" enableKernelCache="true" /> </system.webServer> </configuration>

 

  打開IIS建立站點,站點名稱、應用程序池名稱爲:Example1.3。端口號:8013。

  給應用程序池添加對logs文件夾的讀寫權限。

  在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.3\logs" /grant "IIS AppPool\Example1.3":(OI)(CI)RW

  將states_ugl.dbf、states_ugl.shp、states_ugl.shx等文件複製到Example1.3中的data文件夾下面。

  mapfile(example1_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 -------------------------------
  DEBUG 5
  CONFIG "MS_ERRORFILE" "logs\ms.log"
END # All map files must come to an end just as all other things must come to...

  在瀏覽器中輸入:http://localhost:8013/mapserv?map=../apps/Example1.3/example1_3.map&layer=states_poly&layer=states_line&mode=map

  

  URL參數解析通上一章《MapServer Tutorial——MapServer7.2.1教程學習——第一節用例實踐:Example1.2 Static Map with Two Layers》相同,詳見上一章URL參數解析。

  一樣的shp數據文件,只是mapfile文件不一樣,而後 polygon 區域的顏色就不一樣。這就是mapfile中CLASS對象的不一樣配置致使的。

3、MapFile文件解析

  mapfile文件結構以下:

                            MAP
(states_poly) LAYER----------|---------LAYER (states_line)
               |                        |
(land) CLASS---|---CLASS (water)        |-CLASS
          |         |                      |
    STYLE-|         |-STYLE                |-STYLE

  當前mapfile一樣只有兩個圖層(layer),可是 polygon(NAME值爲states_poly)的圖層卻有兩個 CLASS 對象。其中 polygon 顏色的區分是經過:CLASSITEM、EXPRESSION等兩個對象座椅區分的。

  CLASSITEM

    當一個layer使用多個CLASS時,CLASSITEM用於指定DBF文件(可理解爲shp文件的數據庫表)中,根據哪一個屬性(可理解爲當前表的字段)去使用對應的CLASS。

    當前案例 states_ugl.shp 文件有多個 polygon 區域,states_ugl.shp記錄這個區域的矢量數據 polygon 所在區域、數量等。同時每一個polygon還包含其餘哪些屬性。

    可是,states_ugl.shp 中的 polygon 的其餘屬性數據在 states_ugl.dbf 文件裏面。

    打開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\Example1.3\data

    在cmd中輸入:ogrinfo -al -so states_ugl.shp,查看當前shp文件的相關屬性數據。數據以下:

    

INFO: Open of `states_ugl.shp'
      using driver `ESRI Shapefile' successful.

Layer name: states_ugl
Metadata:
  DBF_DATE_LAST_UPDATE=2002-03-07
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: Integer64 (11.0)
STATE: String (20.0)
STATE_FIPS: String (2.0)
CLASS: String (5.0)

 

    能夠看到,shp的屬性中包含一個叫作CLASS的屬性。

    在cmd中輸入:ogrinfo -al -ro states_ugl.dbf 。能夠查看當前shp文件的全部數據。

    固然,輸入:ogrinfo -al -ro states_ugl.shp也能夠。由於目前案例中的數據文件是ESRI Shapefiles,因此最少包含三個文件,後綴名分別是:dbf、shp、shx,能夠查看:https://www.mapserver.org/input/vector/format_types.html

    相關命令查看:https://gdal.org/ogrinfo.html

    

  EXPRESSION

    EXPRESSION,字面意思就是表達式。

    當前mapfile中,對於 CLASS 對象,咱們根據 EXPRESSION 指定的屬性值去選擇 CLASS 應用到 LAYER 層上。

    固然,EXPRESSION 能夠多個值,也能夠是邏輯表達式。詳情請看:https://www.mapserver.org/mapfile/expressions.html#expressions

4、後記

  經過當前案例,主要學習了CLASSITEM、EXPRESSION等的簡單使用。

  同時,也知道了如何經過GDAL包中的相關ogrinfo命令去查看shape文件中的相關數據。

相關文章
相關標籤/搜索