Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable map #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion ckanext/mapviews/plugin.py
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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'
Expand All @@ -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 = {
Expand Down
25 changes: 18 additions & 7 deletions ckanext/mapviews/theme/public/navigablemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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);
Expand All @@ -53,13 +55,22 @@ this.ckan.views.mapviews.navigablemap = (function () {
return map;
}

function _addBaseLayer(map) {
var attribution = 'Map data © OpenStreetMap contributors, Tiles ' +
'Courtesy of <a href="http://www.mapquest.com/"' +
'target="_blank">MapQuest</a> <img' +
'src="//developer.mapquest.com/content/osm/mq_logo.png">';
function _addBaseLayer(map, mapBaseLayerConfig) {
var baseLayerUrl;
var attribution;

if (mapBaseLayerConfig.type == 'custom') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add the mapbox option to keep it consistent with the rest of spatial widgets

// 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 <a href="http://stamen.com">Stamen Design</a> (<a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>). Data by <a href="http://openstreetmap.org">OpenStreetMap</a> (<a href="http://creativecommons.org/licenses/by-sa/3.0">CC BY SA</a>)';
}

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);
Expand Down
2 changes: 2 additions & 0 deletions ckanext/mapviews/theme/templates/choroplethmap_view.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% resource "mapviews/main" %}


<div
data-module = "choroplethmap"
data-module-resource-id = "{{ resource.id }}"
Expand All @@ -8,6 +9,7 @@
data-module-resource-label-field = "{{ resource_view.resource_label_field }}"
data-module-geojson-url = "{{ resource_view.geojson_url }}"
data-module-geojson-key-field = "{{ resource_view.geojson_key_field }}"
data-module-map-config = "{{ h.dump_json(map_config) }}"
data-module-redirect-to-url = "{{ resource_view.redirect_to_url }}"
data-module-filter-fields = "{{ h.dump_json(resource_view.filters) if resource_view.filters else '' }}"
id = "choroplethmap-{{ resource_view.id }}"
Expand Down