Add initial ol.TileGrid skeleton

This commit is contained in:
Tom Payne
2012-07-05 16:03:54 +02:00
committed by Tom Payne
parent afb37035dc
commit 9900084f46
4 changed files with 246 additions and 67 deletions

View File

@@ -50,7 +50,6 @@ GSLINT_EXCLUDES= \
src/ol/renderer/WebGL.js \
src/ol/Tile.js \
src/ol/TileCache.js \
src/ol/TileSet.js \
src/ol.export.js \
src/ol.js

View File

@@ -7,4 +7,5 @@ goog.require('ol.MVCArray');
goog.require('ol.MVCObject');
goog.require('ol.TileBounds');
goog.require('ol.TileCoord');
goog.require('ol.TileGrid');
goog.require('ol.TileUrl');

View File

@@ -1,66 +0,0 @@
goog.provide('ol.TileSet');
/**
* The TileSet class. A TileSet instance represents a collection of
* tiles. Tiles of a TileSet have the same resolution, width and
* height.
* @constructor
* @param {Array.<Array.<ol.Tile>>} tiles
* @param {number} tileWidth
* @param {number} tileHeight
* @param {number} resolution
*/
ol.TileSet = function(tiles, tileWidth, tileHeight, resolution) {
/**
* @private
* @type {Array.<Array.<ol.Tile>>}
*/
this.tiles_ = tiles;
/**
* @private
* @type {number}
*/
this.tileWidth_ = tileWidth;
/**
* @private
* @type {number}
*/
this.tileHeight_ = tileHeight;
/**
* @private
* @type {number}
*/
this.resolution_ = resolution;
};
/**
* @return {Array.<Array.<ol.Tile>>}
*/
ol.TileSet.prototype.getTiles = function() {
return this.tiles_;
};
/**
* @return {number}
*/
ol.TileSet.prototype.getResolution = function() {
return this.resolution_;
};
/**
* @return {number}
*/
ol.TileSet.prototype.getTileHeight = function() {
return this.tileHeight_;
};
/**
* @return {number}
*/
ol.TileSet.prototype.getTileWidth = function() {
return this.tileWidth_;
};

245
src/ol/tilegrid.js Normal file
View File

