Add ol.TileUrlFunction.createBboxParam
This commit is contained in:
@@ -30,8 +30,10 @@ ol.source.TiledWMS = function(tiledWMSOptions) {
|
||||
var version = goog.isDef(tiledWMSOptions.version) ?
|
||||
tiledWMSOptions.version : '1.3';
|
||||
|
||||
var tileGrid = tiledWMSOptions.tileGrid;
|
||||
if (!goog.isDef(tileGrid)) {
|
||||
var tileGrid;
|
||||
if (goog.isDef(tiledWMSOptions.tileGrid)) {
|
||||
tileGrid = tiledWMSOptions.tileGrid;
|
||||
} else {
|
||||
// FIXME Factor this out to a more central/generic place.
|
||||
var size = Math.max(
|
||||
projectionExtent.maxX - projectionExtent.minX,
|
||||
@@ -48,37 +50,34 @@ ol.source.TiledWMS = function(tiledWMSOptions) {
|
||||
});
|
||||
}
|
||||
|
||||
function tileUrlFunction(tileCoord) {
|
||||
if (goog.isNull(tileCoord)) {
|
||||
return undefined;
|
||||
}
|
||||
var tileSize = tileGrid.getTileSize();
|
||||
var tileExtent = tileGrid.getTileCoordExtent(tileCoord);
|
||||
var params = {
|
||||
'SERVICE': 'WMS',
|
||||
'VERSION': version,
|
||||
'REQUEST': 'GetMap',
|
||||
'WIDTH': tileSize.width,
|
||||
'HEIGHT': tileSize.height,
|
||||
'STYLES': '',
|
||||
'FORMAT': 'image/png',
|
||||
'TRANSPARENT': true,
|
||||
// FIXME Projection dependant axis order.
|
||||
'BBOX': [
|
||||
tileExtent.minX, tileExtent.minY, tileExtent.maxX, tileExtent.maxY
|
||||
].join(',')
|
||||
};
|
||||
params[version >= '1.3' ? 'CRS' : 'SRS'] = projection.getCode();
|
||||
goog.object.extend(params, tiledWMSOptions.params);
|
||||
var url = tiledWMSOptions.urls ?
|
||||
tiledWMSOptions.urls[goog.math.modulo(
|
||||
tileCoord.hash(), tiledWMSOptions.urls.length)] :
|
||||
tiledWMSOptions.url;
|
||||
for (var param in params) {
|
||||
url += (~url.indexOf('?') ? '&' : '?') +
|
||||
param + '=' + encodeURIComponent(params[param]);
|
||||
}
|
||||
return url;
|
||||
var baseParams = {
|
||||
'SERVICE': 'WMS',
|
||||
'VERSION': version,
|
||||
'REQUEST': 'GetMap',
|
||||
'STYLES': '',
|
||||
'FORMAT': 'image/png',
|
||||
'TRANSPARENT': true
|
||||
};
|
||||
var tileSize = tileGrid.getTileSize();
|
||||
baseParams['WIDTH'] = tileSize.width;
|
||||
baseParams['HEIGHT'] = tileSize.height;
|
||||
baseParams[version >= '1.3' ? 'CRS' : 'SRS'] = projection.getCode();
|
||||
goog.object.extend(baseParams, tiledWMSOptions.params);
|
||||
|
||||
var tileUrlFunction;
|
||||
if (tiledWMSOptions.urls) {
|
||||
var tileUrlFunctions = goog.array.map(
|
||||
tiledWMSOptions.urls, function(url) {
|
||||
return ol.TileUrlFunction.createBboxParam(
|
||||
url, baseParams, tileGrid);
|
||||
});
|
||||
tileUrlFunction = ol.TileUrlFunction.createFromTileUrlFunctions(
|
||||
tileUrlFunctions);
|
||||
} else if (tiledWMSOptions.url) {
|
||||
tileUrlFunction = ol.TileUrlFunction.createBboxParam(
|
||||
tiledWMSOptions.url, baseParams, tileGrid);
|
||||
} else {
|
||||
tileUrlFunction = ol.TileUrlFunction.nullTileUrlFunction;
|
||||
}
|
||||
|
||||
function tileCoordTransform(tileCoord) {
|
||||
|
||||
@@ -3,6 +3,7 @@ goog.provide('ol.TileUrlFunctionType');
|
||||
|
||||
goog.require('goog.math');
|
||||
goog.require('ol.TileCoord');
|
||||
goog.require('ol.tilegrid.TileGrid');
|
||||
|
||||
|
||||
/**
|
||||
@@ -67,6 +68,35 @@ ol.TileUrlFunction.createFromTileUrlFunctions = function(tileUrlFunctions) {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {string} baseUrl WMS base URL.
|
||||
* @param {Object} baseParams Query string parameters.
|
||||
* @param {ol.tilegrid.TileGrid} tileGrid Tile grid.
|
||||
* @return {ol.TileUrlFunctionType} Tile URL function.
|
||||
*/
|
||||
ol.TileUrlFunction.createBboxParam = function(baseUrl, baseParams, tileGrid) {
|
||||
return function(tileCoord) {
|
||||
if (goog.isNull(tileCoord)) {
|
||||
return undefined;
|
||||
} else {
|
||||
var tileExtent = tileGrid.getTileCoordExtent(tileCoord);
|
||||
var params = goog.object.clone(baseParams);
|
||||
// FIXME Projection dependant axis order.
|
||||
var bboxValue = [
|
||||
tileExtent.minX, tileExtent.minY, tileExtent.maxX, tileExtent.maxY
|
||||
].join(',');
|
||||
goog.object.extend(params, {'BBOX': bboxValue});
|
||||
var url = baseUrl;
|
||||
for (var p in params) {
|
||||
url += (~url.indexOf('?') ? '&' : '?') +
|
||||
p + '=' + encodeURIComponent(params[p]);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.TileCoord} tileCoord Tile coordinate.
|
||||
* @return {string|undefined} Tile URL.
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
goog.require('ol.TileUrlFunction');
|
||||
goog.require('ol.tilegrid.XYZ');
|
||||
|
||||
describe('ol.TileUrlFunction', function() {
|
||||
|
||||
describe('createFromTemplate', function() {
|
||||
@@ -58,6 +61,37 @@ describe('ol.TileUrlFunction', function() {
|
||||
expect(tileUrl(null)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('createBboxParam', function() {
|
||||
var tileGrid;
|
||||
beforeEach(function() {
|
||||
tileGrid = new ol.tilegrid.XYZ({
|
||||
maxZoom: 10
|
||||
});
|
||||
});
|
||||
describe('base params in object', function() {
|
||||
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);
|
||||
});
|
||||
});
|
||||
describe('base params in URL', function() {
|
||||
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');
|
||||
|
||||
Reference in New Issue
Block a user