QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgskde.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgskde.h
3  --------
4  Date : October 2016
5  Copyright : (C) 2016 by Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSKDE_H
17 #define QGSKDE_H
18 
19 #include "qgsrectangle.h"
20 #include "qgsogrutils.h"
21 #include <QString>
22 
23 // GDAL includes
24 #include <gdal.h>
25 #include <cpl_string.h>
26 #include <cpl_conv.h>
27 #include "qgis_analysis.h"
28 
29 class QgsFeatureSource;
30 class QgsFeature;
31 
32 
39 class ANALYSIS_EXPORT QgsKernelDensityEstimation
40 {
41  public:
42 
45  {
46  KernelQuartic = 0,
51  };
52 
55  {
56  OutputRaw = 0,
58  };
59 
61  enum Result
62  {
68  };
69 
71  struct Parameters
72  {
74  QgsFeatureSource *source = nullptr;
75 
77  double radius;
78 
80  QString radiusField;
81 
83  QString weightField;
84 
86  double pixelSize;
87 
90 
92  double decayRatio;
93 
96  };
97 
102  QgsKernelDensityEstimation( const Parameters &parameters, const QString &outputFile, const QString &outputFormat );
103 
107  QgsKernelDensityEstimation &operator=( const QgsKernelDensityEstimation &other ) = delete;
108 
113  Result run();
114 
121  Result prepare();
122 
128  Result addFeature( const QgsFeature &feature );
129 
135  Result finalise();
136 
137  private:
138 
140  double calculateKernelValue( double distance, double bandwidth, KernelShape shape, OutputValues outputType ) const;
142  double uniformKernel( double distance, double bandwidth, OutputValues outputType ) const;
144  double quarticKernel( double distance, double bandwidth, OutputValues outputType ) const;
146  double triweightKernel( double distance, double bandwidth, OutputValues outputType ) const;
148  double epanechnikovKernel( double distance, double bandwidth, OutputValues outputType ) const;
150  double triangularKernel( double distance, double bandwidth, OutputValues outputType ) const;
151 
152  QgsRectangle calculateBounds() const;
153 
154  QgsFeatureSource *mSource = nullptr;
155 
156  QString mOutputFile;
157  QString mOutputFormat;
158 
159  int mRadiusField;
160  int mWeightField;
161  double mRadius;
162  double mPixelSize;
163  QgsRectangle mBounds;
164 
165  KernelShape mShape;
166  double mDecay;
167  OutputValues mOutputValues;
168 
169  int mBufferSize;
170 
171  gdal::dataset_unique_ptr mDatasetH;
172  GDALRasterBandH mRasterBandH;
173 
175  bool createEmptyLayer( GDALDriverH driver, const QgsRectangle &bounds, int rows, int columns ) const;
176  int radiusSizeInPixels( double radius ) const;
177 
178 #ifdef SIP_RUN
180 #endif
181 };
182 
183 
184 #endif // QGSKDE_H
QgsKernelDensityEstimation::Parameters::weightField
QString weightField
Field name for weighting field, or empty if not using weights.
Definition: qgskde.h:83
QgsKernelDensityEstimation::KernelEpanechnikov
@ KernelEpanechnikov
Epanechnikov kernel.
Definition: qgskde.h:50
QgsKernelDensityEstimation::Parameters::outputValues
QgsKernelDensityEstimation::OutputValues outputValues
Type of output value.
Definition: qgskde.h:95
QgsKernelDensityEstimation::KernelTriangular
@ KernelTriangular
Triangular kernel.
Definition: qgskde.h:47
QgsKernelDensityEstimation::Parameters
KDE parameters.
Definition: qgskde.h:71
QgsKernelDensityEstimation::InvalidParameters
@ InvalidParameters
Input parameters were not valid.
Definition: qgskde.h:65
qgsrectangle.h
QgsFeatureSource
An interface for objects which provide features via a getFeatures method.
Definition: qgsfeaturesource.h:37
QgsKernelDensityEstimation::Parameters::pixelSize
double pixelSize
Size of pixel in output file.
Definition: qgskde.h:86
qgsogrutils.h
QgsKernelDensityEstimation::KernelUniform
@ KernelUniform
Uniform (flat) kernel.
Definition: qgskde.h:48
QgsKernelDensityEstimation::Parameters::shape
QgsKernelDensityEstimation::KernelShape shape
Kernel shape.
Definition: qgskde.h:89
QgsKernelDensityEstimation::Success
@ Success
Operation completed successfully.
Definition: qgskde.h:63
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsKernelDensityEstimation::DriverError
@ DriverError
Could not open the driver for the specified format.
Definition: qgskde.h:64
gdal::dataset_unique_ptr
std::unique_ptr< std::remove_pointer< GDALDatasetH >::type, GDALDatasetCloser > dataset_unique_ptr
Scoped GDAL dataset.
Definition: qgsogrutils.h:139
QgsKernelDensityEstimation::RasterIoError
@ RasterIoError
Error writing to raster.
Definition: qgskde.h:67
QgsKernelDensityEstimation::Parameters::radius
double radius
Fixed radius, in map units.
Definition: qgskde.h:77
QgsKernelDensityEstimation
Performs Kernel Density Estimation ("heatmap") calculations on a vector layer.
Definition: qgskde.h:39
QgsKernelDensityEstimation::Result
Result
Result of operation.
Definition: qgskde.h:61
QgsKernelDensityEstimation::OutputScaled
@ OutputScaled
Output mathematically correct scaled values.
Definition: qgskde.h:57
QgsKernelDensityEstimation::KernelShape
KernelShape
Kernel shape type.
Definition: qgskde.h:44
QgsKernelDensityEstimation::Parameters::decayRatio
double decayRatio
Decay ratio (Triangular kernels only)
Definition: qgskde.h:92
QgsKernelDensityEstimation::Parameters::radiusField
QString radiusField
Field for radius, or empty if using a fixed radius.
Definition: qgskde.h:80
QgsKernelDensityEstimation::FileCreationError
@ FileCreationError
Error creating output file.
Definition: qgskde.h:66
QgsKernelDensityEstimation::OutputValues
OutputValues
Output values type.
Definition: qgskde.h:54
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsKernelDensityEstimation::KernelTriweight
@ KernelTriweight
Triweight kernel.
Definition: qgskde.h:49