liujiusheng / blog

个人博客,blog
19 stars 0 forks source link

Mapserver一种比Geoserver更好用的GIS引擎(一、技术方案探索) #183

Open liujiusheng opened 4 years ago

liujiusheng commented 4 years ago

近日有心对MapServer和GeoServer做一次对比,讲真,如果要想做二次开发一定是基于MapServer,如果只是简单用一下,首选肯定得是GeoServer。MapServer是基于C语言的,而且是CGI模式,很好用其它语言包装(Wrapper)。

Mapserver为什么不被大众熟知,主要还是因为它没有可视化的操作界面,其官网使用和阅读起来也很困难。

所以后续我将投入精力基于Mapserver来构建一套GIS服务引擎,当然我可能会收费,但是价格将会极低。

参考信息:

Mapserver官网

MS4W版本:4.0.5

ms4w下载地址

(Mapserver编译过程较复杂,本次将不介绍编译过程,直接采用已编译好的ms4w内的相关包,直接下载zip Archive,里面带有数据。MS4W是目前唯一一个Mapserver在Windows上维护得还可以用的已编译好版本,主要基于Apache进行CGI的实现,另外还集成了GDAL、proj等GIS专业库)

ms4w_4.0.5.zip解压后文件结构: image

实验环境:windows10

Node.js版本:10.17.0

Node.js框架:EGG2.2.1

研究过程:

晚上研究到1.30,眼睛都快瞎了,终于找到了问题的关键。

一些关键点:

解压ms4w_4.0.5.zip

复制ms4w\Apache\cgi-bin文件夹内容到自己需要的地方

复制ms4w\apps\local-demo文件夹内容到自己需要的地方

修改local-demo/local.map的内容如下(主要是注释掉一些会报错的东西,如:SYMBOLSET、FONTSET、PROJECTION、IMAGEPATH、IMAGEURL、FONT等),:

/*
 Description:  Simple map to display on MS4W localhost ( http://127.0.0.1 )
               Also configured for WMS service, and includes GeoJSON output
 Data source:  NaturalEarth dataset, in SpatiaLite format.
 Other notes:  Open this mapfile in Notepad++, and use the color syntax file
               from https://ms4w.com/trac/wiki/Notepad++MapServerStyle
 Author:       Jeff McKenna, GatewayGeo, info@gatewaygeomatics.com
 Last updated: 2020-11-24
*/

MAP
NAME "local-demo"
STATUS ON
SIZE 600 400
# SYMBOLSET "../etc/symbols.txt"
EXTENT -180 -90 180 90
UNITS DD
SHAPEPATH "./data"
IMAGECOLOR 255 255 255
# FONTSET "../etc/fonts.txt"
# PROJECTION
#   "init=epsg:4326"
# END  

WEB
  IMAGEPATH "/temp/"
  IMAGEURL "/temp/"
  METADATA
    "wms_title"             "MS4W Demo WMS Server"
    "wms_abstract"          "This demonstration server was setup by GatewayGeo (https://gatewaygeomatics.com/) and is powered by MS4W (https://ms4w.com/)."      
    "wms_onlineresource"    "http://127.0.0.1/cgi-bin/mapserv.exe?map=/ms4w/apps/local-demo/local.map"
    "wms_service_onlineresource" "https://gatewaygeomatics.com/"    
    "wms_contactperson"     "Jeff McKenna" 
    "wms_contactorganization" "GatewayGeo"
    "wms_contactposition"   "President"
    "wms_contactelectronicmailaddress" "info@gatewaygeomatics.com"
    "wms_srs"               "EPSG:4326 EPSG:3857 EPSG:4269"
    "wms_getfeatureinfo_formatlist" "text/plain,text/html,application/json,application/vnd.ogc.gml,gml"
    "wms_enable_request"    "*"
  END
END

OUTPUTFORMAT
  NAME "png"
  DRIVER AGG/PNG
  MIMETYPE "image/png"
  IMAGEMODE RGB
  EXTENSION "png"
  FORMATOPTION "GAMMA=0.75"
END

OUTPUTFORMAT
  NAME "application/json"
  DRIVER "OGR/GEOJSON"
  MIMETYPE "application/json"
  FORMATOPTION "FORM=SIMPLE"
  FORMATOPTION "STORAGE=memory"
END

/* Ocean */
LAYER
  NAME "ocean"
  METADATA
    "wms_title" "Ocean"
    "wms_include_items" "all"
    "gml_include_items" "all"
    "gml_featureid"     "ogc_fid"   
  END
  TYPE POLYGON
  STATUS ON
  CONNECTIONTYPE OGR
  CONNECTION "demo.db"
  DATA "ocean"
  # PROJECTION
  #   "init=epsg:4326"
  # END 
  CLASS
    NAME "Ocean"
    STYLE
      COLOR 134 204 249
    END
  END
  TEMPLATE "ttt.html"
END # layer

