1

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);

1

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".

|improve this answer|||||
  • Thanks for your advice. I changed the source code but the result stays the same. The feature.length is zero and firebug returns an error "TypeError: feature.geometry is undefined". There must be still one error. – steffen Jun 6 '14 at 13:10
  • There is not need to guess what is the name of the geometry attribute because you can ask it from WFS with requst http://server.org/geoserver/wfs?service=WFS&version=1.1.0&request=DescribeFeatureType&typeName=namespace:Gebaeude – user30184 Jun 6 '14 at 14:33
1

Finally got it to work. Changed the selectFeature funtion. For anyone who is interested in the solution:

function selectFeature(id) {

    var feature;
    for (var i = 0; i < wfsLayer.features.length; i++){
        gebnr = wfsLayer.features[i].attributes.Gebaeude_Nr;
        if (String(gebnr).localeCompare(String(id)) == 0)
            feature = wfsLayer.features[i];
    }

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

getFeaturesByAttribute() behaves strange to me. While feature = wfsLayer.getFeatureByFid("Gebaeude.6"); and feature = wfsLayer.getFeatureBy("fid","Gebaeude.6"); work, var feature = wfsLayer.getFeaturesByAttribute("Gebaeude_Nr", 03); doesn't. As the function parameter (id) does not represent the fid, those requests can not be used in my case. Maybe not the best solution but solved it without calling getFeaturesByAttribute() now.

Thanks for the answers.

|improve this answer|||||

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.