gws.base.grabber.core

Base raster grabber.

Source code: gws.base.grabber.core

Module Contents

gws.base.grabber.core.BLOCK_LOCK_TIMEOUT = 60

Seconds to wait for a block being composed by another process.

gws.base.grabber.core.EPHEMERAL_MAX_AGE = 60

Lifetime (seconds) of tiles in the ephemeral store.

gws.base.grabber.core.MAX_LEVEL = 30

levels beyond this are never served or precomputed.

Type:

Serving stopper

class gws.base.grabber.core.Object(opts: Options)

Bases: gws.Grabber

Base raster grabber.

Implements the gws.Grabber API on top of the composition methods (compose_*), which subclasses implement for their kind of source.

cache

Cache settings; the name (<layer cache name>_<srid>) keys the tile store.

defaultEphemeralStore: gws.TileStore

Short-lived store for static tiles outside the cache settings, so that blocks are composed once.

extent

Extent covered by this grabber, in the target CRS.

grid

The fixed target grid for the CRS.

imageFormat

Format tiles are stored and returned in.

maxLevel: int

Finest served level.

mimeType: str

Mime type of imageFormat.

minLevel: int

Coarsest served level.

mtrByLevel: dict[int, gws.MapTileRange]

Tile range covered by extent, per served level.

requestTiles: int

Tiles per side composed in one block; 1 means no meta-tiling.

store

Persistent tile store.

targetCrs

Target CRS; one grabber serves exactly one CRS.

block_start_tile(mt: gws.MapTile) → gws.MapTile

Return the first tile of the block containing a tile.

Parameters:

mt – The tile.

Returns:

The top-left tile of the block.

abstractmethod compose_box_as_image(extent: gws.Extent, w: int, h: int, params: dict | None = None) → gws.Image

Compose an image for an arbitrary extent and pixel size from the source.

Subclasses must implement this.

Parameters:
  • extent – Extent in the target CRS.

  • w – Width in pixels.

  • h – Height in pixels.

  • params – Dynamic request parameters.

Returns:

The image.

Raises:

NotImplementedError – In the base class.

compose_tile_as_image(mt: gws.MapTile, params: dict | None = None) → gws.Image

Compose a single tile over its own extent.

Parameters:
  • mt – The tile.

  • params – Dynamic request parameters.

Returns:

The tile image.

compose_tile_block_as_image_dict(mt: gws.MapTile, params: dict | None = None) → dict[gws.MapTile, gws.Image]

Compose the block of requestTiles x requestTiles tiles containing a tile.

The base implementation supports only requestTiles 1 and composes the tile alone.

Parameters:
  • mt – A tile in the block.

  • params – Dynamic request parameters.

Returns:

Images of the tiles of the block within the layer’s tile range.

Raises:

NotImplementedError – If requestTiles is not 1.

empty_box(w: int, h: int) → bytes

Return a transparent image of the given size, as encoded bytes.

Parameters:
  • w – Width in pixels.

  • h – Height in pixels.

Returns:

The encoded image.

empty_image(w: int, h: int) → gws.Image

Return a transparent image of the given size.

Parameters:
  • w – Width in pixels.

  • h – Height in pixels.

Returns:

The image.

empty_tile() → bytes

Return the transparent tile, encoded once and then reused.

Returns:

The encoded tile.

extent_to_source_crs(extent: gws.Extent) → gws.Extent | None

Transform a target extent into the source CRS, clipped to the source area of use.

Parameters:

extent – Extent in the target CRS.

Returns:

The extent in the source CRS, or None if it lies outside the source area of use.

get_box_as_bytes(extent, w, h, params=None)

Return an encoded image for arbitrary extent and pixel size.

A box overlapping no data is a transparent image.

Parameters:
  • extent – Extent in the target CRS.

  • w – Width in pixels.

  • h – Height in pixels.

  • params – Dynamic request parameters. With params, the persistent store is bypassed.

Returns:

The encoded image.

Raises:

Exception – Errors of the source request are passed through.

get_box_as_image(extent, w, h, params=None)

Return an image for arbitrary extent and pixel size.

Same contract as get_box_as_bytes, decoded pixels instead of bytes.

Parameters:
  • extent – Extent in the target CRS.

  • w – Width in pixels.

  • h – Height in pixels.

  • params – Dynamic request parameters. With params, the persistent store is bypassed.

