diff --git a/ckanext/spatial/public/js/common_map.js b/ckanext/spatial/public/js/common_map.js index 3bd8f263..030ac403 100644 --- a/ckanext/spatial/public/js/common_map.js +++ b/ckanext/spatial/public/js/common_map.js @@ -53,6 +53,56 @@ baseLayerUrl = mapConfig['custom.url']; if (mapConfig.subdomains) leafletBaseLayerOptions.subdomains = mapConfig.subdomains; leafletBaseLayerOptions.attribution = mapConfig.attribution; + // Custom multi-layer map + } else if (mapConfig.type === 'multilayer') { + // parse layer-specific mapConfig properties into array of layers + mapConfig.layerprops = (function (mc) { + var match = []; + var ma; + for (var mprop in mc) { + if ((ma = /^(layer_)(\d+)\.(.+)$/.exec(mprop))) { + match.push(ma); + } + } + return(match); + })(mapConfig); + // construct a sorted list of layernames + mapConfig.layerlist = (function (mc) { + var ll = []; + var layername; + var anum; + var bnum; + // get layer-number from layername + function tonum(s) { + return(parseInt(/^layer_(\d+)$/i.exec(s)[1])); + }; + // build list of layernames + for (var i = 0; i < mc.layerprops.length; i++) { + layername = mc.layerprops[i][1] + mc.layerprops[i][2]; + if (ll.indexOf(layername) === -1) { + ll.push(layername); + } + } + // sort layerlist + ll = ll.sort(function (a, b) { + return(tonum(a) - tonum(b)); + }); + return(ll); + })(mapConfig); + // update mapConfig to contain strucutred layer-properties + mapConfig = (function (mc) { + var l; + var newkey; + for (var i = 0; i < mc.layerprops.length; i++) { + l = mc.layerprops[i]; + newkey = l[1] + l[2]; + mc[newkey] = mc[newkey] || {}; + mc[newkey][l[3]] = mc[l[0]]; + delete mc[l[0]]; + } + delete mc.layerprops; + return(mc); + })(mapConfig); } else { // MapQuest OpenStreetMap base map if (isHttps) { @@ -64,11 +114,23 @@ leafletBaseLayerOptions.attribution = mapConfig.attribution || 'Map data © OpenStreetMap contributors, Tiles Courtesy of MapQuest '; } - var baseLayer = new L.TileLayer(baseLayerUrl, leafletBaseLayerOptions); - map.addLayer(baseLayer); - - return map; - - } - + if (mapConfig.type === 'multilayer') { + mapConfig.layerlist.forEach(function (lname) { + var url = mapConfig[lname].url; + var options = {}; + // extract layeroptions + delete mapConfig[lname].url; + for (var prop in mapConfig[lname]) { + if (mapConfig[lname].hasOwnProperty(prop)) { + options[prop] = mapConfig[lname][prop]; + } + } + map.addLayer(new L.TileLayer(url, options)); + }); + } else { + var baseLayer = new L.TileLayer(baseLayerUrl, leafletBaseLayerOptions); + map.addLayer(baseLayer); + } + return map; + }; })(this.ckan, this.jQuery); diff --git a/ckanext/spatial/templates/spatial/snippets/map_attribution.html b/ckanext/spatial/templates/spatial/snippets/map_attribution.html index 2287ea2f..b961ba02 100644 --- a/ckanext/spatial/templates/spatial/snippets/map_attribution.html +++ b/ckanext/spatial/templates/spatial/snippets/map_attribution.html @@ -1,4 +1,4 @@ -{% if map_config.type == 'custom' %} +{% if map_config.type == 'custom' or map_config.type == 'multilayer' %}
{{ map_config.attribution|safe }}
{% else %}
Map data © OpenStreetMap contributors