Skip to content

Commit f212746

Browse files
committed
Move heatmap generation code to analysis lib
And clean it up a lot
1 parent c558d51 commit f212746

File tree

5 files changed

+729
-0
lines changed

5 files changed

+729
-0
lines changed

python/analysis/analysis.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
%Include raster/qgsderivativefilter.sip
4545
%Include raster/qgsaspectfilter.sip
4646
%Include raster/qgshillshadefilter.sip
47+
%Include raster/qgskde.sip
4748
%Include raster/qgsninecellfilter.sip
4849
%Include raster/qgsrastercalcnode.sip
4950
%Include raster/qgsrastercalculator.sip

python/analysis/raster/qgskde.sip

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* \class QgsKernelDensityEstimation
3+
* \ingroup analysis
4+
* Performs Kernel Density Estimation ("heatmap") calculations on a vector layer.
5+
* @note added in QGIS 3.0
6+
*/
7+
class QgsKernelDensityEstimation
8+
{
9+
%TypeHeaderCode
10+
#include <qgskde.h>
11+
%End
12+
13+
public:
14+
15+
//! Kernel shape type
16+
enum KernelShape
17+
{
18+
KernelQuartic, //!< Quartic kernel
19+
KernelTriangular, //!< Triangular kernel
20+
KernelUniform, //!< Uniform (flat) kernel
21+
KernelTriweight, //!< Triweight kernel
22+
KernelEpanechnikov, //!< Epanechnikov kernel
23+
};
24+
25+
//! Output values type
26+
enum OutputValues
27+
{
28+
OutputRaw, //!< Output the raw KDE values
29+
OutputScaled, //!< Output mathematically correct scaled values
30+
};
31+
32+
//! Result of operation
33+
enum Result
34+
{
35+
Success, //!< Operation completed successfully
36+
DriverError, //!< Could not open the driver for the specified format
37+
InvalidParameters, //!< Input parameters were not valid
38+
FileCreationError, //!< Error creating output file
39+
RasterIoError, //!< Error writing to raster
40+
};
41+
42+
//! KDE parameters
43+
struct Parameters
44+
{
45+
//! Vector point layer
46+
QgsVectorLayer* vectorLayer;
47+
48+
//! Fixed radius, in map units
49+
double radius;
50+
51+
//! Field for radius, or empty if using a fixed radius
52+
QString radiusField;
53+
54+
//! Field name for weighting field, or empty if not using weights
55+
QString weightField;
56+
57+
//! Size of pixel in output file
58+
double pixelSize;
59+
60+
//! Kernel shape
61+
QgsKernelDensityEstimation::KernelShape shape;
62+
63+
//! Decay ratio (Triangular kernels only)
64+
double decayRatio;
65+
66+
//! Type of output value
67+
QgsKernelDensityEstimation::OutputValues outputValues;
68+
};
69+
70+
/**
71+
* Constructor for QgsKernelDensityEstimation. Requires a Parameters object specifying the options to use
72+
* to generate the surface. The output path and file format are also required.
73+
*/
74+
QgsKernelDensityEstimation( const Parameters& parameters, const QString& outputFile, const QString& outputFormat );
75+
76+
/**
77+
* Runs the KDE calculation across the whole layer at once. Either call this method, or manually
78+
* call run(), addFeature() and finalise() separately.
79+
*/
80+
Result run();
81+
82+
/**
83+
* Prepares the output file for writing and setups up the surface calculation. This must be called
84+
* before adding features via addFeature().
85+
* @see addFeature()
86+
* @see finalise()
87+
*/
88+
Result prepare();
89+
90+
/**
91+
* Adds a single feature to the KDE surface. prepare() must be called before adding features.
92+
* @see prepare()
93+
* @see finalise()
94+
*/
95+
Result addFeature( const QgsFeature& feature );
96+
97+
/**
98+
* Finalises the output file. Must be called after adding all features via addFeature().
99+
* @see prepare()
100+
* @see addFeature()
101+
*/
102+
Result finalise();
103+
104+
};

src/analysis/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ SET(QGIS_ANALYSIS_SRCS
2727
raster/qgsruggednessfilter.cpp
2828
raster/qgsderivativefilter.cpp
2929
raster/qgshillshadefilter.cpp
30+
raster/qgskde.cpp
3031
raster/qgsslopefilter.cpp
3132
raster/qgsaspectfilter.cpp
3233
raster/qgstotalcurvaturefilter.cpp
@@ -106,6 +107,7 @@ SET(QGIS_ANALYSIS_HDRS
106107
raster/qgsaspectfilter.h
107108
raster/qgsderivativefilter.h
108109
raster/qgshillshadefilter.h
110+
raster/qgskde.h
109111
raster/qgsninecellfilter.h
110112
raster/qgsrastercalculator.h
111113
raster/qgsrelief.h

0 commit comments

Comments
 (0)