OpenLayers 2 WMS – Using WMSGetFeatureInfo on Mapserver Layer

getfeatureinfomapserveropenlayers-2wms

I'm able to use OpenLayers' OpenLayers.Layer.Mapserver declaration to generate a raster from GRIB data. I've read in the MapServer docs and on forums that it should be possible to get point information back from the raster using OL's WMSGetFeatureInfo. I've tried a few combinations of others' reported solutions. However, as the control is for a regular WMS and also nearly all of the solutions involve GeoServer, I've pretty much reached a wall. So this is my code:

<script type="text/javascript">

var map, info, wmsmap;


function init() {

    map = new OpenLayers.Map("map", {

        projection: new OpenLayers.Projection("EPSG:3857"),
        units: "m",
        maxResolution: 156543.0339,
        maxExtent: new OpenLayers.Bounds(
            -20037508, -20037508, 20037508, 20037508.34),
        displayProjection: new OpenLayers.Projection("EPSG:4326"),
        controls: [
            new OpenLayers.Control.Navigation(),
            new OpenLayers.Control.KeyboardDefaults(),
            new OpenLayers.Control.PanZoomBar(),
            new OpenLayers.Control.Scale(),
            new OpenLayers.Control.Attribution()
        ]

    });

    var osmLayer = new OpenLayers.Layer.OSM("OpenStreetMap");

// Test MapServer WMS layer

    wmsmap = new OpenLayers.Layer.MapServer( "grib plot", "/cgi-bin/mapserv", 
            {mode: "map",
             map: "wxmap/glmpvis.map", layers: "mosaic", format:'image/png',
         transparent:true, reaspect: "true"},

            {singleTile: true, ratio: 1, isBaseLayer: false, opacity: 1, visibility: true,
             projection: new OpenLayers.Projection("EPSG:3857")});

// add layers to map
   map.addLayers([osmLayer, wmsmap]);

// add controls

   map.addControl(new OpenLayers.Control.LayerSwitcher());
   map.addControl(new OpenLayers.Control.Permalink());

   map.addControl(new OpenLayers.Control.MousePosition(
       {div: document.getElementById('ex_llmousectl'),
        id: "ll_mouse", formatOutput: formatLonlats} ));
   map.addControl(new OpenLayers.Control.MousePosition( 
       {div: document.getElementById('utm_mousectl'),
         id: "utm_mouse", prefix: "UTM ", displayProjection: map.baseLayer.projection, numDigits: 0} ));
   map.setCenter(new OpenLayers.LonLat(-117, 33).transform(
        new OpenLayers.Projection("EPSG:4326"),
        map.getProjectionObject()),6);


   info =  new OpenLayers.Control.WMSGetFeatureInfo({

                     url: "/cgi-bin/mapserv",
                     maxFeatures: 1,
                     title: 'Identify features by clicking',
                     queryVisible: true,
                     layers: [wmsmap],
                     vendorParams: {

                         map: "wxmap/glmpvis.map",

                     },
                     eventListeners: {
                         getfeatureinfo: function(event) {
                             map.addPopup(new OpenLayers.Popup.FramedCloud(
                                     "chicken",
                                     map.getLonLatFromPixel(event.xy),
                                     null,
                                     event.text,
                                     null,
                                     true
                              )); //map.addPopup


                         } // getfeatureinfo
                     } // eventListeners

   });

   map.addControl(info);
   info.activate();

   function formatLonlats(lonLat) {
        var lat = lonLat.lat;
        var long = lonLat.lon;
        var ns = OpenLayers.Util.getFormattedLonLat(lat);
        var ew = OpenLayers.Util.getFormattedLonLat(long,'lon');
        return ns + ', ' + ew + ' (' + (Math.round(lat * 100) / 100) + ', ' + (Math.round(long * 100) / 100) + ')';
    }


} // end of init

</script>

And my mapfile (some url and file locations changed for privacy, but not necessary for this question)

MAP
  NAME "testgrib"
  IMAGETYPE      PNG
  EXTENT         -14000000 3000000 -7000000 7000000
  STATUS         ON
  SIZE           2145 1377

  SHAPEPATH      "../shapefiles"
  SYMBOLSET      "../symbols/symbols35.sym"
  FONTSET        "../fonts/fonts.list"
  DATAPATTERN "^.*$"
  IMAGECOLOR     255 255 255

  PROJECTION
   "init=epsg:3857"
  END
  WEB
    IMAGEPATH "test/img/tmp/ms_tmp/"
    IMAGEURL "http://localhost:8080/wxmap/test/img/tmp/ms_tmp/"
    METADATA
      "wms_title"                  "WMS Test " ## REQUIRED
      "wms_onlineresource"         "http://localhost:8080/cgi-bin/mapserv?" ## Recommended
      "wms_srs"                    "ESPG:3857 EPSG:4326 EPSG:4269 EPSG:3978 EPSG:900913" ## Recommended
      "wms_abstract"               "This text describes my WMS service." ## Recommended
      "wms_enable_request" "*"
      "ows_sld_enable" "true"
    # testing 
      "wms_feature_info_mime_type" "text/html"
    END
  END

  OUTPUTFORMAT
    NAME "png"
    DRIVER GD/PNG
    MIMETYPE "image/png"
    IMAGEMODE RGBA
    EXTENSION "png"
    TRANSPARENT ON
  END


  LAYER # GRIB attempt
    NAME         mosaic
    STATUS       ON
    TYPE         RASTER

    DATA         ../grib/ds.vis.bin

    CLASSITEM "[pixel]"
    METADATA
     "gml_include_items" "all"
     "wms_include_items" "all"
    END

