1. 程式人生 > >MapServer Tutorial——MapServer7.2.1教程學習——第一節用例實踐:Example1.5 Adding a raster layer

MapServer Tutorial——MapServer7.2.1教程學習——第一節用例實踐:Example1.5 Adding a raster layer

alpha extent def 沒有 oci 信息 ndis 效果 bitmap

MapServer Tutorial——MapServer7.2.1教程學習——第一節用例實踐:Example1.5 Adding a raster layer

一、前言

  MapServer不僅支持矢量數據(point, lines, polygons, and annotations),同時也支持柵格數據。通過GDAL庫,MapServer可以輸入輸出多種類型的柵格數據。

  在4.x版本前,MapServer輸出柵格數據僅限於單個圖層、灰度圖像或偽彩色圖像。

  當前版本支持RGB圖像和多光譜圖像(多層級multi-layer)。此示例演示如何選擇使用多光譜數據時要顯示的圖層。官網地址:https://www.mapserver.org/tutorial/example1-5.html

  註意 在使用RGB和多光譜圖像時,可能會出現明顯的性能問題。

  因為MapServer5.x後使用AGG或GD2.x來生成輸出的圖像,它還支持RGB(24位或真彩色)輸出。因此,除了8位(灰度圖像或偽彩色圖像)png8之外,現在還可以使用png(真顏色)進行輸出。此示例使用PNG作為ImageType。

  註意 與RGB輸入一樣,與PNG8相比,使用PNG時可能會出現明顯的性能問題。

  MapServer實際上也可以使用gdal生成輸出圖像,但這是另一個主題。如果您想了解更多信息,請查看mapfile引用中的outputformat對象。

二、創建站點Example1.5 Adding a raster layer

  1.文件準備

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

    在cmd中輸入:cd Example1.5

    在cmd中輸入:md data

    在cmd中輸入:md logs

    在cmd中輸入:md fonts

    在cmd中輸入:md symbols

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

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

    在cmd中輸入:cd data

    在cmd中輸入:md raster

    修改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>

    將 app\tutorial\fonts 中的所有內容拷貝到 app\Example1.5\fonts 文件夾中(字體文件)

    將 app\tutorial\data 中的 states_ugl.dbf、states_ugl.shp、states_ugl.shx 拷貝到 app\Example1.5\data 文件夾中(數據文件)

    將 app\tutorial\symbols 中的 symbols35.sym 拷貝到 app\Example1.5\symbols 文件夾中(符號文件)

    將 app\tutorial\data\raster 中的 mod09a12003161_ugl_ll_8bit.tif、mod09a12003161_ugl_ll_idxa.tif 拷貝到 app\Example1.5\data\raster 文件夾中(圖層數據文件)

  2.在IIS中創建站點Example1.5

    應用程序池名稱:Example1.5;站點端口:8015

  3.給應用程序池讀寫log文件夾權限

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

  4.修改example1_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      PNG
  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         POLYGON           #ANNOTATION 此類型的LAYER已經被移除。此處需要表達的是標簽,請查看上一章《MapServer Tutorial——MapServer7.2.1教程學習——第一節用例實踐:Example 1.4 Labeling the Map》
                     #LAYER 官網:https://www.mapserver.org/mapfile/layer.html ,定位到 TYPE 對象處查看。 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 # deprecated, use STYLE and GEOMTRANSFORM #SHADOWSIZE 2 2 # deprecated, use STYLE and GEOMTRANSFORM 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 ------------------------------- 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...

  5.瀏覽查看

    在瀏覽器中輸入:http://localhost:8015/mapserv?map=../apps/Example1.5/example1_5.map&layer=states&layer=modis&layer=states_line&layer=states_label&mode=map

    技術分享圖片

三、MapFile文件解析

  1.文件結構

                                             MAP
             LAYER #1-------------LAYER #2----|----LAYER #3--------LAYER #4
          (states)              (modis)       (states_line)   (states_label)
             |                                     |               |
(land) CLASS-|-CLASS (water)                       |-CLASS         |-CLASS
          |     |                                     |               |
    STYLE-|     |-STYLE                               |-STYLE   STYLE-|-LABEL

    當前案例分為四個圖層:

      • tsates用來展示幾何區域
      • modis展示tiff圖像文件,柵格數據
      • states_line展示的是CLASSITEM=CLASS,EXPRESSION=land 的線條數據,其中線條符號為 line5
# 12
Symbol
NAME ‘line5‘
Type VECTOR
TRANSPARENT 0
Points
1 0
0 1
0 1
1 0
END
END 

