Add a getTileBounds method to gridded layers. Thanks to crschmidt for the tests on this one. Committed with a few minor ndoc changes (closes 482).

git-svn-id: http://svn.openlayers.org/trunk/openlayers@4088 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2007-08-28 23:16:23 +00:00
parent 6acf9e75c5
commit d7776e77a7
4 changed files with 94 additions and 6 deletions

View File

@@ -229,13 +229,16 @@ OpenLayers.Layer.Grid = OpenLayers.Class(OpenLayers.Layer.HTTPRequest, {
}, },
/** /**
* Method: getTilesBounds * APIMethod: getTileBounds
* Get the bounds of the grid * Returns The tile bounds for a layer given a pixel location.
* *
* Return: * Parameters:
* viewPortPx - {<OpenLayers.Pixel>} The location in the viewport.
*
* Returns:
* {<OpenLayers.Bounds>} A Bounds object representing the bounds of all the * {<OpenLayers.Bounds>} A Bounds object representing the bounds of all the
* currently loaded tiles (including those partially or not at all seen * currently loaded tiles (including those partially or not at all seen
* onscreen) * onscreen).
*/ */
getTilesBounds: function() { getTilesBounds: function() {
var bounds = null; 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" CLASS_NAME: "OpenLayers.Layer.Grid"
}); });

View File

@@ -226,5 +226,27 @@ OpenLayers.Layer.KaMap = OpenLayers.Class(OpenLayers.Layer.Grid, {
return obj; return obj;
}, },
/**
* APIMethod: getTileBounds
* Returns The tile bounds for a layer given a pixel location.
*
* Parameters:
* viewPortPx - {<OpenLayers.Pixel>} The location in the viewport.
*
* Returns:
* {<OpenLayers.Bounds>} 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" CLASS_NAME: "OpenLayers.Layer.KaMap"
}); });

View File

@@ -528,6 +528,24 @@
height = layer.tileSize.h; height = layer.tileSize.h;
t.ok(width == parseInt(width) && height == parseInt(height), "calculated tileSize width/height are integer values"); 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) { function test_99_Layer_Grid_destroy (t) {
@@ -563,5 +581,6 @@
</head> </head>
<body> <body>
<div id="map" style="width:499px;height:549px;display:none"></div> <div id="map" style="width:499px;height:549px;display:none"></div>
<div id="map2" style="width:500px;height:550px;display:none"></div>
</body> </body>
</html> </html>

View File

@@ -189,6 +189,24 @@
t.ok( layer.tileSize != null, "tileSize has been set"); t.ok( layer.tileSize != null, "tileSize has been set");
t.ok( (layer.tileSize.h == 50) && (layer.tileSize.w == 500), "tileSize has been set correctly"); 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) { function test_99_Layer_KaMap_destroy (t) {