Class: QgsRenderContext

class qgis.core.QgsRenderContext

Bases: sip.wrapper

Contains information about the context of a rendering operation. The context of a rendering operation defines properties such as the conversion ratio between screen and map units, the extents to be rendered etc.

Methods

convertFromMapUnits

Converts a size from map units to the specified units.

convertMetersToMapUnits

Convert meter distances to active MapUnit values for QgsUnitTypes.RenderMetersInMapUnits note When the sourceCrs() is geographic, the center of the Extent will be used

convertToMapUnits

Converts a size from the specified units to map units.

convertToPainterUnits

Converts a size from the specified units to painter units (pixels).

coordinateTransform

Returns the current coordinate transform for the context.

distanceArea

A general purpose distance and area calculator, capable of performing ellipsoid based calculations.

drawEditingInformation

Returns TRUE if edit markers should be drawn during the render operation.

expressionContext

Gets the expression context.

extent

When rendering a map layer, calling this method returns the “clipping” extent for the layer (in the layer’s CRS).

featureFilterProvider

Gets the filter feature provider used for additional filtering of rendered features.

flags

Returns combination of flags used for rendering.

forceVectorOutput

Returns TRUE if rendering operations should use vector operations instead of any faster raster shortcuts.

fromMapSettings

create initialized QgsRenderContext instance from given QgsMapSettings

fromQPainter

Creates a default render context given a pixel based QPainter destination.

geometry

Returns pointer to the unsegmentized geometry

mapExtent

Returns the original extent of the map being rendered.

mapToPixel

Returns the context’s map to pixel transform, which transforms between map coordinates and device coordinates.

painter

Returns the destination QPainter for the render operation.

pathResolver

Returns the path resolver for conversion between relative and absolute paths during rendering operations, e.g.

rendererScale

Returns the renderer map scale.

renderingStopped

Returns TRUE if the rendering operation has been stopped and any ongoing rendering should be canceled immediately.

scaleFactor

Returns the scaling factor for the render to convert painter units to physical sizes.

segmentationTolerance

Gets the segmentation tolerance applied when rendering curved geometries

segmentationToleranceType

Gets segmentation tolerance type (maximum angle or maximum difference between curve and approximation)

selectionColor

Returns the color to use when rendering selected features.

setCoordinateTransform

Sets the current coordinate transform for the context.

setDistanceArea

A general purpose distance and area calculator, capable of performing ellipsoid based calculations.

setDrawEditingInformation

Sets whether edit markers should be drawn during the render operation.

setExpressionContext

Sets the expression context.

setExtent

When rendering a map layer, calling this method sets the “clipping” extent for the layer (in the layer’s CRS).

setFeatureFilterProvider

Set a filter feature provider used for additional filtering of rendered features.

setFlag

Enable or disable a particular flag (other flags are not affected)

setFlags

Set combination of flags that will be used for rendering.

setForceVectorOutput

Sets whether rendering operations should use vector operations instead of any faster raster shortcuts.

setGeometry

Sets pointer to original (unsegmentized) geometry

setMapExtent

Sets the original extent of the map being rendered.

setMapToPixel

Sets the context’s map to pixel transform, which transforms between map coordinates and device coordinates.

setPainter

Sets the destination QPainter for the render operation.

setPathResolver

Sets the path resolver for conversion between relative and absolute paths during rendering operations, e.g.

setRendererScale

Sets the renderer map scale.

setRenderingStopped

Sets whether the rendering operation has been stopped and any ongoing rendering should be canceled immediately.

setScaleFactor

Sets the scaling factor for the render to convert painter units to physical sizes.

setSegmentationTolerance

Sets the segmentation tolerance applied when rendering curved geometries

setSegmentationToleranceType

Sets segmentation tolerance type (maximum angle or maximum difference between curve and approximation)

setSelectionColor

Sets the color to use when rendering selected features.

setShowSelection

Sets whether vector selections should be shown in the rendered map

setTextRenderFormat

