Skip to content
Permalink
Browse files

Add support for weighting points by expression to heatmap renderer

  • Loading branch information
nyalldawson committed Nov 23, 2014
1 parent 50d2a74 commit 8039895db2f2f868dd0e206d777841995e09d570
@@ -121,5 +121,17 @@ class QgsHeatmapRenderer : QgsFeatureRendererV2
* @see renderQuality
*/
void setRenderQuality( const int 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;

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

};
@@ -65,6 +65,14 @@ void QgsHeatmapRenderer::startRender( QgsRenderContext& context, const QgsFields
return;
}

// find out classification attribute index from name
mWeightAttrNum = fields.fieldNameIndex( mWeightExpressionString );
if ( mWeightAttrNum == -1 )
{
mWeightExpression.reset( new QgsExpression( mWeightExpressionString ) );
mWeightExpression->prepare( fields );
}

initializeValues( context );
}

@@ -101,6 +109,28 @@ bool QgsHeatmapRenderer::renderFeature( QgsFeature& feature, QgsRenderContext& c
return false;
}

double weight = 1.0;
if ( !mWeightExpressionString.isEmpty() )
{
QVariant value;
if ( mWeightAttrNum == -1 )
{
Q_ASSERT( mWeightExpression.data() );
value = mWeightExpression->evaluate( &feature );
}
else
{
const QgsAttributes& attrs = feature.attributes();
value = attrs.value( mWeightAttrNum );
}
bool ok = false;
double evalWeight = value.toDouble( &ok );
if ( ok )
{
weight = evalWeight;
}
}

int width = context.painter()->device()->width() / mRenderQuality;
int height = context.painter()->device()->height() / mRenderQuality;

@@ -128,7 +158,7 @@ bool QgsHeatmapRenderer::renderFeature( QgsFeature& feature, QgsRenderContext& c
continue;
}

double score = quarticKernel( sqrt( distanceSquared ), mRadiusPixels );
double score = weight * quarticKernel( sqrt( distanceSquared ), mRadiusPixels );
double value = mValues[ index ] + score;
if ( value > mCalculatedMaxValue )
{
@@ -181,6 +211,7 @@ double QgsHeatmapRenderer::triangularKernel( const double distance, const int ba
void QgsHeatmapRenderer::stopRender( QgsRenderContext& context )
{
renderImage( context );
mWeightExpression.reset();
}

void QgsHeatmapRenderer::renderImage( QgsRenderContext& context )
@@ -246,6 +277,7 @@ QgsFeatureRendererV2* QgsHeatmapRenderer::clone() const
newRenderer->setRadiusMapUnitScale( mRadiusMapUnitScale );
newRenderer->setMaximumValue( mExplicitMax );
newRenderer->setRenderQuality( mRenderQuality );
newRenderer->setWeightExpression( mWeightExpressionString );

return newRenderer;
}
@@ -282,6 +314,7 @@ QgsFeatureRendererV2* QgsHeatmapRenderer::create( QDomElement& element )
r->setRadiusMapUnitScale( QgsSymbolLayerV2Utils::decodeMapUnitScale( element.attribute( "radius_map_unit_scale", QString() ) ) );
r->setMaximumValue( element.attribute( "max_value", "0.0" ).toFloat() );
r->setRenderQuality( element.attribute( "quality", "0" ).toInt() );
r->setWeightExpression( element.attribute( "weight_expression" ) );

QDomElement sourceColorRampElem = element.firstChildElement( "colorramp" );
if ( !sourceColorRampElem.isNull() && sourceColorRampElem.attribute( "name" ) == "[source]" )
@@ -301,6 +334,7 @@ QDomElement QgsHeatmapRenderer::save( QDomDocument& doc )
rendererElem.setAttribute( "radius_map_unit_scale", QgsSymbolLayerV2Utils::encodeMapUnitScale( mRadiusMapUnitScale ) );
rendererElem.setAttribute( "max_value", QString::number( mExplicitMax ) );
rendererElem.setAttribute( "quality", QString::number( mRenderQuality ) );
rendererElem.setAttribute( "weight_expression", mWeightExpressionString );
if ( mGradientRamp )
{
QDomElement colorRampElem = QgsSymbolLayerV2Utils::saveColorRamp( "[source]", mGradientRamp, doc );
@@ -324,8 +358,19 @@ QgsSymbolV2List QgsHeatmapRenderer::symbols()

QList<QString> QgsHeatmapRenderer::usedAttributes()
{
QList<QString> attributeList;
return attributeList;
QSet<QString> attributes;

// mAttrName can contain either attribute name or an expression.
// Sometimes it is not possible to distinguish between those two,
// e.g. "a - b" can be both a valid attribute name or expression.
// Since we do not have access to fields here, try both options.
attributes << mWeightExpressionString;

QgsExpression testExpr( mWeightExpressionString );
if ( !testExpr.hasParserError() )
attributes.unite( testExpr.referencedColumns().toSet() );

return attributes.toList();
}

QgsHeatmapRenderer* QgsHeatmapRenderer::convertFromRenderer( const QgsFeatureRendererV2 *renderer )
@@ -151,6 +151,18 @@ class CORE_EXPORT QgsHeatmapRenderer : public QgsFeatureRendererV2
*/
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:
/** Private copy constructor. @see clone() */
QgsHeatmapRenderer( const QgsHeatmapRenderer& );
@@ -167,6 +179,10 @@ class CORE_EXPORT QgsHeatmapRenderer : public QgsFeatureRendererV2
QgsSymbolV2::OutputUnit mRadiusUnit;
QgsMapUnitScale mRadiusMapUnitScale;

QString mWeightExpressionString;
int mWeightAttrNum;
QScopedPointer<QgsExpression> mWeightExpression;

QgsVectorColorRampV2* mGradientRamp;
bool mInvertRamp;

@@ -23,6 +23,7 @@
#include "qgsvectorcolorrampv2.h"
#include "qgsvectorgradientcolorrampv2dialog.h"
#include "qgsstylev2.h"
#include "qgsproject.h"
#include <QGridLayout>
#include <QLabel>

@@ -100,6 +101,10 @@ QgsHeatmapRendererWidget::QgsHeatmapRendererWidget( QgsVectorLayer* layer, QgsSt
mInvertCheckBox->blockSignals( true );
mInvertCheckBox->setChecked( mRenderer->invertRamp() );
mInvertCheckBox->blockSignals( false );

mWeightExpressionWidget->setLayer( layer );
mWeightExpressionWidget->setField( mRenderer->weightExpression() );
connect( mWeightExpressionWidget, SIGNAL( fieldChanged( QString ) ), this, SLOT( weightExpressionChanged( QString ) ) );
}

QgsFeatureRendererV2* QgsHeatmapRendererWidget::renderer()
@@ -206,3 +211,8 @@ void QgsHeatmapRendererWidget::on_mInvertCheckBox_toggled( bool v )

mRenderer->setInvertRamp( v );
}

void QgsHeatmapRendererWidget::weightExpressionChanged( QString expression )
{
mRenderer->setWeightExpression( expression );
}
@@ -55,6 +55,7 @@ class GUI_EXPORT QgsHeatmapRendererWidget : public QgsRendererV2Widget, private
void on_mMaxSpinBox_valueChanged( double d );
void on_mQualitySlider_valueChanged( int v );
void on_mInvertCheckBox_toggled( bool v );
void weightExpressionChanged( QString expression );

};

0 comments on commit 8039895

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