diff --git a/ckanext/mapviews/plugin.py b/ckanext/mapviews/plugin.py index e12e7cd..3a3b56c 100644 --- a/ckanext/mapviews/plugin.py +++ b/ckanext/mapviews/plugin.py @@ -1,8 +1,10 @@ import urlparse +import json import ckan.plugins as p import pylons.config as config + Invalid = p.toolkit.Invalid _ = p.toolkit._ not_empty = p.toolkit.get_validator('not_empty') @@ -65,13 +67,16 @@ def setup_template_variables(self, context, data_dict): fields_without_id = _remove_id_and_prepare_to_template(fields) numeric_fields = _filter_numeric_fields_without_id(fields) textual_fields = _filter_textual_fields_without_id(fields) + map_config = _get_map_configuration() return {'resource': resource, 'resource_view': resource_view, 'geojson_resources': geojson_resources, 'fields': fields_without_id, 'numeric_fields': numeric_fields, - 'textual_fields': textual_fields} + 'textual_fields': textual_fields, + 'map_config': map_config + } def view_template(self, context, data_dict): return 'navigablemap_view.html' @@ -98,6 +103,13 @@ def view_template(self, context, data_dict): def form_template(self, context, data_dict): return 'choroplethmap_form.html' +def _get_map_configuration(): + + namespace = 'ckanext.spatial.common_map.' + map_config = dict([(k.replace(namespace, ''), v) for k, v in config.iteritems() if k.startswith(namespace)]) + + return map_config + def _get_geojson_resources(): data = { diff --git a/ckanext/mapviews/theme/public/navigablemap.js b/ckanext/mapviews/theme/public/navigablemap.js index 637fbac..e3878ba 100644 --- a/ckanext/mapviews/theme/public/navigablemap.js +++ b/ckanext/mapviews/theme/public/navigablemap.js @@ -29,6 +29,7 @@ this.ckan.views.mapviews.navigablemap = (function () { geojsonUrl = options.geojsonUrl, geojsonKeyField = options.geojsonKeyField, resourceKeyField = options.resourceKeyField, + mapBaseLayerConfig = options.mapConfig, redirectToUrl = (options.redirectToUrl === true) ? '' : options.redirectToUrl, filterFields = options.filterFields, map = L.map(elementId), @@ -37,12 +38,13 @@ this.ckan.views.mapviews.navigablemap = (function () { maxBounds, router; + var isInOwnResourceViewPage = $(element.parent()).hasClass('ckanext-datapreview'); if (!isInOwnResourceViewPage) { router = _router(resourceKeyField, geojsonKeyField, redirectToUrl, filterFields, featuresValues); } - _addBaseLayer(map); + _addBaseLayer(map, mapBaseLayerConfig); geojsonLayer = _addGeoJSONLayer(map, geojson, geojsonKeyField, noDataLabel, featuresValues, router); bounds = geojsonLayer.getBounds(); maxBounds = bounds.pad(0.1); @@ -53,13 +55,22 @@ this.ckan.views.mapviews.navigablemap = (function () { return map; } - function _addBaseLayer(map) { - var attribution = 'Map data © OpenStreetMap contributors, Tiles ' + - 'Courtesy of MapQuest '; + function _addBaseLayer(map, mapBaseLayerConfig) { + var baseLayerUrl; + var attribution; + + if (mapBaseLayerConfig.type == 'custom') { + // Custom XYZ layer + if (mapBaseLayerConfig['custom.url']) baseLayerUrl = mapBaseLayerConfig['custom.url']; + if (mapBaseLayerConfig['attribution']) attribution = mapBaseLayerConfig['attribution']; + + } else { + // Default to Stamen base map + baseLayerUrl = 'https://stamen-tiles-{s}.a.ssl.fastly.net/terrain/{z}/{x}/{y}.png'; + attribution = 'Map tiles by Stamen Design (CC BY 3.0). Data by OpenStreetMap (CC BY SA)'; + } - return L.tileLayer('http://otile{s}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png', { + return L.tileLayer(baseLayerUrl, { subdomains: '1234', attribution: attribution }).addTo(map); diff --git a/ckanext/mapviews/theme/templates/choroplethmap_view.html b/ckanext/mapviews/theme/templates/choroplethmap_view.html index 99cd809..35ab315 100644 --- a/ckanext/mapviews/theme/templates/choroplethmap_view.html +++ b/ckanext/mapviews/theme/templates/choroplethmap_view.html @@ -1,5 +1,6 @@ {% resource "mapviews/main" %} +