31 #include <QDomDocument> 32 #include <QDomElement> 36 , mCalculatedMaxValue( 0 )
41 , mWeightAttrNum( -1 )
42 , mGradientRamp( nullptr )
43 , mInvertRamp( false )
46 , mFeaturesRendered( 0 )
61 mCalculatedMaxValue = 0;
62 mFeaturesRendered = 0;
64 mRadiusSquared = mRadiusPixels * mRadiusPixels;
77 if ( mWeightAttrNum == -1 )
83 initializeValues( context );
105 Q_UNUSED( selected );
106 Q_UNUSED( drawVertexMarker );
120 if ( !mWeightExpressionString.
isEmpty() )
123 if ( mWeightAttrNum == -1 )
125 Q_ASSERT( mWeightExpression.
data() );
131 value = attrs.
value( mWeightAttrNum );
134 double evalWeight = value.
toDouble( &ok );
156 delete transformedGeom;
157 transformedGeom =
nullptr;
163 int pointX = pixel.
x() / mRenderQuality;
164 int pointY = pixel.
y() / mRenderQuality;
165 for (
int x = qMax( pointX - mRadiusPixels, 0 ); x < qMin( pointX + mRadiusPixels, width ); ++x )
167 for (
int y = qMax( pointY - mRadiusPixels, 0 ); y < qMin( pointY + mRadiusPixels, height ); ++y )
169 int index = y * width + x;
170 if ( index >= mValues.
count() )
174 double distanceSquared = pow( pointX - x, 2.0 ) + pow( pointY - y, 2.0 );
175 if ( distanceSquared > mRadiusSquared )
180 double score = weight * quarticKernel( sqrt( distanceSquared ), mRadiusPixels );
181 double value = mValues.
at( index ) + score;
182 if ( value > mCalculatedMaxValue )
184 mCalculatedMaxValue = value;
186 mValues[
index ] = value;
194 if ( mFeaturesRendered % 200 == 0 )
196 renderImage( context );
203 double QgsHeatmapRenderer::uniformKernel(
const double distance,
const int bandwidth )
const 205 Q_UNUSED( distance );
206 Q_UNUSED( bandwidth );
210 double QgsHeatmapRenderer::quarticKernel(
const double distance,
const int bandwidth )
const 212 return pow( 1. - pow( distance / static_cast< double >( bandwidth ), 2 ), 2 );
215 double QgsHeatmapRenderer::triweightKernel(
const double distance,
const int bandwidth )
const 217 return pow( 1. - pow( distance / static_cast< double >( bandwidth ), 2 ), 3 );
220 double QgsHeatmapRenderer::epanechnikovKernel(
const double distance,
const int bandwidth )
const 222 return ( 1. - pow( distance / static_cast< double >( bandwidth ), 2 ) );
225 double QgsHeatmapRenderer::triangularKernel(
const double distance,
const int bandwidth )
const 227 return ( 1. - ( distance / static_cast< double >( bandwidth ) ) );
232 renderImage( context );
233 mWeightExpression.
reset();
238 if ( !context.
painter() || !mGradientRamp )
245 QImage::Format_ARGB32 );
246 image.
fill( Qt::transparent );
248 double scaleMax = mExplicitMax > 0 ? mExplicitMax : mCalculatedMaxValue;
253 for (
int heightIndex = 0; heightIndex < image.height(); ++heightIndex )
255 QRgb* scanLine =
reinterpret_cast< QRgb*
>( image.scanLine( heightIndex ) );
256 for (
int widthIndex = 0; widthIndex < image.width(); ++widthIndex )
259 pixVal = mValues.
at( idx ) > 0 ? qMin(( mValues.
at( idx ) / scaleMax ), 1.0 ) : 0;
262 pixColor = mGradientRamp->
color( mInvertRamp ? 1 - pixVal : pixVal );
264 scanLine[widthIndex] = pixColor.
rgba();
269 if ( mRenderQuality > 1 )
309 double extension = 0.0;
340 if ( !sourceColorRampElem.
isNull() && sourceColorRampElem.
attribute(
"name" ) ==
"[source]" )
357 rendererElem.
setAttribute(
"weight_expression", mWeightExpressionString );
399 attributes << mWeightExpressionString;
405 return attributes.
toList();
410 if ( renderer->
type() ==
"heatmapRenderer" )
422 delete mGradientRamp;
423 mGradientRamp = ramp;
Class for parsing and evaluation of expressions (formerly called "search strings").
void setInvertRamp(const bool invert)
Sets whether the ramp is inverted.
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
#define RENDERER_TAG_NAME
A rectangle specified with double values.
QgsAttributes attributes() const
Returns the feature's attributes.
virtual ~QgsHeatmapRenderer()
virtual bool renderFeature(QgsFeature &feature, QgsRenderContext &context, int layer=-1, bool selected=false, bool drawVertexMarker=false) override
Render a feature using this renderer in the given context.
QgsPoint asPoint() const
Return contents of the geometry as a point if wkbType is WKBPoint, otherwise returns [0...
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
static QgsVectorColorRampV2 * loadColorRamp(QDomElement &element)
virtual QString dump() const override
for debugging
static QgsFeatureRendererV2 * create(QDomElement &element)
virtual QList< QString > usedAttributes() override
Returns a set of attributes required for this renderer.
QList< QgsSymbolV2 * > QgsSymbolV2List
QDomNode appendChild(const QDomNode &newChild)
void setXMaximum(double x)
Set the maximum x value.
virtual void modifyRequestExtent(QgsRectangle &extent, QgsRenderContext &context) override
Allows for a renderer to modify the extent of a feature request prior to rendering.
QString attribute(const QString &name, const QString &defValue) const
static QDomElement saveColorRamp(const QString &name, QgsVectorColorRampV2 *ramp, QDomDocument &doc)
QStringList referencedColumns() const
Get list of columns referenced by the expression.
QVector< T > & fill(const T &value, int size)
const_iterator constEnd() const
The output shall be in pixels.
static bool isDefaultStack(QgsPaintEffect *effect)
Tests whether a paint effect matches the default effects stack.
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
const QgsGeometry * constGeometry() const
Gets a const pointer to the geometry object associated with this feature.
void setWeightExpression(const QString &expression)
Sets the expression used for weighting points when generating the heatmap.
static QString encodeMapUnitScale(const QgsMapUnitScale &mapUnitScale)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QgsPaintEffect * mPaintEffect
void setRadiusUnit(const QgsSymbolV2::OutputUnit unit)
Sets the units used for the heatmap's radius.
static double pixelSizeScaleFactor(const QgsRenderContext &c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale &scale=QgsMapUnitScale())
Returns scale factor painter units -> pixel dimensions.
double y() const
Get the y value of the point.
QgsMultiPoint asMultiPoint() const
Return contents of the geometry as a multi point if wkbType is WKBMultiPoint, otherwise an empty list...
QgsPoint transform(const QgsPoint &p) const
Transform the point from map (world) coordinates to device coordinates.
virtual QgsFeatureRendererV2 * clone() const =0
The output shall be in millimeters.
QString number(int n, int base)
void fill(uint pixelValue)
A renderer which draws points as a live heatmap.
void setRadiusMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale used for the heatmap's radius.
void setAttribute(const QString &name, const QString &value)
virtual QgsVectorColorRampV2 * clone() const =0
Creates a clone of the color ramp.
int toInt(bool *ok, int base) const
void setYMinimum(double y)
Set the minimum y value.
The output shall be in map unitx.
QPaintDevice * device() const
const QgsCoordinateTransform * coordinateTransform() const
virtual Q_DECL_DEPRECATED QgsSymbolV2List symbols()
For symbol levels.
virtual QDomElement save(QDomDocument &doc) override
store renderer info to XML element
virtual QgsSymbolV2 * symbolForFeature(QgsFeature &feature, QgsRenderContext &context) override
QGis::GeometryType type() const
Returns type of the geometry as a QGis::GeometryType.
virtual void startRender(QgsRenderContext &context, const QgsFields &fields) override
Needs to be called when a new render cycle is started.
A class to represent a point.
void setRenderQuality(const int quality)
Sets the render quality used for drawing the heatmap.
virtual QColor color(double value) const =0
Returns the color corresponding to a specified value.
int fieldNameIndex(const QString &fieldName) const
Look up field's index from name also looks up case-insensitive if there is no match otherwise...
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
QgsExpressionContext & expressionContext()
Gets the expression context.
void setRadius(const double radius)
Sets the radius for the heatmap.
double xMaximum() const
Get the x maximum value (right side of rectangle)
virtual QgsHeatmapRenderer * clone() const override
const T & at(int i) const
QgsFeatureRequest::OrderBy orderBy() const
Get the order in which features shall be processed by this renderer.
const_iterator constBegin() const
void copyRendererData(QgsFeatureRendererV2 *destRenderer) const
Clones generic renderer data to another renderer.
void setColorRamp(QgsVectorColorRampV2 *ramp)
Sets the color ramp to use for shading the heatmap.
Contains information about the context of a rendering operation.
void drawImage(const QRectF &target, const QImage &image, const QRectF &source, QFlags< Qt::ImageConversionFlag > flags)
const QgsMapToPixel & mapToPixel() const
QSet< T > & unite(const QSet< T > &other)
Struct for storing maximum and minimum scales for measurements in map units.
static QgsHeatmapRenderer * convertFromRenderer(const QgsFeatureRendererV2 *renderer)
void setYMaximum(double y)
Set the maximum y value.
QgsFeatureRequest::OrderBy mOrderBy
float toFloat(bool *ok) const
QDomElement firstChildElement(const QString &tagName) const
void CORE_EXPORT save(QDomElement &elem) const
Serialize to XML.
int count(const T &value) const
virtual void stopRender(QgsRenderContext &context) override
Needs to be called when a render cycle has finished to clean up.
double xMinimum() const
Get the x minimum value (left side of rectangle)
int transform(const QgsCoordinateTransform &ct)
Transform this geometry as described by CoordinateTransform ct.
QList< T > toList() const
double yMaximum() const
Get the y maximum value (top side of rectangle)
double toDouble(bool *ok) const
static QgsMapUnitScale decodeMapUnitScale(const QString &str)
QDomElement createElement(const QString &tagName)
Abstract base class for color ramps.
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
void setMaximumValue(const double value)
Sets the maximum value used for shading the heatmap.
QImage scaled(int width, int height, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const
double x() const
Get the x value of the point.
void setXMinimum(double x)
Set the minimum x value.
virtual bool saveProperties(QDomDocument &doc, QDomElement &element) const
Saves the current state of the effect to a DOM element.