diff --git a/externs/olcsx.js b/externs/olcsx.js index a69717890..f04a329db 100644 --- a/externs/olcsx.js +++ b/externs/olcsx.js @@ -83,3 +83,13 @@ olcsx.core.OlFeatureToCesiumContext; * @api */ olcsx.core.RotateAroundAxisOption; + + +/** + * @typedef {{ + * url: string, + * subdomains: string + * }} + * @api + */ +olcsx.core.CesiumUrlDefinition; diff --git a/src/core.js b/src/core.js index 9356e1d9b..1286538af 100644 --- a/src/core.js +++ b/src/core.js @@ -515,3 +515,29 @@ olcs.core.convertColorToCesium = function(olColor) { } goog.asserts.fail('impossible'); }; + + +/** + * Convert an OpenLayers url to Cesium. + * @param {string} url + * @return {!olcsx.core.CesiumUrlDefinition} + * @api + */ +olcs.core.convertUrlToCesium = function(url) { + var subdomains = ''; + var re = /\{(\d|[a-z])-(\d|[a-z])\}/; + var match = re.exec(url); + if (match) { + url = url.replace(re, '{s}'); + var startCharCode = match[1].charCodeAt(0); + var stopCharCode = match[2].charCodeAt(0); + var charCode; + for (charCode = startCharCode; charCode <= stopCharCode; ++charCode) { + subdomains += String.fromCharCode(charCode); + } + } + return { + url: url, + subdomains: subdomains + }; +};