Merge pull request #361 from bbinet/factorize-expandurl-code

Factorize code that expand a template url into multiples urls
This commit is contained in:
Bruno Binet
2013-03-13 09:47:17 -07:00
5 changed files with 97 additions and 84 deletions
+7 -8
View File
@@ -22,20 +22,19 @@ ol.source.TiledWMS = function(tiledWMSOptions) {
tileGrid = tiledWMSOptions.tileGrid; tileGrid = tiledWMSOptions.tileGrid;
} }
var tileUrlFunction; var tileUrlFunction = ol.TileUrlFunction.nullTileUrlFunction;
if (tiledWMSOptions.urls) { var urls = tiledWMSOptions.urls;
if (!goog.isDef(urls) && goog.isDef(tiledWMSOptions.url)) {
urls = ol.TileUrlFunction.expandUrl(tiledWMSOptions.url);
}
if (goog.isDef(urls)) {
var tileUrlFunctions = goog.array.map( var tileUrlFunctions = goog.array.map(
tiledWMSOptions.urls, function(url) { urls, function(url) {
return ol.TileUrlFunction.createWMSParams( return ol.TileUrlFunction.createWMSParams(
url, tiledWMSOptions.params); url, tiledWMSOptions.params);
}); });
tileUrlFunction = ol.TileUrlFunction.createFromTileUrlFunctions( tileUrlFunction = ol.TileUrlFunction.createFromTileUrlFunctions(
tileUrlFunctions); tileUrlFunctions);
} else if (tiledWMSOptions.url) {
tileUrlFunction = ol.TileUrlFunction.createWMSParams(
tiledWMSOptions.url, tiledWMSOptions.params);
} else {
tileUrlFunction = ol.TileUrlFunction.nullTileUrlFunction;
} }
var transparent = goog.isDef(tiledWMSOptions.params['TRANSPARENT']) ? var transparent = goog.isDef(tiledWMSOptions.params['TRANSPARENT']) ?
tiledWMSOptions.params['TRANSPARENT'] : true; tiledWMSOptions.params['TRANSPARENT'] : true;
+17 -29
View File
@@ -66,25 +66,6 @@ ol.source.WMTS = function(wmtsOptions) {
goog.object.extend(kvpParams, context); goog.object.extend(kvpParams, context);
} }
// TODO: factorize the code below so that it is usable by all sources
var urls = wmtsOptions.urls;
if (!goog.isDef(urls)) {
urls = [];
var url = wmtsOptions.url;
goog.asserts.assert(goog.isDef(url));
var match = /\{(\d)-(\d)\}/.exec(url) || /\{([a-z])-([a-z])\}/.exec(url);
if (match) {
var startCharCode = match[1].charCodeAt(0);
var stopCharCode = match[2].charCodeAt(0);
var charCode;
for (charCode = startCharCode; charCode <= stopCharCode; ++charCode) {
urls.push(url.replace(match[0], String.fromCharCode(charCode)));
}
} else {
urls.push(url);
}
}
/** /**
* @param {string} template Template. * @param {string} template Template.
* @return {ol.TileUrlFunctionType} Tile URL function. * @return {ol.TileUrlFunctionType} Tile URL function.
@@ -113,16 +94,23 @@ ol.source.WMTS = function(wmtsOptions) {
}; };
} }
var tileUrlFunction = ol.TileUrlFunction.createFromTileUrlFunctions( var tileUrlFunction = ol.TileUrlFunction.nullTileUrlFunction;
goog.array.map(urls, function(url) { var urls = wmtsOptions.urls;
if (goog.isDef(kvpParams)) { if (!goog.isDef(urls) && goog.isDef(wmtsOptions.url)) {
// TODO: we may want to create our own appendParams function urls = ol.TileUrlFunction.expandUrl(wmtsOptions.url);
// so that params order conforms to wmts spec guidance, }
// and so that we can avoid to escape special template params if (goog.isDef(urls)) {
url = goog.uri.utils.appendParamsFromMap(url, kvpParams); tileUrlFunction = ol.TileUrlFunction.createFromTileUrlFunctions(
} goog.array.map(urls, function(url) {
return createFromWMTSTemplate(url); if (goog.isDef(kvpParams)) {
})); // TODO: we may want to create our own appendParams function
// so that params order conforms to wmts spec guidance,
// and so that we can avoid to escape special template params
url = goog.uri.utils.appendParamsFromMap(url, kvpParams);
}
return createFromWMTSTemplate(url);
}));
}
tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform( tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform(
function(tileCoord, tileGrid, projection) { function(tileCoord, tileGrid, projection) {
+2 -1
View File
@@ -49,7 +49,8 @@ ol.source.XYZ = function(xyzOptions) {
} else if (goog.isDef(xyzOptions.urls)) { } else if (goog.isDef(xyzOptions.urls)) {
tileUrlFunction = ol.TileUrlFunction.createFromTemplates(xyzOptions.urls); tileUrlFunction = ol.TileUrlFunction.createFromTemplates(xyzOptions.urls);
} else if (goog.isDef(xyzOptions.url)) { } else if (goog.isDef(xyzOptions.url)) {
tileUrlFunction = ol.TileUrlFunction.createFromTemplate(xyzOptions.url); tileUrlFunction = ol.TileUrlFunction.createFromTemplates(
ol.TileUrlFunction.expandUrl(xyzOptions.url));
} }
var tileGrid = new ol.tilegrid.XYZ({ var tileGrid = new ol.tilegrid.XYZ({
+29 -21
View File
@@ -20,28 +20,15 @@ ol.TileUrlFunctionType;
* @return {ol.TileUrlFunctionType} Tile URL function. * @return {ol.TileUrlFunctionType} Tile URL function.
*/ */
ol.TileUrlFunction.createFromTemplate = function(template) { ol.TileUrlFunction.createFromTemplate = function(template) {
var match = return function(tileCoord) {
/\{(\d)-(\d)\}/.exec(template) || /\{([a-z])-([a-z])\}/.exec(template); if (goog.isNull(tileCoord)) {
if (match) { return undefined;
var templates = []; } else {
var startCharCode = match[1].charCodeAt(0); return template.replace('{z}', tileCoord.z)
var stopCharCode = match[2].charCodeAt(0); .replace('{x}', tileCoord.x)
var charCode; .replace('{y}', tileCoord.y);
for (charCode = startCharCode; charCode <= stopCharCode; ++charCode) {
templates.push(template.replace(match[0], String.fromCharCode(charCode)));
} }
return ol.TileUrlFunction.createFromTemplates(templates); };
} else {
return function(tileCoord) {
if (goog.isNull(tileCoord)) {
return undefined;
} else {
return template.replace('{z}', tileCoord.z)
.replace('{x}', tileCoord.x)
.replace('{y}', tileCoord.y);
}
};
}
}; };
@@ -120,3 +107,24 @@ ol.TileUrlFunction.withTileCoordTransform =
} }
}; };
}; };
/**
* @param {string} url Url.
* @return {Array.<string>} Array of urls.
*/
ol.TileUrlFunction.expandUrl = function(url) {
var urls = [];
var match = /\{(\d)-(\d)\}/.exec(url) || /\{([a-z])-([a-z])\}/.exec(url);
if (match) {
var startCharCode = match[1].charCodeAt(0);
var stopCharCode = match[2].charCodeAt(0);
var charCode;
for (charCode = startCharCode; charCode <= stopCharCode; ++charCode) {
urls.push(url.replace(match[0], String.fromCharCode(charCode)));
}
} else {
urls.push(url);
}
return urls;
};
+42 -25
View File
@@ -2,37 +2,54 @@ goog.provide('ol.test.TileUrlFunction');
describe('ol.TileUrlFunction', function() { describe('ol.TileUrlFunction', function() {
describe('expandUrl', function() {
describe('with number range', function() {
it('creates expected URLs', function() {
var template = 'http://tile-{1-3}/{z}/{x}/{y}';
var urls = ol.TileUrlFunction.expandUrl(template);
expect(urls).to.eql([
'http://tile-1/{z}/{x}/{y}',
'http://tile-2/{z}/{x}/{y}',
'http://tile-3/{z}/{x}/{y}'
]);
});
});
describe('with character range', function() {
it('creates expected URLs', function() {
var template = 'http://tile-{c-e}/{z}/{x}/{y}';
var urls = ol.TileUrlFunction.expandUrl(template);
expect(urls).to.eql([
'http://tile-c/{z}/{x}/{y}',
'http://tile-d/{z}/{x}/{y}',
'http://tile-e/{z}/{x}/{y}'
]);
});
});
});
describe('createFromTemplate', function() { describe('createFromTemplate', function() {
it('creates expected URL', function() { it('creates expected URL', function() {
var tileUrl = ol.TileUrlFunction.createFromTemplate('{z}/{x}/{y}'); var tileUrl = ol.TileUrlFunction.createFromTemplate('{z}/{x}/{y}');
expect(tileUrl(new ol.TileCoord(3, 2, 1))).to.eql('3/2/1'); expect(tileUrl(new ol.TileCoord(3, 2, 1))).to.eql('3/2/1');
expect(tileUrl(null)).to.be(undefined); expect(tileUrl(null)).to.be(undefined);
}); });
describe('with number range', function() { });
it('creates expected URL', function() {
var template = 'http://tile-{1-3}/{z}/{x}/{y}'; describe('createFromTemplates', function() {
var tileUrlFunction = ol.TileUrlFunction.createFromTemplate(template); it('creates expected URL', function() {
var tileCoord = new ol.TileCoord(3, 2, 1); var templates = [
tileCoord.hash = function() { return 3; }; 'http://tile-1/{z}/{x}/{y}',
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-1/3/2/1'); 'http://tile-2/{z}/{x}/{y}',
tileCoord.hash = function() { return 2; }; 'http://tile-3/{z}/{x}/{y}'
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-3/3/2/1'); ];
tileCoord.hash = function() { return 1; }; var tileUrlFunction = ol.TileUrlFunction.createFromTemplates(templates);
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-2/3/2/1'); var tileCoord = new ol.TileCoord(3, 2, 1);
}); tileCoord.hash = function() { return 3; };
}); expect(tileUrlFunction(tileCoord)).to.eql('http://tile-1/3/2/1');
describe('with character range', function() { tileCoord.hash = function() { return 2; };
it('creates expected URL', function() { expect(tileUrlFunction(tileCoord)).to.eql('http://tile-3/3/2/1');
var template = 'http://tile-{c-e}/{z}/{x}/{y}'; tileCoord.hash = function() { return 1; };
var tileUrlFunction = ol.TileUrlFunction.createFromTemplate(template); expect(tileUrlFunction(tileCoord)).to.eql('http://tile-2/3/2/1');
var tileCoord = new ol.TileCoord(3, 2, 1);
tileCoord.hash = function() { return 3; };
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-c/3/2/1');
tileCoord.hash = function() { return 2; };
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-e/3/2/1');
tileCoord.hash = function() { return 1; };
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-d/3/2/1');
});
}); });
}); });