Skip to content

Commit

Permalink
fix set thumbnail and remove map (#350)
Browse files Browse the repository at this point in the history
* fix set thumbnail and remove map
* add logger

partially fix #207
  • Loading branch information
boney-bun authored Oct 13, 2017
1 parent 552228f commit 861ec7e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
63 changes: 63 additions & 0 deletions geonode/maps/qgis_server_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import json
import requests
import math
import logging

from django.conf import settings
from django.views.generic import CreateView, DetailView, UpdateView
Expand Down Expand Up @@ -49,6 +50,9 @@

elif check_ogc_backend(qgis_server.BACKEND_PACKAGE):
from geonode.qgis_server.helpers import ogc_server_settings
from geonode.qgis_server.tasks.update import create_qgis_server_thumbnail

logger = logging.getLogger("geonode.maps.qgis_server_views")


class MapCreateView(CreateView):
Expand Down Expand Up @@ -548,3 +552,62 @@ def perm_filter(layer):
% map_obj.title + '.qlr'

return response


def set_thumbnail_map(request, mapid):
"""Update thumbnail based on map extent
:param layername: The layer name in Geonode.
:type layername: basestring
:return success: true if success, None if fail.
:type success: bool
"""
if request.method != 'POST':
return HttpResponse('Bad Request')

map_layers = MapLayer.objects.filter(map__id=mapid)
local_layers = [l for l in map_layers if l.local]

layers = {}
for layer in local_layers:
try:
l = Layer.objects.get(typename=layer.name)

layers[l.name] = l
except Layer.DoesNotExist:
msg = 'No Layer found for typename: {0}'.format(layer.name)
logger.debug(msg)

if not layers:
# The signal is called too early, or the map has no layer yet.
return

bbox = _get_bbox_from_layers(layers)

# Give thumbnail creation to celery tasks, and exit.
map_obj = Map.objects.get(id=mapid)
create_qgis_server_thumbnail.delay(map_obj, overwrite=True, bbox=bbox)
retval = {
'success': True
}
return HttpResponse(
json.dumps(retval), content_type="application/json")


def _get_bbox_from_layers(layers):
"""
Calculate the bbox from a given list of Layer objects
"""
bbox = None
for layer in layers:
layer_bbox = layers[layer].bbox_string.split(',')
if bbox is None:
bbox = [float(key) for key in layer_bbox]
else:
bbox[0] = float(min(bbox[0], layer_bbox[0]))
bbox[1] = float(min(bbox[1], layer_bbox[1]))
bbox[2] = float(max(bbox[2], layer_bbox[2]))
bbox[3] = float(max(bbox[3], layer_bbox[3]))

return bbox
5 changes: 4 additions & 1 deletion geonode/maps/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
map_json = 'map_json'
# TODO qlr for geoserver
map_download_qlr = 'map_download_qlr'
map_thumbnail = 'map_thumbnail'

elif check_ogc_backend(qgis_server.BACKEND_PACKAGE):
new_map_view = MapCreateView.as_view()
Expand All @@ -50,6 +51,8 @@
map_json = MapUpdateView.as_view()
from geonode.maps.qgis_server_views import map_download_qlr
map_download_qlr = map_download_qlr
from geonode.maps.qgis_server_views import set_thumbnail_map
map_thumbnail = set_thumbnail_map

urlpatterns = patterns(
'geonode.maps.views',
Expand All @@ -76,7 +79,7 @@
url(r'^(?P<mapid>[^/]+)/metadata_advanced$', 'map_metadata_advanced', name='map_metadata_advanced'),
url(r'^(?P<mapid>[^/]+)/embed$', map_embed, name='map_embed'),
url(r'^(?P<mapid>[^/]+)/history$', 'ajax_snapshot_history'),
url(r'^(?P<mapid>\d+)/thumbnail$', 'map_thumbnail', name='map_thumbnail'),
url(r'^(?P<mapid>\d+)/thumbnail$', map_thumbnail, name='map_thumbnail'),
url(r'^(?P<mapid>[^/]+)/(?P<snapshot>[A-Za-z0-9_\-]+)/view$', 'map_view'),
url(r'^(?P<mapid>[^/]+)/(?P<snapshot>[A-Za-z0-9_\-]+)/info$',
'map_detail'),
Expand Down

0 comments on commit 861ec7e

Please sign in to comment.