Support tile sources without configured projection

This change adds a lot of flexibility to working with tile
layers: Sources where the server projection or tile grid do not
matter can now be constructed without specifying a projection or
tile grid.

The tileUrlFunction/imageUrlFunction now also creates updated
URLs when the params of the layer change, so things like
mergeNewParams in ol2 will be possible.

A nice side effect of this whole change is that there is no more
duplicated code between tiled and single image WMS layers.

While I was at it, I also fixed a WMS 1.1.1 axis order issue
and incorrect STYLES params (STYLES=& instead of STYLES&).
This commit is contained in:
ahocevar
2013-03-05 00:46:58 +01:00
parent cad215e0cc
commit 586f393492
24 changed files with 320 additions and 206 deletions

View File

@@ -14,10 +14,6 @@ describe('ol.source.TileSource', function() {
describe('#findLoadedTiles()', function() {
function isLoaded(tile) {
return !goog.isNull(tile) && tile.getState() === ol.TileState.LOADED;
}
it('adds no tiles if none are already loaded', function() {
// a source with no loaded tiles
var source = new ol.test.source.MockTileSource({});
@@ -25,7 +21,13 @@ describe('ol.source.TileSource', function() {
var loadedTilesByZ = {};
var grid = source.getTileGrid();
var range = grid.getTileRangeForExtentAndZ(source.getExtent(), 3);
source.findLoadedTiles(loadedTilesByZ, isLoaded, 3, range);
function getTileIfLoaded(tileCoord) {
var tile = source.getTile(tileCoord, null, null);
return (!goog.isNull(tile) && tile.getState() === ol.TileState.LOADED) ?
tile : null;
}
source.findLoadedTiles(loadedTilesByZ, getTileIfLoaded, 3, range);
var keys = goog.object.getKeys(loadedTilesByZ);
expect(keys.length).toBe(0);
@@ -41,7 +43,13 @@ describe('ol.source.TileSource', function() {
var loadedTilesByZ = {};
var grid = source.getTileGrid();
var range = grid.getTileRangeForExtentAndZ(source.getExtent(), 0);
source.findLoadedTiles(loadedTilesByZ, isLoaded, 0, range);
function getTileIfLoaded(tileCoord) {
var tile = source.getTile(tileCoord, null, null);
return (!goog.isNull(tile) && tile.getState() === ol.TileState.LOADED) ?
tile : null;
}
source.findLoadedTiles(loadedTilesByZ, getTileIfLoaded, 0, range);
var keys = goog.object.getKeys(loadedTilesByZ);
expect(keys.length).toBe(1);
var tile = loadedTilesByZ['0']['0/0/0'];
@@ -59,7 +67,13 @@ describe('ol.source.TileSource', function() {
var loadedTilesByZ = {};
var grid = source.getTileGrid();
var range = grid.getTileRangeForExtentAndZ(source.getExtent(), 1);
source.findLoadedTiles(loadedTilesByZ, isLoaded, 1, range);
function getTileIfLoaded(tileCoord) {
var tile = source.getTile(tileCoord, null, null);
return (!goog.isNull(tile) && tile.getState() === ol.TileState.LOADED) ?
tile : null;
}
source.findLoadedTiles(loadedTilesByZ, getTileIfLoaded, 1, range);
var keys = goog.object.getKeys(loadedTilesByZ);
expect(keys.length).toBe(1);
var tile = loadedTilesByZ['1']['1/0/0'];
@@ -79,7 +93,13 @@ describe('ol.source.TileSource', function() {
var loadedTilesByZ = {};
var grid = source.getTileGrid();
var range = grid.getTileRangeForExtentAndZ(source.getExtent(), 1);
var loaded = source.findLoadedTiles(loadedTilesByZ, isLoaded, 1, range);
function getTileIfLoaded(tileCoord) {
var tile = source.getTile(tileCoord, null, null);
return (!goog.isNull(tile) && tile.getState() === ol.TileState.LOADED) ?
tile : null;
}
var loaded = source.findLoadedTiles(
loadedTilesByZ, getTileIfLoaded, 1, range);
expect(loaded).toBe(true);
});
@@ -98,7 +118,14 @@ describe('ol.source.TileSource', function() {
};
var grid = source.getTileGrid();
var range = grid.getTileRangeForExtentAndZ(source.getExtent(), 1);
var loaded = source.findLoadedTiles(loadedTilesByZ, isLoaded, 1, range);
function getTileIfLoaded(tileCoord) {
var tile = source.getTile(tileCoord, null, null);
return (!goog.isNull(tile) && tile.getState() === ol.TileState.LOADED) ?
tile : null;
}
var loaded = source.findLoadedTiles(
loadedTilesByZ, getTileIfLoaded, 1, range);
expect(loaded).toBe(true);
});
@@ -114,7 +141,14 @@ describe('ol.source.TileSource', function() {
var loadedTilesByZ = {};
var grid = source.getTileGrid();
var range = grid.getTileRangeForExtentAndZ(source.getExtent(), 1);
var loaded = source.findLoadedTiles(loadedTilesByZ, isLoaded, 1, range);
function getTileIfLoaded(tileCoord) {
var tile = source.getTile(tileCoord, null, null);
return (!goog.isNull(tile) && tile.getState() === ol.TileState.LOADED) ?
tile : null;
}
var loaded = source.findLoadedTiles(
loadedTilesByZ, getTileIfLoaded, 1, range);
expect(loaded).toBe(false);
});
@@ -132,7 +166,14 @@ describe('ol.source.TileSource', function() {
};
var grid = source.getTileGrid();
var range = grid.getTileRangeForExtentAndZ(source.getExtent(), 1);
var loaded = source.findLoadedTiles(loadedTilesByZ, isLoaded, 1, range);
function getTileIfLoaded(tileCoord) {
var tile = source.getTile(tileCoord, null, null);
return (!goog.isNull(tile) && tile.getState() === ol.TileState.LOADED) ?
tile : null;
}
var loaded = source.findLoadedTiles(
loadedTilesByZ, getTileIfLoaded, 1, range);
expect(loaded).toBe(false);
});

View File

@@ -2,7 +2,7 @@ goog.provide('ol.test.source.XYZ');
describe('ol.source.XYZ', function() {
describe('getTileCoordUrl', function() {
describe('tileUrlFunction', function() {
var xyzTileSource, tileGrid;
@@ -18,59 +18,59 @@ describe('ol.source.XYZ', function() {
var coordinate = new ol.Coordinate(829330.2064098881, 5933916.615134273);
var tileUrl;
tileUrl = xyzTileSource.getTileCoordUrl(
tileUrl = xyzTileSource.tileUrlFunction(
tileGrid.getTileCoordForCoordAndZ(coordinate, 0));
expect(tileUrl).toEqual('0/0/0');
tileUrl = xyzTileSource.getTileCoordUrl(
tileUrl = xyzTileSource.tileUrlFunction(
tileGrid.getTileCoordForCoordAndZ(coordinate, 1));
expect(tileUrl).toEqual('1/1/0');
tileUrl = xyzTileSource.getTileCoordUrl(
tileUrl = xyzTileSource.tileUrlFunction(
tileGrid.getTileCoordForCoordAndZ(coordinate, 2));
expect(tileUrl).toEqual('2/2/1');
tileUrl = xyzTileSource.getTileCoordUrl(
tileUrl = xyzTileSource.tileUrlFunction(
tileGrid.getTileCoordForCoordAndZ(coordinate, 3));
expect(tileUrl).toEqual('3/4/2');
tileUrl = xyzTileSource.getTileCoordUrl(
tileUrl = xyzTileSource.tileUrlFunction(
tileGrid.getTileCoordForCoordAndZ(coordinate, 4));
expect(tileUrl).toEqual('4/8/5');
tileUrl = xyzTileSource.getTileCoordUrl(
tileUrl = xyzTileSource.tileUrlFunction(
tileGrid.getTileCoordForCoordAndZ(coordinate, 5));
expect(tileUrl).toEqual('5/16/11');
tileUrl = xyzTileSource.getTileCoordUrl(
tileUrl = xyzTileSource.tileUrlFunction(
tileGrid.getTileCoordForCoordAndZ(coordinate, 6));
expect(tileUrl).toEqual('6/33/22');
});
describe('wrap x', function() {
it('returns the expected URL', function() {
var tileUrl = xyzTileSource.getTileCoordUrl(
var tileUrl = xyzTileSource.tileUrlFunction(
new ol.TileCoord(6, -31, -23));
expect(tileUrl).toEqual('6/33/22');
tileUrl = xyzTileSource.getTileCoordUrl(new ol.TileCoord(6, 33, -23));
tileUrl = xyzTileSource.tileUrlFunction(new ol.TileCoord(6, 33, -23));
expect(tileUrl).toEqual('6/33/22');
tileUrl = xyzTileSource.getTileCoordUrl(new ol.TileCoord(6, 97, -23));
tileUrl = xyzTileSource.tileUrlFunction(new ol.TileCoord(6, 97, -23));
expect(tileUrl).toEqual('6/33/22');
});
});
describe('crop y', function() {
it('returns the expected URL', function() {
var tileUrl = xyzTileSource.getTileCoordUrl(
var tileUrl = xyzTileSource.tileUrlFunction(
new ol.TileCoord(6, 33, -87));
expect(tileUrl).toBeUndefined();
tileUrl = xyzTileSource.getTileCoordUrl(new ol.TileCoord(6, 33, -23));
tileUrl = xyzTileSource.tileUrlFunction(new ol.TileCoord(6, 33, -23));
expect(tileUrl).toEqual('6/33/22');
tileUrl = xyzTileSource.getTileCoordUrl(new ol.TileCoord(6, 33, 41));
tileUrl = xyzTileSource.tileUrlFunction(new ol.TileCoord(6, 33, 41));
expect(tileUrl).toBeUndefined();
});
});

View File

@@ -169,16 +169,16 @@ describe('ol.tilegrid.TileGrid', function() {
expect(grid).toBeA(ol.tilegrid.TileGrid);
var resolutions = grid.getResolutions();
expect(resolutions.length).toBe(19);
expect(resolutions.length).toBe(ol.DEFAULT_MAX_ZOOM + 1);
});
it('accepts a number of zoom levels', function() {
var projection = ol.projection.getFromCode('EPSG:3857');
var grid = ol.tilegrid.createForProjection(projection, 22);
var grid = ol.tilegrid.createForProjection(projection, 18);
expect(grid).toBeA(ol.tilegrid.TileGrid);
var resolutions = grid.getResolutions();
expect(resolutions.length).toBe(23);
expect(resolutions.length).toBe(19);
});
it('accepts a big number of zoom levels', function() {
@@ -192,6 +192,28 @@ describe('ol.tilegrid.TileGrid', function() {
});
describe('getForProjection', function() {
it('gets the default tile grid for a projection', function() {
var projection = ol.projection.getFromCode('EPSG:3857');
var grid = ol.tilegrid.getForProjection(projection);
expect(grid).toBeA(ol.tilegrid.TileGrid);
var resolutions = grid.getResolutions();
expect(resolutions.length).toBe(ol.DEFAULT_MAX_ZOOM + 1);
expect(grid.getTileSize().toString()).toBe('(256 x 256)');
});
it('stores the default tile grid on a projection', function() {
var projection = ol.projection.getFromCode('EPSG:3857');
var grid = ol.tilegrid.getForProjection(projection);
var gridAgain = ol.tilegrid.getForProjection(projection);
expect(grid).toBe(gridAgain);
});
});
describe('getTileCoordFromCoordAndZ', function() {
describe('Y North, X East', function() {

View File

@@ -61,7 +61,7 @@ describe('ol.TileUrlFunction', function() {
});
});
describe('createBboxParam', function() {
describe('createWMSParams', function() {
var tileGrid;
beforeEach(function() {
tileGrid = new ol.tilegrid.XYZ({
@@ -70,24 +70,26 @@ describe('ol.TileUrlFunction', function() {
});
it('creates expected URL', function() {
var epsg3857 = ol.projection.getFromCode('EPSG:3857');
var tileUrlFunction = ol.TileUrlFunction.createBboxParam(
'http://wms?foo=bar', tileGrid, epsg3857.getAxisOrientation());
var tileUrlFunction = ol.TileUrlFunction.createWMSParams(
'http://wms?foo=bar', {});
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' +
'&HEIGHT=256&WIDTH=256';
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&BBOX=-20037508.342789244%2C20037508.342789244%2C0%2C' +
'40075016.68557849&CRS=EPSG%3A3857&STYLES=';
expect(tileUrl).toEqual(expected);
});
it('creates expected URL respecting axis orientation', function() {
var epsg4326 = ol.projection.getFromCode('EPSG:4326');
var tileUrlFunction = ol.TileUrlFunction.createBboxParam(
'http://wms?foo=bar', tileGrid, epsg4326.getAxisOrientation());
var tileUrlFunction = ol.TileUrlFunction.createWMSParams(
'http://wms?foo=bar', {});
var tileCoord = new ol.TileCoord(1, 0, 0);
var tileUrl = tileUrlFunction(tileCoord);
var expected = 'http://wms?foo=bar&BBOX=20037508.342789244' +
'%2C-20037508.342789244%2C40075016.68557849%2C0' +
'&HEIGHT=256&WIDTH=256';
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&BBOX=20037508.342789244%2C-20037508.342789244%2C' +
'40075016.68557849%2C0&CRS=EPSG%3A4326&STYLES=';
expect(tileUrl).toEqual(expected);
});
});