Class: QgsMapRendererCache

class qgis.core.QgsMapRendererCache

Bases: PyQt5.QtCore.QObject

This class is responsible for keeping cache of rendered images resulting from a map rendering job.

Once a job has a rendered image stored in the cache (using setCacheImage(…)), the cache listens to repaintRequested() signals from dependent layers. If triggered, the cache removes the rendered image (and disconnects from the layers).

When user pans/zooms the canvas, the cache is also used in rendering period for particular layers between the first render update and the moment the layer actually has partially rendered something in the resulting image.

The class is thread-safe (multiple classes can access the same instance safely).

New in version 2.4:

Methods

cacheImage

Returns the cached image for the specified cacheKey.

childEvent

clear

Invalidates the cache contents, clearing all cached images.

clearCacheImage

Removes an image from the cache with matching cacheKey.

connectNotify

customEvent

dependentLayers

Returns a list of map layers on which an image in the cache depends.

disconnectNotify

hasAnyCacheImage

Returns True if the cache contains an image with the specified cacheKey with any cache's parameters (extent and scale)

hasCacheImage

Returns True if the cache contains an image with the specified cacheKey that has the same extent and scale as the cache's global extent and scale

init

Initialize cache: sets extent and scale parameters and clears the cache if any parameters have changed since last initialization.

invalidateCacheForLayer

Invalidates cached images which relate to the specified map layer.

isSignalConnected

receivers

sender

senderSignalIndex

setCacheImage

Set the cached image for a particular cacheKey, using the current cache parameters.

setCacheImageWithParameters

Set the cached image for a particular cacheKey, using a specific extent and mapToPixel (which may differ from the current cache parameters).

timerEvent

transformedCacheImage

Returns the cached image for the specified cacheKey transformed to the particular extent and scale.

updateParameters

Sets extent and scale parameters

cacheImage(self, cacheKey: str) QImage

Returns the cached image for the specified cacheKey. The cacheKey usually matches the QgsMapLayer.id() which the image is a render of. Returns a null image if it is not cached.

See also

setCacheImage()

See also

hasCacheImage()

Parameters:

cacheKey (str) –

Return type:

QImage

childEvent(self, QChildEvent)
clear(self)

Invalidates the cache contents, clearing all cached images.

clearCacheImage(self, cacheKey: str)

Removes an image from the cache with matching cacheKey.

See also

clear()

Parameters:

cacheKey (str) –

connectNotify(self, QMetaMethod)
customEvent(self, QEvent)
dependentLayers(self, cacheKey: str) List[QgsMapLayer]

Returns a list of map layers on which an image in the cache depends.

New in version 3.0.

Parameters:

cacheKey (str) –

Return type:

List[QgsMapLayer]

disconnectNotify(self, QMetaMethod)
hasAnyCacheImage(self, cacheKey: str, minimumScaleThreshold: float = 0, maximumScaleThreshold: float = 0) bool

Returns True if the cache contains an image with the specified cacheKey with any cache’s parameters (extent and scale)

The optional minimumScaleThreshold and maximumScaleThreshold arguments can be used to specify a range of acceptable cached scales vs current cache scale parameter. E.g. if the minimumScaleThreshold is 0.5 and maximumScaleThreshold is 2.0, then only cached images with a scale between 0.5 * current cache scale and 2.0 * current cache scale will be considered.

New in version 3.18.

Parameters:
  • cacheKey (str) –

  • minimumScaleThreshold (float = 0) –

  • maximumScaleThreshold (float = 0) –

Return type:

bool

hasCacheImage(self, cacheKey: str) bool

Returns True if the cache contains an image with the specified cacheKey that has the same extent and scale as the cache’s global extent and scale

See also

cacheImage()

New in version 3.0.

Parameters:

cacheKey (str) –

Return type:

bool

init(self, extent: QgsRectangle, scale: float) bool

Initialize cache: sets extent and scale parameters and clears the cache if any parameters have changed since last initialization.

Return type:

bool

Returns:

flag whether the parameters are the same as last time

Deprecated since version QGIS: 3.18 - will be removed in QGIS 4.0. Use the updateParameters() and clear()

Parameters:
invalidateCacheForLayer(self, layer: QgsMapLayer)

Invalidates cached images which relate to the specified map layer.

New in version 3.14.

Parameters:

layer (QgsMapLayer) –

isSignalConnected(self, QMetaMethod) bool
receivers(self, PYQT_SIGNAL) int
sender(self) QObject
senderSignalIndex(self) int
setCacheImage(self, cacheKey: str, image: QImage, dependentLayers: Iterable[QgsMapLayer] = [])

Set the cached image for a particular cacheKey, using the current cache parameters.

The cacheKey usually matches the QgsMapLayer.id() which the image is a render of.

A list of dependentLayers should be passed containing all layer on which this cache image is dependent. If any of these layers triggers a repaint then the cache image will be cleared.

See also

cacheImage()

Parameters:
  • cacheKey (str) –

  • image (QImage) –

  • dependentLayers (Iterable[QgsMapLayer] = []) –

setCacheImageWithParameters(self, cacheKey: str, image: QImage, extent: QgsRectangle, mapToPixel: QgsMapToPixel, dependentLayers: Iterable[QgsMapLayer] = [])

Set the cached image for a particular cacheKey, using a specific extent and mapToPixel (which may differ from the current cache parameters).

The cacheKey usually matches the QgsMapLayer.id() which the image is a render of.

A list of dependentLayers should be passed containing all layer on which this cache image is dependent. If any of these layers triggers a repaint then the cache image will be cleared.

See also

cacheImage()

New in version 3.18.

Parameters:
timerEvent(self, QTimerEvent)
transformedCacheImage(self, cacheKey: str, mtp: QgsMapToPixel) QImage

Returns the cached image for the specified cacheKey transformed to the particular extent and scale.

The cacheKey usually matches the QgsMapLayer.id() which the image is a render of. Returns a null image if it is not cached.

New in version 3.18.

Parameters:
Return type:

QImage

updateParameters(self, extent: QgsRectangle, mtp: QgsMapToPixel) bool

Sets extent and scale parameters

Return type:

bool

Returns:

flag whether the parameters are the same as last time

New in version 3.18.

Parameters: