Skip to content

Commit

Permalink
Layer and tile API update.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tschaub committed Feb 27, 2012
1 parent fc03f57 commit 551c582
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/OpenLayers/Control/UTFGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?
}
Expand Down
54 changes: 25 additions & 29 deletions lib/OpenLayers/Layer/UTFGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -232,50 +237,41 @@ 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
*
* 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;
},

/**
Expand Down
71 changes: 71 additions & 0 deletions lib/OpenLayers/Tile/UTFGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand Down
38 changes: 37 additions & 1 deletion tests/Tile/UTFGrid.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
projection: "EPSG:900913",
layers: [layer],
center: [0, 0],
zoom: 0
zoom: 1
});
}

Expand Down Expand Up @@ -158,6 +158,42 @@
});

}

function test_getFeatureId(t) {
t.plan(3);
setUp();

var tile = layer.grid[1][1];
t.delay_call(0.5, function() {
var id = tile.getFeatureId(16, 60);
t.eq(id, "238", "feature 238 at 16, 60");
t.eq(tile.getFeatureId(18, 63), id, "same feature at 18, 63");

t.eq(tile.getFeatureId(300, 10), undefined, "undefined id outside tile");

tearDown();
});
}

function test_getFeatureData(t) {
t.plan(3);
setUp();

var tile = layer.grid[1][1];
t.delay_call(0.5, function() {
var data = tile.getFeatureData(16, 60);
var exp = {
NAME: "Svalbard",
POP2005: 0
};
t.eq(data, exp, "feature data at 16, 60");
t.eq(tile.getFeatureData(17, 62), exp, "same feature at 17, 62");

t.eq(tile.getFeatureData(300, 10), undefined, "undefined data outside tile");

tearDown();
});
}

</script>
</head>
Expand Down

0 comments on commit 551c582

Please sign in to comment.