MapServer Tutorial——MapServer7.2.1教程學習——第一節用例實踐:Example1.6 Defining Projections and Extents

MapServer Tutorial——MapServer7.2.1教程學習——第一節用例實踐:Example1.6 Defining Projections and Extentshtml

1、前言web

  當在mapfile文件中定義PROJECTION對象時,MapServer將圖層和投影信息傳遞給proj.4庫執行重投影。詳情請看:https://proj4.org/瀏覽器

  有數以千計的地理參考系統。爲了將數據集與不一樣地理參考系統結合到地圖中,必須將數據集轉換(投影)到地圖中選定的地理參考系統。app

  PROJECTION在MapServe中,則是制定使用哪一種地理殘念考系統。(同屬理解,就是你當前的數據是使用哪一種座標信息。)less

  既然PROJECTION是用來指定數據所使用的座標系信息,那麼,它出現的位置就應該在須要使用顯示數據的地方。學習

  第一個就是mapfile的MAP標籤裏面,用來定義輸出座標系;第二個位置就是LAYER標籤裏面,定義輸入座標系。flex

2、在MapServer中使用投影(Projections with MapServer)ui

  若是要使用PROJECTION,你必須在mapfile中MAP標籤下面定義一個輸出PROJECTION。而後每一個LAYER裏面必須定義輸入座標系。(注意是必須定義)this

  PROJECTION對象是有一系列的PROJ.4庫關鍵字構成。要麼直接用關鍵書寫;要麼引用EPSG文件。格式以下。(EPSG詳見:https://www.mapserver.org/glossary.html#term-epsgspa

  EPSG代碼是與座標系定義相關聯的數字代碼。如:EPSG:4326(EPSG:4326就是WGS84)

  ESPG相關資料:

#直接使用PROJ.4庫關鍵字構建PROJECTION

PROJECTION "proj=utm" "ellps=GRS80" "datum=NAD83" "zone=15" "units=m" "north" "no_defs" END

#經過EPSG構建PROJECTION
PROJECTION
   "init=epsg:26915" END

3、建立Example1.6站點

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

  在cmd中輸入:cd Example1.6

  在cmd中輸入:md data

  在cmd中輸入:md fonts

  在cmd中輸入:md symbols

  在cmd中輸入:md logs

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

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

  在cmd中輸入:cd data

  在cmd中輸入:md raster

  將 app/tutorial/fonts 文件夾中的全部文件拷貝到 apps/Example1.6/fonts 文件夾中

  將 app/tutorial/symbols 文件夾中的 symbols35.sym 文件拷貝到 apps/Example1.6/symbols 文件夾中

  將 app/tutorial/data 文件夾中的 states_ugl.dbf、states_ugl.shp、states_ugl.shx 文件拷貝到 apps/Example1.6/data 文件夾中

  將 app/tutorial/data/raster 中的 mod09a12003161_ugl_ll_8bit.tif 文件拷貝到 apps/Example1.6/data/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>

  修改MapFIle(example1_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
  IMAGETYPE      PNG
  #EXTENT       -97.238976 41.619778 -82.122902 49.385620                       # WGS84            經緯度座標數據
  #EXTENT        201621.496941 -294488.285333 1425518.020722 498254.511514      # LAEA 原始官網mapfile中的數據
EXTENT         230147.11 -371641.81 1285308.08 632638.93                      # 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_polygon 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_line 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 POLYGON 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 ------------------------------- 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...

 

  在 IIS 中建立站點 Example1.6,端口爲8016,應用程序池爲 Example1.6

  設置應用程序池對 apps/Example1.6/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.6\logs" /grant "IIS AppPool\Example1.6":(OI)(CI)RW

  在瀏覽器中打開:http://localhost:8016/mapserv?map=../apps/Example1.6/example1_6.map&layer=states_polygon&layer=modis&layer=states_line&layer=states_label&mode=map

 4、EXTENT的改變

  查看mapfile(example1_6.map)文件後,首先,你會發現最明顯的改變是 MAP 標籤下面的 EXTENT 標籤的數據。

  EXTENT 表示當前數據文件的顯示範圍。如今的顯示方爲值以下:

EXTENT       201621.496941 -294488.285333 1425518.020722 498254.511514

#Example1.5 中是:EXTENT         -97.238976 41.619778 -82.122902 49.385620

 

  爲何會發生這樣的改變呢,應爲咱們修改了MapServer 的輸出座標系,即添加了 PROJECTION 對象。

  EXTENT對象的輸出單位必須與 MapServer 中的輸出座標系一致。

  先前的座標系是經緯度。如今 PROJECTION  設置爲:

  # 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 ---

 

  如今的 PROJECTION 設置爲:Lambert Azimuthal Equal-Area,其單位爲米,因此咱們如今的範圍 EXTENT 也要設置爲米。

  能夠經過 ArcView、PROJ4 的 cs2cs 或其餘GIS包作轉換。下面的命令可用於從新投影原始範圍值:

#第一步
#在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\tutorial\data
#在cmd中輸入:ogrinfo -ro -al -so states_ugl.shp
#獲取到states_ugl.shp 的範圍信息:Extent: (-97.238976, 41.619778) - (-82.122902, 49.385620)。 Extent的格式爲:<Lower Left X> <Lower Left Y> <Upper Right X> <Upper Right Y>

#而後在cmd中輸入下面的命令,回車

cs2cs +proj=latlong +datum=WGS84 +to +proj=laea +ellps=clrk66 +lat_0=45 +lon_0=-100

#執行當前計算轉換,是在數據命令後,在輸出原始範圍的西南座標值。即左下角座標值,用空格分割開

-97.238976 41.619778

#回車後獲得三個值,分別爲:西南方向 X、Y、單位精度,其中第三個參數能夠忽略。

230147.11       -371641.81 0.00

#接着再輸入東北方向座標參數

-82.122902 49.385620

#獲得東北方向的轉換輸出範圍:

1285308.08      632638.93 0.00

#因此轉換後的EXTENT值爲:
EXTENT 230147.11 -371641.81 1285308.08 632638.93

#轉換完畢

#可是原始官網mapfile文件中給出的 EXTENT 值與計算值不符合。這是爲何呢?

EXTENT 201621.496941 -294488.285333 1425518.020722 498254.511514      # LAEA 原始官網mapfile中的數據

#由於地圖在數據範圍中並不居中,你能夠適當的增長其範圍,加以調整使其居中。官網案例是經過ArcView獲取的。

 

  當使用本身計算的範圍時和官網經過ArcView修改後的範圍,其顯示爲分別以下:

           

  對比會發現,官網給定的作了相關邊距接線的調整。

5、後記

  在作本樣例中,對MapServer又有了新的認識:

    • 能夠自定義投影座標系的輸出類型。
    • PROJECTION中是如何經過PROJ4關鍵詞參數定義輸出座標系的。
    • PROJECTION是如何EPSG文件。
    • EPSG文件究竟是個什麼東西:http://www.epsg.org/ 
    • EPSG文件也能夠自定義其目錄位置。
    • PROJ4中cs2cs命令的使用
MAP
  CONFIG "PROJ_LIB" "/usr/share/proj/"  #UNIX的絕對路徑,若是是未定義,則按照安裝MapServer中的 E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\bin\proj\SHARE 路徑查找(我的理解)
  PROJECTION
    "init=epsg:3857"
  END # PROJECTION
  ...

 

    後面還想寫一篇關於關於PROJ4座標轉換的文章。一部一部學習。

相關文章
相關標籤/搜索