diff --git a/lib/OpenLayers/Control/UTFGrid.js b/lib/OpenLayers/Control/UTFGrid.js index 0b8249b4ef..4470b68059 100644 --- a/lib/OpenLayers/Control/UTFGrid.js +++ b/lib/OpenLayers/Control/UTFGrid.js @@ -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"; + debug += "
  • j :" + info.j + "
  • "; + debug += "
  • globalrow :" + info.globalRow + "
  • "; + debug += "
  • globalcol :" + info.globalCol + "
  • "; + debug += "
  • gridrow :" + info.gridRow + "
  • "; + debug += "
  • gridcol :" + info.gridCol + "
  • "; + debug += "
  • gridrow offset :" + info.gridRowOffset + "
  • "; + debug += "
  • gridcol offset :" + info.gridColOffset + "
  • "; + debug += ""; + this.debugElement.innerHTML = debug; + } + var tile = info.tile; /* TODO Sanity checks if ((Math.floor(info.i) >= tileSize) || diff --git a/lib/OpenLayers/Layer/UTFGrid.js b/lib/OpenLayers/Layer/UTFGrid.js index 82ed20abb2..412b28e751 100644 --- a/lib/OpenLayers/Layer/UTFGrid.js +++ b/lib/OpenLayers/Layer/UTFGrid.js @@ -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) + }; }, /**