diff --git a/geonode/geoserver/helpers.py b/geonode/geoserver/helpers.py index efbe6c1b66f..0cdd4178154 100755 --- a/geonode/geoserver/helpers.py +++ b/geonode/geoserver/helpers.py @@ -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) @@ -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 @@ -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 @@ -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( @@ -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) @@ -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 = "
\
\ - \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)): @@ -2049,7 +2065,9 @@ def decimal_encode(bbox): height=256, width=256, left=box[0], top=box[1]) _img_request_template += "
" + image = _render_thumbnail(_img_request_template, width=width, height=height) + return image diff --git a/geonode/geoserver/signals.py b/geonode/geoserver/signals.py index 6112dcbf6e3..03b7df0965a 100644 --- a/geonode/geoserver/signals.py +++ b/geonode/geoserver/signals.py @@ -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) diff --git a/geonode/layers/utils.py b/geonode/layers/utils.py index 9c42c446b28..9f19559e060 100644 --- a/geonode/layers/utils.py +++ b/geonode/layers/utils.py @@ -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 @@ -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) diff --git a/geonode/tests/integration.py b/geonode/tests/integration.py index 9e254829cf9..409e6d8abd0 100644 --- a/geonode/tests/integration.py +++ b/geonode/tests/integration.py @@ -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()