Skip to content

Commit

Permalink
Move heatmap generation code to analysis lib
Browse files Browse the repository at this point in the history
And clean it up a lot
  • Loading branch information
nyalldawson committed Dec 6, 2016
1 parent c558d51 commit f212746
Show file tree
Hide file tree
Showing 5 changed files with 729 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/analysis/analysis.sip
Expand Up @@ -44,6 +44,7 @@
%Include raster/qgsderivativefilter.sip
%Include raster/qgsaspectfilter.sip
%Include raster/qgshillshadefilter.sip
%Include raster/qgskde.sip
%Include raster/qgsninecellfilter.sip
%Include raster/qgsrastercalcnode.sip
%Include raster/qgsrastercalculator.sip
Expand Down
104 changes: 104 additions & 0 deletions python/analysis/raster/qgskde.sip
@@ -0,0 +1,104 @@
/**
* \class QgsKernelDensityEstimation
* \ingroup analysis
* Performs Kernel Density Estimation ("heatmap") calculations on a vector layer.
* @note added in QGIS 3.0
*/
class QgsKernelDensityEstimation
{
%TypeHeaderCode
#include <qgskde.h>
%End

public:

//! Kernel shape type
enum KernelShape
{
KernelQuartic, //!< Quartic kernel
KernelTriangular, //!< Triangular kernel
KernelUniform, //!< Uniform (flat) kernel
KernelTriweight, //!< Triweight kernel
KernelEpanechnikov, //!< Epanechnikov kernel
};

//! Output values type
enum OutputValues
{
OutputRaw, //!< Output the raw KDE values
OutputScaled, //!< Output mathematically correct scaled values
};

//! Result of operation
enum Result
{
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
};

//! KDE parameters
struct Parameters
{
//! Vector point layer
QgsVectorLayer* vectorLayer;

//! Fixed radius, in map units
double radius;

//! Field for radius, or empty if using a fixed radius
QString radiusField;

//! Field name for weighting field, or empty if not using weights
QString weightField;

//! Size of pixel in output file
double pixelSize;

//! Kernel shape
QgsKernelDensityEstimation::KernelShape shape;

//! Decay ratio (Triangular kernels only)
double decayRatio;

//! Type of output value
QgsKernelDensityEstimation::OutputValues outputValues;
};

/**
* 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.
*/
QgsKernelDensityEstimation( const Parameters& parameters, const QString& outputFile, const QString& outputFormat );

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

/**
* Prepares the output file for writing and setups up the surface calculation. This must be called
* before adding features via addFeature().
* @see addFeature()
* @see finalise()
*/
Result prepare();

/**
* Adds a single feature to the KDE surface. prepare() must be called before adding features.
* @see prepare()
* @see finalise()
*/
Result addFeature( const QgsFeature& feature );

/**
* Finalises the output file. Must be called after adding all features via addFeature().
* @see prepare()
* @see addFeature()
*/
Result finalise();

};
2 changes: 2 additions & 0 deletions src/analysis/CMakeLists.txt
Expand Up @@ -27,6 +27,7 @@ SET(QGIS_ANALYSIS_SRCS
raster/qgsruggednessfilter.cpp
raster/qgsderivativefilter.cpp
raster/qgshillshadefilter.cpp
raster/qgskde.cpp
raster/qgsslopefilter.cpp
raster/qgsaspectfilter.cpp
raster/qgstotalcurvaturefilter.cpp
Expand Down Expand Up @@ -106,6 +107,7 @@ SET(QGIS_ANALYSIS_HDRS
raster/qgsaspectfilter.h
raster/qgsderivativefilter.h
raster/qgshillshadefilter.h
raster/qgskde.h
raster/qgsninecellfilter.h
raster/qgsrastercalculator.h
raster/qgsrelief.h
Expand Down

0 comments on commit f212746

Please sign in to comment.