Transform tile coords when converting them to URLs
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
goog.provide('ol.TileGrid');
|
goog.provide('ol.TileGrid');
|
||||||
|
goog.provide('ol.TileGrid.createOpenStreetMap');
|
||||||
|
|
||||||
goog.require('goog.array');
|
goog.require('goog.array');
|
||||||
goog.require('goog.asserts');
|
goog.require('goog.asserts');
|
||||||
|
|||||||
+10
-6
@@ -1,4 +1,5 @@
|
|||||||
goog.provide('ol.TileStore');
|
goog.provide('ol.TileStore');
|
||||||
|
goog.provide('ol.TileStore.createOpenStreetMap');
|
||||||
|
|
||||||
goog.require('ol.Store');
|
goog.require('ol.Store');
|
||||||
goog.require('ol.Tile');
|
goog.require('ol.Tile');
|
||||||
@@ -60,11 +61,15 @@ ol.TileStore.createOpenStreetMap = function() {
|
|||||||
|
|
||||||
var projection = ol.Projection.createFromCode('EPSG:3857');
|
var projection = ol.Projection.createFromCode('EPSG:3857');
|
||||||
var tileGrid = ol.TileGrid.createOpenStreetMap(18);
|
var tileGrid = ol.TileGrid.createOpenStreetMap(18);
|
||||||
var tileUrlFunction = ol.TileUrlFunction.createFromTemplates([
|
var tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform(
|
||||||
'http://a.tile.openstreetmap.org/{z}/{x}/{y}.png',
|
function(tileCoord) {
|
||||||
'http://b.tile.openstreetmap.org/{z}/{x}/{y}.png',
|
return new ol.TileCoord(tileCoord.z, tileCoord.x, -tileCoord.y - 1);
|
||||||
'http://c.tile.openstreetmap.org/{z}/{x}/{y}.png'
|
},
|
||||||
]);
|
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 extent = projection.getExtent();
|
||||||
var attribution =
|
var attribution =
|
||||||
'© ' +
|
'© ' +
|
||||||
@@ -109,7 +114,6 @@ ol.TileStore.prototype.getTile = function(tileCoord) {
|
|||||||
* @return {string} Tile coord URL.
|
* @return {string} Tile coord URL.
|
||||||
*/
|
*/
|
||||||
ol.TileStore.prototype.getTileCoordUrl = function(tileCoord) {
|
ol.TileStore.prototype.getTileCoordUrl = function(tileCoord) {
|
||||||
// FIXME maybe wrap x and y
|
|
||||||
return this.tileUrlFunction_(tileCoord);
|
return this.tileUrlFunction_(tileCoord);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
goog.require('goog.testing.jsunit');
|
||||||
|
goog.require('ol.TileStore.createOpenStreetMap');
|
||||||
|
|
||||||
|
|
||||||
|
function testOpenStreetMap() {
|
||||||
|
|
||||||
|
var tileStore = ol.TileStore.createOpenStreetMap(8);
|
||||||
|
var tileGrid = tileStore.getTileGrid();
|
||||||
|
|
||||||
|
var coordinate =
|
||||||
|
new goog.math.Coordinate(829330.2064098881, 5933916.615134273);
|
||||||
|
var tileUrl;
|
||||||
|
|
||||||
|
var getTileCoordPart = function(tileUrl) {
|
||||||
|
return tileUrl.substr(32, tileUrl.length - 36);
|
||||||
|
};
|
||||||
|
|
||||||
|
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));
|
||||||
|
|
||||||
|
}
|
||||||
@@ -44,3 +44,16 @@ ol.TileUrlFunction.createFromTemplates = function(templates) {
|
|||||||
return ol.TileUrlFunction.createFromTileUrlFunctions(
|
return ol.TileUrlFunction.createFromTileUrlFunctions(
|
||||||
goog.array.map(templates, ol.TileUrlFunction.createFromTemplate));
|
goog.array.map(templates, ol.TileUrlFunction.createFromTemplate));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {function(ol.TileCoord): ol.TileCoord} transform Transform.
|
||||||
|
* @param {ol.TileUrlFunctionType} tileUrlFunction Tile URL function.
|
||||||
|
* @return {ol.TileUrlFunctionType} Tile URL function.
|
||||||
|
*/
|
||||||
|
ol.TileUrlFunction.withTileCoordTransform =
|
||||||
|
function(transform, tileUrlFunction) {
|
||||||
|
return function(tileCoord) {
|
||||||
|
return tileUrlFunction(transform(tileCoord));
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|||||||
@@ -9,6 +9,16 @@ function testCreateFromTemplate() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function testWithTileCoordTransform() {
|
||||||
|
var tileUrl = ol.TileUrlFunction.withTileCoordTransform(
|
||||||
|
function(tileCoord) {
|
||||||
|
return new ol.TileCoord(tileCoord.z, tileCoord.x, -tileCoord.y);
|
||||||
|
},
|
||||||
|
ol.TileUrlFunction.createFromTemplate('{z}/{x}/{y}'));
|
||||||
|
assertEquals('3/2/1', tileUrl(new ol.TileCoord(3, 2, -1)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function testCreateFromTileUrlFunctions() {
|
function testCreateFromTileUrlFunctions() {
|
||||||
var tileUrl = ol.TileUrlFunction.createFromTileUrlFunctions([
|
var tileUrl = ol.TileUrlFunction.createFromTileUrlFunctions([
|
||||||
ol.TileUrlFunction.createFromTemplate('a'),
|
ol.TileUrlFunction.createFromTemplate('a'),
|
||||||
|
|||||||
Reference in New Issue
Block a user