geonode.thumbs.utils

Attributes

logger

MISSING_THUMB

BASE64_PATTERN

Functions

make_bbox_to_pixels_transf(→ Callable)

Linear transformation of a bounding box (BBOX) from a Coordinate Reference System (CRS) to pixel values.

transform_bbox(bbox[, target_crs])

Function transforming BBOX in dataset compliant format (xmin, xmax, ymin, ymax, 'EPSG:xxxx') to another CRS,

expand_bbox_to_ratio(bbox[, target_width, target_height])

Function returning an expanded BBOX, ensuring it's ratio, based on the provided BBOX, and width and height

assign_missing_thumbnail(→ None)

Function assigning None in thumbnail_url to a provided instance

get_map(ogc_server_location, layers, bbox[, ...])

Function fetching an image from OGC server.

_build_getmap_request([version, layers, styles, srs, ...])

getmap(base_url[, version, headers, layers, styles, ...])

Request and return an image from the WMS as a file-like object.

epsg_3857_area_of_use()

Shortcut function, returning area of use of EPSG:3857 (in EPSG:4326) in a dataset compliant BBOX

crop_to_3857_area_of_use(→ List)

exceeds_epsg3857_area_of_use(→ bool)

Function checking if a provided BBOX extends the are of use of EPSG:3857. Comparison is performed after casting

clean_bbox(bbox, target_crs)

thumb_path(filename)

Return the complete path of the provided thumbnail file accessible

thumb_exists(filename)

Determine if a thumbnail file exists in storage

thumb_size(filepath)

Determine if a thumbnail file size in storage

thumb_open(filename)

Returns file handler of a thumbnail on the storage

get_thumbs()

Fetches a list of all stored thumbnails

remove_thumb(filename)

Delete a thumbnail from storage

remove_thumbs(name)

Removes all stored thumbnails that start with the same name as the

get_unique_upload_path(filename)

Generates a unique name from the given filename and

_decode_base64(data)

Decode base64, padding being optional.

Module Contents

geonode.thumbs.utils.logger[source]
geonode.thumbs.utils.MISSING_THUMB[source]
geonode.thumbs.utils.BASE64_PATTERN = 'data:image/(jpeg|png|jpg);base64'[source]
geonode.thumbs.utils.make_bbox_to_pixels_transf(src_bbox: List | Tuple, dest_bbox: List | Tuple) Callable[source]

Linear transformation of a bounding box (BBOX) from a Coordinate Reference System (CRS) to pixel values.

(xmin, ymax)         (xmax, ymax)                     (0, 0)          (width, 0)
    ------------------                                    ------------------
    |  x               |                                  |   | y'          |
    |----* (x, y)      |              ->                  |----* (x', y')   |
    |    | y           |                                  | x'              |
    ------------------                                    ------------------
(xmin, ymin)         (xmax, ymin)                     (0, height)   (width, height)

Transformation based on linear proportions:

(x - xmin)        x'                         (y - ymin)      (height - y')
------------ = -------                    ------------ = ----------------
(xmax - xmin)   width                     (ymax - ymin)        height

Note

The Y axis directions are opposite between the CRS and pixel coordinates.

Parameters:
  • src_bbox (Union[List, Tuple]) – The BBOX of the image in a specific CRS, in the form (xmin, ymin, xmax, ymax).

  • dest_bbox (Union[List, Tuple]) – The BBOX of the image in pixels, in the form (0, 0, width, height).

Returns:

A function to translate X, Y coordinates from the CRS to pixel coordinates (x, y).

Return type:

Callable

geonode.thumbs.utils.transform_bbox(bbox: List, target_crs: str = 'EPSG:3857')[source]

Function transforming BBOX in dataset compliant format (xmin, xmax, ymin, ymax, ‘EPSG:xxxx’) to another CRS, preserving overflow values.

geonode.thumbs.utils.expand_bbox_to_ratio(bbox: List, target_width: int = settings.THUMBNAIL_SIZE['width'], target_height: int = settings.THUMBNAIL_SIZE['height'])[source]

Function returning an expanded BBOX, ensuring it’s ratio, based on the provided BBOX, and width and height of the target image.

Parameters:
  • bbox – a dataset compliant BBOX in a certain CRS, in (xmin, xmax, ymin, ymax, ‘EPSG:xxxx’) order

  • target_width – width of the target image in pixels

  • target_height – height of the target image in pixels

Returns:

BBOX (in input’s format) with provided height/width ratio, and unchanged center point (in regard to the input BBOX)

geonode.thumbs.utils.assign_missing_thumbnail(instance) None[source]

Function assigning None in thumbnail_url to a provided instance

Parameters:

instance – instance of Dataset or Map models

geonode.thumbs.utils.get_map(ogc_server_location: str, layers: List, bbox: List, wms_version: str = settings.OGC_SERVER['default'].get('WMS_VERSION', '1.3.0'), mime_type: str = 'image/png', styles: List = None, width: int = 240, height: int = 200, max_retries: int = 3, retry_delay: int = 1)[source]

