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.

I have a WMS base and from that create a Vector Layer for selections. Then I create 3 Control.GetFeature() controls, each using WFS to select a different feature type upon clicking the map (the 'featureselect' event). Doing it this way, however, I end up with as many as 3 Features being selected, when I want a maximum of one.

Now, ideally I'd like GeoServer to pick the best (closest) feature to the mouse click, but even using Javascript in the browser, how would I detect when all the features have come in via WFS so I can test the geometries? And what would be the best way to test them? (mostly multilines and multilinestrings)

Here's the simplified code to give you the idea of what I've got now:

function init() {
    var mapOptions = { /* yada yada */ }
    var map = new OpenLayers.Map('map', mapOptions);

    var wmsOptions = { /* yada yada */ }
    var wmsLayers = 'parcelshape_lin,wmain_lin,wservice_lin,buildings_pol';
    var wms = new OpenLayers.Layer.WMS(
        "Testing",
        baseUrl+"/geoserver/wms",
        { layers: wmsLayers },
        wmsOptions
    );

    var select = new OpenLayers.Layer.Vector("Selection", {
        styleMap: new OpenLayers.Style(OpenLayers.Feature.Vector.style["select"])
    });

    map.addLayers([wms,select]);

    var BuildingsControl = new OpenLayers.Control.GetFeature({
        protocol: OpenLayers.Protocol.WFS.fromWMSLayer(wms, {
            url: baseUrl+"/geoserver/wfs",
            featureType: "buildings_pol"
        }),
        hover: false
    });
    BuildingsControl.events.register("featureselected", this, function(e) {
        select.addFeatures([e.feature]);
    });
    BuildingsControl.events.register("featureunselected", this, function(e) {
        select.removeFeatures([e.feature]);
    });


    var WaterMainsControl = new OpenLayers.Control.GetFeature({
        protocol: OpenLayers.Protocol.WFS.fromWMSLayer(wms, {
            url: baseUrl+"/geoserver/wfs",
            featureType: "wmain_lin"
        }),
        hover: false
    });
    WaterMainsControl.events.register("featureselected", this, function(e) {
        select.addFeatures([e.feature]);
    });
    WaterMainsControl.events.register("featureunselected", this, function(e) {
        select.removeFeatures([e.feature]);
    });

    map.addControl(BuildingsControlCtrl);
    map.addControl(WaterMainsControl);

    BuildingsControl.activate();
    WaterMainsControl.activate();
}
share|improve this question
    
What kind of WFS request are you opening from the controls? Can you post some code? –  unicoletti Dec 27 '11 at 20:11
    
In your Vector Layer, there is a function, onFeatureInsert(feature), that is called after a feature is inserted. Maybe you could override this method to filter what features are allowed to stay in the vector layer? But, I'm not sure of the order the features are added to the layer or how you could get the mouse click to do your processing. (hence comment and not an answer) –  Nate Dec 27 '11 at 22:45
    
I have also tried the way jim novak has did. I could be able to see the features info in firebug response. But when I'm trying to work on the selected features on "features selected" event, it is not even triggering that even. Any help is appreciable. –  user5737 Feb 2 '12 at 9:22

1 Answer 1

up vote 3 down vote accepted

If your WFS supports it (and it looks like you're using GeoServer, which does), OpenLayers can query multiple feature types in a single WFS query:

var featureTypes = ["parcelshape_lin", "wmain_lin", "buildings_pol"];

var allLayersControl = new OpenLayers.Control.GetFeature({
    protocol: OpenLayers.Protocol.WFS.fromWMSLayer(wms, {
        url: baseUrl + "/geoserver/wfs",
        featureType: featureTypes
    }),
    hover: false
});

This is exactly what I do in my OpenLayers/GeoServer installation, the only difference being that I create a new OpenLayers.Protocol.WFS({ ... }) manually instead of using fromWMSLayer. However, it should work identically.

Just register the events on allLayerscontrol and the GetFeature control will take care of parsing the geometries and finding the best match. If you're interested in how it does that, I'd recommend taking a look at the source for the control (GetFeature.js in OpenLayers).

share|improve this answer
1  
Thanks! This works well on the first selection, but suddenly I've got a strange issue where a second GetFeature is issued differently than the first. The first has 3 of these XML elements, each with a different typename: <wfs:Query typeName="feature:buildings_pol" srsName="EPSG:32054"> The second GetFeature has this: <wfs:Query typeName="feature:buildings_pol,wmain_lin,wservice_lin" srsName="EPSG:32054" xmlns:feature="KwuPostGIS"> This 2nd request gives an exception from GeoServer, saying it doesn't know a 'buildings_pol,wmain_lin,wservice_lin' feature. A bug in OpenLayers you suppose? –  Jim N Dec 28 '11 at 15:17
    
Yup, that's definitely an OpenLayers bug. Do you have the latest version? This would be a showstopper for my project too, so I'll try reproducing this in my environment and see if I get similar behavior. –  Rohan Singh Dec 28 '11 at 18:38
    
I'm using OpenLayers-2.11, OpenLayers.js - the big 958KB one. Trying to track through the code, but it's complex. I've never debugged javascript this involved. –  Jim N Dec 28 '11 at 19:00
1  
I'm using OpenLayers 2.11 as well (OpenLayers.VERSION_NUMBER == "Release 2.11") and am not seeing this behavior. Each request always has separate <wfs:Query> elements. You know, I think I may have seen a bug report on this a while back though. I'd recommend constructing an OpenLayers.Protocol.WFS object manually instead of using fromWMSLayer and see if you still get the same behavior. –  Rohan Singh Dec 28 '11 at 19:23
1  
Bravo, sir. TBH, I was using fromWMSLayer() because I couldn't figureout how featureNS functioned in the scheme of things. Apparently not much. Using the standard OpenLayers.Protocol.WFS() constructor has made this second-click glitch disappear. Thanks again for your tips, would have taken me days to work this out. –  Jim N Dec 28 '11 at 20:02

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.