Class: QgsKernelDensityEstimation

Performs Kernel Density Estimation (“heatmap”) calculations on a vector layer.

class qgis.analysis.QgsKernelDensityEstimation[source]

Bases: object

__init__(parameters: QgsKernelDensityEstimation.Parameters, outputFile: str | None, outputFormat: str | None)

Constructor for QgsKernelDensityEstimation. Requires a Parameters object specifying the options to use to generate the surface. The output path and file format are also required.

Parameters:
class KernelShape(*values)

Bases: IntEnum

Kernel shape type

  • Quartic: Quartic kernel

    Available as QgsKernelDensityEstimation.KernelQuartic in older QGIS releases.

  • Triangular: Triangular kernel

    Available as QgsKernelDensityEstimation.KernelTriangular in older QGIS releases.

  • Uniform: Uniform (flat) kernel

    Available as QgsKernelDensityEstimation.KernelUniform in older QGIS releases.

  • Triweight: Triweight kernel

    Available as QgsKernelDensityEstimation.KernelTriweight in older QGIS releases.

  • Epanechnikov: Epanechnikov kernel

    Available as QgsKernelDensityEstimation.KernelEpanechnikov in older QGIS releases.

class OutputValues(*values)

Bases: IntEnum

Output values type

  • Raw: Output the raw KDE values

    Available as QgsKernelDensityEstimation.OutputRaw in older QGIS releases.

  • Scaled: Output mathematically correct scaled values

    Available as QgsKernelDensityEstimation.OutputScaled in older QGIS releases.

class Parameters

Bases: object

KDE parameters

decayRatio: float
outputValues: QgsKernelDensityEstimation.OutputValues
pixelSize: float
radius: float
radiusField: str
shape: QgsKernelDensityEstimation.KernelShape
source: QgsFeatureSource
weightField: str
class Result(*values)

Bases: IntEnum

Result of operation

  • Success: Operation completed successfully

  • DriverError: Could not open the driver for the specified format

  • InvalidParameters: Input parameters were not valid

  • FileCreationError: Error creating output file

  • RasterIoError: Error writing to raster

addFeature(self, feature: QgsFeature) QgsKernelDensityEstimation.Result[source]

Adds a single feature to the KDE surface. prepare() must be called before adding features.

See also

prepare()

See also

finalise()

Parameters:

feature (QgsFeature)

Return type:

QgsKernelDensityEstimation.Result

finalise(self) QgsKernelDensityEstimation.Result[source]

Finalises the output file. Must be called after adding all features via addFeature().

See also

prepare()

See also

addFeature()

Return type:

QgsKernelDensityEstimation.Result

prepare(self) QgsKernelDensityEstimation.Result[source]

Prepares the output file for writing and setups up the surface calculation. This must be called before adding features via addFeature().

See also

addFeature()

See also

finalise()

Return type:

QgsKernelDensityEstimation.Result

run(self) QgsKernelDensityEstimation.Result[source]

Runs the KDE calculation across the whole layer at once. Either call this method, or manually call run(), addFeature() and finalise() separately.

Return type:

QgsKernelDensityEstimation.Result