Function fetching an image from OGC server. For the requests to the configured OGC backend (ogc_server_settings.LOCATION) the function tries to generate an access_token and attach it to the URL. If access_token is not added ant the request is against Geoserver Basic Authentication is used instead. If image retrieval fails, function retries to fetch the image max_retries times, waiting retry_delay seconds between consecutive requests.

Parameters:
  • ogc_server_location – OGC server URL

  • layers – layers which should be fetched from the OGC server

  • bbox – area’s bounding box in format: [west, east, south, north, CRS]

  • wms_version – WMS version of the query (default: 1.1.1)

  • mime_type – mime type of the returned image

  • styles – styles, which OGC server should use for rendering an image

  • width – width of the returned image

  • height – height of the returned image

  • max_retries – maximum number of retries before skipping retrieval

  • retry_delay – number of seconds waited between retries

Returns:

retrieved image

geonode.thumbs.utils._build_getmap_request(version='1.3.0', layers=None, styles=None, srs=None, bbox=None, format=None, size=None, time=None, dimensions={}, elevation=None, transparent=False, bgcolor=None, exceptions=None, **kwargs)[source]
geonode.thumbs.utils.getmap(base_url, version='1.3.0', headers={}, layers=None, styles=None, srs=None, bbox=None, format=None, size=None, time=None, elevation=None, dimensions={}, transparent=False, bgcolor='#FFFFFF', exceptions='XML', method='Get', timeout=None, **kwargs)[source]

Request and return an image from the WMS as a file-like object.

Parameters:
  • layers (list) – List of content layer names.

  • styles (list) – Optional list of named styles, must be the same length as the layers list.

  • srs (string) –

    A spatial reference system identifier.

    Note

    This is an invalid query parameter key for 1.3.0 but is being retained for standardization with 1.1.1.

    Note

    Throws an exception if the spatial reference is ESRI’s “no reference” code (EPSG:0).

  • bbox (tuple) –

    (left, bottom, right, top) in srs units (note, this order does not

    change depending on axis order of the crs).

    CRS:84: (long, lat) EPSG:4326: (lat, long)

  • format (string) – Output image format such as ‘image/jpeg’.

  • size (tuple) – (width, height) in pixels.

  • time (string or list or range) – Optional. Time value of the specified layer as ISO-8601 (per value)

  • elevation (string or list or range) – Optional. Elevation value of the specified layer.

  • dimensions (dict (dimension : string or list or range)) – Optional. Any other Dimension option, as specified in the GetCapabilities

  • transparent (bool) – Optional. Transparent background if True.

  • bgcolor (string) – Optional. Image background color.

  • method (string) – Optional. HTTP DCP method name: Get or Post.

  • **kwargs (extra arguments) – anything else e.g. vendor specific parameters

Example

wms = WebMapService(’http://webservices.nationalatlas.gov/wms/1million’, version=’1.3.0’) img = wms.getmap(layers=[‘airports1m’], styles=[‘default’], srs=’EPSG:4326’, bbox=(-176.646, 17.7016, -64.8017, 71.2854), size=(300, 300), format=’image/jpeg’, transparent=True) out = open(‘example.jpg.jpg’, ‘wb’) out.write(img.read()) out.close()

geonode.thumbs.utils.epsg_3857_area_of_use()[source]

Shortcut function, returning area of use of EPSG:3857 (in EPSG:4326) in a dataset compliant BBOX

geonode.thumbs.utils.crop_to_3857_area_of_use(bbox: List) List[source]
geonode.thumbs.utils.exceeds_epsg3857_area_of_use(bbox: List) bool[source]

Function checking if a provided BBOX extends the are of use of EPSG:3857. Comparison is performed after casting the BBOX to EPSG:4326 (pivot for EPSG:3857).

Parameters:

bbox – a dataset compliant BBOX in a certain CRS, in (xmin, xmax, ymin, ymax, ‘EPSG:xxxx’) order

Returns:

List of indicators whether BBOX’s coord exceeds the area of use of EPSG:3857

geonode.thumbs.utils.clean_bbox(bbox, target_crs)[source]
geonode.thumbs.utils.thumb_path(filename)[source]

Return the complete path of the provided thumbnail file accessible via Django storage API

geonode.thumbs.utils.thumb_exists(filename)[source]

Determine if a thumbnail file exists in storage

geonode.thumbs.utils.thumb_size(filepath)[source]

Determine if a thumbnail file size in storage

geonode.thumbs.utils.thumb_open(filename)[source]

Returns file handler of a thumbnail on the storage

geonode.thumbs.utils.get_thumbs()[source]

Fetches a list of all stored thumbnails

geonode.thumbs.utils.remove_thumb(filename)[source]

Delete a thumbnail from storage

geonode.thumbs.utils.remove_thumbs(name)[source]

Removes all stored thumbnails that start with the same name as the file specified

geonode.thumbs.utils.get_unique_upload_path(filename)[source]

Generates a unique name from the given filename and creates a unique file upload path

geonode.thumbs.utils._decode_base64(data)[source]

Decode base64, padding being optional.

Parameters:

data – Base64 data as an ASCII byte string

Returns:

The decoded byte string.