[GIS] Reading response of GetFeature request with OpenLayers

fields-attributesgeoservergetfeatureopenlayers-2wfs

I am using OpenLayers to send a GetFeature request to GeoServer. With the help of Firebug I can see that the response contains some of the attribute data I need. I am interested in showing some attributes once the user clicked on a feature.

How can I make sure that GeoServer sends all attributes and how can I read them in OpenLayers?

Here is my code, for now I only succeeded at displaying the feature selected on my map using an OpenLayers example:

Feature = new OpenLayers.Layer.WMS(
        "Ramsar sites",
        "http://ramsardev.grid.unep.ch:8080/geoserver/wms",
        {
            layers:'ramsar_sdi:features',
            transparent:'true', 
            visibility: true,
            isBaseLayer: false
        });             
map.addLayer(Feature);

select = new OpenLayers.Layer.Vector("Selection", {styleMap: new OpenLayers.Style(OpenLayers.Feature.Vector.style["select"])});     
map.addLayer(select);

control = new OpenLayers.Control.GetFeature({
        protocol: OpenLayers.Protocol.WFS.fromWMSLayer(Feature),
        box: false,
        hover: false,
        multipleKey: "shiftKey",
        toggleKey: "ctrlKey"
    });

control.events.register("featureselected", this, function(e) {
        select.addFeatures([e.feature]);

    });

control.events.register("featureunselected", this, function(e) {
        select.removeFeatures([e.feature]);
    }); 

map.addControl(control);        
control.activate();     

The problem is that I can't see any of my attributes anywhere in the event object e when the event featureselected is triggered.

Best Answer

Actually you should be able to get the attribute value like this:

e.feature.attributes.Attribute1

But if it does not work you could try one of these options:
If I see correctly you have a wms layer which you convert to a wfs layer and you try to get the attributes... if this is correct, I think you should consider using GetFeatureInfo, which shows the whole attribute table of the clicked feature, based on the wms layer here is an example:
where the attributes show up in the html div "attr"

//support GetFeatureInfo 
 map.events.register('click', map, function (e) {
                    //document.getElementById('attr').innerHTML = "Loading... please wait...";
                    var params = {
                        REQUEST: "GetFeatureInfo",
                        EXCEPTIONS: "application/vnd.ogc.se_xml",
                        BBOX: map.getExtent().toBBOX(),
                        SERVICE: "WMS",
                        INFO_FORMAT: 'text/html',
                        QUERY_LAYERS: 'cite:layer1',
                        FEATURE_COUNT: 1, //this means that exactly one feature shows up in the attribute table. I think if you comment this line out then it will show all features
                        Layers: 'cite:layer1',
                        WIDTH: map.size.w,
                        HEIGHT: map.size.h,
                        format: 'image/png',
                        styles: map.layers[0].params.STYLES,
                        srs: map.layers[0].params.SRS};

                    // handle the wms 1.3 vs wms 1.1 madness
                    if(map.layers[0].params.VERSION == "1.3.0") {
                        params.version = "1.3.0";
                        params.j = parseInt(e.xy.x);
                        params.i = parseInt(e.xy.y);
                    } else {
                        params.version = "1.1.1";
                        params.x = parseInt(e.xy.x);
                        params.y = parseInt(e.xy.y);
                    }

                    // merge filters
                    if(map.layers[0].params.CQL_FILTER != null) {
                        params.cql_filter = map.layers[0].params.CQL_FILTER;
                    } 
                    if(map.layers[0].params.FILTER != null) {
                        params.filter = map.layers[0].params.FILTER;
                    }
                    if(map.layers[0].params.FEATUREID) {
                        params.featureid = map.layers[0].params.FEATUREID;
                    }
                    OpenLayers.loadURL("http://localhost:8080/geoserver/cite/wms", params, this, setHTML, setHTML);
                    OpenLayers.Event.stop(e);
                });

    // sets the HTML provided into the nodelist element

            function setHTML(response){
                document.getElementById('attr').innerHTML = response.responseText;
            };

Or if there is a reason you want to use the getfeature request (e.g dont want to get all of the attributes) then use a WFS request instead of wms (could be slow), like this:

 var wfs_layer = new OpenLayers.Layer.Vector("WFS Layer", {
 strategies: [new OpenLayers.Strategy.BBOX()],
 projection: WGS84,
 protocol: new OpenLayers.Protocol.WFS({
 version: "1.0.0",
 url: "http://localhost:8080/geoserver/wfs",
 featureNS: "http://www.opengeospatial.net/cite",
 maxExtent: mapextent,
 featureType: "layername",
 geometryName: "geom"
  })
  });

and then use a selectcontrol to get the attributes of the feature this way you can write it to a div or something