QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsheatmaprenderer.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsheatmaprenderer.h
3  ---------------------
4  begin : November 2014
5  copyright : (C) 2014 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 #ifndef QGSHEATMAPRENDERER_H
16 #define QGSHEATMAPRENDERER_H
17 
18 #include "qgis_core.h"
19 #include "qgis_sip.h"
20 #include "qgsrenderer.h"
21 #include "qgsexpression.h"
22 #include "qgsgeometry.h"
23 #include "qgsmapunitscale.h"
24 
25 class QgsColorRamp;
26 
33 class CORE_EXPORT QgsHeatmapRenderer : public QgsFeatureRenderer
34 {
35  public:
36 
38  ~QgsHeatmapRenderer() override;
39 
41  QgsHeatmapRenderer( const QgsHeatmapRenderer & ) = delete;
43  QgsHeatmapRenderer &operator=( const QgsHeatmapRenderer & ) = delete;
44 
45  //reimplemented methods
46  QgsHeatmapRenderer *clone() const override SIP_FACTORY;
47  void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
48  bool renderFeature( const QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override SIP_THROW( QgsCsException );
49  void stopRender( QgsRenderContext &context ) override;
51  QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
53  QgsSymbolList symbols( QgsRenderContext &context ) const override;
54  QString dump() const override;
55  QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
57  static QgsFeatureRenderer *create( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
58  QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) override;
59  static QgsHeatmapRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer ) SIP_FACTORY;
60  bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
61 
62  //reimplemented to extent the request so that points up to heatmap's radius distance outside
63  //visible area are included
64  void modifyRequestExtent( QgsRectangle &extent, QgsRenderContext &context ) override;
65 
66  //heatmap specific methods
67 
73  QgsColorRamp *colorRamp() const { return mGradientRamp; }
74 
80  void setColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
81 
89  double radius() const { return mRadius; }
90 
98  void setRadius( const double radius ) { mRadius = radius; }
99 
107  QgsUnitTypes::RenderUnit radiusUnit() const { return mRadiusUnit; }
108 
116  void setRadiusUnit( const QgsUnitTypes::RenderUnit unit ) { mRadiusUnit = unit; }
117 
125  const QgsMapUnitScale &radiusMapUnitScale() const { return mRadiusMapUnitScale; }
126 
134  void setRadiusMapUnitScale( const QgsMapUnitScale &scale ) { mRadiusMapUnitScale = scale; }
135 
142  double maximumValue() const { return mExplicitMax; }
143 
150  void setMaximumValue( const double value ) { mExplicitMax = value; }
151 
158  double renderQuality() const { return mRenderQuality; }
159 
166  void setRenderQuality( const int quality ) { mRenderQuality = quality; }
167 
173  QString weightExpression() const { return mWeightExpressionString; }
174 
180  void setWeightExpression( const QString &expression ) { mWeightExpressionString = expression; }
181 
182  private:
183 
184  QVector<double> mValues;
185 
186  double mCalculatedMaxValue = 0;
187 
188  double mRadius = 10;
189  int mRadiusPixels = 0;
190  double mRadiusSquared = 0;
192  QgsMapUnitScale mRadiusMapUnitScale;
193 
194  QString mWeightExpressionString;
195  int mWeightAttrNum = -1;
196  std::unique_ptr<QgsExpression> mWeightExpression;
197 
198  QgsColorRamp *mGradientRamp = nullptr;
199 
200  double mExplicitMax = 0.0;
201  int mRenderQuality = 3;
202 
203  int mFeaturesRendered = 0;
204 
205  double uniformKernel( double distance, int bandwidth ) const;
206  double quarticKernel( double distance, int bandwidth ) const;
207  double triweightKernel( double distance, int bandwidth ) const;
208  double epanechnikovKernel( double distance, int bandwidth ) const;
209  double triangularKernel( double distance, int bandwidth ) const;
210 
211  QgsMultiPointXY convertToMultipoint( const QgsGeometry *geom );
212  void initializeValues( QgsRenderContext &context );
213  void renderImage( QgsRenderContext &context );
214 };
215 
216 
217 #endif // QGSHEATMAPRENDERER_H
QgsColorRamp
Abstract base class for color ramps.
Definition: qgscolorramp.h:29
QgsUnitTypes::RenderUnit
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:167
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsHeatmapRenderer::weightExpression
QString weightExpression() const
Returns the expression used for weighting points when generating the heatmap.
Definition: qgsheatmaprenderer.h:173
qgsexpression.h
QgsFeatureRenderer::accept
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
Definition: qgsrenderer.cpp:489
QgsHeatmapRenderer::radiusUnit
QgsUnitTypes::RenderUnit radiusUnit() const
Returns the units used for the heatmap's radius.
Definition: qgsheatmaprenderer.h:107
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsHeatmapRenderer
A renderer which draws points as a live heatmap.
Definition: qgsheatmaprenderer.h:33
QgsFeatureRenderer::usedAttributes
virtual QSet< QString > usedAttributes(const QgsRenderContext &context) const =0
Returns a list of attributes required by this renderer.
QgsStyleEntityVisitorInterface
An interface for classes which can visit style entity (e.g. symbol) nodes (using the visitor pattern)...
Definition: qgsstyleentityvisitor.h:33
QgsUnitTypes::RenderMillimeters
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:169
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:92
QgsHeatmapRenderer::setRadius
void setRadius(const double radius)
Sets the radius for the heatmap.
Definition: qgsheatmaprenderer.h:98
QgsHeatmapRenderer::setRadiusUnit
void setRadiusUnit(const QgsUnitTypes::RenderUnit unit)
Sets the units used for the heatmap's radius.
Definition: qgsheatmaprenderer.h:116
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsMultiPointXY
QVector< QgsPointXY > QgsMultiPointXY
A collection of QgsPoints that share a common collection of attributes.
Definition: qgsgeometry.h:82
QgsHeatmapRenderer::setMaximumValue
void setMaximumValue(const double value)
Sets the maximum value used for shading the heatmap.
Definition: qgsheatmaprenderer.h:150
QgsFeatureRenderer::stopRender
virtual void stopRender(QgsRenderContext &context)
Must be called when a render cycle has finished, to allow the renderer to clean up.
Definition: qgsrenderer.cpp:110
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsFeatureRenderer::save
virtual QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context)
Stores renderer properties to an XML element.
Definition: qgsrenderer.cpp:193
QgsCsException
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
QgsHeatmapRenderer::renderQuality
double renderQuality() const
Returns the render quality used for drawing the heatmap.
Definition: qgsheatmaprenderer.h:158
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:198
QgsHeatmapRenderer::setWeightExpression
void setWeightExpression(const QString &expression)
Sets the expression used for weighting points when generating the heatmap.
Definition: qgsheatmaprenderer.h:180
QgsSymbolList
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:44
QgsHeatmapRenderer::colorRamp
QgsColorRamp * colorRamp() const
Returns the color ramp used for shading the heatmap.
Definition: qgsheatmaprenderer.h:73
QgsFeatureRenderer::clone
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
QgsHeatmapRenderer::setRenderQuality
void setRenderQuality(const int quality)
Sets the render quality used for drawing the heatmap.
Definition: qgsheatmaprenderer.h:166
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsFeatureRenderer::symbols
virtual QgsSymbolList symbols(QgsRenderContext &context) const
Returns list of symbols used by the renderer.
Definition: qgsrenderer.cpp:151
QgsHeatmapRenderer::maximumValue
double maximumValue() const
Returns the maximum value used for shading the heatmap.
Definition: qgsheatmaprenderer.h:142
QgsFeatureRenderer::symbolForFeature
virtual QgsSymbol * symbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const =0
To be overridden.
QgsMapUnitScale
Struct for storing maximum and minimum scales for measurements in map units.
Definition: qgsmapunitscale.h:36
qgsrenderer.h
QgsFeatureRenderer::modifyRequestExtent
virtual void modifyRequestExtent(QgsRectangle &extent, QgsRenderContext &context)
Allows for a renderer to modify the extent of a feature request prior to rendering.
Definition: qgsrenderer.cpp:434
qgsgeometry.h
QgsFeatureRenderer
Definition: qgsrenderer.h:101
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsFeatureRenderer::renderFeature
virtual bool renderFeature(const QgsFeature &feature, QgsRenderContext &context, int layer=-1, bool selected=false, bool drawVertexMarker=false) SIP_THROW(QgsCsException)
Render a feature using this renderer in the given context.
Definition: qgsrenderer.cpp:127
qgsmapunitscale.h
QgsHeatmapRenderer::setRadiusMapUnitScale
void setRadiusMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale used for the heatmap's radius.
Definition: qgsheatmaprenderer.h:134
QgsFeatureRenderer::dump
virtual QString dump() const
Returns debug information about this renderer.
Definition: qgsrenderer.cpp:146
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsHeatmapRenderer::radius
double radius() const
Returns the radius for the heatmap.
Definition: qgsheatmaprenderer.h:89
QgsFeatureRenderer::startRender
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)
Must be called when a new render cycle is started.
Definition: qgsrenderer.cpp:96
QgsHeatmapRenderer::radiusMapUnitScale
const QgsMapUnitScale & radiusMapUnitScale() const
Returns the map unit scale used for the heatmap's radius.
Definition: qgsheatmaprenderer.h:125