Merge pull request #402 from ahocevar/more-context

More convenient url functions for WMS. r=@elemoine,@twpayne
This commit is contained in:
ahocevar
2013-03-20 06:22:36 -07:00
6 changed files with 79 additions and 48 deletions

View File

@@ -3,24 +3,26 @@ goog.provide('ol.ImageUrlFunctionType');
goog.require('ol.Extent');
goog.require('ol.Size');
goog.require('ol.source.wms');
/**
* @typedef {function(ol.Extent, ol.Size, ol.Projection): (string|undefined)}
* @typedef {function(this:ol.source.Source, ol.Extent, ol.Size, ol.Projection):
* (string|undefined)}
*/
ol.ImageUrlFunctionType;
/**
* @param {string} baseUrl Base URL (may have query data).
* @param {Object.<string, string|number>} params WMS parameters.
* @param {Object.<string,*>} params to encode in the url.
* @param {function(string, Object.<string,*>, ol.Extent, ol.Size,
* ol.Projection)} paramsFunction params function.
* @return {ol.ImageUrlFunctionType} Image URL function.
*/
ol.ImageUrlFunction.createWMSParams =
function(baseUrl, params) {
ol.ImageUrlFunction.createFromParamsFunction =
function(baseUrl, params, paramsFunction) {
return function(extent, size, projection) {
return ol.source.wms.getUrl(
return paramsFunction(
baseUrl, params, extent, size, projection);
};
};

View File

@@ -5,6 +5,7 @@ goog.require('ol.Image');
goog.require('ol.ImageUrlFunction');
goog.require('ol.Size');
goog.require('ol.source.ImageSource');
goog.require('ol.source.wms');
@@ -15,7 +16,8 @@ goog.require('ol.source.ImageSource');
*/
ol.source.SingleImageWMS = function(options) {
var imageUrlFunction = goog.isDef(options.url) ?
ol.ImageUrlFunction.createWMSParams(options.url, options.params) :
ol.ImageUrlFunction.createFromParamsFunction(
options.url, options.params, ol.source.wms.getUrl) :
ol.ImageUrlFunction.nullImageUrlFunction;
goog.base(this, {

View File

@@ -8,6 +8,7 @@ goog.require('ol.Extent');
goog.require('ol.TileCoord');
goog.require('ol.TileUrlFunction');
goog.require('ol.source.ImageTileSource');
goog.require('ol.source.wms');
@@ -30,12 +31,13 @@ ol.source.TiledWMS = function(tiledWMSOptions) {
if (goog.isDef(urls)) {
var tileUrlFunctions = goog.array.map(
urls, function(url) {
return ol.TileUrlFunction.createWMSParams(
url, tiledWMSOptions.params);
return ol.TileUrlFunction.createFromParamsFunction(
url, tiledWMSOptions.params, ol.source.wms.getUrl);
});
tileUrlFunction = ol.TileUrlFunction.createFromTileUrlFunctions(
tileUrlFunctions);
}
var transparent = goog.isDef(tiledWMSOptions.params['TRANSPARENT']) ?
tiledWMSOptions.params['TRANSPARENT'] : true;
var extent = tiledWMSOptions.extent;

View File

@@ -4,13 +4,12 @@ goog.provide('ol.TileUrlFunctionType');
goog.require('goog.array');
goog.require('goog.math');
goog.require('ol.TileCoord');
goog.require('ol.source.wms');
goog.require('ol.tilegrid.TileGrid');
/**
* @typedef {function(ol.TileCoord, ol.tilegrid.TileGrid, ol.Projection):
* (string|undefined)}
* @typedef {function(this:ol.source.Source, ol.TileCoord, ol.tilegrid.TileGrid,
* ol.Projection): (string|undefined)}
*/
ol.TileUrlFunctionType;
@@ -63,18 +62,20 @@ ol.TileUrlFunction.createFromTileUrlFunctions = function(tileUrlFunctions) {
/**
* @param {string} baseUrl Base URL (may have query data).
* @param {Object.<string, string|number>} params WMS parameters.
* @param {Object.<string,*>} params to encode in the url.
* @param {function(string, Object.<string,*>, ol.Extent, ol.Size,
* ol.Projection)} paramsFunction params function.
* @return {ol.TileUrlFunctionType} Tile URL function.
*/
ol.TileUrlFunction.createWMSParams =
function(baseUrl, params) {
ol.TileUrlFunction.createFromParamsFunction =
function(baseUrl, params, paramsFunction) {
return function(tileCoord, tileGrid, projection) {
if (goog.isNull(tileCoord)) {
return undefined;
} else {
var size = tileGrid.getTileSize(tileCoord.z);
var extent = tileGrid.getTileCoordExtent(tileCoord);
return ol.source.wms.getUrl(
return paramsFunction(
baseUrl, params, extent, size, projection);
}
};
@@ -102,7 +103,7 @@ ol.TileUrlFunction.withTileCoordTransform =
if (goog.isNull(tileCoord)) {
return undefined;
} else {
return tileUrlFunction(
return tileUrlFunction.call(this,
transformFn(tileCoord, tileGrid, projection), tileGrid, projection);
}
};

View File

@@ -0,0 +1,39 @@
goog.provide('ol.source.test.wms');
describe('ol.source.wms', function() {
describe('ol.source.wms.getUrl', function() {
it('creates expected URL', function() {
var epsg3857 = ol.projection.get('EPSG:3857');
var tileGrid = ol.tilegrid.getForProjection(epsg3857);
var tileUrlFunction = ol.TileUrlFunction.createFromParamsFunction(
'http://wms', {'foo': 'bar'}, ol.source.wms.getUrl);
var tileCoord = new ol.TileCoord(1, 0, 0);
var tileUrl = tileUrlFunction(tileCoord, tileGrid, epsg3857);
var expected = 'http://wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=' +
'GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&WIDTH=256&HEIGHT=256&' +
'foo=bar&STYLES=&CRS=EPSG%3A3857&BBOX=' +
'-20037508.342789244%2C-20037508.342789244%2C0%2C0';
expect(tileUrl).to.eql(expected);
});
it('creates expected URL respecting axis orientation', function() {
var epsg4326 = ol.projection.get('EPSG:4326');
var tileGrid = ol.tilegrid.getForProjection(epsg4326);
var tileUrlFunction = ol.TileUrlFunction.createFromParamsFunction(
'http://wms', {'foo': 'bar'}, ol.source.wms.getUrl);
var tileCoord = new ol.TileCoord(1, 0, 0);
var tileUrl = tileUrlFunction(tileCoord, tileGrid, epsg4326);
var expected = 'http://wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=' +
'GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&WIDTH=256&HEIGHT=256&' +
'foo=bar&STYLES=&CRS=EPSG%3A4326&BBOX=-90%2C-180%2C90%2C0';
expect(tileUrl).to.eql(expected);
});
});
});
goog.require('ol.TileCoord');
goog.require('ol.TileUrlFunction');
goog.require('ol.projection');
goog.require('ol.source.wms');

View File

@@ -78,41 +78,26 @@ describe('ol.TileUrlFunction', function() {
});
});
describe('createWMSParams', function() {
var tileGrid;
beforeEach(function() {
tileGrid = new ol.tilegrid.XYZ({
maxZoom: 10
});
});
it('creates expected URL', function() {
var epsg3857 = ol.projection.get('EPSG:3857');
var tileUrlFunction = ol.TileUrlFunction.createWMSParams(
'http://wms?foo=bar', {});
var tileCoord = new ol.TileCoord(1, 0, 0);
var tileUrl = tileUrlFunction(tileCoord, tileGrid, epsg3857);
var expected = 'http://wms?foo=bar&SERVICE=WMS&VERSION=1.3.0&REQUEST=' +
'GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&WIDTH=256&HEIGHT=256&' +
'STYLES=&CRS=EPSG%3A3857&BBOX=-20037508.342789244%2C2' +
'0037508.342789244%2C0%2C40075016.68557849';
expect(tileUrl).to.eql(expected);
});
it('creates expected URL respecting axis orientation', function() {
var epsg4326 = ol.projection.get('EPSG:4326');
var tileUrlFunction = ol.TileUrlFunction.createWMSParams(
'http://wms?foo=bar', {});
var tileCoord = new ol.TileCoord(1, 0, 0);
var tileUrl = tileUrlFunction(tileCoord, tileGrid, epsg4326);
var expected = 'http://wms?foo=bar&SERVICE=WMS&VERSION=1.3.0&REQUEST=' +
'GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&WIDTH=256&HEIGHT=256&' +
'STYLES=&CRS=EPSG%3A4326&BBOX=20037508.342789244%2C' +
'-20037508.342789244%2C40075016.68557849%2C0';
expect(tileUrl).to.eql(expected);
describe('createFromParamsFunction', function() {
var paramsFunction = function(url, params) { return arguments; };
var projection = ol.projection.get('EPSG:3857');
var params = {foo: 'bar'};
var tileUrlFunction = ol.TileUrlFunction.createFromParamsFunction(
'url', params, paramsFunction);
it('calls the passed function with the correct arguments', function() {
var args = tileUrlFunction(new ol.TileCoord(0, 0, 0),
ol.tilegrid.getForProjection(projection), projection);
expect(args[0]).to.eql('url');
expect(args[1]).to.be(params);
expect(args[2]).to.eql(projection.getExtent());
expect(args[3]).to.eql(new ol.Size(256, 256));
expect(args[4]).to.eql(projection);
});
});
});
goog.require('ol.Size');
goog.require('ol.TileCoord');
goog.require('ol.TileUrlFunction');
goog.require('ol.projection');
goog.require('ol.tilegrid.XYZ');