diff --git a/ckanext/spatial/nongeos_plugin.py b/ckanext/spatial/nongeos_plugin.py index 95103d5..69151b5 100644 --- a/ckanext/spatial/nongeos_plugin.py +++ b/ckanext/spatial/nongeos_plugin.py @@ -13,6 +13,10 @@ class WMSPreview(p.SingletonPlugin): 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')) @@ -34,3 +38,42 @@ class WMSPreview(p.SingletonPlugin): def preview_template(self, context, data_dict): return 'dataviewer/wms.html' + + +class GeoJSONPreview(p.SingletonPlugin): + p.implements(p.IConfigurer, inherit=True) + p.implements(p.IResourcePreview, inherit=True) + + GeoJSON = ['gjson', 'geojson'] + + def update_config(self, config): + ''' Set up the resource library, public directory and + 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) + + def can_preview(self, data_dict): + format_lower = data_dict['resource']['format'].lower() + + check = format_lower in self.GeoJSON + if not self.proxy_enabled and check: + check = data_dict['resource']['on_same_domain'] + + return check + + 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' diff --git a/ckanext/spatial/public/css/geojson_preview.css b/ckanext/spatial/public/css/geojson_preview.css new file mode 100644 index 0000000..29d3e64 --- /dev/null +++ b/ckanext/spatial/public/css/geojson_preview.css @@ -0,0 +1,11 @@ +html, body { + height: 100%; + padding: 0; + margin: 0; +} + +#map { + position: absolute; + width: 100%; + height: 100%; +} diff --git a/ckanext/spatial/public/js/geojson_preview.js b/ckanext/spatial/public/js/geojson_preview.js new file mode 100644 index 0000000..46d6c9d --- /dev/null +++ b/ckanext/spatial/public/js/geojson_preview.js @@ -0,0 +1,67 @@ +// geojson preview module +ckan.module('geojsonpreview', function (jQuery, _) { + return { + options: { + }, + initialize: function () { + var self = this; + + self.el.empty(); + self.el.append($("
").attr("id","map")); + self.map = L.map('map'); + + var mapUrl = "http://otile{s}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png"; + var osmAttribution = 'Map data © 2011 OpenStreetMap contributors, Tiles Courtesy of MapQuest '; + var bg = new L.TileLayer(mapUrl, {maxZoom: 18, attribution: osmAttribution, subdomains: '1234'}); + self.map.addLayer(bg); + + // use CORS, if supported by browser and server + if (jQuery.support.cors && preload_resource['original_url'] !== undefined) { + jQuery.getJSON(preload_resource['original_url']) + .done( + function(data){ + self.showPreview(data); + }) + .fail( + function(jqxhr, textStatus, error) { + jQuery.getJSON(preload_resource['url']) + .done( + function(data){ + self.showPreview(data); + }) + .fail( + function(jqXHR, textStatus, errorThrown) { + self.showError(jqXHR, textStatus, errorThrown); + } + ); + } + ); + } else { + jQuery.getJSON(preload_resource['url']).done( + function(data){ + self.showPreview(data); + }) + .fail( + function(jqXHR, textStatus, errorThrown) { + self.showError(jqXHR, textStatus, errorThrown); + } + ); + } + }, + + showError: function (jqXHR, textStatus, errorThrown) { + if (textStatus == 'error' && jqXHR.responseText.length) { + self.el.html(jqXHR.responseText); + } else { + self.el.html(self.i18n('error', {text: textStatus, error: errorThrown})); + } + }, + + showPreview: function (geojsonFeature) { + var self = this; + var gjLayer = L.geoJson().addTo(self.map); + gjLayer.addData(geojsonFeature); + self.map.fitBounds(gjLayer.getBounds()); + } + }; +}); diff --git a/ckanext/spatial/public/resource.config b/ckanext/spatial/public/resource.config index d4bc955..597d0af 100644 --- a/ckanext/spatial/public/resource.config +++ b/ckanext/spatial/public/resource.config @@ -37,3 +37,10 @@ wms = js/wms_preview.js css/wms_preview.css + +geojson = + js/vendor/leaflet/leaflet.js + js/vendor/leaflet/leaflet.css + + js/geojson_preview.js + css/geojson_preview.css \ No newline at end of file diff --git a/ckanext/spatial/templates/dataviewer/geojson.html b/ckanext/spatial/templates/dataviewer/geojson.html new file mode 100644 index 0000000..29c6bc3 --- /dev/null +++ b/ckanext/spatial/templates/dataviewer/geojson.html @@ -0,0 +1,14 @@ +{% extends "dataviewer/base.html" %} + +{% block page %} +
+

+
+
{{ _('Loading...') }}
+

+
+ + {% resource 'ckanext-spatial/geojson' %} + +{% endblock %} + diff --git a/setup.py b/setup.py index e54d471..87ecb86 100644 --- a/setup.py +++ b/setup.py @@ -31,6 +31,7 @@ setup( spatial_metadata=ckanext.spatial.plugin:SpatialMetadata spatial_query=ckanext.spatial.plugin:SpatialQuery wms_preview=ckanext.spatial.nongeos_plugin:WMSPreview + geojson_preview=ckanext.spatial.nongeos_plugin:GeoJSONPreview cswserver=ckanext.spatial.plugin:CatalogueServiceWeb spatial_harvest_metadata_api=ckanext.spatial.plugin:HarvestMetadataApi