Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
});
share|improve this question
add comment

3 Answers 3

up vote 3 down vote accepted

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.

share|improve this answer
    
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 '12 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 '12 at 4:08
    
a working example would be great! thanks a lot to you!! –  Simon Mar 17 '12 at 22:40
    
@Simon : Please view my latest update. –  CaptDragon Mar 19 '12 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 '12 at 20:23
show 2 more comments

I've copyed the source of the html and installed geoserver locally but can't get it to work. Does anyone have any ideas.

share|improve this answer
add comment

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({
        ...
    })
});
share|improve this answer
    
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 '12 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. –  EricSonaron Mar 16 '12 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 '12 at 22:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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