Functions to get key from coord and coord from key

This commit is contained in:
Tim Schaub
2017-10-08 08:50:19 -06:00
parent b0f291973b
commit c679e3042e
2 changed files with 37 additions and 2 deletions

View File

@@ -31,6 +31,26 @@ ol.tilecoord.getKeyZXY = function(z, x, y) {
}; };
/**
* Get the key for a tile coord.
* @param {ol.TileCoord} tileCoord The tile coord.
* @return {string} Key.
*/
ol.tilecoord.getKey = function(tileCoord) {
return ol.tilecoord.getKeyZXY(tileCoord[0], tileCoord[1], tileCoord[2]);
};
/**
* Get a tile coord given a key.
* @param {string} key The tile coord key.
* @return {ol.TileCoord} The tile coord.
*/
ol.tilecoord.fromKey = function(key) {
return key.split('/').map(Number);
};
/** /**
* @param {ol.TileCoord} tileCoord Tile coord. * @param {ol.TileCoord} tileCoord Tile coord.
* @return {number} Hash. * @return {number} Hash.

View File

@@ -1,5 +1,3 @@
goog.require('ol.tilecoord'); goog.require('ol.tilecoord');
goog.require('ol.tilegrid.TileGrid'); goog.require('ol.tilegrid.TileGrid');
@@ -23,6 +21,23 @@ describe('ol.TileCoord', function() {
}); });
}); });
describe('getKey()', function() {
it('returns a key for a tile coord', function() {
var key = ol.tilecoord.getKey([1, 2, 3]);
expect(key).to.eql('1/2/3');
});
});
describe('fromKey()', function() {
it('returns a tile coord given a key', function() {
var tileCoord = [1, 2, 3];
var key = ol.tilecoord.getKey(tileCoord);
var returned = ol.tilecoord.fromKey(key);
expect(returned).to.eql(tileCoord);
});
});
describe('hash', function() { describe('hash', function() {
it('produces different hashes for different tile coords', function() { it('produces different hashes for different tile coords', function() {
var tileCoord1 = [3, 2, 1]; var tileCoord1 = [3, 2, 1];