From d1bb2724a5860e112e762e5b2868678983a550d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Wed, 20 Aug 2014 17:49:33 +0200 Subject: [PATCH] Stateless GetFeatureInfo for ol.source.ImageWMS --- src/ol/source/imagewmssource.js | 50 ++++++++-------------- test/spec/ol/source/imagewmssource.test.js | 22 +++++----- 2 files changed, 27 insertions(+), 45 deletions(-) diff --git a/src/ol/source/imagewmssource.js b/src/ol/source/imagewmssource.js index 95b9980219..2f417a7193 100644 --- a/src/ol/source/imagewmssource.js +++ b/src/ol/source/imagewmssource.js @@ -87,18 +87,6 @@ ol.source.ImageWMS = function(opt_options) { */ this.imageSize_ = [0, 0]; - /** - * @private - * @type {ol.proj.Projection} - */ - this.renderedProjection_ = null; - - /** - * @private - * @type {number} - */ - this.renderedResolution_ = NaN; - /** * @private * @type {number} @@ -115,6 +103,14 @@ ol.source.ImageWMS = function(opt_options) { goog.inherits(ol.source.ImageWMS, ol.source.Image); +/** + * @const + * @type {ol.Size} + * @private + */ +ol.source.ImageWMS.GETFEATUREINFO_IMAGE_SIZE_ = [101, 101]; + + /** * Return the GetFeatureInfo URL for the passed coordinate, resolution, and * projection. Return `undefined` if the GetFeatureInfo URL cannot be @@ -134,22 +130,13 @@ ol.source.ImageWMS.prototype.getGetFeatureInfoUrl = goog.asserts.assert(!('VERSION' in params)); - if (!goog.isDef(this.url_) || goog.isNull(this.image_)) { + if (!goog.isDef(this.url_)) { return undefined; } - goog.asserts.assert(this.imageSize_[0] !== 0 && - this.imageSize_[1] !== 0); - goog.asserts.assert(!isNaN(this.renderedResolution_)); - goog.asserts.assert(!goog.isNull(this.renderedProjection_)); - - if (resolution != this.renderedResolution_ || - !ol.proj.equivalent(projection, this.renderedProjection_)) { - return undefined; - } - - var extent = this.image_.getExtent(); - var pixelRatio = this.image_.getPixelRatio(); + var extent = ol.extent.getForViewAndSize( + coordinate, resolution, 0, + ol.source.ImageWMS.GETFEATUREINFO_IMAGE_SIZE_); var baseParams = { 'SERVICE': 'WMS', @@ -161,15 +148,14 @@ ol.source.ImageWMS.prototype.getGetFeatureInfoUrl = }; goog.object.extend(baseParams, this.params_, params); - var imageResolution = resolution / pixelRatio; - - var x = Math.floor((coordinate[0] - extent[0]) / imageResolution); - var y = Math.floor((extent[3] - coordinate[1]) / imageResolution); + var x = Math.floor((coordinate[0] - extent[0]) / resolution); + var y = Math.floor((extent[3] - coordinate[1]) / resolution); goog.object.set(baseParams, this.v13_ ? 'I' : 'X', x); goog.object.set(baseParams, this.v13_ ? 'J' : 'Y', y); - return this.getRequestUrl_(extent, this.imageSize_, pixelRatio, projection, - baseParams); + return this.getRequestUrl_( + extent, ol.source.ImageWMS.GETFEATUREINFO_IMAGE_SIZE_, + 1, projection, baseParams); }; @@ -251,8 +237,6 @@ ol.source.ImageWMS.prototype.getImage = this.image_ = new ol.Image(extent, resolution, pixelRatio, this.getAttributions(), url, this.crossOrigin_); - this.renderedProjection_ = projection; - this.renderedResolution_ = resolution; this.renderedRevision_ = this.getRevision(); return this.image_; diff --git a/test/spec/ol/source/imagewmssource.test.js b/test/spec/ol/source/imagewmssource.test.js index 88585008f0..b407a27913 100644 --- a/test/spec/ol/source/imagewmssource.test.js +++ b/test/spec/ol/source/imagewmssource.test.js @@ -140,7 +140,6 @@ describe('ol.source.ImageWMS', function() { it('returns the expected GetFeatureInfo URL', function() { var source = new ol.source.ImageWMS(options); - source.getImage(extent, resolution, pixelRatio, projection); var url = source.getGetFeatureInfoUrl( [20, 30], resolution, projection, {INFO_FORMAT: 'text/plain'}); @@ -149,12 +148,12 @@ describe('ol.source.ImageWMS', function() { expect(uri.getDomain()).to.be('example.com'); expect(uri.getPath()).to.be('/wms'); var queryData = uri.getQueryData(); - expect(queryData.get('BBOX')).to.be('20,10,40,30'); + expect(queryData.get('BBOX')).to.be('24.95,14.95,35.05,25.05'); expect(queryData.get('CRS')).to.be('EPSG:4326'); expect(queryData.get('FORMAT')).to.be('image/png'); - expect(queryData.get('HEIGHT')).to.be('200'); - expect(queryData.get('I')).to.be('100'); - expect(queryData.get('J')).to.be('100'); + 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'); @@ -163,13 +162,12 @@ describe('ol.source.ImageWMS', function() { 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('200'); + expect(queryData.get('WIDTH')).to.be('101'); expect(uri.getFragment()).to.be.empty(); }); it('sets the QUERY_LAYERS param as expected', function() { var source = new ol.source.ImageWMS(options); - source.getImage(extent, resolution, pixelRatio, projection); var url = source.getGetFeatureInfoUrl( [20, 30], resolution, projection, {INFO_FORMAT: 'text/plain', QUERY_LAYERS: 'foo,bar'}); @@ -178,12 +176,12 @@ describe('ol.source.ImageWMS', function() { expect(uri.getDomain()).to.be('example.com'); expect(uri.getPath()).to.be('/wms'); var queryData = uri.getQueryData(); - expect(queryData.get('BBOX')).to.be('20,10,40,30'); + expect(queryData.get('BBOX')).to.be('24.95,14.95,35.05,25.05'); expect(queryData.get('CRS')).to.be('EPSG:4326'); expect(queryData.get('FORMAT')).to.be('image/png'); - expect(queryData.get('HEIGHT')).to.be('200'); - expect(queryData.get('I')).to.be('100'); - expect(queryData.get('J')).to.be('100'); + 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('foo,bar'); expect(queryData.get('REQUEST')).to.be('GetFeatureInfo'); @@ -192,7 +190,7 @@ describe('ol.source.ImageWMS', function() { 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('200'); + expect(queryData.get('WIDTH')).to.be('101'); expect(uri.getFragment()).to.be.empty(); }); });