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) {
        return -1;
    return 1;

var response = prot.read({
    callback: _CallBack

2 Answers 2


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.


       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.

  • thank you again!! i tried this.. it seems the defaultFilter doesn't work very well from that test; more importantly, where to put spatial filter in? i.e., BBOX...
    – Simon
    Mar 16, 2012 at 21:21
  • I can wip up a working example by tuesday based off your code + the BBOX strategy using event. But basically you code looks fine. I think if there's a problem, it's small, probably with the filter structure. I place my filter in the defaultFilter and it works out fine, there are other ways though. And FYI, I'll be consuming a public random WFS service to use as an example. You should just need to modify the attributes.
    – CaptDragon
    Mar 17, 2012 at 4:08
  • a working example would be great! thanks a lot to you!!
    – Simon
    Mar 17, 2012 at 22:40
  • @Simon : Please view my latest update.
    – CaptDragon
    Mar 19, 2012 at 19:15
  • thanks a lot, it works! only one thing I am not sure about is this approach requests features every time, no matter current extent beyond previous extent or not. If I understand correctly, by using BBOX strategy, zoom-in won't trigger feature request, since they are already returned in the request based on previous extent.
    – Simon
    Mar 20, 2012 at 20:23

Use Vector layer instead WFS. The vector layer accepts some properties like:

  • protocol: which allows to set the kind of protocol to be used to read/write data. It can be WFS, GeoJSON, ...

  • strategies: an array with the Strategy instnaces used to refresh the layer content.

  • filter: this is what you want. Here you can specify the filter to be use when requesting data.

var vectorLayer = new OpenLayers.Layer.Vector("States", {
    protocol: new OpenLayers.Protocol.WFS({
        url: "WFS_URL",
        featureType: "xxx",
        featureNS: "xxx"
    strategies: [new OpenLayers.Strategy.BBOX()],
    filter: new OpenLayers.Filter.Logical({
  • thanks. but i am looking for a way to monitor the response, i.e., when the features are returned and added to layer, etc..
    – Simon
    Mar 16, 2012 at 21:20
  • What about registering a listener on the vector layer for the 'beforefeatureadded' or similar ??? In addition you can use the Protocol.WFS instance out side the layer, read from the source and handle the features by hand.
    – acanimal
    Mar 16, 2012 at 21:51
  • yes. am exploring this method based on the help from here. it just didnt work out yet. thanks for your attention and help!
    – Simon
    Mar 17, 2012 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.