/
qgsheatmaprenderer.h
201 lines (167 loc) · 7.78 KB
/
qgsheatmaprenderer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/***************************************************************************
qgsheatmaprenderer.h
---------------------
begin : November 2014
copyright : (C) 2014 Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSHEATMAPRENDERER_H
#define QGSHEATMAPRENDERER_H
#include "qgis_core.h"
#include "qgis.h"
#include "qgsrenderer.h"
#include "qgssymbol.h"
#include "qgsexpression.h"
#include "qgsgeometry.h"
class QgsColorRamp;
/** \ingroup core
* \class QgsHeatmapRenderer
* \brief A renderer which draws points as a live heatmap
* \since QGIS 2.7
*/
class CORE_EXPORT QgsHeatmapRenderer : public QgsFeatureRenderer
{
public:
QgsHeatmapRenderer();
virtual ~QgsHeatmapRenderer();
//! Direct copies are forbidden. Use clone() instead.
QgsHeatmapRenderer( const QgsHeatmapRenderer & ) = delete;
//! Direct copies are forbidden. Use clone() instead.
QgsHeatmapRenderer &operator=( const QgsHeatmapRenderer & ) = delete;
//reimplemented methods
virtual QgsHeatmapRenderer *clone() const override;
virtual void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
virtual bool renderFeature( QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override;
virtual void stopRender( QgsRenderContext &context ) override;
//! \note symbolForFeature2 in Python bindings
virtual QgsSymbol *symbolForFeature( QgsFeature &feature, QgsRenderContext &context ) override;
//! \note symbol2 in Python bindings
virtual QgsSymbolList symbols( QgsRenderContext &context ) override;
virtual QString dump() const override;
virtual QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
//! Creates a new heatmap renderer instance from XML
static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
virtual QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
static QgsHeatmapRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer ) SIP_FACTORY;
//reimplemented to extent the request so that points up to heatmap's radius distance outside
//visible area are included
virtual void modifyRequestExtent( QgsRectangle &extent, QgsRenderContext &context ) override;
//heatmap specific methods
/** Returns the color ramp used for shading the heatmap.
* \returns color ramp for heatmap
* \see setColorRamp
*/
QgsColorRamp *colorRamp() const { return mGradientRamp; }
/** Sets the color ramp to use for shading the heatmap.
* \param ramp color ramp for heatmap. Ownership of ramp is transferred to the renderer.
* \see colorRamp
*/
void setColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
/** Returns the radius for the heatmap
* \returns heatmap radius
* \see setRadius
* \see radiusUnit
* \see radiusMapUnitScale
*/
double radius() const { return mRadius; }
/** Sets the radius for the heatmap
* \param radius heatmap radius
* \see radius
* \see setRadiusUnit
* \see setRadiusMapUnitScale
*/
void setRadius( const double radius ) { mRadius = radius; }
/** Returns the units used for the heatmap's radius
* \returns units for heatmap radius
* \see radius
* \see setRadiusUnit
* \see radiusMapUnitScale
*/
QgsUnitTypes::RenderUnit radiusUnit() const { return mRadiusUnit; }
/** Sets the units used for the heatmap's radius
* \param unit units for heatmap radius
* \see radiusUnit
* \see setRadius
* \see radiusMapUnitScale
*/
void setRadiusUnit( const QgsUnitTypes::RenderUnit unit ) { mRadiusUnit = unit; }
/** Returns the map unit scale used for the heatmap's radius
* \returns map unit scale for heatmap's radius
* \see radius
* \see radiusUnit
* \see setRadiusMapUnitScale
*/
const QgsMapUnitScale &radiusMapUnitScale() const { return mRadiusMapUnitScale; }
/** Sets the map unit scale used for the heatmap's radius
* \param scale map unit scale for heatmap's radius
* \see setRadius
* \see setRadiusUnit
* \see radiusMapUnitScale
*/
void setRadiusMapUnitScale( const QgsMapUnitScale &scale ) { mRadiusMapUnitScale = scale; }
/** Returns the maximum value used for shading the heatmap.
* \returns maximum value for heatmap shading. If 0, then maximum value will be automatically
* calculated.
* \see setMaximumValue
*/
double maximumValue() const { return mExplicitMax; }
/** Sets the maximum value used for shading the heatmap.
* \param value maximum value for heatmap shading. Set to 0 for automatic calculation of
* maximum value.
* \see maximumValue
*/
void setMaximumValue( const double value ) { mExplicitMax = value; }
/** Returns the render quality used for drawing the heatmap.
* \returns render quality. A value of 1 indicates maximum quality, and increasing the
* value will result in faster drawing but lower quality rendering.
* \see setRenderQuality
*/
double renderQuality() const { return mRenderQuality; }
/** Sets the render quality used for drawing the heatmap.
* \param quality render quality. A value of 1 indicates maximum quality, and increasing the
* value will result in faster drawing but lower quality rendering.
* \see renderQuality
*/
void setRenderQuality( const int quality ) { mRenderQuality = quality; }
/** Returns the expression used for weighting points when generating the heatmap.
* \returns point weight expression. If empty, all points are equally weighted.
* \see setWeightExpression
*/
QString weightExpression() const { return mWeightExpressionString; }
/** Sets the expression used for weighting points when generating the heatmap.
* \param expression point weight expression. If set to empty, all points are equally weighted.
* \see weightExpression
*/
void setWeightExpression( const QString &expression ) { mWeightExpressionString = expression; }
private:
QVector<double> mValues;
double mCalculatedMaxValue;
double mRadius;
int mRadiusPixels;
double mRadiusSquared;
QgsUnitTypes::RenderUnit mRadiusUnit;
QgsMapUnitScale mRadiusMapUnitScale;
QString mWeightExpressionString;
int mWeightAttrNum;
std::unique_ptr<QgsExpression> mWeightExpression;
QgsColorRamp *mGradientRamp = nullptr;
double mExplicitMax;
int mRenderQuality;
int mFeaturesRendered;
double uniformKernel( const double distance, const int bandwidth ) const;
double quarticKernel( const double distance, const int bandwidth ) const;
double triweightKernel( const double distance, const int bandwidth ) const;
double epanechnikovKernel( const double distance, const int bandwidth ) const;
double triangularKernel( const double distance, const int bandwidth ) const;
QgsMultiPoint convertToMultipoint( const QgsGeometry *geom );
void initializeValues( QgsRenderContext &context );
void renderImage( QgsRenderContext &context );
};
#endif // QGSHEATMAPRENDERER_H