Merge pull request #85 from elemoine/tilefunction

Add ol.TileUrlFunction.createBboxParam
This commit is contained in:
Éric Lemoine
2012-11-04 09:52:09 -08:00
3 changed files with 78 additions and 33 deletions
+34 -33
View File
@@ -5,6 +5,7 @@ goog.provide('ol.source.TiledWMS');
goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('goog.object'); goog.require('goog.object');
goog.require('goog.uri.utils');
goog.require('ol.Attribution'); goog.require('ol.Attribution');
goog.require('ol.Projection'); goog.require('ol.Projection');
goog.require('ol.TileCoord'); goog.require('ol.TileCoord');
@@ -30,8 +31,10 @@ ol.source.TiledWMS = function(tiledWMSOptions) {
var version = goog.isDef(tiledWMSOptions.version) ? var version = goog.isDef(tiledWMSOptions.version) ?
tiledWMSOptions.version : '1.3'; tiledWMSOptions.version : '1.3';
var tileGrid = tiledWMSOptions.tileGrid; var tileGrid;
if (!goog.isDef(tileGrid)) { if (goog.isDef(tiledWMSOptions.tileGrid)) {
tileGrid = tiledWMSOptions.tileGrid;
} else {
// FIXME Factor this out to a more central/generic place. // FIXME Factor this out to a more central/generic place.
var size = Math.max( var size = Math.max(
projectionExtent.maxX - projectionExtent.minX, projectionExtent.maxX - projectionExtent.minX,
@@ -48,37 +51,35 @@ ol.source.TiledWMS = function(tiledWMSOptions) {
}); });
} }
function tileUrlFunction(tileCoord) { var baseParams = {
if (goog.isNull(tileCoord)) { 'SERVICE': 'WMS',
return undefined; 'VERSION': version,
} 'REQUEST': 'GetMap',
var tileSize = tileGrid.getTileSize(); 'STYLES': '',
var tileExtent = tileGrid.getTileCoordExtent(tileCoord); 'FORMAT': 'image/png',
var params = { 'TRANSPARENT': true
'SERVICE': 'WMS', };
'VERSION': version, var tileSize = tileGrid.getTileSize();
'REQUEST': 'GetMap', baseParams['WIDTH'] = tileSize.width;
'WIDTH': tileSize.width, baseParams['HEIGHT'] = tileSize.height;
'HEIGHT': tileSize.height, baseParams[version >= '1.3' ? 'CRS' : 'SRS'] = projection.getCode();
'STYLES': '', goog.object.extend(baseParams, tiledWMSOptions.params);
'FORMAT': 'image/png',
'TRANSPARENT': true, var tileUrlFunction;
// FIXME Projection dependant axis order. if (tiledWMSOptions.urls) {
'BBOX': [ var tileUrlFunctions = goog.array.map(
tileExtent.minX, tileExtent.minY, tileExtent.maxX, tileExtent.maxY tiledWMSOptions.urls, function(url) {
].join(',') url = goog.uri.utils.appendParamsFromMap(url, baseParams);
}; return ol.TileUrlFunction.createBboxParam(url, tileGrid);
params[version >= '1.3' ? 'CRS' : 'SRS'] = projection.getCode(); });
goog.object.extend(params, tiledWMSOptions.params); tileUrlFunction = ol.TileUrlFunction.createFromTileUrlFunctions(
var url = tiledWMSOptions.urls ? tileUrlFunctions);
tiledWMSOptions.urls[goog.math.modulo( } else if (tiledWMSOptions.url) {
tileCoord.hash(), tiledWMSOptions.urls.length)] : var url = goog.uri.utils.appendParamsFromMap(
tiledWMSOptions.url; tiledWMSOptions.url, baseParams);
for (var param in params) { tileUrlFunction = ol.TileUrlFunction.createBboxParam(url, tileGrid);
url += (~url.indexOf('?') ? '&' : '?') + } else {
param + '=' + encodeURIComponent(params[param]); tileUrlFunction = ol.TileUrlFunction.nullTileUrlFunction;
}
return url;
} }
function tileCoordTransform(tileCoord) { function tileCoordTransform(tileCoord) {
+23
View File
@@ -2,7 +2,9 @@ goog.provide('ol.TileUrlFunction');
goog.provide('ol.TileUrlFunctionType'); goog.provide('ol.TileUrlFunctionType');
goog.require('goog.math'); goog.require('goog.math');
goog.require('goog.uri.utils');
goog.require('ol.TileCoord'); goog.require('ol.TileCoord');
goog.require('ol.tilegrid.TileGrid');
/** /**
@@ -67,6 +69,27 @@ ol.TileUrlFunction.createFromTileUrlFunctions = function(tileUrlFunctions) {
}; };
/**
* @param {string} baseUrl Base URL (may have query data).
* @param {ol.tilegrid.TileGrid} tileGrid Tile grid.
* @return {ol.TileUrlFunctionType} Tile URL function.
*/
ol.TileUrlFunction.createBboxParam = function(baseUrl, tileGrid) {
return function(tileCoord) {
if (goog.isNull(tileCoord)) {
return undefined;
} else {
var tileExtent = tileGrid.getTileCoordExtent(tileCoord);
// FIXME Projection dependant axis order.
var bboxValue = [
tileExtent.minX, tileExtent.minY, tileExtent.maxX, tileExtent.maxY
].join(',');
return goog.uri.utils.appendParam(baseUrl, 'BBOX', bboxValue);
}
};
};
/** /**
* @param {ol.TileCoord} tileCoord Tile coordinate. * @param {ol.TileCoord} tileCoord Tile coordinate.
* @return {string|undefined} Tile URL. * @return {string|undefined} Tile URL.
+21
View File
@@ -1,3 +1,6 @@
goog.require('ol.TileUrlFunction');
goog.require('ol.tilegrid.XYZ');
describe('ol.TileUrlFunction', function() { describe('ol.TileUrlFunction', function() {
describe('createFromTemplate', function() { describe('createFromTemplate', function() {
@@ -58,6 +61,24 @@ describe('ol.TileUrlFunction', function() {
expect(tileUrl(null)).toBeUndefined(); expect(tileUrl(null)).toBeUndefined();
}); });
}); });
describe('createBboxParam', function() {
var tileGrid;
beforeEach(function() {
tileGrid = new ol.tilegrid.XYZ({
maxZoom: 10
});
});
it('creates expected URL', function() {
var tileUrlFunction = ol.TileUrlFunction.createBboxParam(
'http://wms?foo=bar', tileGrid);
var tileCoord = new ol.TileCoord(1, 0, 0);
var tileUrl = tileUrlFunction(tileCoord);
var expected = 'http://wms?foo=bar&BBOX=-20037508.342789244' +
'%2C20037508.342789244%2C0%2C40075016.68557849';
expect(tileUrl).toEqual(expected);
});
});
}); });
goog.require('ol.TileCoord'); goog.require('ol.TileCoord');