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

[Fixes #4552] Improve/fix thumbnail generation for Layers and Maps at creation time #4555

Merged
merged 2 commits into from
Jun 24, 2019
Merged
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
28 changes: 23 additions & 5 deletions geonode/geoserver/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,8 @@ def wps_execute_layer_attribute_statistics(layer_name, field):
url,
method='POST',
data=request,
headers=headers)
headers=headers,
user=ogc_server_settings.credentials.username)

exml = etree.fromstring(content)

Expand Down Expand Up @@ -1601,7 +1602,10 @@ def _stylefilterparams_geowebcache_layer(layer_name):
url = '%sgwc/rest/layers/%s.xml' % (ogc_server_settings.LOCATION, layer_name)

# read GWC configuration
req, content = http_client.get(url, headers=headers)
req, content = http_client.get(
url,
headers=headers,
user=ogc_server_settings.credentials.username)
if req.status_code != 200:
line = "Error {0} reading Style Filter Params GeoWebCache at {1}".format(
req.status_code, url
Expand All @@ -1622,7 +1626,11 @@ def _stylefilterparams_geowebcache_layer(layer_name):
param_filters[0].append(style_filters_elem)
body = ET.tostring(tree)
if body:
req, content = http_client.post(url, data=body, headers=headers)
req, content = http_client.post(
url,
data=body,
headers=headers,
user=ogc_server_settings.credentials.username)
if req.status_code != 200:
line = "Error {0} writing Style Filter Params GeoWebCache at {1}".format(
req.status_code, url
Expand All @@ -1640,7 +1648,11 @@ def _invalidate_geowebcache_layer(layer_name, url=None):
""".strip().format(layer_name)
if not url:
url = '%sgwc/rest/masstruncate' % ogc_server_settings.LOCATION
req, content = http_client.post(url, data=body, headers=headers)
req, content = http_client.post(
url,
data=body,
headers=headers,
user=ogc_server_settings.credentials.username)

if req.status_code != 200:
line = "Error {0} invalidating GeoWebCache at {1}".format(
Expand Down Expand Up @@ -2007,6 +2019,7 @@ def decimal_encode(bbox):
width_acc = 256 + left
first_row = [tmp_tile]
# Add tiles to fill image width
_n_step = 0
while width > width_acc:
c = mercantile.ul(tmp_tile.x + 1, tmp_tile.y, zoom)
lng = _v(c.lng, x=True, target_srid=4326)
Expand All @@ -2015,12 +2028,15 @@ def decimal_encode(bbox):
tmp_tile = mercantile.tile(lng, bounds[3], zoom)
first_row.append(tmp_tile)
width_acc = width_acc + 256
_n_step = _n_step + 1
if width < width_acc or _n_step > numberOfRows:
break

# Build Image Request Template
_img_request_template = "<div style='height:{height}px; width:{width}px;'>\
<div style='position: absolute; top:{top}px; left:{left}px; z-index: 749; \
transform: translate3d(0px, 0px, 0px) scale3d(1, 1, 1);'> \
\n" .format(height=height, width=width, top=top, left=left)
\n".format(height=height, width=width, top=top, left=left)

for row in range(0, numberOfRows):
for col in range(0, len(first_row)):
Expand Down Expand Up @@ -2049,7 +2065,9 @@ def decimal_encode(bbox):
height=256, width=256,
left=box[0], top=box[1])
_img_request_template += "</div></div>"

image = _render_thumbnail(_img_request_template, width=width, height=height)

return image


Expand Down
2 changes: 1 addition & 1 deletion geonode/geoserver/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,6 @@ def geoserver_pre_save_maplayer(instance, sender, **kwargs):

def geoserver_post_save_map(instance, sender, created, **kwargs):
instance.set_missing_info()
if created:
if not created:
logger.info("... Creating Thumbnail for Map [%s]" % (instance.title))
create_gs_thumbnail(instance, overwrite=False, check_bbox=True)
117 changes: 69 additions & 48 deletions geonode/layers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import tarfile

from osgeo import gdal, osr
from urlparse import urlparse
from zipfile import ZipFile, is_zipfile
from datetime import datetime

Expand Down Expand Up @@ -927,57 +928,77 @@ def create_thumbnail(instance, thumbnail_remote_url, thumbnail_create_url=None,
ogc_client = http_client

if ogc_client:
try:
params = {
'width': width,
'height': height
}
# Add the bbox param only if the bbox is different to [None, None,
# None, None]
if None not in instance.bbox:
params['bbox'] = instance.bbox_string
params['crs'] = instance.srid

for _p in params.keys():
if _p.lower() not in thumbnail_create_url.lower():
thumbnail_create_url = thumbnail_create_url + '&%s=%s' % (_p, params[_p])
headers = {}
if check_ogc_backend(geoserver.BACKEND_PACKAGE):
_ogc_server_settings = settings.OGC_SERVER['default']
_user = _ogc_server_settings['USER'] if 'USER' in _ogc_server_settings else 'admin'
_pwd = _ogc_server_settings['PASSWORD'] if 'PASSWORD' in _ogc_server_settings else 'geoserver'
import base64
valid_uname_pw = base64.b64encode(
b"%s:%s" % (_user, _pwd)).decode("ascii")
headers['Authorization'] = 'Basic {}'.format(valid_uname_pw)
resp, image = ogc_client.request(thumbnail_create_url, headers=headers)
if 'ServiceException' in image or \
resp.status_code < 200 or resp.status_code > 299:
msg = 'Unable to obtain thumbnail: %s' % image
logger.error(msg)
if check_ogc_backend(geoserver.BACKEND_PACKAGE):
if image is None:
request_body = {
'width': width,
'height': height
}
if instance.bbox and \
(not thumbnail_create_url or 'bbox' not in thumbnail_create_url):
instance_bbox = instance.bbox[0:4]
request_body['bbox'] = [str(coord) for coord in instance_bbox]
request_body['srid'] = instance.srid

if thumbnail_create_url:
params = urlparse(thumbnail_create_url).query.split('&')
request_body = {key: value for (key, value) in
[(lambda p: (p.split("=")[0], p.split("=")[1]))(p) for p in params]}
# request_body['thumbnail_create_url'] = thumbnail_create_url
if 'bbox' in request_body and isinstance(request_body['bbox'], basestring):
request_body['bbox'] = [str(coord) for coord in request_body['bbox'].split(",")]
request_body['bbox'] = [
request_body['bbox'][0],
request_body['bbox'][2],
request_body['bbox'][1],
request_body['bbox'][3]
]
if 'crs' in request_body and 'srid' not in request_body:
request_body['srid'] = request_body['crs']
elif instance.alternate:
request_body['layers'] = instance.alternate

image = _prepare_thumbnail_body_from_opts(request_body)

if image is None:
try:
params = {
'width': width,
'height': height
}
# Add the bbox param only if the bbox is different to [None, None,
# None, None]
if None not in instance.bbox:
params['bbox'] = instance.bbox_string
params['crs'] = instance.srid

for _p in params.keys():
if _p.lower() not in thumbnail_create_url.lower():
thumbnail_create_url = thumbnail_create_url + '&%s=%s' % (_p, params[_p])
headers = {}
if check_ogc_backend(geoserver.BACKEND_PACKAGE):
_ogc_server_settings = settings.OGC_SERVER['default']
_user = _ogc_server_settings['USER'] if 'USER' in _ogc_server_settings else 'admin'
_pwd = _ogc_server_settings['PASSWORD'] if \
'PASSWORD' in _ogc_server_settings else 'geoserver'
import base64
valid_uname_pw = base64.b64encode(
b"%s:%s" % (_user, _pwd)).decode("ascii")
headers['Authorization'] = 'Basic {}'.format(valid_uname_pw)
resp, image = ogc_client.request(thumbnail_create_url, headers=headers)
if 'ServiceException' in image or \
resp.status_code < 200 or resp.status_code > 299:
msg = 'Unable to obtain thumbnail: %s' % image
logger.error(msg)

# Replace error message with None.
image = None

except BaseException as e:
logger.exception(e)

# Replace error message with None.
image = None
except BaseException as e:
logger.exception(e)

# Replace error message with None.
image = None

if check_ogc_backend(geoserver.BACKEND_PACKAGE):
if image is None and instance.bbox:
instance_bbox = instance.bbox[0:4]
request_body = {
'bbox': [str(coord) for coord in instance_bbox],
'srid': instance.srid,
'width': width,
'height': height
}
if thumbnail_create_url:
request_body['thumbnail_create_url'] = thumbnail_create_url
elif instance.alternate:
request_body['layers'] = instance.alternate
image = _prepare_thumbnail_body_from_opts(request_body)

if image is not None:
instance.save_thumbnail(thumbnail_name, image=image)
Expand Down
2 changes: 1 addition & 1 deletion geonode/tests/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ def test_map_thumbnail(self):

thumbnail_url = map_obj.get_thumbnail_url()

self.assertEqual(thumbnail_url, staticfiles.static(settings.MISSING_THUMBNAIL))
self.assertNotEqual(thumbnail_url, staticfiles.static(settings.MISSING_THUMBNAIL))
finally:
# Cleanup
saved_layer.delete()
Expand Down