Some refactoring and cleaning up rounding errors in tile math. Added
debugging mechanism.
This commit is contained in:
@@ -29,6 +29,7 @@ OpenLayers.Control.UTFGrid = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
* {DOMElement}
|
* {DOMElement}
|
||||||
*/
|
*/
|
||||||
element: null,
|
element: null,
|
||||||
|
debugElement: null,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* APIProperty: prefix
|
* APIProperty: prefix
|
||||||
@@ -176,7 +177,20 @@ OpenLayers.Control.UTFGrid = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
for (var i=0, len=layers.length; i<len; i++) {
|
for (var i=0, len=layers.length; i<len; i++) {
|
||||||
layer = layers[i];
|
layer = layers[i];
|
||||||
var info = layer.getTileInfo( lonLat );
|
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
|
TODO Sanity checks
|
||||||
if ((Math.floor(info.i) >= tileSize) ||
|
if ((Math.floor(info.i) >= tileSize) ||
|
||||||
|
|||||||
@@ -150,48 +150,46 @@ OpenLayers.Layer.UTFGrid = OpenLayers.Class(OpenLayers.Layer.Grid, {
|
|||||||
*/
|
*/
|
||||||
getTileInfo: function(loc) {
|
getTileInfo: function(loc) {
|
||||||
var res = this.getServerResolution();
|
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 fx = (loc.lon - this.tileOrigin.lon) / (res * this.tileSize.w);
|
||||||
var fy = (this.tileOrigin.lat - loc.lat) / (res * this.tileSize.h);
|
var fy = (this.tileOrigin.lat - loc.lat) / (res * this.tileSize.h);
|
||||||
var globalCol = Math.floor(fx);
|
var globalCol = Math.floor(fx);
|
||||||
var globalRow = Math.floor(fy);
|
var globalRow = Math.floor(fy);
|
||||||
|
|
||||||
var row = globalRow - Math.floor(gridRowOffset);
|
// Get the current grid offset
|
||||||
var col = globalCol - Math.floor(gridColOffset);
|
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;
|
// Calculate the grid XY for the tile
|
||||||
var therow = this.grid[row];
|
var gridCol = globalCol - Math.round(gridColOffset);
|
||||||
if (typeof(therow) !== 'undefined' && therow !== null) {
|
var gridRow = globalRow - Math.round(gridRowOffset);
|
||||||
tile = therow[col];
|
|
||||||
}
|
var resolutions = this.serverResolutions || this.resolutions;
|
||||||
return tile;
|
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)
|
||||||
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user