gws.gis.cache.core

Tile cache inventory, filtering and maintenance.

Source code: gws.gis.cache.core

Module Contents

gws.gis.cache.core.add_stats(inv: Inventory)

Read store statistics into the inventory.

Sets the number and size of stored tiles and the percentage cached for each level and cache.

Parameters:

inv – Inventory to update.

gws.gis.cache.core.apply_filter(inv: Inventory, flt: Filter)

Filter the inventory in place.

Selects caches by layer, cache name prefix and CRS, then restricts their levels to flt.levels and to the tile ranges covering flt.bbox, which must be in the CRS of the caches. Caches without levels are removed.

Parameters:
  • inv – Inventory to filter.

  • flt – Filter.

class gws.gis.cache.core.Cache(*args, **kwargs)

Bases: gws.Data

A tile cache, shared by all layers with the same cache name.

cachedTiles: int

Number of stored tiles.

dir: str

Path to the cache directory, empty if it does not exist.

fileSize: int

Total size of the stored tiles in bytes.

grabber: gws.Grabber

Grabber of the first layer that uses this cache.

layers: list[gws.Layer]

Layers that use this cache.

layerTitle: str

Title of the first layer.

layerType: str

Type of the first layer.

levels: list[Level]

Zoom levels of the cache, up to the cache max. level.

name: str

Cache name, also the name of the cache directory.

seedStatus: str

Status of the last seeding run, e.g. timeout, empty if it completed.

gws.gis.cache.core.cleanup(root: gws.Root)

Remove orphan cache directories.

Parameters:

root – Configuration root.

gws.gis.cache.core.drop(root: gws.Root, flt: Filter | None = None)

Remove cached tiles.

If the filter has neither levels nor a bbox, the whole store of each selected cache is removed. Otherwise, the tile ranges covering the bbox, or the selected levels, are removed.

Parameters:
  • root – Configuration root.

  • flt – Filter, selects all caches if omitted.

class gws.gis.cache.core.Filter(*args, **kwargs)

Bases: gws.Data

Selects caches, levels and tiles from an inventory. Empty fields select everything.

bbox: gws.Bounds | None

Select tiles in this area, which must be in the CRS of the caches.

cacheNames: list[str]

Select caches whose names start with any of these prefixes.

layerUids: list[str]

Select caches used by any of these layers.

levels: list[int]

Select these zoom levels.

srids: list[int]

Select caches in these CRS.

class gws.gis.cache.core.GlobalConfig(*args, **kwargs)

Bases: gws.Config

Global tile cache options.

seedingConcurrency: int = 1

Number of parallel threads for cache seeding.

seedingMaxTime: gws.Duration = '10m'

Time limit for a cache seeding run.

class gws.gis.cache.core.Inventory(*args, **kwargs)

Bases: gws.Data

All tile caches of a configuration.

caches: list[Cache]

Configured caches.

orphanDirs: list[str]

Directories in the cache directory that belong to no configured cache.

gws.gis.cache.core.inventory(root: gws.Root) → Inventory

Collect all tile caches of a configuration.

Caches are taken from the grabbers of all layers, skipping grabbers with a zero max. age. Each cache gets its levels up to the cache max. level and the existing cache directory. Directories in the cache directory that belong to no cache are listed as orphans. Statistics are not computed here, see add_stats.

Parameters:

root – Configuration root.

Returns:

The inventory, caches sorted by layer type, layer title and name.

class gws.gis.cache.core.LayerConfig(*args, **kwargs)

Bases: gws.Config

Tile cache settings of a layer.

crs: list[gws.CrsName] | None

CRS to cache tiles in. (added in 8.5)

maxAge: gws.Duration = '7d'

Max. age of cached tiles.

maxLevel: int = 18

Finest zoom level to cache. (changed in 8.5)

name: str = ''

Cache directory name. (added in 8.5)

requestBuffer: int = 64

Pixel buffer around source requests, to keep labels consistent across tiles. (changed in 8.5)

requestTiles: int = 4

Tiles per side of the block rendered in one source request. (changed in 8.5)

class gws.gis.cache.core.Level(*args, **kwargs)

Bases: gws.Data

A zoom level of a cache, with statistics.

cachedRange: gws.MapTileRange | None

Bounding tile range of the stored tiles.

cachedTiles: int

Number of tiles already in the store.

failedTiles: int

Number of tiles that failed during seeding.

fetchedTiles: int

Number of tiles fetched during seeding.

fileSize: int

Total size of the stored tiles in bytes.

gridRange: gws.MapTileRange

Tile range of the level, possibly restricted by a filter.

gridSize: gws.Size

Number of tile columns and rows in gridRange.

percentCached: int

Percentage of cached tiles.

resolution: float

Resolution of the level in map units per pixel.

seedTime: float

Seconds spent seeding this level.

totalTiles: int

Number of tiles in gridRange.

z: int

Zoom level.

gws.gis.cache.core.percent_cached(lv: Level) → int

Compute the percentage of cached and fetched tiles of a level.

Parameters:

lv – Level.

Returns:

A percentage from 0 to 100, at least 1 if any tile is present.

gws.gis.cache.core.percentage_by_level(c: Cache) → list[int]

Compute the cached percentages of all levels of a cache.

Parameters:

c – Cache.

Returns:

A list of percentages indexed by level, up to the cache max. level. Levels not in the cache are 0.

class gws.gis.cache.core.SeedOptions(*args, **kwargs)

Bases: gws.Data

Options for a seeding run.

concurrency: int

Number of worker threads.

filter: Filter

Selects caches, levels and tiles to seed.

maxAge: int | None

Refetch tiles older than this (seconds), capped by the cache max. age.

maxTime: int

Time limit in seconds.

class gws.gis.cache.core.SeedResult(*args, **kwargs)

Bases: gws.Data

Result of a seeding run.

caches: list[Cache]

Seeded caches, with per-level statistics.

seedStatus: str

empty if completed, timeout, interrupted or locked.

Type:

Status of the run

seedTime: float

Total run time in seconds.

gws.gis.cache.core.store_in_web_cache(url: str, img: bytes)

Store an image in the web cache.

Writes the image to FASTCACHE_DIR under the URL path. Write errors are logged and ignored.

Parameters:
  • url – URL path, used as the file path in the web cache.

  • img – Image data.