Sets the text render format, which dictates how text is rendered (e.g.

setTransformContext

Sets the context’s coordinate transform context, which stores various information regarding which datum transforms should be used when transforming points from a source to destination coordinate reference system.

setUseAdvancedEffects

Used to enable or disable advanced effects such as blend modes

setUseRenderingOptimization

param enabled

setVectorSimplifyMethod

param simplifyMethod

showSelection

Returns true if vector selections should be shown in the rendered map

testFlag

Check whether a particular flag is enabled.

textRenderFormat

Returns the text render format, which dictates how text is rendered (e.g.

transformContext

Returns the context’s coordinate transform context, which stores various information regarding which datum transforms should be used when transforming points from a source to destination coordinate reference system.

useAdvancedEffects

Returns true if advanced effects such as blend modes such be used

useRenderingOptimization

Returns true if the rendering optimization (geometry simplification) can be executed

vectorSimplifyMethod

Added in QGIS v2.4

Attributes

Antialiasing

DrawEditingInfo

DrawSelection

DrawSymbolBounds

ForceVectorOutput

RenderMapTile

RenderPartialOutput

RenderPreviewJob

TextFormatAlwaysOutlines

TextFormatAlwaysText

UseAdvancedEffects

UseRenderingOptimization

Antialiasing = 128
DrawEditingInfo = 1
DrawSelection = 16
DrawSymbolBounds = 32
class Flag

Bases: int

class Flags

Bases: sip.wrapper

QgsRenderContext.Flags(Union[QgsRenderContext.Flags, QgsRenderContext.Flag]) QgsRenderContext.Flags(QgsRenderContext.Flags)

ForceVectorOutput = 2
RenderMapTile = 64
RenderPartialOutput = 256
RenderPreviewJob = 512
TextFormatAlwaysOutlines = 0
TextFormatAlwaysText = 1
class TextRenderFormat

Bases: int

UseAdvancedEffects = 4
UseRenderingOptimization = 8
convertFromMapUnits(self, sizeInMapUnits: float, outputUnit: QgsUnitTypes.RenderUnit) → float

Converts a size from map units to the specified units.

New in version 3.0.

Parameters
Return type

float

convertMetersToMapUnits(self, meters: float) → float

Convert meter distances to active MapUnit values for QgsUnitTypes.RenderMetersInMapUnits note When the sourceCrs() is geographic, the center of the Extent will be used

New in version 3.0.

Parameters

meters (float) –

Return type

float

convertToMapUnits(self, size: float, unit: QgsUnitTypes.RenderUnit, scale: QgsMapUnitScale = QgsMapUnitScale()) → float

Converts a size from the specified units to map units. The conversion respects the limits specified by the optional scale parameter.

New in version 3.0.

Parameters
Return type

float

convertToPainterUnits(self, size: float, unit: QgsUnitTypes.RenderUnit, scale: QgsMapUnitScale = QgsMapUnitScale()) → float

Converts a size from the specified units to painter units (pixels). The conversion respects the limits specified by the optional scale parameter.

New in version 3.0.

Parameters
Return type

float

coordinateTransform(self) → QgsCoordinateTransform

Returns the current coordinate transform for the context.

This represents the coordinate transform required to transform a layer which is being rendered back to the CRS of the rendered map. If no coordinate transformation is required, or the render context is not associated with a map layer render, then an invalid coordinate transformation is returned.

Return type

QgsCoordinateTransform

distanceArea(self) → QgsDistanceArea

A general purpose distance and area calculator, capable of performing ellipsoid based calculations.

New in version 3.0.

Return type

QgsDistanceArea

drawEditingInformation(self) → bool

Returns TRUE if edit markers should be drawn during the render operation.

Return type

bool

expressionContext(self) → QgsExpressionContext

Gets the expression context. This context should be used for all expression evaluation associated with this render context.

New in version 2.12.

Return type

QgsExpressionContext

extent(self) → QgsRectangle

When rendering a map layer, calling this method returns the “clipping” extent for the layer (in the layer’s CRS).

This extent is a “worst-case” scenario, which is guaranteed to cover the complete visible portion of the layer when it is rendered to a map. It is often larger than the actual visible portion of that layer.

Warning

For some layers, depending on the visible extent and the coordinate transforms involved, this extent will represent the entire globe. This method should never be used to determine the actual visible extent of a map render.

See also

setExtent()

See also

mapExtent()

Return type

QgsRectangle

featureFilterProvider(self) → QgsFeatureFilterProvider

Gets the filter feature provider used for additional filtering of rendered features.

Return type

QgsFeatureFilterProvider

Returns

the filter feature provider

New in version 2.14.

flags(self) → QgsRenderContext.Flags

Returns combination of flags used for rendering.

New in version 2.14.

Return type

QgsRenderContext.Flags

forceVectorOutput(self) → bool

Returns TRUE if rendering operations should use vector operations instead of any faster raster shortcuts.

Return type

bool

fromMapSettings(mapSettings: QgsMapSettings) → QgsRenderContext

create initialized QgsRenderContext instance from given QgsMapSettings

New in version 2.4.

Parameters

mapSettings (QgsMapSettings) –

Return type

QgsRenderContext

fromQPainter(painter: QPainter) → QgsRenderContext

Creates a default render context given a pixel based QPainter destination. If no painter is specified or the painter has no device, then a default DPI of 88 will be assumed.

New in version 3.0.

Parameters

painter (QPainter) –

Return type

QgsRenderContext

geometry(self) → QgsAbstractGeometry

Returns pointer to the unsegmentized geometry

Return type

QgsAbstractGeometry

mapExtent(self) → QgsRectangle

Returns the original extent of the map being rendered.

Unlike extent(), this extent is always in the final destination CRS for the map render and represents the exact bounds of the map being rendered.

See also

extent()

See also

setMapExtent()

New in version 3.4.8.

Return type

QgsRectangle

mapToPixel(self) → QgsMapToPixel

Returns the context’s map to pixel transform, which transforms between map coordinates and device coordinates.

See also

setMapToPixel()

Return type

QgsMapToPixel

painter(self) → QPainter

Returns the destination QPainter for the render operation.

See also

setPainter()

Return type

QPainter

pathResolver(self) → QgsPathResolver

Returns the path resolver for conversion between relative and absolute paths during rendering operations, e.g. for resolving relative symbol paths.

New in version 3.0.

Return type

QgsPathResolver

rendererScale(self) → float

Returns the renderer map scale. This will match the desired scale denominator for the rendered map, eg 1000.0 for a 1:1000 map render.

Return type

float

renderingStopped(self) → bool

Returns TRUE if the rendering operation has been stopped and any ongoing rendering should be canceled immediately.

Return type

bool

scaleFactor(self) → float

Returns the scaling factor for the render to convert painter units to physical sizes. This is usually equal to the number of pixels per millimeter.

See also

setScaleFactor()

Return type

float

segmentationTolerance(self) → float

Gets the segmentation tolerance applied when rendering curved geometries

Return type

float

segmentationToleranceType(self) → QgsAbstractGeometry.SegmentationToleranceType

Gets segmentation tolerance type (maximum angle or maximum difference between curve and approximation)

Return type

QgsAbstractGeometry.SegmentationToleranceType

selectionColor(self) → QColor

Returns the color to use when rendering selected features.

Return type

QColor

setCoordinateTransform(self, t: QgsCoordinateTransform)

Sets the current coordinate transform for the context.

This represents the coordinate transform required to transform the layer which is being rendered back to the CRS of the rendered map.

Set to an invalid QgsCoordinateTransform to indicate that no transformation is required.

Parameters

t (QgsCoordinateTransform) –

setDistanceArea(self, distanceArea: QgsDistanceArea)

A general purpose distance and area calculator, capable of performing ellipsoid based calculations. Will be used to convert meter distances to active MapUnit values for QgsUnitTypes.RenderMetersInMapUnits

New in version 3.0.

Parameters

distanceArea (QgsDistanceArea) –

setDrawEditingInformation(self, b: bool)

Sets whether edit markers should be drawn during the render operation.

Parameters

b (bool) –

setExpressionContext(self, context: QgsExpressionContext)

Sets the expression context. This context is used for all expression evaluation associated with this render context.

New in version 2.12.

Parameters

context (QgsExpressionContext) –

setExtent(self, extent: QgsRectangle)

When rendering a map layer, calling this method sets the “clipping” extent for the layer (in the layer’s CRS).

This extent should be a “worst-case” scenario, which is guaranteed to completely cover the entire visible portion of the layer when it is rendered to the map. It may be larger than the actual visible area, but MUST contain at least the entire visible area.

See also

setExtent()

See also

setMapExtent()

Parameters

extent (QgsRectangle) –

setFeatureFilterProvider(self, ffp: QgsFeatureFilterProvider)

Set a filter feature provider used for additional filtering of rendered features.

Parameters

ffp (QgsFeatureFilterProvider) – the filter feature provider

New in version 2.14.

setFlag(self, flag: QgsRenderContext.Flag, on: bool = True)

Enable or disable a particular flag (other flags are not affected)

New in version 2.14.

Parameters
setFlags(self, flags: Union[QgsRenderContext.Flags, QgsRenderContext.Flag])

Set combination of flags that will be used for rendering.

New in version 2.14.

Parameters

flags (Union[QgsRenderContext.Flags) –

setForceVectorOutput(self, force: bool)

Sets whether rendering operations should use vector operations instead of any faster raster shortcuts.

Parameters

force (bool) –

setGeometry(self, geometry: QgsAbstractGeometry)

Sets pointer to original (unsegmentized) geometry

Parameters

geometry (QgsAbstractGeometry) –

setMapExtent(self, extent: QgsRectangle)

Sets the original extent of the map being rendered.

Unlike setExtent(), this extent is always in the final destination CRS for the map render and represents the exact bounds of the map being rendered.

See also

mapExtent()

See also

setExtent()

New in version 3.4.8.

Parameters

extent (QgsRectangle) –

setMapToPixel(self, mtp: QgsMapToPixel)

Sets the context’s map to pixel transform, which transforms between map coordinates and device coordinates.

See also

mapToPixel()

Parameters

mtp (QgsMapToPixel) –

setPainter(self, p: QPainter)

Sets the destination QPainter for the render operation. Ownership of the painter is not transferred and the QPainter destination must stay alive for the duration of any rendering operations.

See also

painter()

Parameters

p (QPainter) –

setPathResolver(self, resolver: QgsPathResolver)

Sets the path resolver for conversion between relative and absolute paths during rendering operations, e.g. for resolving relative symbol paths.

See also

pathResolver()

New in version 3.0.

Parameters

resolver (QgsPathResolver) –

setRendererScale(self, scale: float)

Sets the renderer map scale. This should match the desired scale denominator for the rendered map, eg 1000.0 for a 1:1000 map render.

See also

rendererScale()

Parameters

scale (float) –

setRenderingStopped(self, stopped: bool)

Sets whether the rendering operation has been stopped and any ongoing rendering should be canceled immediately.

Parameters

stopped (bool) –

setScaleFactor(self, factor: float)

Sets the scaling factor for the render to convert painter units to physical sizes. This should usually be equal to the number of pixels per millimeter.

See also

scaleFactor()

Parameters

factor (float) –

setSegmentationTolerance(self, tolerance: float)

Sets the segmentation tolerance applied when rendering curved geometries

Parameters

tolerance (float) – the segmentation tolerance*

setSegmentationToleranceType(self, type: QgsAbstractGeometry.SegmentationToleranceType)

Sets segmentation tolerance type (maximum angle or maximum difference between curve and approximation)

Parameters

type (QgsAbstractGeometry.SegmentationToleranceType) – the segmentation tolerance typename*

setSelectionColor(self, color: Union[QColor, Qt.GlobalColor, QGradient])

Sets the color to use when rendering selected features.

See also

selectionColor()

Parameters

color (Union[QColor) –

setShowSelection(self, showSelection: bool)

Sets whether vector selections should be shown in the rendered map

Parameters

showSelection (bool) – set to true if selections should be shown

See also

showSelection()

New in version 2.4.

setTextRenderFormat(self, format: QgsRenderContext.TextRenderFormat)

Sets the text render format, which dictates how text is rendered (e.g. as paths or real text objects).

New in version 3.4.3.

Parameters

format (QgsRenderContext.TextRenderFormat) –

setTransformContext(self, context: QgsCoordinateTransformContext)

Sets the context’s coordinate transform context, which stores various information regarding which datum transforms should be used when transforming points from a source to destination coordinate reference system.

New in version 3.0.

Parameters

context (QgsCoordinateTransformContext) –

setUseAdvancedEffects(self, enabled: bool)

Used to enable or disable advanced effects such as blend modes

Parameters

enabled (bool) –

setUseRenderingOptimization(self, enabled: bool)
Parameters

enabled (bool) –

setVectorSimplifyMethod(self, simplifyMethod: QgsVectorSimplifyMethod)
Parameters

simplifyMethod (QgsVectorSimplifyMethod) –

showSelection(self) → bool

Returns true if vector selections should be shown in the rendered map

Return type

bool

Returns

true if selections should be shown

See also

selectionColor()

New in version 2.4.

testFlag(self, flag: QgsRenderContext.Flag) → bool

Check whether a particular flag is enabled.

New in version 2.14.

Parameters

flag (QgsRenderContext.Flag) –

Return type

bool

textRenderFormat(self) → QgsRenderContext.TextRenderFormat

Returns the text render format, which dictates how text is rendered (e.g. as paths or real text objects).

New in version 3.4.3.

Return type

QgsRenderContext.TextRenderFormat

transformContext(self) → QgsCoordinateTransformContext

Returns the context’s coordinate transform context, which stores various information regarding which datum transforms should be used when transforming points from a source to destination coordinate reference system.

New in version 3.0.

Return type

QgsCoordinateTransformContext

useAdvancedEffects(self) → bool

Returns true if advanced effects such as blend modes such be used

Return type

bool

useRenderingOptimization(self) → bool

Returns true if the rendering optimization (geometry simplification) can be executed

Return type

bool

vectorSimplifyMethod(self) → QgsVectorSimplifyMethod

Added in QGIS v2.4

Return type

QgsVectorSimplifyMethod