diff --git a/README.rst b/README.rst index cc0fed5..865b573 100644 --- a/README.rst +++ b/README.rst @@ -2,6 +2,9 @@ ckanext-spatial - Geo related plugins for CKAN ============================================== +.. image:: https://travis-ci.org/ckan/ckanext-spatial.svg?branch=master + :target: https://travis-ci.org/ckan/ckanext-spatial + This extension contains plugins that add geospatial capabilities to CKAN_, including: diff --git a/ckanext/spatial/nongeos_plugin.py b/ckanext/spatial/nongeos_plugin.py index 1597e01..02fb126 100644 --- a/ckanext/spatial/nongeos_plugin.py +++ b/ckanext/spatial/nongeos_plugin.py @@ -1,3 +1,5 @@ +# TODO: Move these to ckanext-geoviews + import mimetypes from logging import getLogger @@ -7,33 +9,68 @@ from ckan import plugins as p log = getLogger(__name__) -class WMSPreview(p.SingletonPlugin): - +class DataViewBase(p.SingletonPlugin): + '''This base class is for view extensions. ''' + if p.toolkit.check_ckan_version(min_version='2.3'): + p.implements(p.IResourceView, inherit=True) + else: + p.implements(p.IResourcePreview, inherit=True) p.implements(p.IConfigurer, inherit=True) - p.implements(p.IResourcePreview, inherit=True) + p.implements(p.IConfigurable, inherit=True) - WMS = ['wms'] + proxy_is_enabled = False + same_domain = False def update_config(self, config): - p.toolkit.add_public_directory(config, 'public') p.toolkit.add_template_directory(config, 'templates') p.toolkit.add_resource('public', 'ckanext-spatial') - self.proxy_enabled = p.toolkit.asbool(config.get('ckan.resource_proxy_enabled', 'False')) + config['ckan.resource_proxy_enabled'] = p.plugin_loaded('resource_proxy') + + def configure(self, config): + enabled = config.get('ckan.resource_proxy_enabled', False) + self.proxy_is_enabled = enabled def setup_template_variables(self, context, data_dict): import ckanext.resourceproxy.plugin as proxy - if self.proxy_enabled and not data_dict['resource']['on_same_domain']: - p.toolkit.c.resource['proxy_url'] = proxy.get_proxified_resource_url(data_dict) - else: - p.toolkit.c.resource['proxy_url'] = data_dict['resource']['url'] + self.same_domain = data_dict['resource'].get('on_same_domain') + if self.proxy_is_enabled and not self.same_domain: + data_dict['resource']['original_url'] = data_dict['resource']['url'] + data_dict['resource']['url'] = proxy.get_proxified_resource_url(data_dict) + + + +class WMSView(DataViewBase): + WMS = ['wms'] + + # IResourceView (CKAN >=2.3) + def info(self): + return {'name': 'wms_view', + 'title': 'WMS', + 'icon': 'map-marker', + 'iframed': True, + 'default_title': p.toolkit._('WMS'), + } + + def can_view(self, data_dict): + resource = data_dict['resource'] + format_lower = resource['format'].lower() + + if format_lower in self.WMS: + return self.same_domain or self.proxy_is_enabled + return False + + def view_template(self, context, data_dict): + return 'dataviewer/wms.html' + + # IResourcePreview (CKAN < 2.3) def can_preview(self, data_dict): format_lower = data_dict['resource']['format'].lower() correct_format = format_lower in self.WMS - can_preview_from_domain = self.proxy_enabled or data_dict['resource']['on_same_domain'] + can_preview_from_domain = self.proxy_is_enabled or data_dict['resource']['on_same_domain'] quality = 2 if p.toolkit.check_ckan_version('2.1'): @@ -52,10 +89,21 @@ class WMSPreview(p.SingletonPlugin): def preview_template(self, context, data_dict): return 'dataviewer/wms.html' + def setup_template_variables(self, context, data_dict): + import ckanext.resourceproxy.plugin as proxy + self.same_domain = data_dict['resource'].get('on_same_domain') + if self.proxy_is_enabled and not self.same_domain: + data_dict['resource']['proxy_url'] = proxy.get_proxified_resource_url(data_dict) -class GeoJSONPreview(p.SingletonPlugin): - p.implements(p.IConfigurer, inherit=True) - p.implements(p.IResourcePreview, inherit=True) + else: + data_dict['resource']['proxy_url'] = data_dict['resource']['url'] + + +class WMSPreview(WMSView): + pass + + +class GeoJSONView(DataViewBase): p.implements(p.ITemplateHelpers, inherit=True) GeoJSON = ['gjson', 'geojson'] @@ -65,20 +113,35 @@ class GeoJSONPreview(p.SingletonPlugin): template directory for the preview ''' - p.toolkit.add_public_directory(config, 'public') - p.toolkit.add_template_directory(config, 'templates') - p.toolkit.add_resource('public', 'ckanext-spatial') - - self.proxy_enabled = config.get( - 'ckan.resource_proxy_enabled', False) - mimetypes.add_type('application/json', '.geojson') + # IResourceView (CKAN >=2.3) + def info(self): + return {'name': 'geojson_view', + 'title': 'GeoJSON', + 'icon': 'map-marker', + 'iframed': True, + 'default_title': p.toolkit._('GeoJSON'), + } + + def can_view(self, data_dict): + resource = data_dict['resource'] + format_lower = resource['format'].lower() + + if format_lower in self.GeoJSON: + return self.same_domain or self.proxy_is_enabled + return False + + def view_template(self, context, data_dict): + return 'dataviewer/geojson.html' + + # IResourcePreview (CKAN < 2.3) + def can_preview(self, data_dict): format_lower = data_dict['resource']['format'].lower() correct_format = format_lower in self.GeoJSON - can_preview_from_domain = self.proxy_enabled or data_dict['resource']['on_same_domain'] + can_preview_from_domain = self.proxy_is_enabled or data_dict['resource']['on_same_domain'] quality = 2 if p.toolkit.check_ckan_version('2.1'): @@ -94,18 +157,10 @@ class GeoJSONPreview(p.SingletonPlugin): return correct_format and can_preview_from_domain - def setup_template_variables(self, context, data_dict): - import ckanext.resourceproxy.plugin as proxy - if (self.proxy_enabled - and not data_dict['resource']['on_same_domain']): - p.toolkit.c.resource['original_url'] = p.toolkit.c.resource['url'] - p.toolkit.c.resource['url'] = proxy.get_proxified_resource_url( - data_dict) - def preview_template(self, context, data_dict): return 'dataviewer/geojson.html' - ## ITemplateHelpers + # ITemplateHelpers def get_helpers(self): from ckanext.spatial import helpers as spatial_helpers @@ -116,3 +171,6 @@ class GeoJSONPreview(p.SingletonPlugin): return { 'get_common_map_config_geojson' : spatial_helpers.get_common_map_config, } + +class GeoJSONPreview(GeoJSONView): + pass diff --git a/doc/install.rst b/doc/install.rst index b77532f..1de188a 100644 --- a/doc/install.rst +++ b/doc/install.rst @@ -58,6 +58,9 @@ Ubuntu 14.04 (PostgreSQL 9.3 and PostGIS 2.1) Ubuntu 12.04 (PostgreSQL 9.1 and PostGIS 1.5) +++++++++++++++++++++++++++++++++++++++++++++ +.. note:: You can also install PostGIS 2.x on Ubuntu 12.04 using the packages + on the UbuntuGIS_ repository. Check the documentation there for details. + #. Install PostGIS:: sudo apt-get install postgresql-9.1-postgis @@ -98,29 +101,13 @@ Ubuntu 12.04 (PostgreSQL 9.1 and PostGIS 1.5) sudo apt-get install python-dev libxml2-dev libxslt1-dev libgeos-c1 +.. _UbuntuGIS: https://wiki.ubuntu.com/UbuntuGIS + Install the extension --------------------- 1. Install this extension into your python environment (where CKAN is also - installed). - - .. note:: Depending on the CKAN core version you are targeting you will need - to use a different branch from the extension. - - For a production site, use the ``stable`` branch, unless there is a specific - branch that targets the CKAN core version that you are using. - - To target the latest CKAN core release:: - - (pyenv) $ pip install -e "git+https://github.com/okfn/ckanext-spatial.git@stable#egg=ckanext-spatial" - - To target an old release (if a release branch exists, otherwise use - ``stable``):: - - (pyenv) $ pip install -e "git+https://github.com/okfn/ckanext-spatial.git@release-v1.8#egg=ckanext-spatial" - - To target CKAN ``master``, use the extension ``master`` branch (ie no - branch defined):: + installed):: (pyenv) $ pip install -e "git+https://github.com/okfn/ckanext-spatial.git#egg=ckanext-spatial" diff --git a/doc/previews.rst b/doc/previews.rst index cf1b562..71a31cc 100644 --- a/doc/previews.rst +++ b/doc/previews.rst @@ -17,13 +17,14 @@ The GeoJSON previewer is based on Leaflet_. It will render GeoJSON_ files on a map and add a popup showing the features properties, for those resources that have a format of ``geojson`` or ``gjson``. -To enable the GeoJSON previewer you need to add the ``geojson_preview`` plugin +To enable the GeoJSON previewer you need to add the ``geojson_view`` plugin to your ini file. This plugin also requires the `resource_proxy`_ plugin (Make sure you load the ``resource_proxy`` plugin before any other from the spatial extension):: - ckan.plugins = resource_proxy geojson_preview + ckan.plugins = resource_proxy geojson_view +.. note:: If using CKAN < 2.3, use `geojson_preview` WMS Preview ----------- @@ -39,12 +40,14 @@ just the main WMS service endpoint, for example: http://vmap0.tiles.osgeo.org/wms/vmap0?SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.1.1 -To enable the WMS previewer you need to add the ``wms_preview`` plugin to your +To enable the WMS previewer you need to add the ``wms_view`` plugin to your ini file. This plugin also requires the `resource_proxy`_ plugin (Make sure you load the ``resource_proxy`` plugin before any other from the spatial extension:: - ckan.plugins = resource_proxy wms_preview + ckan.plugins = resource_proxy wms_view + +.. note:: If using CKAN < 2.3, use `geojson_preview` .. note:: Please note that the WMS previewer included in ckanext-spatial is just a proof of concept and has important limitations, and is diff --git a/setup.py b/setup.py index 484edda..f402b0e 100644 --- a/setup.py +++ b/setup.py @@ -30,6 +30,8 @@ setup( [ckan.plugins] spatial_metadata=ckanext.spatial.plugin:SpatialMetadata spatial_query=ckanext.spatial.plugin:SpatialQuery + wms_view=ckanext.spatial.nongeos_plugin:WMSView + geojson_view=ckanext.spatial.nongeos_plugin:GeoJSONView wms_preview=ckanext.spatial.nongeos_plugin:WMSPreview geojson_preview=ckanext.spatial.nongeos_plugin:GeoJSONPreview cswserver=ckanext.spatial.plugin:CatalogueServiceWeb