#line的數據類型為 VECTOR 矢量數據,詳情請看:https://www.mapserver.org/mapfile/symbology/construction.html中 Symbols of TYPE vector and ellipse
#同時,請查看:https://www.mapserver.org/mapfile/symbol.html
#此處可以理解為構建了一個矢量區域,有個坐標系,點(1,0)、(0,1)、(0,1)、(1,0)畫了一個矩形,然而此矩形看起來沒有寬度,長為 2 的開方(三角形勾股定理)
#同時,將這個矢量矩形(看起來就是短的線)中心基於 dbf 數據中 CLASS = land 的 POLYGON 中的點數據來展示,這樣看起來就給 land 外面畫了一個線。

  2.對象參數解釋

    IMAGETYPE

      MapServer生成輸出數據的格式。在cmd中輸入:mapserv -v 查看。值PNG是值輸出24位PNG或真色PNG。

    SYMBOLSET

      SYMBOL集合的文件位置。可以使絕對路徑,也可以是和mapfile(example1_5.map)的相對路徑。字符集位置。可以先了解一下SYMBOL,資料如下:

      SYMBOL的講解:https://www.mapserver.org/mapfile/symbol.html

      創建字符集文件:https://www.mapserver.org/mapfile/symbology/construction.html

    DATA raster/mod09a12003161_ugl_ll_8bit.tif

      在NAME為modis的圖層中,添加的是一個GeoTIFF圖層數據。MapServer支持矢量數據和柵格數據。這個圖層為柵格類型。這個圖層是通過GDAL的庫來完成支持的。MapSeerver上不同的柵格支持和生成請查看:https://www.mapserver.org/input/raster.html#raster

      modis數據了解請進:https://modis.gsfc.nasa.gov/

    TYPE RASTER

      在NAME為modis的圖層中,當前LAYER的數據類型。此處使用的是RASTER。同時在LAYER中,MapServer還支持POLYGON,LINE,POINT,LABEL中支持ANNOTATION等。

    PROCESSING "BANDS=1,2,3"

      這個位置我覺得很難理解。查了相關資料。大概意思是,MapServer調用GDAL庫來處理tiff圖像,並輸出顯示。

      PROCESSING表示進程。根據進程值來決定調用那個庫。請查看:https://www.mapserver.org/mapfile/layer.html 中PROCESSING 段落。

      BANDS表示波段,GDAL源碼中通過RasterIO來讀取波段數據。

      TIFF文件時位圖,在windows中的存儲格式為BGR,然後MapServer根據參數調用時,對參數做了調整順序的相關處理。1,2,3表示Red,Green和blue三種波段選擇。

      具體的我也不大清楚,總之嘗試後,發現1,2,3在瀏覽器中顯示的是和直接看到tiff相差不遠,只是灰色的底色變成白色的。

      同時也請查看:https://www.mapserver.org/input/raster.html 中 BANDS=red_or_grey[,green,blue[,alpha]] 處的解釋。

    OFFSITE

      設置當前LAYER層的背景顏值組成。(Sets the color index to treat as transparent for raster layers.)

      告訴MapServer按照像素值渲染背景(或忽略)。

      想了半天沒弄懂,最後亂改這個值也沒發現什麽。知道讀到or ignore之後。忽略,這個tif文件正好背景顏色值RGB是:71 74 65 ,是否設置OFFSITE值和背景顏色值一樣時,做忽略處理。

      但是在設置為其他顏色值時,卻還是顯示當前tif文件的背景顏色。這就十分尷尬了,是否是因為當前tif文件有背景顏色,然後將OFFSITE設置的顏色給覆蓋了。

      顏色組成分多種形式,當前案例是:Indexed Color Image(偽彩色圖像)。還有:RGB、RGBA等。

四、RGB vs Indexed Color Image 真彩色和偽彩色的對比

  1.關於偽彩色,可以查看:https://en.wikipedia.org/wiki/Indexed_color

  2.修改mapfile的相關配置,將

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

  看看效果

技術分享圖片

  顯示的效果已經改變。去掉GDAL的程序處理後,也不顯示背景色了。

五、後記

  感覺模擬樣例做一個很簡單,但是弄清楚相關的mapfile對象卻很頭痛,涉及的知識點太多,不理解的位置太多。比如:OFFSITE就不動。繼續深入學習,相信隨著了解的越來越多,就會知道其作用。

MapServer Tutorial——MapServer7.2.1教程學習——第一節用例實踐:Example1.5 Adding a raster layer