Ejemplo n.º 1
0
def create_qgis_server_thumbnail(instance, overwrite=False, bbox=None):
    """Task to update thumbnails.

    This task will formulate OGC url to generate thumbnail and then pass it
    to geonode

    :param instance: Resource instance, can be a layer or map
    :type instance: Layer, Map

    :param overwrite: set True to overwrite
    :type overwrite: bool

    :param bbox: Bounding box of thumbnail in 4 tuple format
        [xmin,ymin,xmax,ymax]
    :type bbox: list(float)

    :return:
    """
    thumbnail_remote_url = None
    try:
        # to make sure it is executed after the instance saved
        if isinstance(instance, Layer):
            thumbnail_remote_url = layer_thumbnail_url(instance,
                                                       bbox=bbox,
                                                       internal=False)
        elif isinstance(instance, Map):
            thumbnail_remote_url = map_thumbnail_url(instance,
                                                     bbox=bbox,
                                                     internal=False)
        else:
            # instance type does not have associated thumbnail
            return True
        if not thumbnail_remote_url:
            return True
        logger.debug('Create thumbnail for %s' % thumbnail_remote_url)

        if overwrite:
            # if overwrite, then delete existing thumbnail links
            instance.link_set.filter(resource=instance.get_self_resource(),
                                     name="Remote Thumbnail").delete()
            instance.link_set.filter(resource=instance.get_self_resource(),
                                     name="Thumbnail").delete()

        create_thumbnail(instance,
                         thumbnail_remote_url,
                         overwrite=overwrite,
                         check_bbox=False)
        return True
    # if it is socket exception, we should raise it, because there is
    # something wrong with the url
    except socket.error as e:
        logger.error('Thumbnail url not accessed {url}'.format(
            url=thumbnail_remote_url))
        logger.exception(e)
        # reraise exception with original traceback
        raise
    except Exception as e:
        logger.exception(e)
        return False
Ejemplo n.º 2
0
def create_qgis_server_thumbnail(instance, overwrite=False, bbox=None):
    """Task to update thumbnails.

    This task will formulate OGC url to generate thumbnail and then pass it
    to geonode

    :param instance: Resource instance, can be a layer or map
    :type instance: Layer, Map

    :param overwrite: set True to overwrite
    :type overwrite: bool

    :param bbox: Bounding box of thumbnail in 4 tuple format
        [xmin,ymin,xmax,ymax]
    :type bbox: list(float)

    :return:
    """
    thumbnail_remote_url = None
    try:
        # to make sure it is executed after the instance saved
        if isinstance(instance, Layer):
            thumbnail_remote_url = layer_thumbnail_url(
                instance, bbox=bbox, internal=False)
        elif isinstance(instance, Map):
            thumbnail_remote_url = map_thumbnail_url(
                instance, bbox=bbox, internal=False)
        else:
            # instance type does not have associated thumbnail
            return True
        if not thumbnail_remote_url:
            return True
        logger.debug('Create thumbnail for %s' % thumbnail_remote_url)

        if overwrite:
            # if overwrite, then delete existing thumbnail links
            instance.link_set.filter(
                resource=instance.get_self_resource(),
                name="Remote Thumbnail").delete()
            instance.link_set.filter(
                resource=instance.get_self_resource(),
                name="Thumbnail").delete()

        create_thumbnail(
            instance, thumbnail_remote_url,
            overwrite=overwrite, check_bbox=False)
        return True
    # if it is socket exception, we should raise it, because there is
    # something wrong with the url
    except socket.error as e:
        logger.error('Thumbnail url not accessed {url}'.format(
            url=thumbnail_remote_url))
        logger.exception(e)
        # reraise exception with original traceback
        raise
    except Exception as e:
        logger.exception(e)
        return False