Merge pull request #361 from bbinet/factorize-expandurl-code
Factorize code that expand a template url into multiples urls
This commit is contained in:
@@ -22,20 +22,19 @@ ol.source.TiledWMS = function(tiledWMSOptions) {
|
||||
tileGrid = tiledWMSOptions.tileGrid;
|
||||
}
|
||||
|
||||
var tileUrlFunction;
|
||||
if (tiledWMSOptions.urls) {
|
||||
var tileUrlFunction = ol.TileUrlFunction.nullTileUrlFunction;
|
||||
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(
|
||||
tiledWMSOptions.urls, function(url) {
|
||||
urls, function(url) {
|
||||
return ol.TileUrlFunction.createWMSParams(
|
||||
url, tiledWMSOptions.params);
|
||||
});
|
||||
tileUrlFunction = ol.TileUrlFunction.createFromTileUrlFunctions(
|
||||
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']) ?
|
||||
tiledWMSOptions.params['TRANSPARENT'] : true;
|
||||
|
||||
@@ -66,25 +66,6 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} template Template.
|
||||
* @return {ol.TileUrlFunctionType} Tile URL function.
|
||||
@@ -113,16 +94,23 @@ ol.source.WMTS = function(wmtsOptions) {
|
||||
};
|
||||
}
|
||||
|
||||
var tileUrlFunction = ol.TileUrlFunction.createFromTileUrlFunctions(
|
||||
goog.array.map(urls, function(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);
|
||||
}));
|
||||
var tileUrlFunction = ol.TileUrlFunction.nullTileUrlFunction;
|
||||
var urls = wmtsOptions.urls;
|
||||
if (!goog.isDef(urls) && goog.isDef(wmtsOptions.url)) {
|
||||
urls = ol.TileUrlFunction.expandUrl(wmtsOptions.url);
|
||||
}
|
||||
if (goog.isDef(urls)) {
|
||||
tileUrlFunction = ol.TileUrlFunction.createFromTileUrlFunctions(
|
||||
goog.array.map(urls, function(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(
|
||||
function(tileCoord, tileGrid, projection) {
|
||||
|
||||
@@ -49,7 +49,8 @@ ol.source.XYZ = function(xyzOptions) {
|
||||
} else if (goog.isDef(xyzOptions.urls)) {
|
||||
tileUrlFunction = ol.TileUrlFunction.createFromTemplates(xyzOptions.urls);
|
||||
} 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({
|
||||
|
||||
@@ -20,28 +20,15 @@ ol.TileUrlFunctionType;
|
||||
* @return {ol.TileUrlFunctionType} Tile URL function.
|
||||
*/
|
||||
ol.TileUrlFunction.createFromTemplate = function(template) {
|
||||
var match =
|
||||
/\{(\d)-(\d)\}/.exec(template) || /\{([a-z])-([a-z])\}/.exec(template);
|
||||
if (match) {
|
||||
var templates = [];
|
||||
var startCharCode = match[1].charCodeAt(0);
|
||||
var stopCharCode = match[2].charCodeAt(0);
|
||||
var charCode;
|
||||
for (charCode = startCharCode; charCode <= stopCharCode; ++charCode) {
|
||||
templates.push(template.replace(match[0], String.fromCharCode(charCode)));
|
||||
return function(tileCoord) {
|
||||
if (goog.isNull(tileCoord)) {
|
||||
return undefined;
|
||||
} else {
|
||||
return template.replace('{z}', tileCoord.z)
|
||||
.replace('{x}', tileCoord.x)
|
||||
.replace('{y}', tileCoord.y);
|
||||
}
|
||||
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;
|
||||
};
|
||||
|
||||
@@ -2,37 +2,54 @@ 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).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() {
|
||||
it('creates expected URL', function() {
|
||||
var tileUrl = ol.TileUrlFunction.createFromTemplate('{z}/{x}/{y}');
|
||||
expect(tileUrl(new ol.TileCoord(3, 2, 1))).to.eql('3/2/1');
|
||||
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}';
|
||||
var tileUrlFunction = ol.TileUrlFunction.createFromTemplate(template);
|
||||
var tileCoord = new ol.TileCoord(3, 2, 1);
|
||||
tileCoord.hash = function() { return 3; };
|
||||
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-1/3/2/1');
|
||||
tileCoord.hash = function() { return 2; };
|
||||
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-3/3/2/1');
|
||||
tileCoord.hash = function() { return 1; };
|
||||
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-2/3/2/1');
|
||||
});
|
||||
});
|
||||
describe('with character range', function() {
|
||||
it('creates expected URL', function() {
|
||||
var template = 'http://tile-{c-e}/{z}/{x}/{y}';
|
||||
var tileUrlFunction = ol.TileUrlFunction.createFromTemplate(template);
|
||||
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');
|
||||
});
|
||||
});
|
||||
|
||||
describe('createFromTemplates', function() {
|
||||
it('creates expected URL', function() {
|
||||
var templates = [
|
||||
'http://tile-1/{z}/{x}/{y}',
|
||||
'http://tile-2/{z}/{x}/{y}',
|
||||
'http://tile-3/{z}/{x}/{y}'
|
||||
];
|
||||
var tileUrlFunction = ol.TileUrlFunction.createFromTemplates(templates);
|
||||
var tileCoord = new ol.TileCoord(3, 2, 1);
|
||||
tileCoord.hash = function() { return 3; };
|
||||
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-1/3/2/1');
|
||||
tileCoord.hash = function() { return 2; };
|
||||
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-3/3/2/1');
|
||||
tileCoord.hash = function() { return 1; };
|
||||
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-2/3/2/1');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user