[GIS] OpenLayers request a WFS feature

openlayers-2wfs

I am trying to request features of a wfs layer and then zoom to them. The wfs layer is defined as follows:

wfsLayer = new OpenLayers.Layer.Vector("University", {
      strategies : [new OpenLayers.Strategy.Fixed(), cluster_strategy],
             styleMap : new OpenLayers.StyleMap({
                 "default" : gebaeudeNrStyle,
                 "select" : gebaeudeNrStyle
             }),
      protocol : new OpenLayers.Protocol.WFS({
                 url : "/geoserver/wfs?",
                 featureType : "Gebaeude",
                 featureNS : "namespace",
                 geometryName : "the_geom",
                 srsName : "EPSG:4326",
                 version : "1.1.0"
      })
});

Here the selectFeature function, which requests features of the above wfs layer by an id and zooms to it.

function selectFeature(id) {

    var feature = wfsLayer.getFeaturesByAttribute("Gebaeude_Nr", 03);

    if (feature) {
         map.zoomToExtent(feature.geometry.getBounds());
    }
}

In this example, i'd like to zoom on the third id (Gebaeude_Nr), which inexplicably does not work. getFeaturesByAttribute() delivers an empty array. The feature.length is zero and firebug returns an error "TypeError: feature.geometry is undefined".
However, the underlying database table for the featureType "Gebaeude" contains a column "Gebaeude_Nr" and an entry "03".

enter image description here

How can i search for a WFS feature and zoom to it in OpenLayers?
Thanks in advance

Side note:

for (var i = 0; i < wfsLayer.features.length; i++){
    alert(wfsLayer.features[i].geometry);
}

works, which gives me outputs like POINT(895866.7304909064 6849316.098470407) for every feature. However, i haven't found out yet how to get all column names of the layer in order to compare the results with the database. This would allow to check if the above column "Gebaeude_Nr" exists for getFeaturesByAttribute("Gebaeude_Nr",03);

Best Answer

Try this. In your protocol change geometryName : "the_geom", --> geometryName : "geom",

That is you geometry column name in your postgis.

At one time postgis used "the_geom" for the geometry column name but in later versions it is "geom".