diff --git a/lib/OpenLayers/Layer/Grid.js b/lib/OpenLayers/Layer/Grid.js index b456b0a0a1..28fbb1a39f 100644 --- a/lib/OpenLayers/Layer/Grid.js +++ b/lib/OpenLayers/Layer/Grid.js @@ -229,13 +229,16 @@ OpenLayers.Layer.Grid = OpenLayers.Class(OpenLayers.Layer.HTTPRequest, { }, /** - * Method: getTilesBounds - * Get the bounds of the grid - * - * Return: + * APIMethod: getTileBounds + * Returns The tile bounds for a layer given a pixel location. + * + * Parameters: + * viewPortPx - {} The location in the viewport. + * + * Returns: * {} A Bounds object representing the bounds of all the - * currently loaded tiles (including those partially or not at all seen - * onscreen) + * currently loaded tiles (including those partially or not at all seen + * onscreen). */ getTilesBounds: function() { var bounds = null; @@ -673,5 +676,31 @@ OpenLayers.Layer.Grid = OpenLayers.Class(OpenLayers.Layer.HTTPRequest, { } }, + /** + * APIMethod: getTileBounds + * Returns The tile bounds for a layer given a pixel location + * + * Parameters: + * viewPortPx - {OpenLayers.Pixel} The location in the viewport + */ + getTileBounds: function(viewPortPx) { + var maxExtent = this.map.getMaxExtent(); + var resolution = this.getResolution(); + var tileMapWidth = resolution * this.tileSize.w; + var tileMapHeight = resolution * this.tileSize.h; + var mapPoint = this.getLonLatFromViewPortPx(viewPortPx); + var tileLeft = maxExtent.left + (tileMapWidth * + Math.floor((mapPoint.lon - + maxExtent.left) / + tileMapWidth)); + var tileBottom = maxExtent.bottom + (tileMapHeight * + Math.floor((mapPoint.lat - + maxExtent.bottom) / + tileMapHeight)); + return new OpenLayers.Bounds(tileLeft, tileBottom, + tileLeft + tileMapWidth, + tileBottom + tileMapHeight); + }, + CLASS_NAME: "OpenLayers.Layer.Grid" }); diff --git a/lib/OpenLayers/Layer/KaMap.js b/lib/OpenLayers/Layer/KaMap.js index 880797c5fc..da9dbc3c91 100644 --- a/lib/OpenLayers/Layer/KaMap.js +++ b/lib/OpenLayers/Layer/KaMap.js @@ -225,6 +225,28 @@ OpenLayers.Layer.KaMap = OpenLayers.Class(OpenLayers.Layer.Grid, { return obj; }, + + /** + * APIMethod: getTileBounds + * Returns The tile bounds for a layer given a pixel location. + * + * Parameters: + * viewPortPx - {} The location in the viewport. + * + * Returns: + * {} Bounds of the tile at the given pixel location. + */ + getTileBounds: function(viewPortPx) { + var resolution = this.getResolution(); + var tileMapWidth = resolution * this.tileSize.w; + var tileMapHeight = resolution * this.tileSize.h; + var mapPoint = this.getLonLatFromViewPortPx(viewPortPx); + var tileLeft = tileMapWidth * Math.floor(mapPoint.lon / tileMapWidth); + var tileBottom = tileMapHeight * Math.floor(mapPoint.lat / tileMapHeight); + return new OpenLayers.Bounds(tileLeft, tileBottom, + tileLeft + tileMapWidth, + tileBottom + tileMapHeight); + }, CLASS_NAME: "OpenLayers.Layer.KaMap" }); diff --git a/tests/Layer/test_Grid.html b/tests/Layer/test_Grid.html index 62aecbd9ce..42dc2f3a10 100644 --- a/tests/Layer/test_Grid.html +++ b/tests/Layer/test_Grid.html @@ -528,6 +528,24 @@ height = layer.tileSize.h; t.ok(width == parseInt(width) && height == parseInt(height), "calculated tileSize width/height are integer values"); } + function test_12_Layer_Grid_getTileBounds(t) { + t.plan(2); + var map = new OpenLayers.Map("map2"); + var url = "http://octo.metacarta.com/cgi-bin/mapserv"; + layer = new OpenLayers.Layer.WMS(name, url, params); + + var newParams = { layers: 'sooper', + chickpeas: 'image/png'}; + + map.addLayer(layer); + map.zoomToMaxExtent(); + map.zoomIn(); + var bounds = layer.getTileBounds(new OpenLayers.Pixel(200,200)); + t.eq(bounds.toBBOX(), "-180,-90,0,90", "get tile bounds returns correct bounds"); + map.pan(200,0); + var bounds = layer.getTileBounds(new OpenLayers.Pixel(200,200)); + t.eq(bounds.toBBOX(), "0,-90,180,90", "get tile bounds returns correct bounds after pan"); + } function test_99_Layer_Grid_destroy (t) { @@ -563,5 +581,6 @@ + diff --git a/tests/Layer/test_KaMap.html b/tests/Layer/test_KaMap.html index f7ad1c7ffd..19e265d0eb 100644 --- a/tests/Layer/test_KaMap.html +++ b/tests/Layer/test_KaMap.html @@ -189,6 +189,24 @@ t.ok( layer.tileSize != null, "tileSize has been set"); t.ok( (layer.tileSize.h == 50) && (layer.tileSize.w == 500), "tileSize has been set correctly"); } + function test_12_Layer_KaMap_getTileBounds(t) { + t.plan(2); + var map = new OpenLayers.Map("map"); + var url = "http://octo.metacarta.com/cgi-bin/mapserv"; + layer = new OpenLayers.Layer.KaMap(name, url, params); + + var newParams = { layers: 'sooper', + chickpeas: 'image/png'}; + + map.addLayer(layer); + map.zoomToMaxExtent(); + map.zoomIn(); + var bounds = layer.getTileBounds(new OpenLayers.Pixel(200,200)); + t.eq(bounds.toBBOX(), "-180,0,0,180", "get tile bounds returns correct bounds"); + map.pan(200,0); + var bounds = layer.getTileBounds(new OpenLayers.Pixel(200,200)); + t.eq(bounds.toBBOX(), "0,0,180,180", "get tile bounds returns correct bounds after pan"); + } function test_99_Layer_KaMap_destroy (t) {