3

Currently I am able to display a popup of a feature that was clicked on. I would like to be able to instead of click, to select an area and then show the feature(s) from the selected area. Here is what I have so far for the click.

How would I handle "selection" instead of "click"?

Here is the current working code (handling click event):

featurePicker = new OpenLayers.Control.WMSGetFeatureInfo({
                url: "http://server01:8080/geoserver/sf/wms",
                title: 'identify features on click',
                layers: [PointsLayer],
                queryVisible: true
            });
            featurePicker.infoFormat = 'text/html';
            featurePicker.events.register("getfeatureinfo", this, showFeatureInfo);
            map.addControl(featurePicker);
            featurePicker.activate();

            function showFeatureInfo(e) {
                if (e.text.indexOf("<table class=\"featureInfo\">") != -1){ 
                    map.addPopup(new OpenLayers.Popup.FramedCloud(
                                "frame",
                                map.getLonLatFromPixel(e.xy),
                                null,
                                e.text,
                                null,
                                true
                            ));
                }
            }

How do I change to handle the "selection" event instead of "click" event?

In another words, to handle event when an area is selected with the mouse instead of clicking on a point?

Additionally it would be nice to have an additional popup/selection window before the framecloud is displayed to allow the user to select which feature he would like to be displayed on the framecloud from the ones found in the selected area.

The layer in question is WMS but I can create a "dummy" WFS layer for mouse selection if this is what is required to get this working.


I found something that might help but not sure how to implement it in my case:

   selectControl  = new OpenLayers.Control.GetFeature({
                protocol: OpenLayers.Protocol.WFS.fromWMSLayer(PointsLayer),
                box: true,
                hover: false,
                multipleKey: "shiftKey",
                toggleKey: "ctrlKey"
            });

closed as too broad by PolyGeo Jul 9 at 21:47

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • The WMS GetFeatureInfo request is only supported with a pair of coordinates. This limitation is based on the nature of raster layers. For doing such a thing, you have to set up a matrix based on the bounding box with a specific interval based on the resolution of your layers. Then you have to send a GetFeatureInfo request to the server with all of your coordinate pairs. Finally, you have to sort out the duplicates from the returned values, and you're done. However, this method is far from ideal. Please don't do it, your server will cry. – Gabor Farkas Jan 7 '15 at 23:30
  • Thanks Gabor, so what would be the way to do this? I am running GeoServer. Thanks. – user3657279 Jan 9 '15 at 15:15
  • You have to write a function on the client side (JavaScript) to achieve this. First, get the bounding box of the selection box. Then calculate a matrix (two dimensional array) based on the box. E.g. if you have a bounding box with a (0,0) and (2,2) coordinate pairs, and a raster resolution of 1 map unit, you will end up with a matrix like this: [[0,0], [0,1], [0,2], [1,0], [1,1], [1,2], [2,0], [2,1], [2,2]]. Next, iterate through the array and send a GetFeatureInfo request with all of the pair of coordinates. Finally, sort out the duplicates with an if clause. – Gabor Farkas Jan 11 '15 at 17:33
2

WMS is not going to support a bounding box selection, but it looks like you're hosting your own instance of geoserver, so WFS is probably available to you as well, assuming you have not intentionally disabled it, It's turned on by default. See http://docs.geoserver.org/latest/en/user/services/wfs/reference.html#getfeature to look at how you can pass a bbox param to the WFS end point to query for a bounding box. But, you don't need to manually do this. Openlayers can help.

Openlayers also supports WFS, so you don't need to understand the details of WFS all that well. See the openlayers WFS example here and look at the javascript source, specifically, at this part:

 control = new OpenLayers.Control.GetFeature({
                protocol: OpenLayers.Protocol.WFS.fromWMSLayer(layer),
                box: true,
                hover: true,
                multipleKey: "shiftKey",
                toggleKey: "ctrlKey"
            });

What this example does is configure a control that knows how to query a WFS service. It does this by looking at the existing WMS layer, and figuring out the WFS service URL from that. Since the WMS & WFS service are both hosted in the same place, it's as simple for openlayers to replace 'wms' in the url with 'wfs', i.e http://server01:8080/geoserver/sf/wms => http://server01:8080/geoserver/sf/wfs and it will know where to get the WFS info from.

After that bit of code, several feature selection and feature hover events for the control are added that tell openlayers what the WFS control should do with the features you are selecting or hovering over. This is where you may need to add your own event handlers that suit your applications needs. In this particular example, the selected features are added to a vector layer with specific styling to differentiate them and show them as selected.

The example doesn't seem to be loading the layers for me right now, but the code is still there.

  • Take a look at this link to get an idea of how the selection I am looking for should work: dev.openlayers.org/examples/getfeature-wfs.html . But not for WFS, I need it for a WMS layer. – user3657279 Jan 6 '15 at 20:15
  • isn't that the same link I posted? Anyway, I'm not sure you're able to do that. The bbox param of a WMS GetfeatureInfo request is the displayed map extent, not a bounding box for querying. The best you can do with WMS is a point and/or a radius around it. Not a bounding box. However, I'm no expert, so maybe there is. Do you control the WMS server in question? Why is a WFS request ruled out? – MWrenn Jan 6 '15 at 20:29
  • Correct WFS is not possible. The link is to show how the mouse should work (selection instead of a click event). Thanks. – user3657279 Jan 6 '15 at 20:55
  • Then what you're asking for is not likely possible with the current set of requirements. WMS does not support bounding box queries, only point query with an optional radius, so you could maybe do a bounding circle query. See also gis.stackexchange.com/questions/72358/… which is basically a duplicate of your question with a similar answer. – MWrenn Jan 6 '15 at 21:00
  • 1
    What you are asking is simply not possible with WMS, regardless of whether you set a bounty or not. If you want to query individual features using a BBOX you have to use something vector-based, WFS seems like the easiest solution for you – atlefren Jan 13 '15 at 10:23

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