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 am using the WMSGetFeatureInfo and I am also obtaining the Info of the base raster and i do not need that. Is it possible to obtain the FeatureInfo of only selected WMS features?

How can I modify this code to accommodate only selected WMS layer?

info = new OpenLayers.Control.WMSGetFeatureInfo({
            url : 'http://niles.iplantcollaborative.org:8080/geoserver/wms',
            title : 'Identify features by clicking',
            queryVisible : true,
            eventListeners : {
            getfeatureinfo : function(event) {
            map.addPopup(new OpenLayers.Popup.FramedCloud("chicken", map.getLonLatFromPixel(event.xy), null, event.text, null, true));
            }
            }
            });
            map.addControl(info);
            info.activate();

UPDATE 2.0

I have added the code,

gfLayers = [];
            var lyr = map.layers;

            for(var a=0; a < lyr.length; a++){
                if(lyr[a].CLASS_NAME == "OpenLayers.Layer.WMS" && lyr[a].visibility == true){
                    if(lyr[a].isBaseLayer == false){
                        gfLayers.push(lyr[a].name);
                    }

                } 
            };

            info = {
                hover : new OpenLayers.Control.WMSGetFeatureInfo({
                    url : 'http://niles.iplantcollaborative.org:8080/geoserver/wms',
                    title : 'Identify features by clicking',
                    //layers : gfLayers,
                    layers : [pima, carib],
                    queryVisible : true,
                    hover: true,
                    eventListeners : {
                        getfeatureinfo : function(event) {
                            deletePopUp();
                            popup = new OpenLayers.Popup.FramedCloud("popInfo", map.getLonLatFromPixel(event.xy), null, event.text, null, true);
                            map.addPopup(popup);
                        }
                    }
                })
            };
            map.addControl(info.hover);
            info.hover.activate();

Now I am getting Empty PopUps. not sure why?

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

I think the answer is covered in this example's source code : http://dev.openlayers.org/releases/OpenLayers-2.11/examples/getfeatureinfo-control.html

Please pay attention to "layers: [water]" at this code piece :

infoControls = {
            click: new OpenLayers.Control.WMSGetFeatureInfo({
                url: 'http://demo.opengeo.org/geoserver/wms', 
                title: 'Identify features by clicking',
                layers: [water],
                queryVisible: true
            }),
            hover: new OpenLayers.Control.WMSGetFeatureInfo({
                url: 'http://demo.opengeo.org/geoserver/wms', 
                title: 'Identify features by clicking',
                layers: [water],
                hover: true,
                // defining a custom format options here
                formatOptions: {
                    typeName: 'water_bodies', 
                    featureNS: 'http://www.openplans.org/topp'
                },
                queryVisible: true
            })
        }

And :

function toggleLayers(element) {
        for (var key in infoControls) {
            var control = infoControls[key];
            if (element.value == 'Specified') {
                control.layers = [water];
            } else {
                control.layers = null;
            }
        }
    } 

So if you don't define the layer that you are interested then it requests info from all layers for the selected point.

share|improve this answer
    
I tried by adding the code in the example but i am now getting empty popups for basemap. –  Sam007 Jun 6 '12 at 20:43
    
I am not JS expert nor Openlayers here but I think you should register a listener for "getfeatureinfo" event otherwise the other events (that might be coming from basemap) may cause that... Please check the code example I gave before, if you look at the source code at line 112 you will see : infoControls[i].events.register("getfeatureinfo", this, showInfo); It might be useful to follow the code from there... Let me know if it helps –  oalan Jun 7 '12 at 13:07
    
It is still not working. Here is the jsfiddle. –  Sam007 Jun 8 '12 at 17:33
    
as I see in the code you've registered showInfo rather than popup function. So isn't it showing the info by appending the text in the page ? or do you mean it is not working in the sense of popups ? –  oalan Jun 8 '12 at 22:05
    
Ya it is appending to the page as you can see here but i can still see empty popups which should not be the case. (in the application I have removed the popups now, as they were so annoying) –  Sam007 Jun 8 '12 at 22:36
show 1 more comment

try this:

gfLayers = [];
var lyr = map.layers;

for(var a= 0; a < lyr.length; a++){
    if(lyr[a].CLASS_NAME == "OpenLayers.Layer.WMS" && lyr[a].visibility == true){
           gfLayers.push(lyr[a].name);
    }

    };

then add this array to in your layers defination in WMSGetFeatureInfo layers as

...
layers: gfLayers,
queryVisible: true
...

i hope it helps you...

share|improve this answer
    
I tried adding the gfLayers but it was not successful. Despite the fact that when i run alert(gfLayers); in the console i do get the layer names. When I hard code them, it is working fine. I have updated the code above. –  Sam007 Jun 6 '12 at 18:37
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.