4

I've created a simple mapping application that shows a number of WMS layers (from GeoServer/Oracle) on a map, with check-boxes to turn them on and off. The layer list is generated dynamically from a table in a database. What I need to do now is add the ability to query multiple layers. Many of the layers overlap so I need to be able to click on a point and it show all the information from the selected features for the layers that have been turned on.

I can do this for a single layer (either by index in the collection or filtering by name) using the OpenLayers 3 example on their site. However, I want to be able to click on the map, and any layers that are turned on are queried and added to the same popup window. So far I have this code which generates the URL for each layer:

map.on('singleclick', function(evt) {
var coordinate = evt.coordinate; //Picks up click coordinates
var viewResolution = view.getResolution(); //Picks up map current resolution
var layersCollection = map.getLayers(); //Creates collection of layers object from layers that have been turned on. 
var i;
for (i = 1; i <= ((layersCollection.a.length-1)); i++)
{ //Loops through all layers excluding baselayer at index 0 and gets their URL
    var layerID = layersCollection.item(i);
    var url = layerID.getSource().getGetFeatureInfoUrl(evt.coordinate, viewResolution, Projection,{'INFO_FORMAT': 'text/plain'});
}

This generates a URL for each layer. Normally then for a single layer I would use an iframe to get the information into the overlay like this:

if (url) {
overlay.setPosition(coordinate);
    content.innerHTML = 
    '<iframe  src="' + url + '"></iframe>';
    container.style.display = 'block';
};

But I can't work out how to do this for multiple layers. Anybody have any ideas? Apologies for my basic JavaScript skills.

1 Answer 1

3

Build a list of the selected layers and send one combined GetFeatureInfo request. Here are two examples which worked at least a minute ago:

http://demo.opengeo.org/geoserver/wms?BBOX=44.3115939344240175,-103.93616932504362182,44.55018319949628136,-103.57561215835877988&CRS=EPSG:4326&QUERY_LAYERS=topp:states,og:streams&SERVICE=WMS&HEIGHT=714&REQUEST=GetFeatureInfo&STYLES=&I=775&J=246&WIDTH=1079&FEATURE_COUNT=10&VERSION=1.3.0&FORMAT=image/png&LAYERS=topp:states,og:streams&info_format=text/plain

http://demo.opengeo.org/geoserver/wms?BBOX=44.3115939344240175,-103.93616932504362182,44.55018319949628136,-103.57561215835877988&CRS=EPSG:4326&QUERY_LAYERS=topp:states,og:streams&SERVICE=WMS&HEIGHT=714&REQUEST=GetFeatureInfo&STYLES=&I=775&J=246&WIDTH=1079&FEATURE_COUNT=10&VERSION=1.3.0&FORMAT=image/png&LAYERS=topp:states,og:streams&info_format=text/html

5
  • 1
    That's done the trick, thanks very much. Just to add, the featurecount parameter is critical I think, because if it's not included it defaults to 1 on geoserver so only one feature from one layer will return.
    – user29976
    Sep 18, 2014 at 13:20
  • It defaults to one on every WMS server because so it is written in the WMS standard.
    – user30184
    Sep 18, 2014 at 13:21
  • I have many layers came from different servers.... what can I do?
    – Magno C
    Jun 27, 2016 at 19:22
  • I fear you must use your imagination, make one request per server and combine the results on the client side. WMS standard does not help you and different servers do not even know that other servers exist. Alternatively you can cascade all WMS servers through one of your own but cascading GetFeatureInfo requests is not relieable because GFI is so loosely defined in the standard.
    – user30184
    Jun 27, 2016 at 19:51
  • @user29976 can you post your working code? I am still trying to do what you are doing and can't get it to work
    – JasonBK
    Dec 6, 2017 at 14:19

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.