原文:http://blog.csdn.net/xuhaiyan8825/article/details/6926829
0. 檢查
檢查當前MS4W是否支持OGC WMS服務
使用DOS命令行進入MS4W的安裝路徑(如D:\ms4w\Apache\cgi-bin), 執行mapserv -v
C:\ms4w\Apache\cgi-bin>mapserv-v
MapServerversion 5.2.1 OUTPUT=GIF OUTPUT=PNG OUTPUT=JPEG OUTPUT=WBMP OUTPUT=PDF
OUTPUT=SWF OUTPUT=SVG SUPPORTS=PROJSUPPORTS=AGG SUPPORTS=FREETYPE SUPPORTS=ICO
NVSUPPORTS=FRIBIDI SUPPORTS=WMS_SERVER SUPPORTS=WMS_CLIENT SUPPORTS=WFS_SERVER
SUPPORTS=WFS_CLIENTSUPPORTS=WCS_SERVER SUPPORTS=SOS_SERVER SUPPORTS=FASTCGI SUP
PORTS=THREADSSUPPORTS=GEOS SUPPORTS=RGBA_PNG INPUT=JPEG INPUT=POSTGIS INPUT=OGR
INPUT=GDAL INPUT=SHAPEFILE
1. 安裝qgis
下載osgeo4w-setup.exe 最新版,直接運行,選擇桌面GIS即可
安裝完成後如下
2. 運行QGIS,生成*.map文件
在插件選項中,添加 mapserver export 插件
打開幾個shp圖層,右擊每個圖層,在屬性中設置使用oldsymbology,否則不能導出到mapserver
選了3個圖層,生成的map文件如下:
# Map file created from QGIS project file I:/GIS/freeGIS/QGIS/chianmap_final.qgs
# Edit this file to customize for your map interface
# (Created with PyQgis MapServer Export plugin)
MAP
NAME"QGIS-MAP"
# Map image size
SIZE 1200 800
UNITS meters
EXTENT 49.189774-3.159989 173.282634 76.327090
FONTSET'fontset.txt'
SYMBOLSET'symbols.txt'
PROJECTION
'proj=longlat'
'ellps=WGS84'
'towgs84=0,0,0,0,0,0,0'
'no_defs'
END
# Backgroundcolor for the map canvas -- change as desired
IMAGECOLOR 255255 255
IMAGEQUALITY 95
IMAGETYPE agg
OUTPUTFORMAT
NAME agg
DRIVER AGG/PNG
IMAGEMODE RGB
END
# Legend
LEGEND
IMAGECOLOR255 255 255
STATUS ON
KEYSIZE 18 12
LABEL
TYPE BITMAP
SIZE MEDIUM
COLOR 0 0 89
END
END
# Web interfacedefinition. Only the template parameter
# is required todisplay a map. See MapServer documentation
WEB
# Set IMAGEPATHto the path where MapServer should
# write itsoutput.
IMAGEPATH'/tmp/'
# Set IMAGEURLto the url that points to IMAGEPATH
# as defined inyour web server configuration
IMAGEURL'/tmp/'
# WMS serversettings
METADATA
'ows_title' 'QGIS-MAP'
'ows_onlineresource' 'http://192.168.1.94:8080/cgi-bin/mapserv.exe?map=I:/GIS/freeGIS/QGIS/tt.map'
'ows_srs' 'EPSG:4326'
END
#Scale range atwhich web interface will operate
# Template andheader/footer settings
# Only thetemplate parameter is required to display a map. See MapServer documentation
TEMPLATE'fooOnlyForWMSGetFeatureInfo'
END
LAYER
NAME 'bou2_4p'
TYPE POLYGON
DUMP true
TEMPLATEfooOnlyForWMSGetFeatureInfo
EXTENT 49.189774-3.159989 173.282634 76.327090
DATA'I:\GIS\maps\chinamap_final\bou2_4p.shp'
METADATA
'ows_title' 'bou2_4p'
END
STATUS OFF
TRANSPARENCY100
PROJECTION
'proj=longlat'
'ellps=WGS84'
'towgs84=0,0,0,0,0,0,0'
'no_defs'
END
CLASS
NAME'bou2_4p'
STYLE
WIDTH 1
OUTLINECOLOR 0 0 0
COLOR 21091 200
END
END
END
LAYER
NAME'chinaMapL'
TYPE LINE
DUMP true
TEMPLATEfooOnlyForWMSGetFeatureInfo
EXTENT 49.189774-3.159989 173.282634 76.327090
DATA'I:\GIS\maps\chinamap_final\chinaMapL.shp'
METADATA
'ows_title''chinaMapL'
END
STATUS OFF
TRANSPARENCY100
PROJECTION
'proj=longlat'
'ellps=WGS84'
'towgs84=0,0,0,0,0,0,0'
'no_defs'
END
CLASS
NAME'chinaMapL'
STYLE
WIDTH 1
COLOR 182134 241
END
END
END
LAYER
NAME 'railway'
TYPE LINE
DUMP true
TEMPLATEfooOnlyForWMSGetFeatureInfo
EXTENT 49.189774-3.159989 173.282634 76.327090
DATA'I:\GIS\maps\chinamap_final\railway.shp'
METADATA
'ows_title''railway'
END
STATUS OFF
TRANSPARENCY100
PROJECTION
'proj=longlat'
'ellps=WGS84'
'towgs84=0,0,0,0,0,0,0'
'no_defs'
END
CLASS
NAME'railway'
STYLE
WIDTH 1
COLOR 13595 134
END
END
END
END
注意需要把裏面的WIDTH 改爲1,之前是0.91,否則會出錯
在apache目錄的htdocs下新建一map文件夾,將字體、symbols等文件
並且把map文件裏面的FONTSET 和SYMBOLSET修改如下:
FONTSET 'fontset.txt'
SYMBOLSET 'symbols.txt'
3. 用瀏覽器訪問地圖服務
4. 更改apache的設置
在\Apache\cgi-bin複製mapserv.exe一份,改名xmap.exe
打開ms4w安裝目錄\Apache\conf\httpd.conf編輯
最後一行添加:
#用於碰到url中有/cgi-bin/xmap.exe,就調用../htdocs/map/test.map作爲地圖
SetEnvIf Request_URI "/cgi-bin/xmap.exe"MS_MAPFILE=../htdocs/map/test.map
然後使用瀏覽器訪問
http://192.168.1.94:8080/cgi-bin/xmap.exe?LAYERS=ALL&MODE=MAP
結果如下:
5. 在openlayers 中調用
在調用自己的mapserver發佈的wms服務時,地圖的option中,單位需要與wms的單位一致,如 units : "dd",否則距離量算不準確。
最大最小分辨率決定了地圖在最小最大縮放時的大小
必須設置一個圖層爲基層圖層,其他圖層爲非基基礎圖層,並且透明度設置爲true,如下:
var layer1 = new OpenLayers.Layer.WMS("layer1", "http://localhost:8080/cgi-bin/xmap.exe?",{ layers: 'railway',transparent: "true",IMAGETYPE: 'PNG'}, { isBaseLayer: false});
var layer2 = new OpenLayers.Layer.WMS("layer2", "http://localhost:8080/cgi-bin/xmap.exe?",{ layers: 'chinaMapL',transparent: "true",IMAGETYPE: 'PNG'}, { isBaseLayer: false});
var layer3 = new OpenLayers.Layer.WMS("layer3", "http://localhost:8080/cgi-bin/xmap.exe?",{ layers: 'bou2_4p',IMAGETYPE: 'PNG'}, { isBaseLayer: true});
map.addLayers([layer1,layer2,layer3]);
如下:
6. Qgis中可以設置地圖顯示樣式,比如polygon對象的顏色分級,label標註字段、字體,圖層的顯示比例尺範圍等。
設置好以後,還是直接導出爲mapserver所需的*.map文件,注意修改裏面的
FONTSET 'fontset.txt'
SYMBOLSET 'symbols.txt'
以及WIDTH爲1.
就可以用了,實時渲染就是非常慢,本機都慢。
7. 下一步用mapserver發佈WFS。
add:
symbols.txt的內容如下,直接拷貝即可:
Symbol
Name 'tent'
Type VECTOR
Filled TRUE
Points
0 1
.5 0
1 1
.75 1
.5 .5
.25 1
0 1
END
END
Symbol
Name 'star'
Type VECTOR
Filled TRUE
Points
0 .375
.35 .375
.5 0
.65 .375
1 .375
.75 .625
.875 1
.5 .75
.125 1
.25 .625
END
END
Symbol
Name 'triangle'
Type VECTOR
Filled TRUE
Points
0 1
.5 0
1 1
0 1
END
END
Symbol
Name 'square'
Type VECTOR
Filled TRUE
Points
0 1
0 0
1 0
1 1
0 1
END
END
Symbol
Name 'plus'
Type VECTOR
Points
.5 0
.5 1
-99 -99
0 .5
1 .5
END
END
Symbol
Name 'cross'
Type VECTOR
Points
0 0
1 1
-99 -99
0 1
1 0
END
END
Symbol
Name 'circle'
Type ELLIPSE
Filled TRUE
Points
1 1
END
END
Symbol
Type VECTOR
TRANSPARENT 0
Points
0 1
1 1
END
END
Symbol
Type VECTOR
TRANSPARENT 0
Points
1 0
1 1
END
END
Symbol
Type VECTOR
TRANSPARENT 0
Points
0 0
1 1
END
END
Symbol
Type VECTOR
TRANSPARENT 0
Points
0 1
1 0
END
END
Symbol
Type VECTOR
Points
0 1
1 0
-99 -99
0 0
1 1
END
END
Symbol
Type ELLIPSE
Filled TRUE
Points
1 1
END
END
fontset.txt內容如下:
simhei Fonts\simhei.ttf
arial Fonts\arial.ttf
simfang Fonts\simfang.ttf
simkai Fonts\simkai.ttf
Fonts文件夾內容如下,你只拷貝fontset.txt中的字體文件進去即可: