2
votes

I am serving up a wms layer from Geoserver in OpenLayers and am trying to extract a specific attribute from an object in this layer when I click on the map.

I am defining my wms layer like this:

oas = new OpenLayers.Layer.WMS(
"oas",
"http://www.bwgeoserver.com/geoserver/UK_Admin/wms",
    {
      "LAYERS": 'wealthmap_bdy',
      transparent: 'true',
      extractAttributes: 'true'
    },
    {
      buffer: 0,
      opacity: 1,
      displayOutsideMaxExtent: true,
      isBaseLayer: false
    }
    );
map.addLayer(oas);

The layer is displaying OK on the map. I am using a proxy server and there isn't a cross-domain problem as I can select and display all of the attributes in a popup. "OA11Code" is definitely a field on the wms layer, and it displays ok with the other attributes in the popup.

But when I try to extract this attribute from the selected object using this code:

oaPicker = new OpenLayers.Control.WMSGetFeatureInfo({
             url: 'http://www.bwgeoserver.com/geoserver/UK_Admin/wms',
             title: 'identify features on click',
             layers: [oas],
             queryVisible: true
         });

oaPicker.events.register('getfeatureinfo', this, selectid);
map.addControl(oaPicker);
oaPicker.activate();

function selectid(e) {
    var val = e.features[0].attributes.OA11Code;
}

I get an error in the Firefox debugger saying:

"TypeError: e.features[0] is undefined".

All I want is to be able to click on the map, extract the value of an attribute from the wms layer, assign it to a javascript variable and do something with it. Any help would be greatly appreciated.

1
Can you test the value of e in your selectid(e) function? Can to use browser debuggers to intercept the getfeatureinfo request to your geoserver and the response? Geoservers logs could provide some insight as well if these aren't conclusive.Jason Koopmans

1 Answers

0
votes

By default GeoServer will return plain text for a GetFeaureinfo request but parsing that can be a bit of a lottery (especially if any of the features could be null or contain special characters).

A better solution is to set the info_format parameter to application/json so that you get a JSON response like this that is easy to parse.