QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 "qgssymbol.h"
22 #include "qgsexpression.h"
23 #include "qgsgeometry.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 
61  //reimplemented to extent the request so that points up to heatmap's radius distance outside
62  //visible area are included
63  void modifyRequestExtent( QgsRectangle &extent, QgsRenderContext &context ) override;
64 
65  //heatmap specific methods
66 
72  QgsColorRamp *colorRamp() const { return mGradientRamp; }
73 
79  void setColorRamp( QgsColorRamp *ramp SIP_TRANSFER );
80 
88  double radius() const { return mRadius; }
89 
97  void setRadius( const double radius ) { mRadius = radius; }
98 
106  QgsUnitTypes::RenderUnit radiusUnit() const { return mRadiusUnit; }
107 
115  void setRadiusUnit( const QgsUnitTypes::RenderUnit unit ) { mRadiusUnit = unit; }
116 
124  const QgsMapUnitScale &radiusMapUnitScale() const { return mRadiusMapUnitScale; }
125 
133  void setRadiusMapUnitScale( const QgsMapUnitScale &scale ) { mRadiusMapUnitScale = scale; }
134 
141  double maximumValue() const { return mExplicitMax; }
142 
149  void setMaximumValue( const double value ) { mExplicitMax = value; }
150 
157  double renderQuality() const { return mRenderQuality; }
158 
165  void setRenderQuality( const int quality ) { mRenderQuality = quality; }
166 
172  QString weightExpression() const { return mWeightExpressionString; }
173 
179  void setWeightExpression( const QString &expression ) { mWeightExpressionString = expression; }
180 
181  private:
182 
183  QVector<double> mValues;
184 
185  double mCalculatedMaxValue = 0;
186 
187  double mRadius = 10;
188  int mRadiusPixels = 0;
189  double mRadiusSquared = 0;
191  QgsMapUnitScale mRadiusMapUnitScale;
192 
193  QString mWeightExpressionString;
194  int mWeightAttrNum = -1;
195  std::unique_ptr<QgsExpression> mWeightExpression;
196 
197  QgsColorRamp *mGradientRamp = nullptr;
198 
199  double mExplicitMax = 0.0;
200  int mRenderQuality = 3;
201 
202  int mFeaturesRendered = 0;
203 
204  double uniformKernel( double distance, int bandwidth ) const;
205  double quarticKernel( double distance, int bandwidth ) const;
206  double triweightKernel( double distance, int bandwidth ) const;
207  double epanechnikovKernel( double distance, int bandwidth ) const;
208  double triangularKernel( double distance, int bandwidth ) const;
209 
210  QgsMultiPointXY convertToMultipoint( const QgsGeometry *geom );
211  void initializeValues( QgsRenderContext &context );
212  void renderImage( QgsRenderContext &context );
213 };
214 
215 
216 #endif // QGSHEATMAPRENDERER_H
The class is used as a container of context for various read/write operations on other objects...
A rectangle specified with double values.
Definition: qgsrectangle.h:41
double renderQuality() const
Returns the render quality used for drawing the heatmap.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:61
virtual QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context)
store renderer info to XML element
Abstract base class for color ramps.
Definition: qgscolorramp.h:31
double maximumValue() const
Returns the maximum value used for shading the heatmap.
Container of fields for a vector layer.
Definition: qgsfields.h:42
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
void setWeightExpression(const QString &expression)
Sets the expression used for weighting points when generating the heatmap.
QVector< QgsPointXY > QgsMultiPointXY
A collection of QgsPoints that share a common collection of attributes.
Definition: qgsgeometry.h:79
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
virtual QgsSymbol * symbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const =0
To be overridden.
virtual QSet< QString > usedAttributes(const QgsRenderContext &context) const =0
Returns a list of attributes required by this renderer.
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:43
A renderer which draws points as a live heatmap.
void setRadiusMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale used for the heatmap&#39;s radius.
virtual void modifyRequestExtent(QgsRectangle &extent, QgsRenderContext &context)
Allows for a renderer to modify the extent of a feature request prior to rendering.
double radius() const
Returns the radius for the heatmap.
const QgsMapUnitScale & radiusMapUnitScale() const
Returns the map unit scale used for the heatmap&#39;s radius.
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsColorRamp * colorRamp() const
Returns the color ramp used for shading the heatmap.
#define SIP_FACTORY
Definition: qgis_sip.h:69
void setRenderQuality(const int quality)
Sets the render quality used for drawing the heatmap.
void setRadiusUnit(const QgsUnitTypes::RenderUnit unit)
Sets the units used for the heatmap&#39;s radius.
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.
void setRadius(const double radius)
Sets the radius for the heatmap.
Contains information about the context of a rendering operation.
Struct for storing maximum and minimum scales for measurements in map units.
virtual QgsSymbolList symbols(QgsRenderContext &context) const
Returns list of symbols used by the renderer.
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)
Must be called when a new render cycle is started.
Definition: qgsrenderer.cpp:93
virtual void stopRender(QgsRenderContext &context)
Must be called when a render cycle has finished, to allow the renderer to clean up.
#define SIP_THROW(name)
Definition: qgis_sip.h:177
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
QString weightExpression() const
Returns the expression used for weighting points when generating the heatmap.
void setMaximumValue(const double value)
Sets the maximum value used for shading the heatmap.
QgsUnitTypes::RenderUnit radiusUnit() const
Returns the units used for the heatmap&#39;s radius.
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:111
virtual QString dump() const
Returns debug information about this renderer.