@@ -0,0 +1,245 @@
goog.provide('ol.TileGrid');
goog.require('goog.asserts');
goog.require('goog.math.Size');
goog.require('goog.positioning.Corner');
goog.require('ol.Extent');
goog.require('ol.TileBounds');
goog.require('ol.TileCoord');
/**
* @constructor
* @param {Array.<number>} resolutions Resolutions.
* @param {ol.Extent} extent Extent.
* @param {goog.positioning.Corner} corner Corner.
* @param {goog.math.Coordinate|Array.<goog.math.Coordinate>} origin Origin.
* @param {goog.math.Size=} opt_tileSize Tile size.
*/
ol.TileGrid = function(resolutions, extent, corner, origin, opt_tileSize) {
/**
* @private
* @type {Array.<number>}
*/
this.resolutions_ = resolutions;
/**
* @private
* @type {ol.Extent}
*/
this.extent_ = extent;
/**
* @private
* @type {goog.positioning.Corner}
*/
this.corner_ = corner;
/**
* @private
* @type {goog.math.Coordinate}
*/
this.origin_ = null;
/**
* @private
* @type {Array.<goog.math.Coordinate>}
*/
this.origins_ = null;
if (origin instanceof goog.math.Coordinate) {
this.origin_ = origin;
} else if (goog.isArray(origin)) {
goog.asserts.assert(origin.length == this.resolutions_.length);
this.origins_ = origin;
} else {
goog.asserts.assert(false);
}
/**
* @private
* @type {goog.math.Size}
*/
this.tileSize_ = goog.isDef(opt_tileSize) ?
opt_tileSize : new goog.math.Size(256, 256);
};
/**
* @return {goog.positioning.Corner} Corner.
*/
ol.TileGrid.prototype.getCorner = function() {
return this.corner_;
};
/**
* @return {ol.Extent} Extent.
*/
ol.TileGrid.prototype.getExtent = function() {
return this.extent_;
};
/**
* @return {number} Maximum resolution.
*/
ol.TileGrid.prototype.getMaxResolution = function() {
return this.getResolutions()[0];
};
/**
* @param {number} z Z.
* @return {goog.math.Coordinate} Origin.
*/
ol.TileGrid.prototype.getOrigin = function(z) {
if (!goog.isNull(this.origin_)) {
return this.origin_;
} else {
goog.asserts.assert(!goog.isNull(this.origins_));
goog.asserts.assert(0 <= z && z < this.origins_.length);
return this.origins_[z];
}
};
/**
* @param {number} z Z.
* @return {number} Resolution.
*/
ol.TileGrid.prototype.getResolution = function(z) {
goog.asserts.assert(0 <= z && z < this.resolutions_.length);
return this.resolutions_[z];
};
/**
* @return {Array.<number>} Resolutions.
*/
ol.TileGrid.prototype.getResolutions = function() {
return this.resolutions_;
};
/**
* @param {number} z Z.
* @param {ol.Extent} extent Extent.
* @return {ol.TileBounds} Tile bounds.
*/
ol.TileGrid.prototype.getTileBounds = function(z, extent) {
var topRight = new goog.math.Coordinate(extent.right, extent.top);
var bottomLeft = new goog.math.Coordinate(extent.left, extent.bottom);
return ol.TileBounds.boundingTileBounds(
this.getTileCoord(z, topRight),
this.getTileCoord(z, bottomLeft));
};
/**
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @return {goog.math.Coordinate} Tile center.
*/
ol.TileGrid.prototype.getTileCenter = function(tileCoord) {
var corner = this.corner_;
var origin = this.getOrigin(tileCoord.z);
var resolution = this.getResolution(tileCoord.z);
var tileSize = this.tileSize_;
var x = origin.x + (tileCoord.x + 0.5) * tileSize.width * resolution;
var y;
if (corner == goog.positioning.Corner.TOP_LEFT) {
y = origin.y - (tileCoord.y + 0.5) * tileSize.height * resolution;
} else {
goog.asserts.assert(corner == goog.positioning.Corner.TOP_RIGHT);
y = origin.y + (tileCoord.y + 0.5) * tileSize.height * resolution;
}
return new goog.math.Coordinate(x, y);
};
/**
* @param {number} z Z.
* @param {goog.math.Coordinate} coordinate Coordinate.
* @return {ol.TileCoord} Tile coordinate.
*/
ol.TileGrid.prototype.getTileCoord = function(z, coordinate) {
if (!this.extent_.contains(coordinate)) {
return null;
}
var corner = this.corner_;
var origin = this.getOrigin(z);
var resolution = this.getResolution(z);
var tileSize = this.getTileSize();
var x =
Math.floor((coordinate.x - origin.x) / (tileSize.width * resolution));
var y;
if (corner == goog.positioning.Corner.TOP_LEFT) {
y = Math.floor((origin.y - coordinate.y) / (tileSize.height * resolution));
} else {
goog.asserts.assert(corner == goog.positioning.Corner.BOTTOM_LEFT);
y = Math.floor((coordinate.y - origin.y) / (tileSize.height * resolution));
}
return new ol.TileCoord(z, x, y);
};
/**
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @return {ol.Extent} Extent.
*/
ol.TileGrid.prototype.getTileCoordExtent = function(tileCoord) {
var corner = this.corner_;
var origin = this.getOrigin(tileCoord.z);
var resolution = this.getResolution(tileCoord.z);
var tileSize = this.tileSize_;
var left = origin.x + tileCoord.x * tileSize.width * resolution;
var right = left + tileSize.width * resolution;
var top, bottom;
if (corner == goog.positioning.Corner.TOP_LEFT) {
top = origin.y - tileCoord.y * tileSize.height * resolution;
bottom = top - tileSize.height * resolution;
} else {
goog.asserts.assert(corner == goog.positioning.Corner.BOTTOM_LEFT);
bottom = origin.y + tileCoord.y * tileSize.height * resolution;
top = bottom + tileSize.height * resolution;
}
return new ol.Extent(top, right, bottom, left);
};
/**
* @return {goog.math.Size} Tile size.
*/
ol.TileGrid.prototype.getTileSize = function() {
return this.tileSize_;
};
/**
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @return {number} Tile resolution.
*/
ol.TileGrid.prototype.getTileResolution = function(tileCoord) {
goog.asserts.assert(
0 <= tileCoord.z && tileCoord.z < this.resolutions_.length);
return this.resolutions_[tileCoord.z];
};
/**
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @param {function(ol.TileBounds): boolean} callback Callback.
*/
ol.TileGrid.prototype.yieldTileCoordParents = function(tileCoord, callback) {
var extent = this.getTileCoordExtent(tileCoord);
var z = tileCoord.z - 1;
while (z >= 0) {
if (callback(this.getTileBounds(z, extent))) {
return;
}
--z;
}
};