/* Countries */
LAYER
  NAME "countries"
  METADATA
    "wms_title"         "World Countries"
    "wms_include_items" "all"
    "gml_include_items" "all"
    "gml_featureid"     "ogc_fid"
  END
  TYPE POLYGON
  STATUS ON
  CONNECTIONTYPE OGR
  CONNECTION "demo.db"
  DATA "countries"
  # PROJECTION
  #   "init=epsg:4326"
  # END
  LABELITEM "name"
  CLASS
    NAME "World Countries"
    STYLE
      COLOR 200 200 200
      OUTLINECOLOR 0 0 0
      WIDTH 0.1
    END
    LABEL
      # FONT sans
      TYPE truetype
      POSITION AUTO     
      PARTIALS FALSE
      BUFFER 5
      SIZE 5 
      COLOR 75 75 75
      MINFEATURESIZE 10
    END     
  END
  TEMPLATE "ttt.html"  
END # layer

/* Lakes */
LAYER
  NAME "lakes"
  METADATA
    "wms_title" "World Lakes"
    "wms_include_items" "all"
    "gml_include_items" "all"
    "gml_featureid"     "ogc_fid"   
  END
  TYPE POLYGON
  STATUS ON
  CONNECTIONTYPE OGR
  CONNECTION "demo.db"
  DATA "lakes"
  # PROJECTION
  #   "init=epsg:4326"
  # END 
  CLASS
    NAME "Lakes"
    STYLE
      COLOR 153 204 255
    END
  END
  TEMPLATE "ttt.html"  
END # layer

/* Ocean labels */
LAYER
  NAME "ocean-labels"
  METADATA
    "wms_title" "Ocean Labels"
    "wms_include_items" "all"
    "gml_include_items" "all"
    "gml_featureid"     "ogc_fid"   
  END
  TYPE POLYGON
  STATUS ON
  CONNECTIONTYPE OGR
  CONNECTION "demo.db"
  DATA "marine_region_poly"
  # PROJECTION
  #   "init=epsg:4326"
  # END
  LABELITEM "name"
  CLASS
    NAME "Ocean Labels"
    LABEL
      COLOR  75 75 75
      # FONT sans-italic
      TYPE truetype
      SIZE 5
      POSITION AUTO      
      PARTIALS FALSE
      BUFFER 5
    END
    #MAXSCALEDENOM 50000000
  END
  TEMPLATE "ttt.html"  
END # layer

/* Places */
LAYER
  NAME "places"
  METADATA
    "wms_title" "Populated Places"
    "wms_include_items" "all"
    "gml_include_items" "all"
    "gml_featureid"     "ogc_fid"   
  END
  TYPE POINT
  STATUS ON
  CONNECTIONTYPE OGR
  CONNECTION "demo.db"
  DATA "places"
  # PROJECTION
  #   "init=epsg:4326"
  # END
  LABELITEM "name"
  CLASS
    NAME "Populated Places"
    SYMBOL "square"
    SIZE 4
    COLOR 150 150 150
    LABEL
      COLOR  75 75 75
      # FONT sans
      TYPE truetype
      SIZE 5
      POSITION AUTO      
      PARTIALS FALSE
      BUFFER 5
    END
    MAXSCALEDENOM 50000000
  END
  TEMPLATE "ttt.html"  
END # layer

END # Map File

获取示例url请求参数:

启动ms4w,浏览器打开:http://localhost/

打开F12,拖动默认地图,获取到network里面的请求地址:

http://localhost/cgi-bin/mapserv.exe?map=/ms4w/apps/local-demo/local.map&layers=ocean%20countries%20lakes%20ocean-labels%20places&mode=map&map_imagetype=png&mapext=-85.980848625+-78.768824625+94.319751375+41.431575375&imgext=-85.980848625+-78.768824625+94.319751375+41.431575375&map_size=600+400&imgx=300&imgy=200&imgxy=600+400

image

如果需要让坐标系转换不报错修改如下配置,FONT字体同理

配置坐标系转换参数(好像忘记这个配置是自己在.map文件里自己加还是在其它地方改了,估计是自己加吧)

CONFIG "PROJ_LIB" "D:\software\nginx-1.13.4\proj\nad"

修改请求参数,并用本地命令行执行输出到本地文件(要使用cmd来执行,不能用powershell执行,原因未知),命令如下:

.\mapserv.exe -nh QUERY_STRING="map=../mapconfig/local-demo/local.map&layers=ocean%20countries%20lakes%20ocean-labels%20places&mode=map&map_imagetype=png&mapext=75.2755005+24.56595675+120.3506505+54.61605675&imgext=75.2755005+24.56595675+120.3506505+54.61605675&map_size=600+400&imgx=300&imgy=200&imgxy=600+400" >test.png

本地输出的时候要加上-nh ,防止http请求头被写入到文件中,导致文件读不出来。

epsg:4326要小写,它是大小写敏感的。

本地磁盘生成了test.png图片,并且可以打开,说明测试成功。

经对比发现mapserver在300万数据量情况下输出png文件的速度大约为mapserver的3倍。

怎么把它成体系地用起来呢,这是个大问题。