Replace corner with xEast/ySouth

This commit is contained in:
Tom Payne
2012-07-07 17:13:30 +02:00
committed by Tom Payne
parent d444618a11
commit 58e9ba322e
3 changed files with 73 additions and 42 deletions

View File

@@ -171,7 +171,8 @@ Map
TileGrid
resolutions Array.<number>
extent ol.Extent
corner TOP_LEFT | BOTTOM_LEFT
xEast boolean
ySouth boolean
origin(s) Coord|Array.<Coord>
tileSize goog.math.Size
getExtentTileBounds(z, extent) -> TileBounds

View File

@@ -3,7 +3,6 @@ goog.provide('ol.TileGrid');
goog.require('goog.array');
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');
@@ -14,11 +13,13 @@ 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 {boolean=} opt_xEast Tile coordinates increase eastwards.
* @param {boolean=} opt_ySouth Tile coordinates increas southwards.
* @param {goog.math.Size=} opt_tileSize Tile size.
*/
ol.TileGrid = function(resolutions, extent, corner, origin, opt_tileSize) {
ol.TileGrid =
function(resolutions, extent, origin, opt_xEast, opt_ySouth, opt_tileSize) {
/**
* @private
@@ -43,9 +44,15 @@ ol.TileGrid = function(resolutions, extent, corner, origin, opt_tileSize) {
/**
* @private
* @type {goog.positioning.Corner}
* @type {boolean}
*/
this.corner_ = corner;
this.xEast_ = goog.isDef(opt_xEast) ? opt_xEast : true;
/**
* @private
* @type {boolean}
*/
this.ySouth_ = goog.isDef(opt_ySouth) ? opt_ySouth : true;
/**
* @private
@@ -79,10 +86,18 @@ ol.TileGrid = function(resolutions, extent, corner, origin, opt_tileSize) {
/**
* @return {goog.positioning.Corner} Corner.
* @return {boolean} X East.
*/
ol.TileGrid.prototype.getCorner = function() {
return this.corner_;
ol.TileGrid.prototype.getXEast = function() {
return this.xEast_;
};
/**
* @return {boolean} Y South.
*/
ol.TileGrid.prototype.getYSouth = function() {
return this.ySouth_;
};
@@ -155,17 +170,19 @@ ol.TileGrid.prototype.getExtentTileBounds = function(z, extent) {
* @return {ol.TileCoord} Tile coordinate.
*/
ol.TileGrid.prototype.getTileCoord = function(z, coordinate) {
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 x;
if (this.xEast_) {
x = Math.floor((coordinate.x - origin.x) / (tileSize.width * resolution));
} else {
x = Math.floor((origin.x - coordinate.x) / (tileSize.width * resolution));
}
var y;
if (corner == goog.positioning.Corner.TOP_LEFT) {
if (this.ySouth_) {
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);
@@ -177,16 +194,19 @@ ol.TileGrid.prototype.getTileCoord = function(z, coordinate) {
* @return {goog.math.Coordinate} Tile center.
*/
ol.TileGrid.prototype.getTileCoordCenter = 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 x;
if (this.xEast_) {
x = origin.x + (tileCoord.x + 0.5) * tileSize.width * resolution;
} else {
x = origin.x - (tileCoord.x + 0.5) * tileSize.width * resolution;
}
var y;
if (corner == goog.positioning.Corner.TOP_LEFT) {
if (this.ySouth_) {
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);
@@ -198,18 +218,22 @@ ol.TileGrid.prototype.getTileCoordCenter = function(tileCoord) {
* @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 left, right;
if (this.xEast_) {
left = origin.x + tileCoord.x * tileSize.width * resolution;
right = left + tileSize.width * resolution;
} else {
right = origin.x - tileCoord.x * tileSize.width * resolution;
left = right - tileSize.height * resolution;
}
var top, bottom;
if (corner == goog.positioning.Corner.TOP_LEFT) {
if (this.ySouth_) {
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;
}

View File

@@ -1,33 +1,35 @@
goog.require('goog.math.Coordinate');
goog.require('goog.math.Size');
goog.require('goog.positioning.Corner');
goog.require('goog.testing.jsunit');
goog.require('ol.Extent');
goog.require('ol.TileCoord');
goog.require('ol.TileGrid');
var corner;
var extent;
var resolutions;
var origin;
var origins;
var tileSize;
var xEast;
var ySouth;
function setUp() {
corner = goog.positioning.Corner.TOP_LEFT;
resolutions = [1000, 500, 250, 100];
extent = new ol.Extent(100000, 100000, 0, 0);
origin = new goog.math.Coordinate(0, 100000);
origins = [];
tileSize = new goog.math.Size(100, 100);
xEast = true;
ySouth = true;
}
function testCreateValid() {
assertNotThrows(function() {
return new ol.TileGrid(resolutions, extent, corner, origin, tileSize);
return new ol.TileGrid(
resolutions, extent, origin, xEast, ySouth, tileSize);
});
}
@@ -35,7 +37,8 @@ function testCreateValid() {
function testCreateDuplicateResolutions() {
var resolutions = [100, 50, 50, 25, 10];
assertThrows(function() {
return new ol.TileGrid(resolutions, extent, corner, origin, tileSize);
return new ol.TileGrid(
resolutions, extent, origin, xEast, ySouth, tileSize);
});
}
@@ -43,7 +46,8 @@ function testCreateDuplicateResolutions() {
function testCreateOutOfOrderResolutions() {
var resolutions = [100, 25, 50, 10];
assertThrows(function() {
return new ol.TileGrid(resolutions, extent, corner, origin, tileSize);
return new ol.TileGrid(
resolutions, extent, origin, xEast, ySouth, tileSize);
});
}
@@ -52,7 +56,8 @@ function testCreateOrigins() {
var resolutions = [100, 50, 25, 10];
var origins = [origin, origin, origin, origin];
assertNotThrows(function() {
return new ol.TileGrid(resolutions, extent, corner, origins, tileSize);
return new ol.TileGrid(
resolutions, extent, origins, xEast, ySouth, tileSize);
});
}
@@ -61,7 +66,8 @@ function testCreateTooFewOrigins() {
var resolutions = [100, 50, 25, 10];
var origins = [origin, origin, origin];
assertThrows(function() {
return new ol.TileGrid(resolutions, extent, corner, origins, tileSize);
return new ol.TileGrid(
resolutions, extent, origins, xEast, ySouth, tileSize);
});
}
@@ -70,17 +76,17 @@ function testCreateTooManyOrigins() {
var resolutions = [100, 50, 25, 10];
var origins = [origin, origin, origin, origin, origin];
assertThrows(function() {
return new ol.TileGrid(resolutions, extent, corner, origins, tileSize);
return new ol.TileGrid(
resolutions, extent, origins, xEast, ySouth, tileSize);
});
}
function testGetTileCoordTopLeft() {
function testGetTileCoord() {
corner = goog.positioning.Corner.TOP_LEFT;
origin = new goog.math.Coordinate(0, 100000);
var tileGrid =
new ol.TileGrid(resolutions, extent, corner, origin, tileSize);
new ol.TileGrid(resolutions, extent, origin, xEast, ySouth, tileSize);
var tileCoord;
tileCoord = tileGrid.getTileCoord(3, new goog.math.Coordinate(0, 0));
@@ -107,12 +113,12 @@ function testGetTileCoordTopLeft() {
}
function testGetTileCoordBottomLeft() {
function testGetTileCoordYNorth() {
corner = goog.positioning.Corner.BOTTOM_LEFT;
ySouth = false;
origin = new goog.math.Coordinate(0, 0);
var tileGrid =
new ol.TileGrid(resolutions, extent, corner, origin, tileSize);
new ol.TileGrid(resolutions, extent, origin, xEast, ySouth, tileSize);
var tileCoord;
tileCoord = tileGrid.getTileCoord(3, new goog.math.Coordinate(0, 0));
@@ -142,7 +148,7 @@ function testGetTileCoordBottomLeft() {
function testGetTileCoordCenter() {
var tileGrid =
new ol.TileGrid(resolutions, extent, corner, origin, tileSize);
new ol.TileGrid(resolutions, extent, origin, xEast, ySouth, tileSize);
var center;
center = tileGrid.getTileCoordCenter(new ol.TileCoord(0, 0, 0));
@@ -163,7 +169,7 @@ function testGetTileCoordCenter() {
function testGetTileCoordExtent() {
var tileGrid =
new ol.TileGrid(resolutions, extent, corner, origin, tileSize);
new ol.TileGrid(resolutions, extent, origin, xEast, ySouth, tileSize);
var tileCoordExtent;
tileCoordExtent = tileGrid.getTileCoordExtent(new ol.TileCoord(0, 0, 0));
@@ -190,7 +196,7 @@ function testGetTileCoordExtent() {
function testGetExtentTileBounds() {
var tileGrid =
new ol.TileGrid(resolutions, extent, corner, origin, tileSize);
new ol.TileGrid(resolutions, extent, origin, xEast, ySouth, tileSize);
var e = new ol.Extent(15000, 55000, 5000, 45000);
var tileBounds;
@@ -225,7 +231,7 @@ function testGetExtentTileBounds() {
function testForEachTileCoordParent() {
var tileGrid =
new ol.TileGrid(resolutions, extent, corner, origin, tileSize);
new ol.TileGrid(resolutions, extent, origin, xEast, ySouth, tileSize);
var zs = [], tileBoundss = [];
tileGrid.forEachTileCoordParent(