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:
@@ -180,7 +180,7 @@ OpenLayers.Control.UTFGrid = OpenLayers.Class(OpenLayers.Control, {
|
||||
for (var i=0, len=layers.length; i<len; i++) {
|
||||
layer = layers[i];
|
||||
idx = this.map.layers.indexOf(layer);
|
||||
dataLookup[idx] = layer.getData(lonLat);
|
||||
dataLookup[idx] = layer.getFeatureData(lonLat);
|
||||
}
|
||||
this.callback(dataLookup); // perhaps pass tile, lonLat?
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user