More

Scripting getfeatureinfo for multiple wms layers

Scripting getfeatureinfo for multiple wms layers


I am not really a programmer and I'm trying to create a webservice. I copy and pasted some script examples which made my script what it is now. I have multiple wms layers, but want to show different info in the infowindow for each wms. So I thought this can be done by an if-then-else statement. First, I created the getfeatureinfo variable including the layers which need to have information shown.

var control_identify = new OpenLayers.Control.WMSGetFeatureInfo({ title : 'Klik op een lijn voor informatie', layers : [ AmiceHerkomst, AmiceVerwerking ], queryVisible : true, infoFormat : 'application/vnd.ogc.gml', vendorParams: { buffer: 5 // geoserver buffer in pixels }, eventListeners : { getfeatureinfo : function(event){ showInfo(event); } } }); control_identify.events.register("getfeatureinfo", this, showInfo);

then I create the showinfo function. Here I want to make an if-then-else for each wms layer I have. Something in the line of if(evt.features.length != 0) and layer = AmiceHerkomst { followed by the attributes I want from that layer. For the next layer I add at the end else if (evt.features.length != 0) and layer = AmiceVerwerking { with the attributes I want for this layer and so on. Scripting like I said above will give an error, but I think something like this should be possible?

function showInfo(evt) { var f = 0; var temstr = ""; if(evt.features.length != 0){ for (f = 0; f <= evt.features.length - 1; f++) { temstr = temstr + ""; for ( var key in evt.features[f].attributes) { temstr += ""; switch (key){ case 'afvalstroo': temstr += ""; break; case 'plaatsloca': temstr += ""; break; default: }; temstr += ""; } temstr = temstr + "
Afvalstroom" + evt.features[f].attributes[key] + "Plaatsnaam" + evt.features[f].attributes[key] + "
"; }; log("Informatie
" + temstr, true); } else { log("Informatie
" + "Niets gevonden op deze locatie", true); } }

You can switch to response format JSON and iterate through the response-features:

info = new OpenLayers.Control.WMSGetFeatureInfo({ url: 'http://demo.opengeo.org/geoserver/wms', title: 'Identify features by clicking', queryVisible: true, infoFormat : 'application/json', drillDown: true, eventListeners: { getfeatureinfo: function(event) {; retrieved_response=JSON.parse(event.text); retrieved_objects=retrieved_response.features; if(retrieved_objects.length>0) { for(var i=0;i
"+loop_layer_id+"
STATE: "+retrieved_objects[i].properties.STATE+"
"; } else if(loop_layer_id=="tasmania_roads") { popuphtml=popuphtml+"
"+loop_layer_id+"
TYPE: "+retrieved_objects[i].properties.TYPE+"
"; } else if(loop_layer_id=="tasmania_water_bodies") { popuphtml=popuphtml+"
"+loop_layer_id+"
WaterTYPE: "+retrieved_objects[i].properties.WATER_TYPE+"
"; } else { popuphtml=popuphtml+"
other content"+"
"; } } //console.log(popuphtml); map.addPopup(new OpenLayers.Popup.FramedCloud( "chicken", map.getLonLatFromPixel(event.xy), null, popuphtml , null, true )); } else { alert("no response… "); } } } }); map.addControl(info); info.activate();