Providing a method to get data from the layer.

I think it should be the job of the layer to retrieve data for a given location (instead of the control).  The first part of this change creates a `getData` method on the layer and updates the control to use this method.

The second part of this change removes the assumption that the data returned will be an simple object representing feature attributes.  The UTFGrid specification doesn't say anything about the structure of property values in the optional data member.  The examples given in the spec use string values.  The default callback previously assumed that the data could be rendered in a two column table.  I think it would make more sense not to make this assumption.  With this change, the user must always provide a callback to do anything with returned data.
This commit is contained in:
Tim Schaub
2012-02-25 17:33:32 -07:00
parent 55ffdffd60
commit e1dffacdde
4 changed files with 130 additions and 127 deletions

View File

@@ -180,7 +180,7 @@ OpenLayers.Layer.UTFGrid = OpenLayers.Class(OpenLayers.Layer.Grid, {
utfgridResolution: 4,
/**
* APIMethod: getTileInfo
* Method: getTileInfo
* Get tile information for a given location at the current map resolution.
*
* Parameters:
@@ -246,6 +246,53 @@ OpenLayers.Layer.UTFGrid = OpenLayers.Class(OpenLayers.Layer.Grid, {
j: Math.floor((fy - globalRow) * this.tileSize.h)
};
},
/**
* APIProperty: getData
* Get tile data associated with a map location.
*
* Parameters:
* location - {<OpenLayers.LonLat>} map location
*
* Returns:
* {Object} The UTFGrid data corresponding to the given map location.
*/
getData: function(location) {
var info = this.getTileInfo(location);
var tile = info.tile;
var data;
if (tile) {
var resolution = this.utfgridResolution;
var json = tile.json
if (json) {
var code = this.resolveCode(json.grid[
Math.floor((info.j) / resolution)
].charCodeAt(
Math.floor((info.i) / resolution)
));
data = json.data[json.keys[code]];
}
}
return data;
},
/**
* 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;
},
/**
* APIProperty: tileClass