2

I added 4 WMS layers (three polygons and one have point geometry) from GeoServer on my webpage

My requirement is:

  1. I have to select a single polygon of one wms layer using a mouse click. After the selection, the polygon color should be changed. If you select another polygon, the previous selected polygon color is to be changed to original color.

  2. If I select a polygon of one wms layer, I want to get the NAME of the selected polygon as an alert.

    layer = new OpenLayers.Layer.WMS(

    "Layer", "http://********:8080/geoserver/POC/wms",

            {
                LAYERS: 'POC:CIRCLE_VIEW',
                STYLES: '', 
                wrapDateLine: true, 
                format: format,
                transparent:true
    
            },
            {
               singleTile: true, 
               ratio: 1, 
               isBaseLayer: false,
               yx : {'EPSG:42106' : false}
    
            }
    
        ); 
    

`

I added four layers in this way..

var infoControls = {
         click: new OpenLayers.Control.WMSGetFeatureInfo({
             url: 'http://********:8080/geoserver/POC/wms', 
             title: 'Details',
             layers: [layer,layer2,layer3,layer4],
             queryVisible: true
         }),
         hover: new OpenLayers.Control.WMSGetFeatureInfo({
        url: 'http://********:8080/geoserver/POC/wms',
             title: 'Details',
           layers: [layer,layer2,layer3,layer4],
             hover: true,
             queryVisible: true
         })
     };
    for (var i in infoControls) 
    {
        
        infoControls[i].events.register("getfeatureinfo", this, showInfo);
        map.addControl(infoControls[i]); 
    }
        infoControls.click.activate();
function showInfo(evt) {
            if (evt.features && evt.features.length) {
                
                 highlightLayer.destroyFeatures();
                 highlightLayer.addFeatures(evt.features);
                 highlightLayer.redraw();
            } else {
                highlightLayer.destroyFeatures();
                 highlightLayer.addFeatures(evt.features);
                 highlightLayer.redraw();
                document.getElementById('responseText').innerHTML = evt.text;
            }
        }

In this I am showing attribute data of selected polygon.

0

1 Answer 1

2

The WMS specification does not provide a function to select features and/or colorize them by selection. It only provides pretty dumb map images. With GetFeatureInfo you only can query what is under a specific coordinate / pixel, to get the attributes of the feature which was used to render the map image.

For a select features function you also have to use a WFS Service in the background additionaly to the WMS which is used to render a nice looking map.

You are right, it is possible to hack around with Styled Layer Descriptors and GetFeatureInfo Requests to emulate a Feature Highlight only with WMS. Actually I don't know what are the disadvantages of this hack, if there are any, but what I can say is, that the WMS protocol was not intended to perform this (mind that every selection triggers a new rendering of the map image on the server). Selecting and Querying of vector data is the main reason for the WFS specification. Check out this stable example on the OpenLayers page http://openlayers.org/dev/examples/getfeature-wfs.html

6
  • Thank you ver much.. I will try to use WFS Service.. Could u please share the link of WFS service example
    – Arun
    Jun 27, 2013 at 6:34
  • openlayers.org/dev/examples/SLDSelect.html But, in this example he is using WMS service only.. Please check the example
    – Arun
    Jun 27, 2013 at 6:35
  • modified my answer to cover SLD Select. Would be nice to know if this hack fits all your needs (do you get back a selection set for further actions on it for example?) Jun 27, 2013 at 10:40
  • Jürgen Zornig-- I tried with the example u shown, it is working when I have one layer. But I have 4 layers. I want to switch the control from one layer to other at a time.. Please do some help.. Thank u
    – Arun
    Jun 27, 2013 at 12:44
  • Now we get in the pitfalls of Openlayers, I have to say that I am also very often messing around with these ones. There is a MultiLayer Selection example in Openlayers openlayers.org/dev/examples/select-feature-multilayer.html which uses the SelectControl and just adds more layers to it, while the WFS Selection example does not make use of this control. I only can guess, that the solution is to just make Layers out of your WFS services and add them to a SelectionControl. Unfortunately I don't really know if that works. :( Jun 27, 2013 at 12:51

Your Answer

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

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