Refactor build system and layout
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
goog.provide('ol.tilestore.createOpenStreetMap');
|
||||
|
||||
goog.require('goog.math');
|
||||
goog.require('ol.Projection');
|
||||
goog.require('ol.TileCoord');
|
||||
goog.require('ol.TileGrid');
|
||||
goog.require('ol.TileStore');
|
||||
goog.require('ol.TileUrlFunction');
|
||||
goog.require('ol.tilegrid.createOpenStreetMap');
|
||||
|
||||
|
||||
/**
|
||||
* @return {ol.TileStore} Tile store.
|
||||
*/
|
||||
ol.tilestore.createOpenStreetMap = function() {
|
||||
|
||||
var projection = ol.Projection.createFromCode('EPSG:3857');
|
||||
var tileGrid = ol.tilegrid.createOpenStreetMap(18);
|
||||
var tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform(
|
||||
function(tileCoord) {
|
||||
var n = 1 << tileCoord.z;
|
||||
var y = -tileCoord.y - 1;
|
||||
if (y < 0 || n <= y) {
|
||||
return null;
|
||||
} else {
|
||||
var x = goog.math.modulo(tileCoord.x, n);
|
||||
return new ol.TileCoord(tileCoord.z, x, y);
|
||||
}
|
||||
},
|
||||
ol.TileUrlFunction.createFromTemplates([
|
||||
'http://a.tile.openstreetmap.org/{z}/{x}/{y}.png',
|
||||
'http://b.tile.openstreetmap.org/{z}/{x}/{y}.png',
|
||||
'http://c.tile.openstreetmap.org/{z}/{x}/{y}.png'
|
||||
]));
|
||||
var extent = projection.getExtent();
|
||||
var attribution =
|
||||
'© ' +
|
||||
'<a href="http://www.openstreetmap.org">OpenStreetMap</a> ' +
|
||||
'contributors, ' +
|
||||
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC BY-SA</a>';
|
||||
var crossOrigin = '';
|
||||
|
||||
return new ol.TileStore(
|
||||
projection, tileGrid, tileUrlFunction, extent, attribution, crossOrigin);
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
goog.provide('ol.TileStore');
|
||||
|
||||
goog.require('ol.Store');
|
||||
goog.require('ol.Tile');
|
||||
goog.require('ol.TileCoord');
|
||||
goog.require('ol.TileGrid');
|
||||
goog.require('ol.TileUrlFunctionType');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.Store}
|
||||
* @param {ol.Projection} projection Projection.
|
||||
* @param {ol.TileGrid} tileGrid Tile grid.
|
||||
* @param {ol.TileUrlFunctionType} tileUrlFunction Tile URL.
|
||||
* @param {ol.Extent=} opt_extent Extent.
|
||||
* @param {string=} opt_attribution Attribution.
|
||||
* @param {string=} opt_crossOrigin Cross origin.
|
||||
*/
|
||||
ol.TileStore = function(projection, tileGrid, tileUrlFunction, opt_extent,
|
||||
opt_attribution, opt_crossOrigin) {
|
||||
|
||||
goog.base(this, projection, opt_extent, opt_attribution);
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.TileGrid}
|
||||
*/
|
||||
this.tileGrid_ = tileGrid;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.TileUrlFunctionType}
|
||||
*/
|
||||
this.tileUrlFunction_ = tileUrlFunction;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {string|undefined}
|
||||
*/
|
||||
this.crossOrigin_ = opt_crossOrigin;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Object.<string, ol.Tile>}
|
||||
* FIXME will need to expire elements from this cache
|
||||
* FIXME see elemoine's work with goog.structs.LinkedMap
|
||||
*/
|
||||
this.tileCache_ = {};
|
||||
|
||||
};
|
||||
goog.inherits(ol.TileStore, ol.Store);
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.TileStore.prototype.getResolutions = function() {
|
||||
return this.tileGrid_.getResolutions();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.TileCoord} tileCoord Tile coordinate.
|
||||
* @return {ol.Tile} Tile.
|
||||
*/
|
||||
ol.TileStore.prototype.getTile = function(tileCoord) {
|
||||
var key = tileCoord.toString();
|
||||
if (goog.object.containsKey(this.tileCache_, key)) {
|
||||
return this.tileCache_[key];
|
||||
} else {
|
||||
var tileUrl = this.getTileCoordUrl(tileCoord);
|
||||
var tile;
|
||||
if (goog.isDef(tileUrl)) {
|
||||
tile = new ol.Tile(tileCoord, tileUrl, this.crossOrigin_);
|
||||
} else {
|
||||
tile = null;
|
||||
}
|
||||
this.tileCache_[key] = tile;
|
||||
return tile;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.TileCoord} tileCoord Tile coordinate.
|
||||
* @return {string} Tile coord URL.
|
||||
*/
|
||||
ol.TileStore.prototype.getTileCoordUrl = function(tileCoord) {
|
||||
return this.tileUrlFunction_(tileCoord);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {ol.TileGrid} Tile grid.
|
||||
*/
|
||||
ol.TileStore.prototype.getTileGrid = function() {
|
||||
return this.tileGrid_;
|
||||
};
|
||||
@@ -0,0 +1,73 @@
|
||||
goog.require('goog.testing.jsunit');
|
||||
goog.require('ol.TileCoord');
|
||||
goog.require('ol.tilestore.createOpenStreetMap');
|
||||
|
||||
|
||||
function getTileCoordPart(tileUrl) {
|
||||
return tileUrl.substr(32, tileUrl.length - 36);
|
||||
}
|
||||
|
||||
|
||||
function testOpenStreetMap() {
|
||||
|
||||
var tileStore = ol.tilestore.createOpenStreetMap(8);
|
||||
var tileGrid = tileStore.getTileGrid();
|
||||
|
||||
var coordinate =
|
||||
new goog.math.Coordinate(829330.2064098881, 5933916.615134273);
|
||||
var tileUrl;
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(tileGrid.getTileCoord(0, coordinate));
|
||||
assertEquals('0/0/0', getTileCoordPart(tileUrl));
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(tileGrid.getTileCoord(1, coordinate));
|
||||
assertEquals('1/1/0', getTileCoordPart(tileUrl));
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(tileGrid.getTileCoord(2, coordinate));
|
||||
assertEquals('2/2/1', getTileCoordPart(tileUrl));
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(tileGrid.getTileCoord(3, coordinate));
|
||||
assertEquals('3/4/2', getTileCoordPart(tileUrl));
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(tileGrid.getTileCoord(4, coordinate));
|
||||
assertEquals('4/8/5', getTileCoordPart(tileUrl));
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(tileGrid.getTileCoord(5, coordinate));
|
||||
assertEquals('5/16/11', getTileCoordPart(tileUrl));
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(tileGrid.getTileCoord(6, coordinate));
|
||||
assertEquals('6/33/22', getTileCoordPart(tileUrl));
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testOpenStreetMapWrapX() {
|
||||
|
||||
var tileStore = ol.tilestore.createOpenStreetMap(8);
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(new ol.TileCoord(6, -31, -23));
|
||||
assertEquals('6/33/22', getTileCoordPart(tileUrl));
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(new ol.TileCoord(6, 33, -23));
|
||||
assertEquals('6/33/22', getTileCoordPart(tileUrl));
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(new ol.TileCoord(6, 97, -23));
|
||||
assertEquals('6/33/22', getTileCoordPart(tileUrl));
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testOpenStreetMapCropY() {
|
||||
|
||||
var tileStore = ol.tilestore.createOpenStreetMap(8);
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(new ol.TileCoord(6, 33, -87));
|
||||
assertUndefined(tileUrl);
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(new ol.TileCoord(6, 33, -23));
|
||||
assertEquals('6/33/22', getTileCoordPart(tileUrl));
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(new ol.TileCoord(6, 33, 41));
|
||||
assertUndefined(tileUrl);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user