diff --git a/src/ol/source/imagewms.js b/src/ol/source/imagewms.js index c44511b017..62f8ef9fcf 100644 --- a/src/ol/source/imagewms.js +++ b/src/ol/source/imagewms.js @@ -10,6 +10,7 @@ goog.require('ol.events.EventType'); goog.require('ol.extent'); goog.require('ol.obj'); goog.require('ol.proj'); +goog.require('ol.reproj'); goog.require('ol.source.Image'); goog.require('ol.source.WMSServerType'); goog.require('ol.string'); @@ -136,6 +137,13 @@ ol.source.ImageWMS.prototype.getGetFeatureInfoUrl = function(coordinate, resolut if (this.url_ === undefined) { return undefined; } + var projectionObj = ol.proj.get(projection); + var sourceProjectionObj = this.getProjection(); + + if (sourceProjectionObj && sourceProjectionObj !== projectionObj) { + resolution = ol.reproj.calculateSourceResolution(sourceProjectionObj, projectionObj, coordinate, resolution); + coordinate = ol.proj.transform(coordinate, projectionObj, sourceProjectionObj); + } var extent = ol.extent.getForViewAndSize( coordinate, resolution, 0, @@ -158,7 +166,7 @@ ol.source.ImageWMS.prototype.getGetFeatureInfoUrl = function(coordinate, resolut return this.getRequestUrl_( extent, ol.source.ImageWMS.GETFEATUREINFO_IMAGE_SIZE_, - 1, ol.proj.get(projection), baseParams); + 1, sourceProjectionObj || projectionObj, baseParams); }; diff --git a/src/ol/source/tilewms.js b/src/ol/source/tilewms.js index b3e1ddcb62..f7984d4464 100644 --- a/src/ol/source/tilewms.js +++ b/src/ol/source/tilewms.js @@ -10,6 +10,7 @@ goog.require('ol.extent'); goog.require('ol.obj'); goog.require('ol.math'); goog.require('ol.proj'); +goog.require('ol.reproj'); goog.require('ol.size'); goog.require('ol.source.TileImage'); goog.require('ol.source.WMSServerType'); @@ -110,14 +111,14 @@ ol.inherits(ol.source.TileWMS, ol.source.TileImage); */ ol.source.TileWMS.prototype.getGetFeatureInfoUrl = function(coordinate, resolution, projection, params) { var projectionObj = ol.proj.get(projection); + var sourceProjectionObj = this.getProjection(); var tileGrid = this.getTileGrid(); if (!tileGrid) { tileGrid = this.getTileGridForProjection(projectionObj); } - var tileCoord = tileGrid.getTileCoordForCoordAndResolution( - coordinate, resolution); + var tileCoord = tileGrid.getTileCoordForCoordAndResolution(coordinate, resolution); if (tileGrid.getResolutions().length <= tileCoord[0]) { return undefined; @@ -125,14 +126,19 @@ ol.source.TileWMS.prototype.getGetFeatureInfoUrl = function(coordinate, resoluti var tileResolution = tileGrid.getResolution(tileCoord[0]); var tileExtent = tileGrid.getTileCoordExtent(tileCoord, this.tmpExtent_); - var tileSize = ol.size.toSize( - tileGrid.getTileSize(tileCoord[0]), this.tmpSize); + var tileSize = ol.size.toSize(tileGrid.getTileSize(tileCoord[0]), this.tmpSize); + var gutter = this.gutter_; if (gutter !== 0) { tileSize = ol.size.buffer(tileSize, gutter, this.tmpSize); - tileExtent = ol.extent.buffer(tileExtent, - tileResolution * gutter, tileExtent); + tileExtent = ol.extent.buffer(tileExtent, tileResolution * gutter, tileExtent); + } + + if (sourceProjectionObj && sourceProjectionObj !== projectionObj) { + tileResolution = ol.reproj.calculateSourceResolution(sourceProjectionObj, projectionObj, coordinate, tileResolution); + tileExtent = ol.proj.transformExtent(tileExtent, projectionObj, sourceProjectionObj); + coordinate = ol.proj.transform(coordinate, projectionObj, sourceProjectionObj); } var baseParams = { @@ -152,7 +158,7 @@ ol.source.TileWMS.prototype.getGetFeatureInfoUrl = function(coordinate, resoluti baseParams[this.v13_ ? 'J' : 'Y'] = y; return this.getRequestUrl_(tileCoord, tileSize, tileExtent, - 1, projectionObj, baseParams); + 1, sourceProjectionObj || projectionObj, baseParams); }; diff --git a/test/spec/ol/source/imagewms.test.js b/test/spec/ol/source/imagewms.test.js index 2bb892a18a..a96a0e7ac7 100644 --- a/test/spec/ol/source/imagewms.test.js +++ b/test/spec/ol/source/imagewms.test.js @@ -6,7 +6,7 @@ goog.require('ol.proj'); describe('ol.source.ImageWMS', function() { - var extent, pixelRatio, options, projection, resolution; + var extent, pixelRatio, options, optionsReproj, projection, resolution; beforeEach(function() { extent = [10, 20, 30, 40]; pixelRatio = 1; @@ -19,6 +19,14 @@ describe('ol.source.ImageWMS', function() { ratio: 1, url: 'http://example.com/wms' }; + optionsReproj = { + params: { + 'LAYERS': 'layer' + }, + ratio: 1, + url: 'http://example.com/wms', + projection: 'EPSG:3857' + }; }); describe('#getImage', function() { @@ -223,7 +231,7 @@ describe('ol.source.ImageWMS', function() { }); - describe('#getGetFeatureInfo', function() { + describe('#getGetFeatureInfoUrl', function() { it('returns the expected GetFeatureInfo URL', function() { var source = new ol.source.ImageWMS(options); @@ -253,6 +261,34 @@ describe('ol.source.ImageWMS', function() { expect(uri.hash.replace('#', '')).to.be.empty(); }); + it('returns the expected GetFeatureInfo URL when source\'s projection is different from the parameter', function() { + var source = new ol.source.ImageWMS(optionsReproj); + var url = source.getGetFeatureInfoUrl( + [20, 30], resolution, projection, + {INFO_FORMAT: 'text/plain'}); + var uri = new URL(url); + expect(uri.protocol).to.be('http:'); + expect(uri.hostname).to.be('example.com'); + expect(uri.pathname).to.be('/wms'); + var queryData = uri.searchParams; + expect(queryData.get('BBOX')).to.be('1577259.402312431,2854419.4299513334,2875520.229418512,4152680.2570574144'); + expect(queryData.get('CRS')).to.be('EPSG:3857'); + expect(queryData.get('FORMAT')).to.be('image/png'); + expect(queryData.get('HEIGHT')).to.be('101'); + expect(queryData.get('I')).to.be('50'); + expect(queryData.get('J')).to.be('50'); + expect(queryData.get('LAYERS')).to.be('layer'); + expect(queryData.get('QUERY_LAYERS')).to.be('layer'); + expect(queryData.get('REQUEST')).to.be('GetFeatureInfo'); + expect(queryData.get('SERVICE')).to.be('WMS'); + expect(queryData.get('SRS')).to.be(null); + expect(queryData.get('STYLES')).to.be(''); + expect(queryData.get('TRANSPARENT')).to.be('true'); + expect(queryData.get('VERSION')).to.be('1.3.0'); + expect(queryData.get('WIDTH')).to.be('101'); + expect(uri.hash.replace('#', '')).to.be.empty(); + }); + it('sets the QUERY_LAYERS param as expected', function() { var source = new ol.source.ImageWMS(options); var url = source.getGetFeatureInfoUrl( diff --git a/test/spec/ol/source/tilewms.test.js b/test/spec/ol/source/tilewms.test.js index 8045d184c5..885a0b6208 100644 --- a/test/spec/ol/source/tilewms.test.js +++ b/test/spec/ol/source/tilewms.test.js @@ -9,7 +9,7 @@ goog.require('ol.tilegrid.TileGrid'); describe('ol.source.TileWMS', function() { - var options; + var options, optionsReproj; beforeEach(function() { options = { params: { @@ -17,6 +17,13 @@ describe('ol.source.TileWMS', function() { }, url: 'http://example.com/wms' }; + optionsReproj = { + params: { + 'LAYERS': 'layer' + }, + url: 'http://example.com/wms', + projection: 'EPSG:4326' + }; }); describe('constructor', function() { @@ -193,7 +200,7 @@ describe('ol.source.TileWMS', function() { }); - describe('#getGetFeatureInfo', function() { + describe('#getGetFeatureInfoUrl', function() { it('returns the expected GetFeatureInfo URL', function() { var source = new ol.source.TileWMS(options); @@ -229,6 +236,36 @@ describe('ol.source.TileWMS', function() { expect(uri.hash.replace('#', '')).to.be.empty(); }); + it('returns the expected GetFeatureInfo URL when source\'s projection is different from the parameter', function() { + var source = new ol.source.TileWMS(optionsReproj); + source.pixelRatio_ = 1; + var url = source.getGetFeatureInfoUrl( + [-7000000, -12000000], + 19567.87924100512, ol.proj.get('EPSG:3857'), + {INFO_FORMAT: 'text/plain'}); + var uri = new URL(url); + expect(uri.protocol).to.be('http:'); + expect(uri.hostname).to.be('example.com'); + expect(uri.pathname).to.be('/wms'); + var queryData = uri.searchParams; + expect(queryData.get('BBOX')).to.be('-79.17133464081945,-90,-66.51326044311186,-45'); + expect(queryData.get('CRS')).to.be('EPSG:4326'); + expect(queryData.get('FORMAT')).to.be('image/png'); + expect(queryData.get('HEIGHT')).to.be('256'); + expect(queryData.get('I')).to.be('517'); + expect(queryData.get('J')).to.be('117'); + expect(queryData.get('LAYERS')).to.be('layer'); + expect(queryData.get('QUERY_LAYERS')).to.be('layer'); + expect(queryData.get('REQUEST')).to.be('GetFeatureInfo'); + expect(queryData.get('SERVICE')).to.be('WMS'); + expect(queryData.get('SRS')).to.be(null); + expect(queryData.get('STYLES')).to.be(''); + expect(queryData.get('TRANSPARENT')).to.be('true'); + expect(queryData.get('VERSION')).to.be('1.3.0'); + expect(queryData.get('WIDTH')).to.be('256'); + expect(uri.hash.replace('#', '')).to.be.empty(); + }); + it('sets the QUERY_LAYERS param as expected', function() { var source = new ol.source.TileWMS(options); source.pixelRatio_ = 1;