Skip to content
Permalink
Browse files

[FEATURE] Live heatmap renderer

Allows for live, dynamic heatmaps for point layers. Options include
specifying heatmap radius in pixels, mm or map units, choice of
color ramp, and a slider for selecting a tradeoff between render
speed and quality.
  • Loading branch information
nyalldawson committed Nov 22, 2014
1 parent c924401 commit 727747658bf24d0be6dc5b795abb5689d9544a1e
@@ -237,6 +237,7 @@
%Include symbology-ng/qgspointdisplacementrenderer.sip
%Include symbology-ng/qgsrulebasedrendererv2.sip
%Include symbology-ng/qgsinvertedpolygonrenderer.sip
%Include symbology-ng/qgsheatmaprenderer.sip
%Include symbology-ng/qgsrendererv2.sip
%Include symbology-ng/qgsrendererv2registry.sip

@@ -0,0 +1,112 @@
class QgsHeatmapRenderer : QgsFeatureRendererV2
{
%TypeHeaderCode
#include <qgsheatmaprenderer.h>
%End
public:

QgsHeatmapRenderer();
virtual ~QgsHeatmapRenderer();

//reimplemented methods
virtual QgsFeatureRendererV2* clone() const /Factory/;
virtual void startRender( QgsRenderContext& context, const QgsFields& fields );
virtual bool renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false );
virtual void stopRender( QgsRenderContext& context );
virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature );
virtual QgsSymbolV2List symbols();
virtual QString dump() const;
virtual QList<QString> usedAttributes();
static QgsFeatureRendererV2* create( QDomElement& element ) /Factory/;
virtual QDomElement save( QDomDocument& doc );
static QgsHeatmapRenderer* convertFromRenderer( const QgsFeatureRendererV2* renderer ) /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 );

//heatmap specific methods

/**Returns the color ramp used for shading the heatmap.
* @returns color ramp for heatmap
* @see setColorRamp
*/
QgsVectorColorRampV2* colorRamp() const;
/**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( QgsVectorColorRampV2* ramp /Transfer/ );

/**Returns the radius for the heatmap
* @returns heatmap radius
* @see setRadius
* @see radiusUnit
* @see radiusMapUnitScale
*/
double radius() const;
/**Sets the radius for the heatmap
* @param radius heatmap radius
* @see radius
* @see setRadiusUnit
* @see setRadiusMapUnitScale
*/
void setRadius( const double radius );

/**Returns the units used for the heatmap's radius
* @returns units for heatmap radius
* @see radius
* @see setRadiusUnit
* @see radiusMapUnitScale
*/
QgsSymbolV2::OutputUnit radiusUnit() const;
/**Sets the units used for the heatmap's radius
* @param unit units for heatmap radius
* @see radiusUnit
* @see setRadius
* @see radiusMapUnitScale
*/
void setRadiusUnit( const QgsSymbolV2::OutputUnit 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;
/**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 );

/**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;
/**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 );

/**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;
/**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 );

};
@@ -179,6 +179,14 @@ class QgsFeatureRendererV2
//! @note added in 2.6
virtual QgsSymbolV2List originalSymbolsForFeature( QgsFeature& feat );

/**Allows for a renderer to modify the extent of a feature request prior to rendering
* @param extent reference to request's filter extent. Modify extent to change the
* extent of feature request
* @param context render context
* @note added in QGIS 2.7
*/
virtual void modifyRequestExtent( QgsRectangle& extent, QgsRenderContext& context );

protected:
QgsFeatureRendererV2( QString type );

@@ -148,6 +148,7 @@
%Include symbology-ng/qgsellipsesymbollayerv2widget.sip
%Include symbology-ng/qgsgraduatedsymbolrendererv2widget.sip
%Include symbology-ng/qgsinvertedpolygonrendererwidget.sip
%Include symbology-ng/qgsheatmaprendererwidget.sip
%Include symbology-ng/qgslayerpropertieswidget.sip
%Include symbology-ng/qgspenstylecombobox.sip
%Include symbology-ng/qgspointdisplacementrendererwidget.sip
@@ -0,0 +1,23 @@
class QgsHeatmapRendererWidget : QgsRendererV2Widget
{
%TypeHeaderCode
#include <qgsheatmaprendererwidget.h>
%End
public:
/** static creation method
* @param layer the layer where this renderer is applied
* @param style
* @param renderer the mask renderer (will take ownership)
*/
static QgsRendererV2Widget* create( QgsVectorLayer* layer, QgsStyleV2* style, QgsFeatureRendererV2* renderer ) /Factory/;

/** Constructor
* @param layer the layer where this renderer is applied
* @param style
* @param renderer the mask renderer (will take ownership)
*/
QgsHeatmapRendererWidget( QgsVectorLayer* layer, QgsStyleV2* style, QgsFeatureRendererV2* renderer );

/** @returns the current feature renderer */
virtual QgsFeatureRendererV2* renderer();
};
@@ -30,6 +30,7 @@ SET(QGIS_CORE_SRCS
symbology-ng/qgscategorizedsymbolrendererv2.cpp
symbology-ng/qgsgraduatedsymbolrendererv2.cpp
symbology-ng/qgsrulebasedrendererv2.cpp
symbology-ng/qgsheatmaprenderer.cpp
symbology-ng/qgsinvertedpolygonrenderer.cpp
symbology-ng/qgsvectorcolorrampv2.cpp
symbology-ng/qgscptcityarchive.cpp
@@ -650,6 +651,7 @@ SET(QGIS_CORE_HDRS
symbology-ng/qgsrendererv2registry.h
symbology-ng/qgsrulebasedrendererv2.h
symbology-ng/qgssinglesymbolrendererv2.h
symbology-ng/qgsheatmaprenderer.h
symbology-ng/qgsinvertedpolygonrenderer.h
symbology-ng/qgsstylev2.h
symbology-ng/qgssvgcache.h
@@ -127,8 +127,11 @@ bool QgsVectorLayerRenderer::render()

mRendererV2->startRender( mContext, mFields );

QgsRectangle requestExtent = mContext.extent();
mRendererV2->modifyRequestExtent( requestExtent, mContext );

QgsFeatureRequest featureRequest = QgsFeatureRequest()
.setFilterRect( mContext.extent() )
.setFilterRect( requestExtent )
.setSubsetOfAttributes( mAttrNames, mFields );

// enable the simplification of the geometries (Using the current map2pixel context) before send it to renderer engine.

1 comment on commit 7277476

@nirvn

This comment has been minimized.

Copy link
Contributor

@nirvn nirvn commented on 7277476 Nov 22, 2014

@nyalldawson wonderful work!

The two things IMHO need follow up implementation:

  • Allow for a field to be used to compute weight of points (instead of making one point = 1 weight)
  • Allow for the color ramp to be inverted via a [x] invert checkbox to prevent forcing users to create reversed-color band of an already present band

But, beyond that, amazingly useful. Love the feeling of it when you zoom in and out of the map.

Please sign in to comment.
You can’t perform that action at this time.