1

My OpenLayers script tries to get some features values from a WMS (which is a shapefile), to open a page in function of the attribute value returned.

My problem is the following: - the code returns a GET which contains the html code for displaying a table with each parameter names/values, and the value do really correspond to the clicked polygon. I can print the same values from event.text - event.features is empty!

Obviously that's not a proxy problem (since I get the values back). How can I fix the code to access the attributes values?

Here is the code:

var extent = new OpenLayers.Bounds(-180,-90,180,90);
var options = {restrictedExtent:extent};
var map = new OpenLayers.Map("map-id", options);
var imagery = new OpenLayers.Layer.WMS(
        "Global Imagery",
        "http://maps.opengeo.org/geowebcache/service/wms",
        {layers: "bluemarble", isBaseLayer:false}
    );

var lmes=new OpenLayers.Layer.WMS(
        "LMEs",
        "http://aaa.bbb.ccc.dd/geoserver/ocean/wms",
        {layers:'ocean:LME66', transparent:true, styles:'LMES_colors'},
        {isBaseLayer:false, opacity:1, singleTile:true, visibility:true}
    );
map.addLayers([lmes, imagery]);
map.zoomToMaxExtent();

function toggleRestrictedExtent() {
      if(map.restrictedExtent == null) {
        map.setOptions({restrictedExtent: extent});
      } else {
        map.setOptions({restrictedExtent: null});
      }
    }
    var info = new OpenLayers.Control.WMSGetFeatureInfo({
       url:'http://aaa.bbb.ccc.ddd/geoserver/ocean/wms',
       title:'identify feature by clicking',
       output:'features',

       eventListeners: {
        getfeatureinfo: function(event) {
          console.log(event);
          console.log(event.features);
          console.log(event.text);
        }
       }
    });

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

1 Answer 1

1

You need to set the infoFormat property of OpenLayers.Control.WMSGetFeatureInfo to something that OpenLayers can parse in order to create features. When no infoFormat property is set a default mime type of text/html is used, this is why event.text contains the HTML markup of a table.

Along with setting infoFormat you also need to set the format property which OpenLayers will use to parse the response from the server. So, in order to have an output of features you must:

  1. Know what response formats the target WMS server supports (you can get this from the response to a GetCapabilities request

  2. Set the infoFormat so OpenLayers sets the mime type to request

  3. Set the format property to an OpenLayers.Format that can be used to parse the response

Most WMS servers will support common formats like GML and GeoJSON which are both supported well by OpenLayers. As an example, if the target server supported GML then you could use the following code to create the WMSGetFeatureInfo control:

var info = new OpenLayers.Control.WMSGetFeatureInfo({
    url: 'http://[server_url_here]',
    output: 'features',
    infoFormat: 'application/gml+xml',
    format: new OpenLayers.Format.GML,

    eventListeners: {
        getfeatureinfo: function(event){
            console.log(event);
            console.log(event.features);
            console.log(event.text);
        }
    }
})

You should now see an array of features as well as the raw GML response from the server.

1
  • I'm using geoserver, so it support GML, and the layer is queryable. Oct 1, 2013 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.