Refactor build system and layout
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
goog.provide('ol.tilegrid.createOpenStreetMap');
|
||||
|
||||
goog.require('goog.math.Coordinate');
|
||||
goog.require('goog.math.Size');
|
||||
goog.require('ol.Projection');
|
||||
goog.require('ol.TileGrid');
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} maxZoom Maximum zoom.
|
||||
* @return {ol.TileGrid} Tile grid.
|
||||
*/
|
||||
ol.tilegrid.createOpenStreetMap = function(maxZoom) {
|
||||
|
||||
var resolutions = new Array(maxZoom + 1);
|
||||
var z;
|
||||
for (z = 0; z <= maxZoom; ++z) {
|
||||
resolutions[z] = ol.Projection.EPSG_3857_HALF_SIZE / (128 << z);
|
||||
}
|
||||
|
||||
var extent = ol.Projection.EPSG_3857_EXTENT;
|
||||
var origin = new goog.math.Coordinate(
|
||||
-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, tileSize);
|
||||
|
||||
};
|
||||
@@ -0,0 +1,247 @@
|
||||
goog.provide('ol.TileGrid');
|
||||
|
||||
goog.require('goog.array');
|
||||
goog.require('goog.asserts');
|
||||
goog.require('goog.math.Coordinate');
|
||||
goog.require('goog.math.Size');
|
||||
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.math.Coordinate|!Array.<goog.math.Coordinate>} origin Origin.
|
||||
* @param {goog.math.Size=} opt_tileSize Tile size.
|
||||
*/
|
||||
ol.TileGrid = function(resolutions, extent, origin, opt_tileSize) {
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Array.<number>}
|
||||
*/
|
||||
this.resolutions_ = resolutions;
|
||||
goog.asserts.assert(goog.array.isSorted(resolutions, function(a, b) {
|
||||
return -goog.array.defaultCompare(a, b);
|
||||
}, true));
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {number}
|
||||
*/
|
||||
this.numResolutions_ = this.resolutions_.length;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.Extent}
|
||||
*/
|
||||
this.extent_ = extent;
|
||||
|
||||
/**
|
||||
* @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.numResolutions_);
|
||||
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);
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.TileCoord} tileCoord Tile coordinate.
|
||||
* @param {function(number, ol.TileBounds): boolean} callback Callback.
|
||||
*/
|
||||
ol.TileGrid.prototype.forEachTileCoordParent = function(tileCoord, callback) {
|
||||
var tileCoordExtent = this.getTileCoordExtent(tileCoord);
|
||||
var z = tileCoord.z - 1;
|
||||
while (z >= 0) {
|
||||
if (callback(z, this.getExtentTileBounds(z, tileCoordExtent))) {
|
||||
return;
|
||||
}
|
||||
--z;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {ol.Extent} Extent.
|
||||
*/
|
||||
ol.TileGrid.prototype.getExtent = function() {
|
||||
return this.extent_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} z Z.
|
||||
* @param {ol.Extent} extent Extent.
|
||||
* @return {ol.TileBounds} Tile bounds.
|
||||
*/
|
||||
ol.TileGrid.prototype.getExtentTileBounds = function(z, extent) {
|
||||
var min =
|
||||
this.getTileCoord(z, new goog.math.Coordinate(extent.minX, extent.minY));
|
||||
var max =
|
||||
this.getTileCoord(z, new goog.math.Coordinate(extent.maxX, extent.maxY));
|
||||
return new ol.TileBounds(min.x, min.y, max.x, max.y);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @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.numResolutions_);
|
||||
return this.resolutions_[z];
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {Array.<number>} Resolutions.
|
||||
*/
|
||||
ol.TileGrid.prototype.getResolutions = function() {
|
||||
return this.resolutions_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} z Z.
|
||||
* @param {goog.math.Coordinate} coordinate Coordinate.
|
||||
* @return {ol.TileCoord} Tile coordinate.
|
||||
*/
|
||||
ol.TileGrid.prototype.getTileCoord = function(z, coordinate) {
|
||||
var origin = this.getOrigin(z);
|
||||
var resolution = this.getResolution(z);
|
||||
var tileSize = this.getTileSize();
|
||||
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);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.TileCoord} tileCoord Tile coordinate.
|
||||
* @return {goog.math.Coordinate} Tile center.
|
||||
*/
|
||||
ol.TileGrid.prototype.getTileCoordCenter = function(tileCoord) {
|
||||
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 = origin.y + (tileCoord.y + 0.5) * tileSize.height * resolution;
|
||||
return new goog.math.Coordinate(x, y);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} z Z.
|
||||
* @param {ol.TileBounds} tileBounds Tile bounds.
|
||||
* @return {ol.Extent} Extent.
|
||||
*/
|
||||
ol.TileGrid.prototype.getTileBoundsExtent = function(z, tileBounds) {
|
||||
var origin = this.getOrigin(z);
|
||||
var resolution = this.getResolution(z);
|
||||
var tileSize = this.tileSize_;
|
||||
var minX = origin.x + tileBounds.minX * tileSize.width * resolution;
|
||||
var minY = origin.y + tileBounds.minY * tileSize.height * resolution;
|
||||
var maxX = origin.x + (tileBounds.maxX + 1) * tileSize.width * resolution;
|
||||
var maxY = origin.y + (tileBounds.maxY + 1) * tileSize.height * resolution;
|
||||
return new ol.Extent(minX, minY, maxX, maxY);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.TileCoord} tileCoord Tile coordinate.
|
||||
* @return {ol.Extent} Extent.
|
||||
*/
|
||||
ol.TileGrid.prototype.getTileCoordExtent = function(tileCoord) {
|
||||
var origin = this.getOrigin(tileCoord.z);
|
||||
var resolution = this.getResolution(tileCoord.z);
|
||||
var tileSize = this.tileSize_;
|
||||
var minX = origin.x + tileCoord.x * tileSize.width * resolution;
|
||||
var minY = origin.y + tileCoord.y * tileSize.height * resolution;
|
||||
var maxX = minX + tileSize.width * resolution;
|
||||
var maxY = minY + tileSize.height * resolution;
|
||||
return new ol.Extent(minX, minY, maxX, maxY);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.TileCoord} tileCoord Tile coordinate.
|
||||
* @return {number} Tile resolution.
|
||||
*/
|
||||
ol.TileGrid.prototype.getTileCoordResolution = function(tileCoord) {
|
||||
goog.asserts.assert(0 <= tileCoord.z && tileCoord.z < this.numResolutions_);
|
||||
return this.resolutions_[tileCoord.z];
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {goog.math.Size} Tile size.
|
||||
*/
|
||||
ol.TileGrid.prototype.getTileSize = function() {
|
||||
return this.tileSize_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} resolution Resolution.
|
||||
* @return {number} Z.
|
||||
*/
|
||||
ol.TileGrid.prototype.getZForResolution = function(resolution) {
|
||||
var z;
|
||||
for (z = 0; z < this.numResolutions_; ++z) {
|
||||
if (this.resolutions_[z] == resolution) {
|
||||
return z;
|
||||
} else if (this.resolutions_[z] < resolution) {
|
||||
if (z === 0) {
|
||||
return z;
|
||||
} else if (resolution - this.resolutions_[z] <=
|
||||
this.resolutions_[z - 1] - resolution) {
|
||||
return z;
|
||||
} else {
|
||||
return z - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return this.numResolutions_ - 1;
|
||||
};
|
||||
@@ -0,0 +1,283 @@
|
||||
goog.require('goog.math.Coordinate');
|
||||
goog.require('goog.math.Size');
|
||||
goog.require('goog.testing.jsunit');
|
||||
goog.require('ol.Extent');
|
||||
goog.require('ol.TileCoord');
|
||||
goog.require('ol.TileGrid');
|
||||
|
||||
|
||||
var extent;
|
||||
var resolutions;
|
||||
var origin;
|
||||
var origins;
|
||||
var tileSize;
|
||||
|
||||
|
||||
function setUp() {
|
||||
resolutions = [1000, 500, 250, 100];
|
||||
extent = new ol.Extent(0, 0, 100000, 100000);
|
||||
origin = new goog.math.Coordinate(0, 0);
|
||||
origins = [];
|
||||
tileSize = new goog.math.Size(100, 100);
|
||||
}
|
||||
|
||||
|
||||
function testCreateValid() {
|
||||
assertNotThrows(function() {
|
||||
return new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function testCreateDuplicateResolutions() {
|
||||
var resolutions = [100, 50, 50, 25, 10];
|
||||
assertThrows(function() {
|
||||
return new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function testCreateOutOfOrderResolutions() {
|
||||
var resolutions = [100, 25, 50, 10];
|
||||
assertThrows(function() {
|
||||
return new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function testCreateOrigins() {
|
||||
var resolutions = [100, 50, 25, 10];
|
||||
var origins = [origin, origin, origin, origin];
|
||||
assertNotThrows(function() {
|
||||
return new ol.TileGrid(resolutions, extent, origins, tileSize);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function testCreateTooFewOrigins() {
|
||||
var resolutions = [100, 50, 25, 10];
|
||||
var origins = [origin, origin, origin];
|
||||
assertThrows(function() {
|
||||
return new ol.TileGrid(resolutions, extent, origins, tileSize);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function testCreateTooManyOrigins() {
|
||||
var resolutions = [100, 50, 25, 10];
|
||||
var origins = [origin, origin, origin, origin, origin];
|
||||
assertThrows(function() {
|
||||
return new ol.TileGrid(resolutions, extent, origins, tileSize);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function testGetTileCoord() {
|
||||
|
||||
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(0, tileCoord.y);
|
||||
|
||||
tileCoord = tileGrid.getTileCoord(3, new goog.math.Coordinate(0, 100000));
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(0, tileCoord.x);
|
||||
assertEquals(10, tileCoord.y);
|
||||
|
||||
tileCoord = tileGrid.getTileCoord(3, new goog.math.Coordinate(100000, 0));
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(10, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
tileCoord =
|
||||
tileGrid.getTileCoord(3, new goog.math.Coordinate(100000, 100000));
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(10, tileCoord.x);
|
||||
assertEquals(10, tileCoord.y);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testGetTileCoordYSouth() {
|
||||
|
||||
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(-10, tileCoord.y);
|
||||
|
||||
tileCoord = tileGrid.getTileCoord(3, new goog.math.Coordinate(0, 100000));
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(0, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
tileCoord = tileGrid.getTileCoord(3, new goog.math.Coordinate(100000, 0));
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(10, tileCoord.x);
|
||||
assertEquals(-10, tileCoord.y);
|
||||
|
||||
tileCoord =
|
||||
tileGrid.getTileCoord(3, new goog.math.Coordinate(100000, 100000));
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(10, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testGetTileCoordCenter() {
|
||||
|
||||
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
var center;
|
||||
|
||||
center = tileGrid.getTileCoordCenter(new ol.TileCoord(0, 0, 0));
|
||||
assertEquals(50000, center.x);
|
||||
assertEquals(50000, center.y);
|
||||
|
||||
center = tileGrid.getTileCoordCenter(new ol.TileCoord(3, 0, 0));
|
||||
assertEquals(5000, center.x);
|
||||
assertEquals(5000, center.y);
|
||||
|
||||
center = tileGrid.getTileCoordCenter(new ol.TileCoord(3, 9, 9));
|
||||
assertEquals(95000, center.x);
|
||||
assertEquals(95000, center.y);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testGetTileCoordExtent() {
|
||||
|
||||
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
var tileCoordExtent;
|
||||
|
||||
tileCoordExtent = tileGrid.getTileCoordExtent(new ol.TileCoord(0, 0, 0));
|
||||
assertEquals(0, tileCoordExtent.minX);
|
||||
assertEquals(0, tileCoordExtent.minY);
|
||||
assertEquals(100000, tileCoordExtent.maxX);
|
||||
assertEquals(100000, tileCoordExtent.maxY);
|
||||
|
||||
tileCoordExtent = tileGrid.getTileCoordExtent(new ol.TileCoord(3, 9, 0));
|
||||
assertEquals(90000, tileCoordExtent.minX);
|
||||
assertEquals(0, tileCoordExtent.minY);
|
||||
assertEquals(100000, tileCoordExtent.maxX);
|
||||
assertEquals(10000, tileCoordExtent.maxY);
|
||||
|
||||
tileCoordExtent = tileGrid.getTileCoordExtent(new ol.TileCoord(3, 0, 9));
|
||||
assertEquals(0, tileCoordExtent.minX);
|
||||
assertEquals(90000, tileCoordExtent.minY);
|
||||
assertEquals(10000, tileCoordExtent.maxX);
|
||||
assertEquals(100000, tileCoordExtent.maxY);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testGetExtentTileBounds() {
|
||||
|
||||
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
var e = new ol.Extent(45000, 5000, 55000, 15000);
|
||||
var tileBounds;
|
||||
|
||||
tileBounds = tileGrid.getExtentTileBounds(0, e);
|
||||
assertEquals(0, tileBounds.minY);
|
||||
assertEquals(0, tileBounds.minX);
|
||||
assertEquals(0, tileBounds.maxX);
|
||||
assertEquals(0, tileBounds.maxY);
|
||||
|
||||
tileBounds = tileGrid.getExtentTileBounds(1, e);
|
||||
assertEquals(0, tileBounds.minX);
|
||||
assertEquals(0, tileBounds.minY);
|
||||
assertEquals(1, tileBounds.maxX);
|
||||
assertEquals(0, tileBounds.maxY);
|
||||
|
||||
tileBounds = tileGrid.getExtentTileBounds(2, e);
|
||||
assertEquals(1, tileBounds.minX);
|
||||
assertEquals(0, tileBounds.minY);
|
||||
assertEquals(2, tileBounds.maxX);
|
||||
assertEquals(0, tileBounds.maxY);
|
||||
|
||||
tileBounds = tileGrid.getExtentTileBounds(3, e);
|
||||
assertEquals(4, tileBounds.minX);
|
||||
assertEquals(0, tileBounds.minY);
|
||||
assertEquals(5, tileBounds.maxX);
|
||||
assertEquals(1, tileBounds.maxY);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testForEachTileCoordParent() {
|
||||
|
||||
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
var zs = [], tileBoundss = [];
|
||||
|
||||
tileGrid.forEachTileCoordParent(
|
||||
new ol.TileCoord(3, 7, 3),
|
||||
function(z, tileBounds) {
|
||||
zs.push(z);
|
||||
tileBoundss.push(tileBounds);
|
||||
return false;
|
||||
});
|
||||
|
||||
assertEquals(3, zs.length);
|
||||
assertEquals(3, tileBoundss.length);
|
||||
|
||||
assertEquals(2, zs[0]);
|
||||
assertEquals(2, tileBoundss[0].minX);
|
||||
assertEquals(1, tileBoundss[0].minY);
|
||||
assertEquals(3, tileBoundss[0].maxX);
|
||||
assertEquals(1, tileBoundss[0].maxY);
|
||||
|
||||
assertEquals(1, zs[1]);
|
||||
assertEquals(1, tileBoundss[1].minX);
|
||||
assertEquals(0, tileBoundss[1].minY);
|
||||
assertEquals(1, tileBoundss[1].maxX);
|
||||
assertEquals(0, tileBoundss[1].maxY);
|
||||
|
||||
assertEquals(0, zs[2]);
|
||||
assertEquals(0, tileBoundss[2].minX);
|
||||
assertEquals(0, tileBoundss[2].minY);
|
||||
assertEquals(0, tileBoundss[2].maxX);
|
||||
assertEquals(0, tileBoundss[2].maxY);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testGetZForResolutionExact() {
|
||||
|
||||
var tileGrid =
|
||||
new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
|
||||
assertEquals(0, tileGrid.getZForResolution(1000));
|
||||
assertEquals(1, tileGrid.getZForResolution(500));
|
||||
assertEquals(2, tileGrid.getZForResolution(250));
|
||||
assertEquals(3, tileGrid.getZForResolution(100));
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testGetZForResolutionApproximate() {
|
||||
|
||||
var tileGrid =
|
||||
new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
|
||||
assertEquals(0, tileGrid.getZForResolution(2000));
|
||||
assertEquals(0, tileGrid.getZForResolution(1000));
|
||||
assertEquals(0, tileGrid.getZForResolution(900));
|
||||
assertEquals(1, tileGrid.getZForResolution(750));
|
||||
assertEquals(1, tileGrid.getZForResolution(625));
|
||||
assertEquals(1, tileGrid.getZForResolution(500));
|
||||
assertEquals(1, tileGrid.getZForResolution(475));
|
||||
assertEquals(2, tileGrid.getZForResolution(375));
|
||||
assertEquals(2, tileGrid.getZForResolution(250));
|
||||
assertEquals(2, tileGrid.getZForResolution(200));
|
||||
assertEquals(3, tileGrid.getZForResolution(125));
|
||||
assertEquals(3, tileGrid.getZForResolution(100));
|
||||
assertEquals(3, tileGrid.getZForResolution(50));
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user