From 54dfca34193a4fa1ea334d31cf459c25ef9ac5a3 Mon Sep 17 00:00:00 2001 From: Bruno Binet Date: Tue, 12 Mar 2013 18:26:43 +0100 Subject: [PATCH] Move factorizable code in ol.TileUrlFunction.expandUrl --- src/ol/source/wmtssource.js | 17 ++--------------- src/ol/tileurlfunction.js | 21 +++++++++++++++++++++ test/spec/ol/tileurlfunction.test.js | 25 +++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/src/ol/source/wmtssource.js b/src/ol/source/wmtssource.js index cd5b1b8a85..e881c94145 100644 --- a/src/ol/source/wmtssource.js +++ b/src/ol/source/wmtssource.js @@ -66,23 +66,10 @@ ol.source.WMTS = function(wmtsOptions) { 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); - } + goog.asserts.assert(goog.isDef(wmtsOptions.url)); + urls = ol.TileUrlFunction.expandUrl(wmtsOptions.url); } /** diff --git a/src/ol/tileurlfunction.js b/src/ol/tileurlfunction.js index 209febe19c..1ea5862fc3 100644 --- a/src/ol/tileurlfunction.js +++ b/src/ol/tileurlfunction.js @@ -120,3 +120,24 @@ ol.TileUrlFunction.withTileCoordTransform = } }; }; + + +/** + * @param {string} url Url. + * @return {Array.} 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; +}; diff --git a/test/spec/ol/tileurlfunction.test.js b/test/spec/ol/tileurlfunction.test.js index 5cd059bcd4..800bcf5209 100644 --- a/test/spec/ol/tileurlfunction.test.js +++ b/test/spec/ol/tileurlfunction.test.js @@ -2,6 +2,31 @@ goog.provide('ol.test.TileUrlFunction'); 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).toEqual([ + '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).toEqual([ + 'http://tile-c/{z}/{x}/{y}', + 'http://tile-d/{z}/{x}/{y}', + 'http://tile-e/{z}/{x}/{y}' + ]); + }); + }); + }); + describe('createFromTemplate', function() { it('creates expected URL', function() { var tileUrl = ol.TileUrlFunction.createFromTemplate('{z}/{x}/{y}');