From 284a7c789246ca627ce03e0ede68291849897b0f Mon Sep 17 00:00:00 2001 From: Dusko Bogdanovski Date: Tue, 27 Sep 2016 20:03:34 +0200 Subject: [PATCH 1/2] Add configuration for map --- ckanext/mapviews/helpers.py | 13 +++++++++++++ ckanext/mapviews/plugin.py | 9 +++++++++ ckanext/mapviews/theme/public/ckan_map_modules.js | 1 + .../theme/templates/choroplethmap_view.html | 3 +++ 4 files changed, 26 insertions(+) create mode 100644 ckanext/mapviews/helpers.py diff --git a/ckanext/mapviews/helpers.py b/ckanext/mapviews/helpers.py new file mode 100644 index 0000000..8baa384 --- /dev/null +++ b/ckanext/mapviews/helpers.py @@ -0,0 +1,13 @@ +import logging +from pylons import config + +log = logging.getLogger(__name__) + + +def mapviews_get_common_map_config(): + ''' + Returns a dict with all configuration options related to the common + base map (ie those starting with 'ckanext.spatial.common_map.') + ''' + namespace = 'ckanext.spatial.common_map.' + return dict([(k.replace(namespace, ''), v) for k, v in config.iteritems() if k.startswith(namespace)]) \ No newline at end of file diff --git a/ckanext/mapviews/plugin.py b/ckanext/mapviews/plugin.py index e12e7cd..1d6124d 100644 --- a/ckanext/mapviews/plugin.py +++ b/ckanext/mapviews/plugin.py @@ -3,6 +3,9 @@ import ckan.plugins as p import pylons.config as config + +from ckanext.mapviews import helpers as mapviews_helpers + Invalid = p.toolkit.Invalid _ = p.toolkit._ not_empty = p.toolkit.get_validator('not_empty') @@ -29,6 +32,7 @@ class NavigableMap(p.SingletonPlugin): p.implements(p.IConfigurer, inherit=True) p.implements(p.IResourceView, inherit=True) + p.implements(p.ITemplateHelpers, inherit=True) def update_config(self, config): p.toolkit.add_template_directory(config, 'theme/templates') @@ -79,6 +83,11 @@ def view_template(self, context, data_dict): def form_template(self, context, data_dict): return 'navigablemap_form.html' + def get_helpers(self): + return { + 'mapviews_get_common_map_config' : mapviews_helpers.mapviews_get_common_map_config + } + class ChoroplethMap(NavigableMap): '''Creates a choropleth map view''' diff --git a/ckanext/mapviews/theme/public/ckan_map_modules.js b/ckanext/mapviews/theme/public/ckan_map_modules.js index 5c2f99d..22c7ab6 100644 --- a/ckanext/mapviews/theme/public/ckan_map_modules.js +++ b/ckanext/mapviews/theme/public/ckan_map_modules.js @@ -13,6 +13,7 @@ id: options.resourceId, endpoint: options.endpoint || self.sandbox.client.endpoint + '/api' }; + console.log(options.mapConfig); var filters = []; for (var filter in filterFields){ diff --git a/ckanext/mapviews/theme/templates/choroplethmap_view.html b/ckanext/mapviews/theme/templates/choroplethmap_view.html index 99cd809..042cabd 100644 --- a/ckanext/mapviews/theme/templates/choroplethmap_view.html +++ b/ckanext/mapviews/theme/templates/choroplethmap_view.html @@ -1,5 +1,7 @@ {% resource "mapviews/main" %} + +{% set map_config = h.mapviews_get_common_map_config() %}
Date: Wed, 28 Sep 2016 11:42:40 +0200 Subject: [PATCH 2/2] Add configuration for map base layer --- ckanext/mapviews/helpers.py | 13 ---------- ckanext/mapviews/plugin.py | 21 +++++++++------- .../mapviews/theme/public/ckan_map_modules.js | 1 - ckanext/mapviews/theme/public/navigablemap.js | 25 +++++++++++++------ .../theme/templates/choroplethmap_view.html | 1 - 5 files changed, 30 insertions(+), 31 deletions(-) delete mode 100644 ckanext/mapviews/helpers.py diff --git a/ckanext/mapviews/helpers.py b/ckanext/mapviews/helpers.py deleted file mode 100644 index 8baa384..0000000 --- a/ckanext/mapviews/helpers.py +++ /dev/null @@ -1,13 +0,0 @@ -import logging -from pylons import config - -log = logging.getLogger(__name__) - - -def mapviews_get_common_map_config(): - ''' - Returns a dict with all configuration options related to the common - base map (ie those starting with 'ckanext.spatial.common_map.') - ''' - namespace = 'ckanext.spatial.common_map.' - return dict([(k.replace(namespace, ''), v) for k, v in config.iteritems() if k.startswith(namespace)]) \ No newline at end of file diff --git a/ckanext/mapviews/plugin.py b/ckanext/mapviews/plugin.py index 1d6124d..3a3b56c 100644 --- a/ckanext/mapviews/plugin.py +++ b/ckanext/mapviews/plugin.py @@ -1,11 +1,10 @@ import urlparse +import json import ckan.plugins as p import pylons.config as config -from ckanext.mapviews import helpers as mapviews_helpers - Invalid = p.toolkit.Invalid _ = p.toolkit._ not_empty = p.toolkit.get_validator('not_empty') @@ -32,7 +31,6 @@ class NavigableMap(p.SingletonPlugin): p.implements(p.IConfigurer, inherit=True) p.implements(p.IResourceView, inherit=True) - p.implements(p.ITemplateHelpers, inherit=True) def update_config(self, config): p.toolkit.add_template_directory(config, 'theme/templates') @@ -69,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' @@ -83,11 +84,6 @@ def view_template(self, context, data_dict): def form_template(self, context, data_dict): return 'navigablemap_form.html' - def get_helpers(self): - return { - 'mapviews_get_common_map_config' : mapviews_helpers.mapviews_get_common_map_config - } - class ChoroplethMap(NavigableMap): '''Creates a choropleth map view''' @@ -107,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/ckan_map_modules.js b/ckanext/mapviews/theme/public/ckan_map_modules.js index 22c7ab6..5c2f99d 100644 --- a/ckanext/mapviews/theme/public/ckan_map_modules.js +++ b/ckanext/mapviews/theme/public/ckan_map_modules.js @@ -13,7 +13,6 @@ id: options.resourceId, endpoint: options.endpoint || self.sandbox.client.endpoint + '/api' }; - console.log(options.mapConfig); var filters = []; for (var filter in filterFields){ 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 042cabd..35ab315 100644 --- a/ckanext/mapviews/theme/templates/choroplethmap_view.html +++ b/ckanext/mapviews/theme/templates/choroplethmap_view.html @@ -1,7 +1,6 @@ {% resource "mapviews/main" %} -{% set map_config = h.mapviews_get_common_map_config() %}