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
+25 -29
View File
@@ -107,6 +107,9 @@ OpenLayers.Layer.UTFGrid = OpenLayers.Class(OpenLayers.Layer.Grid, {
*/
initialize: function(name, url, options) {
OpenLayers.Layer.Grid.prototype.initialize.apply(this, [name, url, {}, options]);
this.tileOptions = OpenLayers.Util.extend({
utfgridResolution: this.utfgridResolution
}, this.tileOptions);
},
/**
@@ -158,10 +161,12 @@ OpenLayers.Layer.UTFGrid = OpenLayers.Class(OpenLayers.Layer.Grid, {
/**
* APIProperty: utfgridResolution
* {Number} Number of pixels per grid "cell"
* Defaults to 4
* {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 (specified in
* <OpenLayers.Tile.UTFGrid>).
*/
utfgridResolution: 4,
/**
* Method: getTileInfo
@@ -232,8 +237,8 @@ OpenLayers.Layer.UTFGrid = OpenLayers.Class(OpenLayers.Layer.Grid, {
},
/**
* APIProperty: getData
* Get tile data associated with a map location.
* APIProperty: getFeatureData
* Get feature data from UTFGrid associated with a map location.
*
* Parameters:
* location - {<OpenLayers.LonLat>} map location
@@ -241,41 +246,32 @@ OpenLayers.Layer.UTFGrid = OpenLayers.Class(OpenLayers.Layer.Grid, {
* Returns:
* {Object} The UTFGrid data corresponding to the given map location.
*/
getData: function(location) {
var info = this.getTileInfo(location);
var tile = info.tile;
getFeatureData: function(location) {
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]];
}
var info = this.getTileInfo(location);
if (info.tile) {
data = info.tile.getFeatureData(info.i, info.j);
}
return data;
},
/**
* Method: resolveCode
* Resolve the UTF-8 encoding stored in grids to simple number values.
* See the UTFGrid spec for details.
* APIMethod: getFeatureId
* Get the identifier for the feature associated with a map location.
*
* Parameters:
* key - {Integer}
* location - {<OpenLayers.LonLat>} map location
*
* Returns:
* {Integer} Adjusted key for non-escaped chars
* {Object} The feature identifier corresponding to the given map location.
*/
resolveCode: function(key) {
if (key >= 93) key--;
if (key >= 35) key--;
key -= 32;
return key;
getFeatureId: function(location) {
var id;
var info = this.getTileInfo(location);
if (info.tile) {
id = info.tile.getFeatureId(info.i, info.j);
}
return id;
},
/**