From 9900084f465dcfbfcc466959c2452a139d33ae23 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Thu, 5 Jul 2012 16:03:54 +0200 Subject: [PATCH] Add initial ol.TileGrid skeleton --- Makefile | 1 - src/all.js | 1 + src/ol/TileSet.js | 66 ------------ src/ol/tilegrid.js | 245 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 246 insertions(+), 67 deletions(-) delete mode 100644 src/ol/TileSet.js create mode 100644 src/ol/tilegrid.js diff --git a/Makefile b/Makefile index 0880a60ece..53cf49841f 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/src/all.js b/src/all.js index cd16a8ce30..a9896cb5a1 100644 --- a/src/all.js +++ b/src/all.js @@ -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'); diff --git a/src/ol/TileSet.js b/src/ol/TileSet.js deleted file mode 100644 index 56c8f3885b..0000000000 --- a/src/ol/TileSet.js +++ /dev/null @@ -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.>} tiles - * @param {number} tileWidth - * @param {number} tileHeight - * @param {number} resolution - */ -ol.TileSet = function(tiles, tileWidth, tileHeight, resolution) { - - /** - * @private - * @type {Array.>} - */ - this.tiles_ = tiles; - - /** - * @private - * @type {number} - */ - this.tileWidth_ = tileWidth; - - /** - * @private - * @type {number} - */ - this.tileHeight_ = tileHeight; - - /** - * @private - * @type {number} - */ - this.resolution_ = resolution; -}; - -/** - * @return {Array.>} - */ -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_; -}; diff --git a/src/ol/tilegrid.js b/src/ol/tilegrid.js new file mode 100644 index 0000000000..0cd02d49cf --- /dev/null +++ b/src/ol/tilegrid.js @@ -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.} resolutions Resolutions. + * @param {ol.Extent} extent Extent. + * @param {goog.positioning.Corner} corner Corner. + * @param {goog.math.Coordinate|Array.} origin Origin. + * @param {goog.math.Size=} opt_tileSize Tile size. + */ +ol.TileGrid = function(resolutions, extent, corner, origin, opt_tileSize) { + + /** + * @private + * @type {Array.} + */ + 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.} + */ + 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.} 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; + } +};