Layer and tile API update.

The tile now has responsibility for resolving feature ids and fetching feature data given x, y pixel offsets with getFeatureId and getFeatureData methods.  The layer has corresponding getFeatureId and getFeatureData methods that take a map location, lookup the appropriate tile, and delegate to the tile for the rest of the work.
This commit is contained in:
Tim Schaub
2012-02-26 21:05:12 -07:00
parent fc03f57591
commit 551c582ab1
4 changed files with 134 additions and 31 deletions
+71
View File
@@ -32,6 +32,15 @@ OpenLayers.Tile.UTFGrid = OpenLayers.Class(OpenLayers.Tile, {
*/
url: null,
/**
* Property: utfgridResolution
* {Number}
* Ratio of the pixel width to the width of a UTFGrid data point. If an
* entry in the grid represents a 2x2 block of pixels, the
* utfgridResolution would be 2. Default is 4.
*/
utfgridResolution: 4,
/**
* Property: json
* {Object}
@@ -135,6 +144,68 @@ OpenLayers.Tile.UTFGrid = OpenLayers.Class(OpenLayers.Tile, {
}
this.isLoading = false;
},
/**
* Method: getFeatureData
* Get feature data associated with a pixel offset.
*
* Parameters:
* i - {Number} X-axis pixel offset (from top left of tile)
* j - {Number} Y-axis pixel offset (from top left of tile)
*
* Returns:
* {Object} The UTFGrid data corresponding to the given pixel offset.
*/
getFeatureData: function(i, j) {
var data;
if (this.json) {
data = this.json.data[this.getFeatureId(i, j)];
}
return data;
},
/**
* Method: getFeatureId
* Get the identifier for the feature associated with a pixel offset.
*
* Parameters:
* i - {Number} X-axis pixel offset (from top left of tile)
* j - {Number} Y-axis pixel offset (from top left of tile)
*
* Returns:
* {Object} The feature identifier corresponding to the given pixel offset.
*/
getFeatureId: function(i, j) {
var id;
if (this.json) {
var resolution = this.utfgridResolution;
var code = this.resolveCode(this.json.grid[
Math.floor((j) / resolution)
].charCodeAt(
Math.floor((i) / resolution)
));
id = this.json.keys[code];
}
return id;
},
/**
* Method: resolveCode
* Resolve the UTF-8 encoding stored in grids to simple number values.
* See the UTFGrid spec for details.
*
* Parameters:
* key - {Integer}
*
* Returns:
* {Integer} Adjusted key for non-escaped chars
*/
resolveCode: function(key) {
if (key >= 93) key--;
if (key >= 35) key--;
key -= 32;
return key;
},
/**
* Method: parseData