Some refactoring and cleaning up rounding errors in tile math. Added

debugging mechanism.
This commit is contained in:
Matthew Perry
2012-02-05 11:17:02 -08:00
parent be4db93e9b
commit 6ff3b895c9
2 changed files with 48 additions and 36 deletions

View File

@@ -29,6 +29,7 @@ OpenLayers.Control.UTFGrid = OpenLayers.Class(OpenLayers.Control, {
* {DOMElement}
*/
element: null,
debugElement: null,
/**
* APIProperty: prefix
@@ -176,7 +177,20 @@ OpenLayers.Control.UTFGrid = OpenLayers.Class(OpenLayers.Control, {
for (var i=0, len=layers.length; i<len; i++) {
layer = layers[i];
var info = layer.getTileInfo( lonLat );
var tile = layer.getTile( lonLat );
if (this.debugElement) {
var debug = "<ul>";
debug += "<li>i :" + info.i + "</li>";
debug += "<li>j :" + info.j + "</li>";
debug += "<li>globalrow :" + info.globalRow + "</li>";
debug += "<li>globalcol :" + info.globalCol + "</li>";
debug += "<li>gridrow :" + info.gridRow + "</li>";
debug += "<li>gridcol :" + info.gridCol + "</li>";
debug += "<li>gridrow offset :" + info.gridRowOffset + "</li>";
debug += "<li>gridcol offset :" + info.gridColOffset + "</li>";
debug += "</ul>";
this.debugElement.innerHTML = debug;
}
var tile = info.tile;
/*
TODO Sanity checks
if ((Math.floor(info.i) >= tileSize) ||

View File

@@ -150,48 +150,46 @@ OpenLayers.Layer.UTFGrid = OpenLayers.Class(OpenLayers.Layer.Grid, {
*/
getTileInfo: function(loc) {
var res = this.getServerResolution();
var fx = (loc.lon - this.tileOrigin.lon) / (res * this.tileSize.w);
var fy = (this.tileOrigin.lat - loc.lat) / (res * this.tileSize.h);
var col = Math.floor(fx);
var row = Math.floor(fy);
var resolutions = this.serverResolutions || this.resolutions;
var zoom = this.zoomOffset == 0 ?
OpenLayers.Util.indexOf(resolutions, res) :
this.getServerZoom() + this.zoomOffset;
return {
col: col,
row: row,
zoom: zoom,
i: Math.floor((fx - col) * this.tileSize.w),
j: Math.floor((fy - row) * this.tileSize.h)
};
},
getTile: function(loc) {
var info = this.getTileInfo(loc);
var res = this.getServerResolution();
var gridOrigin = this.grid[0][0].bounds;
var gridColOffset = (gridOrigin.left - this.tileOrigin.lon) / (res * this.tileSize.w);
var gridRowOffset = (this.tileOrigin.lat - gridOrigin.top) / (res * this.tileSize.h);
// Get the global XY for the tile at this zoomlevel
var fx = (loc.lon - this.tileOrigin.lon) / (res * this.tileSize.w);
var fy = (this.tileOrigin.lat - loc.lat) / (res * this.tileSize.h);
var globalCol = Math.floor(fx);
var globalRow = Math.floor(fy);
var row = globalRow - Math.floor(gridRowOffset);
var col = globalCol - Math.floor(gridColOffset);
// Get the current grid offset
var gridOrigin = this.grid[0][0].bounds;
// TODO rounding errors can cause intermittent problems (4.9999 should be 5)
// flooring will cause big problems (4.999 becomes 4)... do round or toFixed later?
var gridColOffset =
(gridOrigin.left - this.tileOrigin.lon) / (res * this.tileSize.w);
var gridRowOffset =
(this.tileOrigin.lat - gridOrigin.top) / (res * this.tileSize.h);
var tile = null;
var therow = this.grid[row];
if (typeof(therow) !== 'undefined' && therow !== null) {
tile = therow[col];
}
return tile;
// Calculate the grid XY for the tile
var gridCol = globalCol - Math.round(gridColOffset);
var gridRow = globalRow - Math.round(gridRowOffset);
var resolutions = this.serverResolutions || this.resolutions;
var zoom = this.zoomOffset == 0 ?
OpenLayers.Util.indexOf(resolutions, res) :
this.getServerZoom() + this.zoomOffset;
var tile = this.grid[gridRow][gridCol];
return {
globalCol: globalCol,
globalRow: globalRow,
gridCol: gridCol,
gridRow: gridRow,
gridColOffset: gridColOffset,
gridRowOffset: gridRowOffset,
tile: tile,
zoom: zoom,
i: Math.floor((fx - globalCol) * this.tileSize.w),
j: Math.floor((fy - globalRow) * this.tileSize.h)
};
},
/**