Correctly size Layer.Grid and Layer.KaMap in rows/cols for all values of Grid.buffer.

Closes bug #928. Thanks a mil to bartvde for identifying the solution.


git-svn-id: http://svn.openlayers.org/trunk/openlayers@4317 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Schuyler Erle
2007-09-15 14:48:22 +00:00
parent 121a5553f4
commit c941651e72
4 changed files with 103 additions and 22 deletions

View File

@@ -306,10 +306,13 @@ OpenLayers.Layer.Grid = OpenLayers.Class(OpenLayers.Layer.HTTPRequest, {
initGriddedTiles:function(bounds) {
// work out mininum number of rows and columns; this is the number of
// tiles required to cover the viewport plus one for panning
// tiles required to cover the viewport plus at least one for panning
var viewSize = this.map.getSize();
var minRows = Math.ceil(viewSize.h/this.tileSize.h) + 1;
var minCols = Math.ceil(viewSize.w/this.tileSize.w) + 1;
var minRows = Math.ceil(viewSize.h/this.tileSize.h) +
Math.max(1, 2 * this.buffer);
var minCols = Math.ceil(viewSize.w/this.tileSize.w) +
Math.max(1, 2 * this.buffer);
var extent = this.map.getMaxExtent();
var resolution = this.map.getResolution();
@@ -528,7 +531,7 @@ OpenLayers.Layer.Grid = OpenLayers.Class(OpenLayers.Layer.HTTPRequest, {
* bounds - {<OpenLayers.Bounds>}
*/
moveGriddedTiles: function(bounds) {
var buffer = (this.buffer) ? this.buffer*1.5 : 1;
var buffer = this.buffer || 1;
while (true) {
var tlLayer = this.grid[0][0].position;
var tlViewPort =

View File

@@ -114,23 +114,25 @@ OpenLayers.Layer.KaMap = OpenLayers.Class(OpenLayers.Layer.Grid, {
/**
* Method: initGriddedTiles
*/
initGriddedTiles:function() {
initGriddedTiles:function(bounds) {
var viewSize = this.map.getSize();
var bounds = this.map.getExtent();
var minRows = Math.ceil(viewSize.h/this.tileSize.h) + Math.max(1, 2*this.buffer);
var minCols = Math.ceil(viewSize.w/this.tileSize.w) + Math.max(1, 2*this.buffer);
var extent = this.map.getMaxExtent();
var resolution = this.map.getResolution();
var tilelon = resolution*this.tileSize.w;
var tilelat = resolution*this.tileSize.h;
var offsetlon = bounds.left;
var tilecol = Math.floor(offsetlon/tilelon);
var tilecol = Math.floor(offsetlon/tilelon) - this.buffer;
var tilecolremain = offsetlon/tilelon - tilecol;
var tileoffsetx = -tilecolremain * this.tileSize.w;
var tileoffsetlon = tilecol * tilelon;
var offsetlat = bounds.top;
var tilerow = Math.ceil(offsetlat/tilelat);
var tilerow = Math.ceil(offsetlat/tilelat) + this.buffer;
var tilerowremain = tilerow - offsetlat/tilelat;
var tileoffsety = -(tilerowremain+1) * this.tileSize.h;
var tileoffsetlat = tilerow * tilelat;
@@ -146,9 +148,7 @@ OpenLayers.Layer.KaMap = OpenLayers.Class(OpenLayers.Layer.Grid, {
var rowidx = 0;
do {
var row;
row = this.grid[rowidx++];
var row = this.grid[rowidx++];
if (!row) {
row = [];
this.grid.push(row);
@@ -160,10 +160,11 @@ OpenLayers.Layer.KaMap = OpenLayers.Class(OpenLayers.Layer.Grid, {
var colidx = 0;
do {
var tileBounds = new OpenLayers.Bounds(tileoffsetlon,
tileoffsetlat,
tileoffsetlon + tilelon,
tileoffsetlat + tilelat);
var tileBounds =
new OpenLayers.Bounds(tileoffsetlon,
tileoffsetlat,
tileoffsetlon + tilelon,
tileoffsetlat + tilelat);
var x = tileoffsetx;
x -= parseInt(this.map.layerContainerDiv.style.left);
@@ -172,11 +173,10 @@ OpenLayers.Layer.KaMap = OpenLayers.Class(OpenLayers.Layer.Grid, {
y -= parseInt(this.map.layerContainerDiv.style.top);
var px = new OpenLayers.Pixel(x, y);
var tile;
tile = row[colidx++];
var tile = row[colidx++];
if (!tile) {
tile = this.addTile(tileBounds, px);
this.addTileMonitoringHooks(tile);
row.push(tile);
} else {
tile.moveTo(tileBounds, px, false);
@@ -184,12 +184,18 @@ OpenLayers.Layer.KaMap = OpenLayers.Class(OpenLayers.Layer.Grid, {
tileoffsetlon += tilelon;
tileoffsetx += this.tileSize.w;
} while (tileoffsetlon <= bounds.right + tilelon * this.buffer)
} while (tileoffsetlon <= bounds.right + tilelon * this.buffer
|| colidx < minCols)
tileoffsetlat -= tilelat;
tileoffsety += this.tileSize.h;
} while(tileoffsetlat >= bounds.bottom - tilelat * this.buffer)
} while(tileoffsetlat >= bounds.bottom - tilelat * this.buffer
|| rowidx < minRows)
//shave off exceess rows and colums
this.removeExcessTiles(rowidx, colidx);
//now actually draw the tiles
this.spiralTileLoad();
},

View File

@@ -547,6 +547,41 @@
t.eq(bounds.toBBOX(), "0,-90,180,90", "get tile bounds returns correct bounds after pan");
}
function test_Layer_Grid_moveTo_buffer_calculation (t) {
t.plan(6);
var map = new OpenLayers.Map( 'map3' ); // odd map size
var layer0 = new OpenLayers.Layer.WMS( "0 buffer: OpenLayers WMS",
"http://labs.metacarta.com/wms/vmap0",
{layers: 'basic'}, {'buffer':0} );
map.addLayer(layer0);
var layer1 = new OpenLayers.Layer.WMS( "1 buffer: OpenLayers WMS",
"http://labs.metacarta.com/wms/vmap0",
{layers: 'basic'}, {'buffer':1} );
map.addLayer(layer1);
var layer2 = new OpenLayers.Layer.WMS( "2 buffer: OpenLayers WMS",
"http://labs.metacarta.com/wms/vmap0",
{layers: 'basic'}, {'buffer':2} );
map.addLayer(layer2);
map.setCenter(new OpenLayers.LonLat(0, 0), 4);
t.eq( layer0.grid.length, 3, "Grid rows with buffer:0" );
map.setBaseLayer(layer1);
t.eq( layer1.grid.length, 4, "Grid rows with buffer:1" );
map.setBaseLayer(layer2);
t.eq( layer2.grid.length, 6, "Grid rows with buffer:2" );
// zooming in on Greenland exercises the bug from pre-r4313
map.setCenter(new OpenLayers.LonLat(0, 90), 4);
t.eq( layer0.grid.length, 3, "Grid rows with buffer:0" );
map.setBaseLayer(layer1);
t.eq( layer1.grid.length, 4, "Grid rows with buffer:1" );
map.setBaseLayer(layer2);
t.eq( layer2.grid.length, 6, "Grid rows with buffer:2" );
}
function test_99_Layer_Grid_destroy (t) {
t.plan( 5 );
@@ -582,5 +617,6 @@
<body>
<div id="map" style="width:499px;height:549px;display:none"></div>
<div id="map2" style="width:500px;height:550px;display:none"></div>
<div id="map3" style="width:594px;height:464px;display:none"></div>
</body>
</html>

View File

@@ -23,6 +23,40 @@
t.ok( layer instanceof OpenLayers.Layer.KaMap, "returns OpenLayers.Layer.KaMap object" );
}
function test_Layer_Grid_moveTo_buffer_calculation (t) {
t.plan(6);
var map = new OpenLayers.Map( 'map3' ); // odd map size
var layer0 = new OpenLayers.Layer.KaMap( "0 buffer: OpenLayers WMS",
"http://labs.metacarta.com/wms/vmap0",
{layers: 'basic'}, {'buffer':0} );
map.addLayer(layer0);
var layer1 = new OpenLayers.Layer.KaMap( "1 buffer: OpenLayers WMS",
"http://labs.metacarta.com/wms/vmap0",
{layers: 'basic'}, {'buffer':1} );
map.addLayer(layer1);
var layer2 = new OpenLayers.Layer.KaMap( "2 buffer: OpenLayers WMS",
"http://labs.metacarta.com/wms/vmap0",
{layers: 'basic'}, {'buffer':2} );
map.addLayer(layer2);
map.setCenter(new OpenLayers.LonLat(0, 0), 4);
t.eq( layer0.grid.length, 3, "Grid rows with buffer:0" );
map.setBaseLayer(layer1);
t.eq( layer1.grid.length, 4, "Grid rows with buffer:1" );
map.setBaseLayer(layer2);
t.eq( layer2.grid.length, 6, "Grid rows with buffer:2" );
// zooming in on Greenland exercises the bug from pre-r4313
map.setCenter(new OpenLayers.LonLat(0, 90), 4);
t.eq( layer0.grid.length, 3, "Grid rows with buffer:0" );
map.setBaseLayer(layer1);
t.eq( layer1.grid.length, 4, "Grid rows with buffer:1" );
map.setBaseLayer(layer2);
t.eq( layer2.grid.length, 6, "Grid rows with buffer:2" );
}
function test_02_Layer_KaMap_inittiles (t) {
t.plan( 2 );
@@ -30,8 +64,8 @@
layer = new OpenLayers.Layer.KaMap(name, url, params, units);
map.addLayer(layer);
map.setCenter(new OpenLayers.LonLat(0,0),5);
t.eq( layer.grid.length, 6, "KaMap rows is correct." );
t.eq( layer.grid[0].length, 4, "KaMap cols is correct." );
t.eq( layer.grid.length, 8, "KaMap rows is correct." );
t.eq( layer.grid[0].length, 6, "KaMap cols is correct." );
}
@@ -237,5 +271,7 @@
</head>
<body>
<div id="map" style="width:500px;height:550px;display:none"></div>
<div id="map2" style="width:500px;height:550px;display:none"></div>
<div id="map3" style="width:594px;height:464px;display:none"></div>
</body>
</html>