Returns:

The image.

Raises:

Exception – Errors of the source request are passed through.

get_tile_as_bytes(mt, params=None)

Return a single tile as an encoded image, composing and storing it if needed.

A tile outside the layer’s range is a transparent image.

Parameters:
  • mt – Tile.

  • params – Dynamic request parameters. With params, the persistent store is bypassed.

Returns:

The encoded tile.

Raises:

Exception – Errors of the source request are passed through.

get_tile_as_image(mt, params=None)

Return a single tile as an image, composing and storing it if needed.

Same contract as get_tile_as_bytes, decoded pixels instead of bytes.

Parameters:
  • mt – Tile.

  • params – Dynamic request parameters. With params, the persistent store is bypassed.

Returns:

The tile image.

Raises:

Exception – Errors of the source request are passed through.

get_tiles_as_bytes_dict(mtr, params=None)

Return a rectangular block of tiles as encoded images.

Parameters:
  • mtr – Tile range.

  • params – Dynamic request parameters. With params, the persistent store is bypassed.

Returns:

contains entries only for tiles present in the grid.

Return type:

Encoded tiles, keyed by tile. Sparse

Raises:

Exception – Errors of the source request are passed through.

get_tiles_as_image_dict(mtr, params=None)

Return a rectangular block of tiles as images.

Same contract as get_tiles_as_bytes_dict, decoded pixels instead of bytes.

Parameters:
  • mtr – Tile range.

  • params – Dynamic request parameters. With params, the persistent store is bypassed.

Returns:

Tile images, keyed by tile.

Raises:

Exception – Errors of the source request are passed through.

is_serving(z: int) → bool

Check if a level is within the serving range.

Parameters:

z – Level.

Returns:

True if the level is served.

is_storing(z: int) → bool

Check if tiles of a level go to the persistent store.

Parameters:

z – Level.

Returns:

True if the cache has a positive maxAge and the level is not beyond cache.maxLevel.

levels()

Return the levels this grabber supports.

Returns:

A list of levels.

normalize_image(img: gws.Image, w: int, h: int) → gws.Image

Ensure a source image is RGBA and has the requested size.

Parameters:
  • img – Image from the source.

  • w – Expected width.

  • h – Expected height.

Returns:

The image converted to RGBA.

Raises:

gws.ExternalServiceError – If the image size differs from the requested size.

pair_to_bytes(bi: _BlobImagePair) → bytes

Return the bytes form of a pair, encoding the image if needed.

Parameters:

bi – A pair of encoded bytes and image, one of which is set.

Returns:

The encoded image.

Raises:

gws.Error – If neither is set.

pair_to_image(bi: _BlobImagePair) → gws.Image

Return the image form of a pair, decoding the bytes if needed.

Parameters:

bi – A pair of encoded bytes and image, one of which is set.

Returns:

The image.

Raises:

gws.Error – If neither is set.

tile_range_for_level(z)

Return the tile range covered by the grabber’s extent at a level.

Parameters:

z – Level.

Returns:

The tile range in the grabber’s grid.

to_bytes(img: gws.Image) → bytes

Encode an image in the grabber’s image format.

Parameters:

img – The image.

Returns:

The encoded image.

to_image(blob: bytes) → gws.Image

Decode an encoded image.

Parameters:

blob – The encoded image.

Returns:

The image.

warp_image(img: gws.Image, src_bounds: gws.Bounds, target_bounds: gws.Bounds, w: int, h: int) → gws.Image

Warp an image covering the source bounds onto the target bounds and pixel size.

Uses nearest neighbour resampling when the CRS and the resolution are the same, and bilinear resampling otherwise.

Parameters:
  • img – Source image.

  • src_bounds – Bounds covered by the source image.

  • target_bounds – Target bounds.

  • w – Target width in pixels.

  • h – Target height in pixels.

Returns:

The warped image.

class gws.base.grabber.core.Options(*args, **kwargs)

Bases: gws.Data

Options for creating a grabber.

cache: gws.MapCache

Cache settings, with the final cache name including the SRID.

crs: gws.Crs

Target CRS.

extent: gws.Extent

Layer extent in the target CRS.

imageFormat: gws.ImageFormat

Format tiles are stored and returned in.