[GIS] OpenLayers WFS protocol: work with filters if the protocol is defined out of layer constructor

filteropenlayers-2requestwfs

follow question:wfs-load-feature-check

the scenario is: i am trying to load a WFS layer, since it takes a while to load all features, I want to give user a notice while waiting for response. the process employs spatial filter (BBOX strategy) and attribute filters. I am wondering whether there is a good way to make WFS protocol work with these filters, in other words, only request the filtered features.

thanks a lot!

code sample:

 var wfsLayer = new OpenLayers.Layer.Vector("WFS", {
        strategies: [new OpenLayers.Strategy.BBOX()],
        styleMap: new OpenLayers.StyleMap({
            strokeWidth: 3,
            strokeColor: "#333333"
        }) 
    });

var filt = new OpenLayers.Filter.Logical({
    type: OpenLayers.Filter.Logical.OR,
    filters: [
        new OpenLayers.Filter.Comparison({
            type: OpenLayers.Filter.Comparison.EQUAL_TO,
            property: "TYPE",
            value: "highway"
        }),
        new OpenLayers.Filter.Comparison({
            type: OpenLayers.Filter.Comparison.EQUAL_TO,
            property: "TYPE",
            value: "road"
        })
    ]
});

var prot =  new OpenLayers.Protocol.WFS({
    url:  "http://demo.opengeo.org/geoserver/wfs",
    featureType: "tasmania_roads",
    featureNS: "http://www.openplans.org/topp"
});

var _CallBack = function(resp) {
    if(resp.error) {
        console.log('error');
        return -1;
    }
    wfsLayer.addFeatures(resp.features);
    console.log('success');
    return 1;
};

var response = prot.read({
    callback: _CallBack
});

Best Answer

If your goal is to know WHEN all features have been added to your wfsLayer so you can hide your "please wait" message, then you could do as neil says and add it all in the vector layer constructor. Then just register the featuresadded event which will be triggered when the features have been added.

Example:

   wfsLayer.events.on({
       featuresadded: function(event){
           //hide loading notification
           alert("I'm done getting features");
       }
   });

The only down side is what if there is an error? how will or your user know without the callback that has this as a response?

From your previous question, it sounded like you were more interested in the errors it might throw. If this is the case, you can add the filter to your protocol constructor like so:

var prot = new OpenLayers.Protocol.WFS({
    url: "http://demo.opengeo.org/geoserver/wfs",
    featureType: "tasmania_roads",
    featureNS: "http://www.openplans.org/topp",
    defaultFilter: filt
});

Working Example (Update)

Here's the working example. Notice how as you pan and zoom the number of features returned are different. This is essentially the same as the strategies: [new OpenLayers.Strategy.BBOX()] but in this case i used map.events.register("moveend", map, QueryWFSService); to do the same thing. Viewsource to view the code.

enter image description here

Update #2

Working example #2 which does not trigger WFS request on zoom in.