MapServer Tutorial——MapServer7.2.1教程學習——第一節用例實踐:Example 1.4 Labeling the Maphtml
1、前言前端
MapServer擁有很是靈活的標籤標記系統。它支持bitmap以及truetype字體等。使用truetype字體同時還支持其縮放。標籤的角度和位置是能夠自定義的。web
經過把標籤的位置和角度以及其餘參數的設置使用,你能夠把你的地圖裝飾得更加美觀,信息體現的更加豐富。express
2、搭建Example1.4站點瀏覽器
全部的學習都要經過實踐,仍是從搭建站點開始。app
在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.4less
在cmd中輸入:cd Example1.4ide
在cmd中輸入:md data學習
在cmd中輸入:md logs
在cmd中輸入:md fonts
在cmd中輸入:cd.>web.config
在cmd中輸入:cd.>example1_4.map
將states_ugl.dbf、states_ugl.shp、states_ugl.shx等三個文件放入到data文件夾下面。
將tutorial/fonts中的fonts.list、Vera.ttf(全部的.ttf字體文件)拷貝到Example1.4/fonts文件夾中。
(若是不清楚tutorial文件夾是怎麼回事,請查看《MapServer Tutorial——MapServer7.2.1教程學習——教程背景》,裏面有樣例說明和應用數據下載地址:http://download.osgeo.org/mapserver/docs/mapserver-tutorial.zip)
在IIS中建立Example1.4站點,端口8014。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>
給應用程序池添加對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.4\logs" /grant "IIS AppPool\Example1.4":(OI)(CI)RW
mapfile(example1_4.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" # 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" # 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" # 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 # 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 TYPE TRUETYPE FONT arial-bold SIZE 12 ANTIALIAS TRUE POSITION CL PARTIALS FALSE MINDISTANCE 300 BUFFER 4 # create a background shadow # Note that the tutorial package has not been updated, so this has # to be done manually! #SHADOWCOLOR 218 218 218 # prior to version 6 #SHADOWSIZE 2 2 # prior to version 6 STYLE # since to version 6 GEOMTRANSFORM 'labelpoly' COLOR 218 218 218 OFFSET 2 2 END # STYLE END # end of label END CLASS NAME 'Water' EXPRESSION 'water' 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 64 64 64 END END END # States line layer ends here # End of LAYER DEFINITIONS -------------------------------
END # All map files must come to an end just as all other things must come to...
在瀏覽器中輸入:http://localhost:8014/mapserv?map=../apps/Example1.4/example1_4.map&layer=states_poly&layer=states_line&mode=map
3、MapFIle文件(example1_4.map)講解
MapFIle文件結構
MAP (states_poly) LAYER----------|---------LAYER (states_line) (land) CLASS-----|-CLASS (water) |-CLASS STYLE-|-LABEL |-STYLE |-STYLE</pre>
LABEL 對象以及其中的關鍵詞
FONTSET
指定字體文件清單文件的全路徑或相對路徑(如:./fonts/fonts.list 或 E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.4\fonts\fonts.list)
fonts.list 文件由兩部分組成,名稱和路徑(相對路徑或絕對路徑)。
名稱是路徑所對應的字體集的實際名稱(如:Vera.ttf 是當前文件,可是其字體的實際名稱是arial)。
路徑是Vera.ttf文件與fonts.list的相對路徑或絕對路徑。
當前案例中,fonts.list 文件與 ttf字體文件均在 fonts 文件夾下面。可查看 fonts.list 內容,此案例中採用的是相對路徑。
# This be the fonts list arial Vera.ttf arial-bold VeraBd.ttf arial-italic VeraIt.ttf arial-bold-italic VeraBI.ttf times VeraSe.ttf times-bold VeraSeBd.ttf andale-mono VeraMono.ttf vera_sans Vera.ttf vera_sans-bold VeraBd.ttf vera_sans-italic VeraIt.ttf vera_sans-bold-italic VeraBI.ttf vera_sans_mono VeraMono.ttf vera_sans_mono-bold VeraMoBd.ttf vera_sans_mono-italic VeraMoIt.ttf vera_sans_mono-bold-italic VeraMoBI.ttf vera_serif VeraSe.ttf vera_serif-bold VeraSeBd.ttf
若是咱們採用絕對路徑試試(順便把mapfile文件中的LABEL對象裏面的FONT修改成arial-italic):
# This be the fonts list arial E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.4\fonts\Vera.ttf arial-bold E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.4\fonts\VeraBd.ttf arial-italic E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.4\fonts\VeraIt.ttf arial-bold-italic E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.4\fonts\VeraBI.ttf times E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.4\fonts\VeraSe.ttf times-bold E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.4\fonts\VeraSeBd.ttf andale-mono E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.4\fonts\VeraMono.ttf vera_sans E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.4\fonts\Vera.ttf vera_sans-bold E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.4\fonts\VeraBd.ttf vera_sans-italic E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.4\fonts\VeraIt.ttf vera_sans-bold-italic E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.4\fonts\VeraBI.ttf vera_sans_mono E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.4\fonts\VeraMono.ttf vera_sans_mono-bold E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.4\fonts\VeraMoBd.ttf vera_sans_mono-italic E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.4\fonts\VeraMoIt.ttf vera_sans_mono-bold-italic E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.4\fonts\VeraMoBI.ttf vera_serif E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.4\fonts\VeraSe.ttf vera_serif-bold E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.4\fonts\VeraSeBd.ttf
刷新頁面,看到效果以下:
可使用相對路徑,也可使用絕對路徑,而後字體已經變爲斜體。
字體相關數據信息網站:https://www.freetype.org/
LABELITEM
上一章學過CLASSITEM,LABELITEM與其有相似的理解。
可是其與CLASSITEM又不相同,LABELITEM是指定顯示某個字段中的數據,當前案例中是「STATE」。
LABELITEM 在 LAYER 標籤中,而 LABEL 在 CLASS 標籤中。因此目前只顯示三個標籤的緣由是:只有 CLASS 中 EXPRESSION 爲 land 的 幾個圖形纔會顯示標籤,同時MINDISTANCE決定了重疊的標籤在執行像素內不顯示。
能夠在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.4\data
在cmd中輸入:ogrinfo -al -ro states_ugl.shp 查看全部的數據。
在cmd中輸入:ogrinfo -al -so -where CLASS='land' states_ugl.shp 查看 CLASS='land' 的概要數據。
若是將MINDISTANCE修改成100時,刷新瀏覽器,你會發現,又出來了一個標籤。
LABEL
以LABEL標籤開頭。LABEL對象在其餘對象中使用,當前案例是在CLASS對象中使用,也能夠在其餘標籤中使用,如:SCALEBAR。
LABEL標籤的詳情見:https://www.mapserver.org/mapfile/label.html
COLOR
目前案例中位於LABEL中的COLOR是用來定義label中的文字顏色。
SHADOWCOLOR
文字的陰影顏色。(自MapServer 6開始,LABEL STYLE 被 GEOMTRANSFORM LABELPOLY 所替代。其實mapfile中已經更新,只是官網上面的說明沒有更新。)
SHADOWSIZE
文字陰影的尺寸,由X,Y組成,2 2 表示2像素寬,2像素高。(自MapServer 6開始,LABEL STYLE 被 GEOMTRANSFORM LABELPOLY 所替代。其實mapfile中已經更新,只是官網上面的說明沒有更新。)
其實看到此處,以及對照官網上面的相關文檔,我又不少疑問。首先查看幾處資料:
1.https://www.mapserver.org/mapfile/geomtransform.html#labels-label-style-only
其中「Labels (LABEL STYLE only)」中寫道以下:
The following simple geometry transformations are available at the LABEL STYLE level: #在LABEL標籤的STYLE標籤中,使用GEOMTRANSFORM有兩種類型,分別爲:labelpnt 和 labelpoly labelpnt labelpoly
These are used for label styling (background colour, background shadow, background frame). #他們是爲label的背景顏色,背景陰影,背景框架作樣式設置的。
Note The result of using labelpnt is affected by the LAYER LABELCACHE setting. If LABELCACHE is ON (the default), the label will be shifted when a non-zero sized symbol is added using labelpnt.
labelpnt and labelpoly
GEOMTRANSFORM 「labelpnt」
GEOMTRANSFORM 「labelpoly」
Note Only available for STYLE in the LABEL context.
These transformations can be used to make background rectangles for labels and add symbols to the label points.
Class definitions for the example:
CLASS STYLE OUTLINECOLOR 255 255 204 END # STYLE LABEL SIZE giant POSITION UC
#好了,第一個的疑問就在此處,一樣 LABEL 中 GEOMTRANSFORM 的值均爲"labelpoly",是如何判斷當前 設置的爲:陰影、背景顏色、仍是邊線呢?
#設置shadow難道是由於style標籤中有 OFFSET 標籤嗎?
#設置 backgground 是由於只有COLOR,沒有其餘標籤嗎?
#設置outline是由於有OUTLINECOLOR標籤,和WIDTH標籤嗎?
#我實在是有點費解。
STYLE # shadow GEOMTRANSFORM "labelpoly" COLOR 153 153 153 OFFSET 3 3 END # Style STYLE # background GEOMTRANSFORM "labelpoly" COLOR 204 255 204 END # Style STYLE # outline GEOMTRANSFORM "labelpoly" OUTLINECOLOR 0 0 255 WIDTH 1 END # Style STYLE GEOMTRANSFORM "labelpnt" SYMBOL 'circlef' #此處是自定義符號,設置紅色圓點的。 COLOR 255 0 0 SIZE 15 END # Style END # Label END # Class
Symbol definition for the circlef symbol:
SYMBOL NAME "circlef" TYPE ellipse FILLED true POINTS 1 1 END # POINTS END # SYMBOL
2.https://www.mapserver.org/mapfile/style.html 中對GEOMTRANSFORM有段定義
GEOMTRANSFORM [bbox|centroid|end|labelpnt|labelpoly|start|vertices|<expression>]
Used to indicate that the current feature will be transformed before the actual style is applied. Introduced in version 5.4.
Note Depends on GEOS.
Example (polygon data set) - draw a two pixel wide line 5 pixels inside the boundary of the polygon:
STYLE OUTLINECOLOR 255 0 0 WIDTH 2 GEOMTRANSFORM (buffer([shape],-5)) END
There is a difference between STYLE and LAYER GEOMTRANSFORM. LAYER-level will receive ground coordinates (meters, degrees, etc) and STYLE-level will receive pixel coordinates. The argument to methods such as simplify() must be in the same units as the coordinates of the shapes at that point of the rendering workflow, i.e. pixels at the STYLE-level and in ground units at the LAYER-level.
LAYER NAME "my_layer" TYPE LINE STATUS DEFAULT DATA "lines.shp" GEOMTRANSFORM (simplify([shape], 10)) ## 10 ground units CLASS STYLE GEOMTRANSFORM (buffer([shape], 5) ## 5 pixels WIDTH 2 COLOR 255 0 0 END END END
3.https://www.mapserver.org/mapfile/label.html 也有一段對STYLE的使用定義
STYLE
The start of a STYLE object.
Label specific mechanisms of the STYLE object are the GEOMTRANSFORM options: #此處也說明了,在STYLE對象中使用 GEOMTRANSFORM ,固然,前提是基於 LABEL 的使用 STYLE
GEOMTRANSFORM [labelpnt|labelpoly]
Creates a geometry that can be used for styling the label. Does not apply to ANGLE FOLLOW labels.
The resulting geometries can be styled using the mechanisms available in the STYLE object.
Example - draw a red background rectangle for the labels (i.e. billboard) with a 「shadow」 in gray:
STYLE GEOMTRANSFORM 'labelpoly' COLOR 153 153 153 OFFSET 3 2 END # STYLE STYLE GEOMTRANSFORM 'labelpoly' COLOR 255 0 0 END # STYLE
New in version 6.0.
TYPE
指定LABEL使用的字體類型。TRUETYPE 或 BITMAP。
FONT
指定使用哪一種字體。若是指定TYPE爲TRUETYPE,你須要指定使用哪一種字體。
SIZE
指定字體的大小,TYPE爲TRUETYPE時,使用像素;TYPE爲BITMAP時,使用如:small、large。
ANTIALIAS
truetype類型字體的抗鋸齒開關,值爲:TRUE或FALSE。
POSITION
label標籤中文字在label標籤中的位置。其值由垂直位置和水平位置組成。
垂直位置的值由三個,分別爲:C(中間,center)、U(上面,upper)、L(下面,lower)。
水平位置的值由三個,分別爲:C(中間,center)、L(左邊,left)、R(右邊、right)。
缺省值:AUTO
PARTIALS
告訴MapServer是否生成完整的標籤文本(我的認爲是,若是標籤中文本長度過程,則缺省顯示),值爲:TRUE或FALSE。
MINDISTANCE
設置重疊標籤的最小距離,像素。(我的認爲是按照兩個標籤的開始位置比對計算。)
BUFFER
設置標籤填充外邊距(像素)。用於加強可讀性。(其實若是會前端CSS的應該比較容易理解padding,簡單理解就是表示兩個盒子之間的空白邊距。)
4、後記
在作這個例子的過程當中,遇到了一些還沒有理解的位置。LABEL中的STYLE 下面 的 GEOMTRANSFORM 重點標記一下。
而後學會了更多的用命令查詢shp數據。 -ro:只讀打開文件。-al:列出全部數據。-rl:將全部數據重點關鍵數據進行展現。-where:像查詢語句同樣,根據條件過濾展現數據。