diff --git a/lib/OpenLayers/Control/WMSGetFeatureInfo.js b/lib/OpenLayers/Control/WMSGetFeatureInfo.js index 3573274362..bf901ad1fc 100644 --- a/lib/OpenLayers/Control/WMSGetFeatureInfo.js +++ b/lib/OpenLayers/Control/WMSGetFeatureInfo.js @@ -345,28 +345,36 @@ OpenLayers.Control.WMSGetFeatureInfo = OpenLayers.Class(OpenLayers.Control, { layerNames = layerNames.concat(layers[i].params.LAYERS); styleNames = styleNames.concat(this.getStyleNames(layers[i])); } + var firstLayer = layers[0]; + // use the firstLayer's projection if it matches the map projection - + // this assumes that all layers will be available in this projection + var projection = this.map.getProjection(); + var layerProj = firstLayer.projection; + if (layerProj && layerProj.equals(this.map.getProjectionObject())) { + projection = layerProj.getCode(); + } var params = OpenLayers.Util.extend({ service: "WMS", - version: layers[0].params.VERSION, + version: firstLayer.params.VERSION, request: "GetFeatureInfo", layers: layerNames, query_layers: layerNames, styles: styleNames, bbox: this.map.getExtent().toBBOX(null, - layers[0].reverseAxisOrder()), + firstLayer.reverseAxisOrder()), feature_count: this.maxFeatures, height: this.map.getSize().h, width: this.map.getSize().w, format: format, info_format: this.infoFormat - }, (parseFloat(layers[0].params.VERSION) >= 1.3) ? + }, (parseFloat(firstLayer.params.VERSION) >= 1.3) ? { - crs: this.map.getProjection(), + crs: projection, i: clickPosition.x, j: clickPosition.y } : { - srs: this.map.getProjection(), + srs: projection, x: clickPosition.x, y: clickPosition.y } diff --git a/tests/Control/WMSGetFeatureInfo.html b/tests/Control/WMSGetFeatureInfo.html index c566f903d8..5d370d76b1 100644 --- a/tests/Control/WMSGetFeatureInfo.html +++ b/tests/Control/WMSGetFeatureInfo.html @@ -451,6 +451,37 @@ t.eq(count, 2, "We expect 2 requests to go off"); map.destroy(); } + + function test_GetFeatureInfo_buildWMSOptions(t) { + t.plan(3); + + var map = new OpenLayers.Map("map", { + getExtent: function() {return(new OpenLayers.Bounds(-180,-90,180,90));}, + projection: "EPSG:900913" + }); + var a = new OpenLayers.Layer.WMS("dummy", "http://localhost/wms", { + layers: "a" + }, {projection: "EPSG:3857"}); + var b = new OpenLayers.Layer.WMS("dummy", "http://localhost/wms", { + layers: "b" + }); + var c = new OpenLayers.Layer.WMS("dummy", "http://localhost/wms", { + layers: "c" + }, {projection: "EPSG:4326"}); + map.addLayers([a, b, c]); + var gfi = new OpenLayers.Control.WMSGetFeatureInfo(); + map.addControl(gfi); + gfi.activate(); + + var options = gfi.buildWMSOptions("http://localhost/wms", [a], {xy: {x: 50, y: 50}}, "text/html"); + t.eq(options.params.SRS, "EPSG:3857", "layer projection used if provided and equal map projection"); + + options = gfi.buildWMSOptions("http://localhost/wms", [b], {xy: {x: 50, y: 50}}, "text/html"); + t.eq(options.params.SRS, "EPSG:900913", "map projection used if layer has no projection configured"); + + options = gfi.buildWMSOptions("http://localhost/wms", [b], {xy: {x: 50, y: 50}}, "text/html"); + t.eq(options.params.SRS, "EPSG:900913", "map projection used if layer configured with an incompatible projection"); + } function test_GetFeatureInfo_WMS13(t) { t.plan(4);