# testing 
    DUMP TRUE
    HEADER  "../templates/test_header.html"
    TEMPLATE "../templates/test_body.html"
    FOOTER "../templates/test_footer.html"

    PROJECTION
     "proj=lcc"
     "lat_1=25" 
     "lat_2=25"
     "lat_0=0" 
     "lon_0=-95" 
     "x_0=0"
     "y_0=0"
     "a=6371200"
     "es=0.0"
     "+no_defs"
    END

    PROCESSING "NODATA=32129"
    PROCESSING "BANDS=01"
    PROCESSING "SCALE=0,11200"
    LABELITEM "[pixel]"

    CLASS
    NAME "< 1/4"
    EXPRESSION ([pixel] < 400 )
    STYLE
        COLOR 100 0 100
    END
    END
    CLASS
    NAME "1/4 - 1/2"
    EXPRESSION ([pixel] >= 400 AND [pixel] < 800 )
    STYLE
        COLOR 200 200 0
    END
    END
    CLASS
    NAME "1/2 - 3/4"
    EXPRESSION ([pixel] >= 800 AND [pixel] < 1200 )
    STYLE
        COLOR 150 0 0
    END
    END
    CLASS
    NAME "3/4 - 1"
    EXPRESSION ([pixel] >= 1200 AND [pixel] < 1600 )
    STYLE
        COLOR 200 0 200
    END
    END
    CLASS
    NAME "1 - 2"
    EXPRESSION ([pixel] >= 1600 AND [pixel] < 3200 )
    STYLE
        COLOR 100 100 0
    END
    END
    CLASS
    NAME "2 - 3"
    EXPRESSION ([pixel] >= 3200 AND [pixel] < 4800 )
    STYLE
        COLOR 255 153 0
    END
    END
    CLASS
    NAME "3 - 5"
    EXPRESSION ([pixel] >= 4800 AND [pixel] < 8000 )
    STYLE
        COLOR 0 0 200
    END
    END
    CLASS
    NAME "5 - 6"
    EXPRESSION ([pixel] >= 8000 AND [pixel] < 9600 )
    STYLE
        COLOR 0 100 0
    END
    END
    CLASS
    NAME "6 - 7"
    EXPRESSION ([pixel]  >= 9600 AND [pixel] < 11200 )
    STYLE
        COLOR 0 200 0
    END
    END
    CLASS
    NAME "> 7"
    EXPRESSION ([pixel] >= 11200 )
    STYLE
        COLOR 250 250 250
    END
    END

  END # End of mosaic layer

END 

And I need template files here for the response:
test_header.html

<!-- MapServer Template -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/transitional.dtd">
<html>
  <head>
    <title>MapServer Template Sample</title>
  </head>

test_body.html

<body>
  band1: [value_0]
</body>

test_footer.html

</html>

I'm not positive that the test_body.html is correct. But, the main issue is that when I click on the image, nothing happens. Firebug doesn't indicate any query is sent to MapServer for the layer. Inserted a breakpoint in the WMSGetFeatureInfo code at the getfeatureinfo function but the execution didn't go into the code.

As I realize that perhaps the issue centered around not declaring the layer as OpenLayers.Layer.WMS instead of MapServer, I also attempted to create the layer using OpenLayers.Layer.WMS. But, that created issues of its own. Kind of a toss up then which question to ask. Might ask the other one later. So, if anyone has any thoughts on how to use this with the Mapserver layer or perhaps a different control, I'd really appreciate the help. Thanks!

Best Answer

I got maps like yours working and the only difference that I could spot is that I use OpenLayers.Layer.WMS instead of OpenLayers.Layer.MapServer.

Since GetFeatureInfo is a WMS protocol operation OL correctly should not apply it to Mapserver layers (even though Mapserver is fully capable of speaking WMS). Try to create the layer as follows:

var wmsmap = new OpenLayers.Layer.WMS( "grib plot",
 "/cgi-bin/mapserv.exe?map=wxmap/glmpvis.map",
     {layers: 'mosaic',
      srs: 'YOUR SRS HERE',
      format: 'image/png',
      isBaseLayer: false,
      visibility: true
     }
);