Mapserver WMS

While I have been working with Google map api, a lots tutorial over the web can be found. But unfortunately, Map server developer are only working but not willing to anything for new developer. May be they think it is very simple, why you need more than the mapserver documentation? But true is that it is very difficult to new developer to run their first hello world program. So, very first time in my blog I have started to write tutorial on map server related issue.

The main objective of this writing is that a simple wms map repository will be created using Osgeo Mapserver and view the map with openlayer. In this example I have used three layers Bangladesh district boundary, Main Road and District head quarter location. The first two layer map source is ESRI Shape file and third one is postgis. For all the layer I have used wgs84(EPSG:4326) projection system. In my another post I wrote on mapserver projection system, click here to check article.

Map file Creation with WMS support:
I am using osgeo mapserver in windows OS. Below definition have been added  for top level in mapfile,

NAME BdDistMap
STATUS ON
SIZE 500 550

# Extent based on full extent of QGIS view
EXTENT 86.7316 20.5896 93.9623 26.6341
UNITS dd
IMAGECOLOR 225 225 255
IMAGETYPE png
FONTSET "fontlist.txt"

Now the web section is very important for wms. It requires to add meta data information to give WMS support. This example I have added minimum wms definition which are all most mandatory.

WEB
 IMAGEPATH "/OSGeo4W/tmp/tmp_img/"
 IMAGEURL "/tmp/tmp_img/"
 HEADER "query_header.html"
 FOOTER "query_footer.html"
 METADATA
    "wms_title" "WMS Demo Server"
    "wms_onlineresource" "http://localhost:8080/cgi-bin/mapserv.exe?map=C:\OSGeo4W\apache\htdocs\openlayer\sampleapps\distGeo.map"
 "wms_srs" "EPSG:4326"
"wms_feature_info_mime_type" "text/html"
 END
END

In metadata section, wms_title, wms_srs are mendatory. Wms_srs is the projection system definition. wms_feature_info_mime_type is used to GetFeatureInfo request output format.

I have used map lavel projection system for all layers. Below is definition,

PROJECTION
"init=epsg:4326"
END

Now in layer definition, I have added meta info tag again. Below is the definition,

LAYER
  NAME dist_geo
  PROJECTION
  	"init=epsg:4326"
  END
  HEADER "hq_query_header.html"
  TEMPLATE "hq_query_body.html"

  METADATA
    "wms_title" "distwms"
  END
  TYPE POLYGON
  STATUS OFF
  DATA "C:/OSGeo4W/apache/htdocs/openlayer/sampleapps/shp/dist_GEO.shp"
    CLASS
      NAME "State Boundary"
      STYLE
        COLOR  150 123 90
        OUTLINECOLOR 229 210 191
      END
    END
END

Here I add projection parameter again. If you want use map level projection, this section can be removing. This include Bangladesh administrator boundary with wms support. The data source is shape file in wgs84 projection.

Below the complete working mapfile is given which is able response wms request.

MAP
NAME BdDistMap
STATUS ON
SIZE 500 550

# Extent based on full extent of QGIS view
EXTENT 86.7316 20.5896 93.9623 26.6341
UNITS dd
IMAGECOLOR 225 225 255
IMAGETYPE png
FONTSET "fontlist.txt"

WEB
 IMAGEPATH "/OSGeo4W/tmp/tmp_img/"
 IMAGEURL "/tmp/tmp_img/"
 HEADER "query_header.html"
 FOOTER "query_footer.html"
 METADATA
    "wms_title" "WMS Demo Server"
    "wms_onlineresource" "http://localhost:8080/cgi-bin/mapserv.exe?map=C:\OSGeo4W\apache\htdocs\openlayer\sampleapps\distGeo.map"    "wms_srs" "EPSG:4326"
	"wms_feature_info_mime_type" "text/html"
 END
END

QUERYMAP
  STATUS ON
  SIZE 500 550
  STYLE HILITE
  COLOR 255 255 0
END

SYMBOL
   NAME "Circle"
   FILLED true
   TYPE ellipse
   POINTS 5 5 END
END

PROJECTION
  "init=epsg:4326"
END

LAYER
  NAME dist_geo
  PROJECTION
  	"init=epsg:4326"
  END
  HEADER "hq_query_header.html"
  TEMPLATE "hq_query_body.html"

  METADATA
    "wms_title" "distwms"
  END
  TYPE POLYGON
  STATUS OFF
  DATA "C:/OSGeo4W/apache/htdocs/openlayer/sampleapps/shp/dist_GEO.shp"
    CLASS
      NAME "State Boundary"
      STYLE
        COLOR  150 123 90
        OUTLINECOLOR 229 210 191
      END
    END
END

LAYER
  NAME road_geo
  HEADER "hq_query_header.html"
  TEMPLATE "hq_query_body.html"
  PROJECTION
  	"init=epsg:4326"
  END
  METADATA
    "wms_title"	"Cities"
  END
  TYPE LINE
  STATUS OFF
  DATA "C:/OSGeo4W/apache/htdocs/openlayer/sampleapps/shp/rds_nr_geo.shp"
    CLASS
      NAME "State Road"
      STYLE
        COLOR 32 32 32
      END
    END
END

LAYER
  NAME dist_hq_geo
  # for HTML queries
  HEADER "hq_query_header.html"
  TEMPLATE "hq_query_body.html"

  PROJECTION
  	"init=epsg:4326"
  END

  METADATA
    "wms_title" "DistHQ"
  END
  TYPE POINT
  STATUS OFF
  CONNECTION "user=postgres password=cegis dbname=postgis host=localhost port=5432"
  CONNECTIONTYPE postgis
  DATA "the_geom from dist_hq_geo USING UNIQUE gid"
  LABELITEM "distname"
  CLASS
	NAME "dist_hq_geo"
	COLOR 200 255 0
	SYMBOL "Circle"
	  LABEL
		COLOR 132 31 31
		SHADOWCOLOR 218 218 218
		SHADOWSIZE 2 2
		TYPE TRUETYPE
		FONT arial
		SIZE 11
		ANTIALIAS TRUE
		POSITION CL
		PARTIALS FALSE
		MINDISTANCE 300
		BUFFER 4
	  END # end of label
	  STYLE
		OPACITY 100
	  END
  END
END

END # Map File

Call mapserver WMS Layer:
Now the important part, how we can test this repository? Write below address in your web browser,
http://localhost:8080/cgi-bin/mapserv.exe?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetCapabilities&map=C:OSGeo4WapachehtdocsopenlayersampleappsdistGeo.map

This will return respose wms request if everything is okay. And also write below address for testing map image generation of mapserver,

http://localhost:8080/cgi-bin/mapserv.exe?mode=map&layer=dist_geo&layer=road_geo&map=C:OSGeo4WapachehtdocsopenlayersampleappsdistGeo.map

In another article, I wrote how to add mapserver wms layer using openlayer.
Click here

For more information about wms layer, please check below links,
here
here

……………………………..
Abul Khayer
GIS programmer
CEGIS
Bangladesh