MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.6 Defining Projections and Extents
一、前言
当在mapfile文件中定义PROJECTION对象时,MapServer将图层和投影信息传递给proj.4库执行重投影。详情请看:https://proj4.org/。
有数以千计的地理参考系统。为了将数据集与不同地理参考系统结合到地图中,必须将数据集转换(投影)到地图中选定的地理参考系统。
PROJECTION在MapServe中,则是制定使用哪种地理残念考系统。(同属理解,就是你当前的数据是使用哪种坐标信息。)
既然PROJECTION是用来指定数据所使用的坐标系信息,那么,它出现的位置就应该在需要使用显示数据的地方。
第一个就是mapfile的MAP标签里面,用来定义输出坐标系;第二个位置就是LAYER标签里面,定义输入坐标系。
二、在MapServer中使用投影(Projections with MapServer)
如果要使用PROJECTION,你必须在mapfile中MAP标签下面定义一个输出PROJECTION。然后每个LAYER里面必须定义输入坐标系。(注意是必须定义)
PROJECTION对象是有一系列的PROJ.4库关键字构成。要么直接用关键书写;要么引用EPSG文件。格式如下。(EPSG详见:https://www.mapserver.org/glossary.html#term-epsg)
EPSG代码是与坐标系定义相关联的数字代码。如:EPSG:4326(EPSG:4326就是WGS84)
ESPG相关资料:
-
- 官网:http://www.epsg.org/
- ESPG数据资料:http://spatialreference.org/
#直接使用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
三、创建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
四、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修改后的范围,其显示为分别如下:
对比会发现,官网给定的做了相关边距接线的调整。
五、后记
在做本样例中,对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坐标转换的文章。一部一部学习。