[#21] Add basic GeoJSON preview and fix set up of wms preview

This commit is contained in:
Dominik Moritz 2013-06-13 22:51:01 +02:00
parent add78d5931
commit 64d1846e90
6 changed files with 143 additions and 0 deletions

View File

@ -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'

View File

@ -0,0 +1,11 @@
html, body {
height: 100%;
padding: 0;
margin: 0;
}
#map {
position: absolute;
width: 100%;
height: 100%;
}

View File

@ -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($("<div></div>").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 &copy; 2011 OpenStreetMap contributors, Tiles Courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a> <img src="http://developer.mapquest.com/content/osm/mq_logo.png">';
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());
}
};
});

View File

@ -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

View File

@ -0,0 +1,14 @@
{% extends "dataviewer/base.html" %}
{% block page %}
<div data-module="geojsonpreview" id="data-preview">
<h4 class="loading-dialog">
<div class="loading-spinner"></div>
<div class="left">{{ _('Loading...') }}</div>
</h4>
</div>
{% resource 'ckanext-spatial/geojson' %}
{% endblock %}

View File

@ -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