Use normalized tile coordinates (bottom left)
This commit is contained in:
@@ -15,12 +15,9 @@ goog.require('ol.TileCoord');
|
||||
* @param {!Array.<number>} resolutions Resolutions.
|
||||
* @param {ol.Extent} extent Extent.
|
||||
* @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, origin, opt_xEast, opt_ySouth, opt_tileSize) {
|
||||
ol.TileGrid = function(resolutions, extent, origin, opt_tileSize) {
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -43,18 +40,6 @@ ol.TileGrid =
|
||||
*/
|
||||
this.extent_ = extent;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.xEast_ = goog.isDef(opt_xEast) ? opt_xEast : true;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.ySouth_ = goog.isDef(opt_ySouth) ? opt_ySouth : true;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {goog.math.Coordinate}
|
||||
@@ -103,7 +88,7 @@ ol.TileGrid.createOpenStreetMap = function(maxZoom) {
|
||||
-ol.Projection.EPSG_3857_HALF_SIZE, ol.Projection.EPSG_3857_HALF_SIZE);
|
||||
var tileSize = new goog.math.Size(256, 256);
|
||||
|
||||
return new ol.TileGrid(resolutions, extent, origin, true, true, tileSize);
|
||||
return new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
|
||||
};
|
||||
|
||||
@@ -188,18 +173,9 @@ ol.TileGrid.prototype.getTileCoord = function(z, coordinate) {
|
||||
var origin = this.getOrigin(z);
|
||||
var resolution = this.getResolution(z);
|
||||
var tileSize = this.getTileSize();
|
||||
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 (this.ySouth_) {
|
||||
y = Math.floor((origin.y - coordinate.y) / (tileSize.height * resolution));
|
||||
} else {
|
||||
y = Math.floor((coordinate.y - origin.y) / (tileSize.height * resolution));
|
||||
}
|
||||
var x, y;
|
||||
x = Math.floor((coordinate.x - origin.x) / (tileSize.width * resolution));
|
||||
y = Math.floor((coordinate.y - origin.y) / (tileSize.height * resolution));
|
||||
return new ol.TileCoord(z, x, y);
|
||||
};
|
||||
|
||||
@@ -212,18 +188,8 @@ ol.TileGrid.prototype.getTileCoordCenter = function(tileCoord) {
|
||||
var origin = this.getOrigin(tileCoord.z);
|
||||
var resolution = this.getResolution(tileCoord.z);
|
||||
var tileSize = this.tileSize_;
|
||||
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 (this.ySouth_) {
|
||||
y = origin.y - (tileCoord.y + 0.5) * tileSize.height * resolution;
|
||||
} else {
|
||||
y = origin.y + (tileCoord.y + 0.5) * tileSize.height * resolution;
|
||||
}
|
||||
var x = origin.x + (tileCoord.x + 0.5) * tileSize.width * resolution;
|
||||
var y = origin.y + (tileCoord.y + 0.5) * tileSize.height * resolution;
|
||||
return new goog.math.Coordinate(x, y);
|
||||
};
|
||||
|
||||
@@ -236,22 +202,10 @@ ol.TileGrid.prototype.getTileCoordExtent = function(tileCoord) {
|
||||
var origin = this.getOrigin(tileCoord.z);
|
||||
var resolution = this.getResolution(tileCoord.z);
|
||||
var tileSize = this.tileSize_;
|
||||
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 (this.ySouth_) {
|
||||
top = origin.y - tileCoord.y * tileSize.height * resolution;
|
||||
bottom = top - tileSize.height * resolution;
|
||||
} else {
|
||||
bottom = origin.y + tileCoord.y * tileSize.height * resolution;
|
||||
top = bottom + tileSize.height * resolution;
|
||||
}
|
||||
var left = origin.x + tileCoord.x * tileSize.width * resolution;
|
||||
var right = left + tileSize.width * resolution;
|
||||
var bottom = origin.y + tileCoord.y * tileSize.height * resolution;
|
||||
var top = bottom + tileSize.height * resolution;
|
||||
return new ol.Extent(top, right, bottom, left);
|
||||
};
|
||||
|
||||
@@ -274,22 +228,6 @@ ol.TileGrid.prototype.getTileSize = function() {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {boolean} X East.
|
||||
*/
|
||||
ol.TileGrid.prototype.getXEast = function() {
|
||||
return this.xEast_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {boolean} Y South.
|
||||
*/
|
||||
ol.TileGrid.prototype.getYSouth = function() {
|
||||
return this.ySouth_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} resolution Resolution.
|
||||
* @return {number} Z.
|
||||
|
||||
@@ -11,25 +11,20 @@ var resolutions;
|
||||
var origin;
|
||||
var origins;
|
||||
var tileSize;
|
||||
var xEast;
|
||||
var ySouth;
|
||||
|
||||
|
||||
function setUp() {
|
||||
resolutions = [1000, 500, 250, 100];
|
||||
extent = new ol.Extent(100000, 100000, 0, 0);
|
||||
origin = new goog.math.Coordinate(0, 100000);
|
||||
origin = new goog.math.Coordinate(0, 0);
|
||||
origins = [];
|
||||
tileSize = new goog.math.Size(100, 100);
|
||||
xEast = true;
|
||||
ySouth = true;
|
||||
}
|
||||
|
||||
|
||||
function testCreateValid() {
|
||||
assertNotThrows(function() {
|
||||
return new ol.TileGrid(
|
||||
resolutions, extent, origin, xEast, ySouth, tileSize);
|
||||
return new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -37,8 +32,7 @@ function testCreateValid() {
|
||||
function testCreateDuplicateResolutions() {
|
||||
var resolutions = [100, 50, 50, 25, 10];
|
||||
assertThrows(function() {
|
||||
return new ol.TileGrid(
|
||||
resolutions, extent, origin, xEast, ySouth, tileSize);
|
||||
return new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -46,8 +40,7 @@ function testCreateDuplicateResolutions() {
|
||||
function testCreateOutOfOrderResolutions() {
|
||||
var resolutions = [100, 25, 50, 10];
|
||||
assertThrows(function() {
|
||||
return new ol.TileGrid(
|
||||
resolutions, extent, origin, xEast, ySouth, tileSize);
|
||||
return new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -56,8 +49,7 @@ function testCreateOrigins() {
|
||||
var resolutions = [100, 50, 25, 10];
|
||||
var origins = [origin, origin, origin, origin];
|
||||
assertNotThrows(function() {
|
||||
return new ol.TileGrid(
|
||||
resolutions, extent, origins, xEast, ySouth, tileSize);
|
||||
return new ol.TileGrid(resolutions, extent, origins, tileSize);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -66,8 +58,7 @@ function testCreateTooFewOrigins() {
|
||||
var resolutions = [100, 50, 25, 10];
|
||||
var origins = [origin, origin, origin];
|
||||
assertThrows(function() {
|
||||
return new ol.TileGrid(
|
||||
resolutions, extent, origins, xEast, ySouth, tileSize);
|
||||
return new ol.TileGrid(resolutions, extent, origins, tileSize);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -76,79 +67,74 @@ function testCreateTooManyOrigins() {
|
||||
var resolutions = [100, 50, 25, 10];
|
||||
var origins = [origin, origin, origin, origin, origin];
|
||||
assertThrows(function() {
|
||||
return new ol.TileGrid(
|
||||
resolutions, extent, origins, xEast, ySouth, tileSize);
|
||||
return new ol.TileGrid(resolutions, extent, origins, tileSize);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function testGetTileCoord() {
|
||||
|
||||
origin = new goog.math.Coordinate(0, 100000);
|
||||
var tileGrid =
|
||||
new ol.TileGrid(resolutions, extent, origin, xEast, ySouth, tileSize);
|
||||
origin = new goog.math.Coordinate(0, 0);
|
||||
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
var tileCoord;
|
||||
|
||||
tileCoord = tileGrid.getTileCoord(3, new goog.math.Coordinate(0, 0));
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(0, tileCoord.x);
|
||||
assertEquals(10, tileCoord.y);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
tileCoord = tileGrid.getTileCoord(3, new goog.math.Coordinate(0, 100000));
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(0, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
assertEquals(10, tileCoord.y);
|
||||
|
||||
tileCoord = tileGrid.getTileCoord(3, new goog.math.Coordinate(100000, 0));
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(10, tileCoord.x);
|
||||
assertEquals(10, tileCoord.y);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
tileCoord =
|
||||
tileGrid.getTileCoord(3, new goog.math.Coordinate(100000, 100000));
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(10, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
assertEquals(10, tileCoord.y);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testGetTileCoordYNorth() {
|
||||
function testGetTileCoordYSouth() {
|
||||
|
||||
ySouth = false;
|
||||
origin = new goog.math.Coordinate(0, 0);
|
||||
var tileGrid =
|
||||
new ol.TileGrid(resolutions, extent, origin, xEast, ySouth, tileSize);
|
||||
origin = new goog.math.Coordinate(0, 100000);
|
||||
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
var tileCoord;
|
||||
|
||||
tileCoord = tileGrid.getTileCoord(3, new goog.math.Coordinate(0, 0));
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(0, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
assertEquals(-10, tileCoord.y);
|
||||
|
||||
tileCoord = tileGrid.getTileCoord(3, new goog.math.Coordinate(0, 100000));
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(0, tileCoord.x);
|
||||
assertEquals(10, tileCoord.y);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
tileCoord = tileGrid.getTileCoord(3, new goog.math.Coordinate(100000, 0));
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(10, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
assertEquals(-10, tileCoord.y);
|
||||
|
||||
tileCoord =
|
||||
tileGrid.getTileCoord(3, new goog.math.Coordinate(100000, 100000));
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(10, tileCoord.x);
|
||||
assertEquals(10, tileCoord.y);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testGetTileCoordCenter() {
|
||||
|
||||
var tileGrid =
|
||||
new ol.TileGrid(resolutions, extent, origin, xEast, ySouth, tileSize);
|
||||
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
var center;
|
||||
|
||||
center = tileGrid.getTileCoordCenter(new ol.TileCoord(0, 0, 0));
|
||||
@@ -157,46 +143,44 @@ function testGetTileCoordCenter() {
|
||||
|
||||
center = tileGrid.getTileCoordCenter(new ol.TileCoord(3, 0, 0));
|
||||
assertEquals(5000, center.x);
|
||||
assertEquals(95000, center.y);
|
||||
assertEquals(5000, center.y);
|
||||
|
||||
center = tileGrid.getTileCoordCenter(new ol.TileCoord(3, 9, 9));
|
||||
assertEquals(95000, center.x);
|
||||
assertEquals(5000, center.y);
|
||||
assertEquals(95000, center.y);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testGetTileCoordExtent() {
|
||||
|
||||
var tileGrid =
|
||||
new ol.TileGrid(resolutions, extent, origin, xEast, ySouth, tileSize);
|
||||
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
var tileCoordExtent;
|
||||
|
||||
tileCoordExtent = tileGrid.getTileCoordExtent(new ol.TileCoord(0, 0, 0));
|
||||
assertEquals(tileCoordExtent.top, 100000);
|
||||
assertEquals(tileCoordExtent.right, 100000);
|
||||
assertEquals(tileCoordExtent.bottom, 0);
|
||||
assertEquals(tileCoordExtent.left, 0);
|
||||
assertEquals(100000, tileCoordExtent.top);
|
||||
assertEquals(100000, tileCoordExtent.right);
|
||||
assertEquals(0, tileCoordExtent.bottom);
|
||||
assertEquals(0, tileCoordExtent.left);
|
||||
|
||||
tileCoordExtent = tileGrid.getTileCoordExtent(new ol.TileCoord(3, 9, 0));
|
||||
assertEquals(tileCoordExtent.top, 100000);
|
||||
assertEquals(tileCoordExtent.right, 100000);
|
||||
assertEquals(tileCoordExtent.bottom, 90000);
|
||||
assertEquals(tileCoordExtent.left, 90000);
|
||||
assertEquals(10000, tileCoordExtent.top);
|
||||
assertEquals(100000, tileCoordExtent.right);
|
||||
assertEquals(0, tileCoordExtent.bottom);
|
||||
assertEquals(90000, tileCoordExtent.left);
|
||||
|
||||
tileCoordExtent = tileGrid.getTileCoordExtent(new ol.TileCoord(3, 0, 9));
|
||||
assertEquals(tileCoordExtent.top, 10000);
|
||||
assertEquals(tileCoordExtent.right, 10000);
|
||||
assertEquals(tileCoordExtent.bottom, 0);
|
||||
assertEquals(tileCoordExtent.left, 0);
|
||||
assertEquals(100000, tileCoordExtent.top);
|
||||
assertEquals(10000, tileCoordExtent.right);
|
||||
assertEquals(90000, tileCoordExtent.bottom);
|
||||
assertEquals(0, tileCoordExtent.left);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testGetExtentTileBounds() {
|
||||
|
||||
var tileGrid =
|
||||
new ol.TileGrid(resolutions, extent, origin, xEast, ySouth, tileSize);
|
||||
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
var e = new ol.Extent(15000, 55000, 5000, 45000);
|
||||
var tileBounds;
|
||||
|
||||
@@ -207,22 +191,22 @@ function testGetExtentTileBounds() {
|
||||
assertEquals(0, tileBounds.left);
|
||||
|
||||
tileBounds = tileGrid.getExtentTileBounds(1, e);
|
||||
assertEquals(1, tileBounds.top);
|
||||
assertEquals(0, tileBounds.top);
|
||||
assertEquals(1, tileBounds.right);
|
||||
assertEquals(1, tileBounds.bottom);
|
||||
assertEquals(0, tileBounds.bottom);
|
||||
assertEquals(0, tileBounds.left);
|
||||
|
||||
tileBounds = tileGrid.getExtentTileBounds(2, e);
|
||||
assertEquals(3, tileBounds.top);
|
||||
assertEquals(0, tileBounds.top);
|
||||
assertEquals(2, tileBounds.right);
|
||||
assertEquals(3, tileBounds.bottom);
|
||||
assertEquals(0, tileBounds.bottom);
|
||||
assertEquals(1, tileBounds.left);
|
||||
|
||||
tileBounds = tileGrid.getExtentTileBounds(3, e);
|
||||
window.console.log(tileBounds);
|
||||
assertEquals(8, tileBounds.top);
|
||||
assertEquals(0, tileBounds.top);
|
||||
assertEquals(5, tileBounds.right);
|
||||
assertEquals(9, tileBounds.bottom);
|
||||
assertEquals(1, tileBounds.bottom);
|
||||
assertEquals(4, tileBounds.left);
|
||||
|
||||
}
|
||||
@@ -230,8 +214,7 @@ function testGetExtentTileBounds() {
|
||||
|
||||
function testForEachTileCoordParent() {
|
||||
|
||||
var tileGrid =
|
||||
new ol.TileGrid(resolutions, extent, origin, xEast, ySouth, tileSize);
|
||||
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
var zs = [], tileBoundss = [];
|
||||
|
||||
tileGrid.forEachTileCoordParent(
|
||||
@@ -269,7 +252,7 @@ function testForEachTileCoordParent() {
|
||||
function testGetZForResolutionExact() {
|
||||
|
||||
var tileGrid =
|
||||
new ol.TileGrid(resolutions, extent, origin, xEast, ySouth, tileSize);
|
||||
new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
|
||||
assertEquals(0, tileGrid.getZForResolution(1000));
|
||||
assertEquals(1, tileGrid.getZForResolution(500));
|
||||
@@ -282,7 +265,7 @@ function testGetZForResolutionExact() {
|
||||
function testGetZForResolutionApproximate() {
|
||||
|
||||
var tileGrid =
|
||||
new ol.TileGrid(resolutions, extent, origin, xEast, ySouth, tileSize);
|
||||
new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
|
||||
assertEquals(0, tileGrid.getZForResolution(2000));
|
||||
assertEquals(0, tileGrid.getZForResolution(1000));
|
||||
|
||||
Reference in New Issue
Block a user