QGIS API Documentation
3.16.0-Hannover (43b64b13f3)
|
Go to the documentation of this file.
32 #include <QDomDocument>
33 #include <QDomElement>
48 mValues.resize( context.
painter()->device()->width() * context.
painter()->device()->height() / ( mRenderQuality * mRenderQuality ) );
50 mCalculatedMaxValue = 0;
51 mFeaturesRendered = 0;
52 mRadiusPixels = std::round( context.
convertToPainterUnits( mRadius, mRadiusUnit, mRadiusMapUnitScale ) / mRenderQuality );
53 mRadiusSquared = mRadiusPixels * mRadiusPixels;
66 mWeightAttrNum = fields.
lookupField( mWeightExpressionString );
67 if ( mWeightAttrNum == -1 )
69 mWeightExpression.reset(
new QgsExpression( mWeightExpressionString ) );
73 initializeValues( context );
95 Q_UNUSED( drawVertexMarker )
109 if ( !mWeightExpressionString.isEmpty() )
112 if ( mWeightAttrNum == -1 )
114 Q_ASSERT( mWeightExpression.get() );
120 value = attrs.value( mWeightAttrNum );
123 double evalWeight = value.toDouble( &ok );
130 int width = context.
painter()->device()->width() / mRenderQuality;
131 int height = context.
painter()->device()->height() / mRenderQuality;
145 for ( QgsMultiPointXY::const_iterator pointIt = multiPoint.constBegin(); pointIt != multiPoint.constEnd(); ++pointIt )
148 int pointX = pixel.
x() / mRenderQuality;
149 int pointY = pixel.
y() / mRenderQuality;
150 for (
int x = std::max( pointX - mRadiusPixels, 0 ); x < std::min( pointX + mRadiusPixels, width ); ++x )
155 for (
int y = std::max( pointY - mRadiusPixels, 0 ); y < std::min( pointY + mRadiusPixels, height ); ++y )
157 int index = y * width + x;
158 if ( index >= mValues.count() )
162 double distanceSquared = std::pow( pointX - x, 2.0 ) + std::pow( pointY - y, 2.0 );
163 if ( distanceSquared > mRadiusSquared )
168 double score = weight * quarticKernel( std::sqrt( distanceSquared ), mRadiusPixels );
169 double value = mValues.at( index ) + score;
170 if ( value > mCalculatedMaxValue )
172 mCalculatedMaxValue = value;
174 mValues[ index ] = value;
182 if ( mFeaturesRendered % 200 == 0 )
184 renderImage( context );
191 double QgsHeatmapRenderer::uniformKernel(
const double distance,
const int bandwidth )
const
194 Q_UNUSED( bandwidth )
198 double QgsHeatmapRenderer::quarticKernel(
const double distance,
const int bandwidth )
const
200 return std::pow( 1. - std::pow( distance /
static_cast< double >( bandwidth ), 2 ), 2 );
203 double QgsHeatmapRenderer::triweightKernel(
const double distance,
const int bandwidth )
const
205 return std::pow( 1. - std::pow( distance /
static_cast< double >( bandwidth ), 2 ), 3 );
208 double QgsHeatmapRenderer::epanechnikovKernel(
const double distance,
const int bandwidth )
const
210 return ( 1. - std::pow( distance /
static_cast< double >( bandwidth ), 2 ) );
213 double QgsHeatmapRenderer::triangularKernel(
const double distance,
const int bandwidth )
const
215 return ( 1. - ( distance /
static_cast< double >( bandwidth ) ) );
222 renderImage( context );
223 mWeightExpression.reset();
233 QImage image( context.
painter()->device()->width() / mRenderQuality,
234 context.
painter()->device()->height() / mRenderQuality,
235 QImage::Format_ARGB32 );
236 image.fill( Qt::transparent );
238 double scaleMax = mExplicitMax > 0 ? mExplicitMax : mCalculatedMaxValue;
243 for (
int heightIndex = 0; heightIndex < image.height(); ++heightIndex )
248 QRgb *scanLine =
reinterpret_cast< QRgb *
>( image.scanLine( heightIndex ) );
249 for (
int widthIndex = 0; widthIndex < image.width(); ++widthIndex )
252 pixVal = mValues.at( idx ) > 0 ? std::min( ( mValues.at( idx ) / scaleMax ), 1.0 ) : 0;
255 pixColor = mGradientRamp->
color( pixVal );
257 scanLine[widthIndex] = pixColor.rgba();
262 if ( mRenderQuality > 1 )
264 QImage resized = image.scaled( context.
painter()->device()->width(),
265 context.
painter()->device()->height() );
266 context.
painter()->drawImage( 0, 0, resized );
270 context.
painter()->drawImage( 0, 0, image );
276 return QStringLiteral(
"[HEATMAP]" );
301 double extension = context.
convertToMapUnits( mRadius, mRadiusUnit, mRadiusMapUnitScale );
312 r->
setRadius( element.attribute( QStringLiteral(
"radius" ), QStringLiteral(
"50.0" ) ).toFloat() );
315 r->
setMaximumValue( element.attribute( QStringLiteral(
"max_value" ), QStringLiteral(
"0.0" ) ).toFloat() );
316 r->
setRenderQuality( element.attribute( QStringLiteral(
"quality" ), QStringLiteral(
"0" ) ).toInt() );
319 QDomElement sourceColorRampElem = element.firstChildElement( QStringLiteral(
"colorramp" ) );
320 if ( !sourceColorRampElem.isNull() && sourceColorRampElem.attribute( QStringLiteral(
"name" ) ) == QLatin1String(
"[source]" ) )
331 rendererElem.setAttribute( QStringLiteral(
"type" ), QStringLiteral(
"heatmapRenderer" ) );
332 rendererElem.setAttribute( QStringLiteral(
"radius" ), QString::number( mRadius ) );
333 rendererElem.setAttribute( QStringLiteral(
"radius_unit" ), QString::number( mRadiusUnit ) );
335 rendererElem.setAttribute( QStringLiteral(
"max_value" ), QString::number( mExplicitMax ) );
336 rendererElem.setAttribute( QStringLiteral(
"quality" ), QString::number( mRenderQuality ) );
337 rendererElem.setAttribute( QStringLiteral(
"weight_expression" ), mWeightExpressionString );
341 rendererElem.appendChild( colorRampElem );
343 rendererElem.setAttribute( QStringLiteral(
"forceraster" ), (
mForceRaster ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) ) );
350 QDomElement
orderBy = doc.createElement( QStringLiteral(
"orderby" ) );
352 rendererElem.appendChild(
orderBy );
354 rendererElem.setAttribute( QStringLiteral(
"enableorderby" ), (
mOrderByEnabled ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) ) );
372 QSet<QString> attributes;
378 attributes << mWeightExpressionString;
389 if ( renderer->
type() == QLatin1String(
"heatmapRenderer" ) )
412 delete mGradientRamp;
413 mGradientRamp = ramp;
void copyRendererData(QgsFeatureRenderer *destRenderer) const
Clones generic renderer data to another renderer.
Abstract base class for color ramps.
OperationResult transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection direction=QgsCoordinateTransform::ForwardTransform, bool transformZ=false) SIP_THROW(QgsCsException)
Transforms this geometry as described by the coordinate transform ct.
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
QgsPaintEffect * mPaintEffect
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
RenderUnit
Rendering size units.
QgsExpressionContext & expressionContext()
Gets the expression context.
The class is used as a container of context for various read/write operations on other objects.
void startRender(QgsRenderContext &context, const QgsFields &fields) override
Must be called when a new render cycle is started.
QSet< QString > referencedColumns() const
Gets list of columns referenced by the expression.
static QString encodeMapUnitScale(const QgsMapUnitScale &mapUnitScale)
void CORE_EXPORT save(QDomElement &elem) const
Serialize to XML.
A color ramp entity for QgsStyle databases.
Container of fields for a vector layer.
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
double convertToMapUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to map units.
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) override
store renderer info to XML element
Contains information about the context of a rendering operation.
A renderer which draws points as a live heatmap.
void stopRender(QgsRenderContext &context) override
Must be called when a render cycle has finished, to allow the renderer to clean up.
An interface for classes which can visit style entity (e.g.
Abstract base class for all rendered symbols.
void setRadius(const double radius)
Sets the radius for the heatmap.
void setRadiusUnit(const QgsUnitTypes::RenderUnit unit)
Sets the units used for the heatmap's radius.
virtual bool saveProperties(QDomDocument &doc, QDomElement &element) const
Saves the current state of the effect to a DOM element.
A rectangle specified with double values.
Contains information relating to the style entity currently being visited.
QVector< QgsPointXY > QgsMultiPointXY
A collection of QgsPoints that share a common collection of attributes.
void setMaximumValue(const double value)
Sets the maximum value used for shading the heatmap.
virtual void stopRender(QgsRenderContext &context)
Must be called when a render cycle has finished, to allow the renderer to clean up.
QgsFeatureRequest::OrderBy orderBy() const
Gets the order in which features shall be processed by this renderer.
~QgsHeatmapRenderer() override
bool isMultipart() const SIP_HOLDGIL
Returns true if WKB of the geometry is of WKBMulti* type.
bool renderingStopped() const
Returns true if the rendering operation has been stopped and any ongoing rendering should be canceled...
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
static bool isDefaultStack(QgsPaintEffect *effect)
Tests whether a paint effect matches the default effects stack.
QString dump() const override
Returns debug information about this renderer.
void setWeightExpression(const QString &expression)
Sets the expression used for weighting points when generating the heatmap.
QList< QgsSymbol * > QgsSymbolList
QSet< QString > usedAttributes(const QgsRenderContext &context) const override
Returns a list of attributes required by this renderer.
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
void setRenderQuality(const int quality)
Sets the render quality used for drawing the heatmap.
QgsSymbol * symbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const override
void modifyRequestExtent(QgsRectangle &extent, QgsRenderContext &context) override
Allows for a renderer to modify the extent of a feature request prior to rendering.
static QDomElement saveColorRamp(const QString &name, QgsColorRamp *ramp, QDomDocument &doc)
Encodes a color ramp's settings to an XML element.
#define RENDERER_TAG_NAME
void setXMinimum(double x) SIP_HOLDGIL
Set the minimum x value.
bool renderFeature(const QgsFeature &feature, QgsRenderContext &context, int layer=-1, bool selected=false, bool drawVertexMarker=false) override SIP_THROW(QgsCsException)
Render a feature using this renderer in the given context.
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
void setXMaximum(double x) SIP_HOLDGIL
Set the maximum x value.
QgsPointXY transform(const QgsPointXY &p) const
Transform the point from map (world) coordinates to device coordinates.
void setColorRamp(QgsColorRamp *ramp)
Sets the color ramp to use for shading the heatmap.
virtual QgsColorRamp * clone() const =0
Creates a clone of the color ramp.
static QgsColorRamp * loadColorRamp(QDomElement &element)
Creates a color ramp from the settings encoded in an XML element.
static QgsHeatmapRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
QgsFeatureRequest::OrderBy mOrderBy
A class to represent a 2D point.
QgsHeatmapRenderer * clone() const override
Create a deep copy of this renderer.
void setYMaximum(double y) SIP_HOLDGIL
Set the maximum y value.
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
void setYMinimum(double y) SIP_HOLDGIL
Set the minimum y value.
A geometry is the spatial representation of a feature.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
virtual QColor color(double value) const =0
Returns the color corresponding to a specified value.
QgsMultiPointXY asMultiPoint() const
Returns the contents of the geometry as a multi-point.
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
void setRadiusMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale used for the heatmap's radius.
QgsSymbolList symbols(QgsRenderContext &context) const override
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
QPainter * painter()
Returns the destination QPainter for the render operation.
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Class for parsing and evaluation of expressions (formerly called "search strings").
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)
Must be called when a new render cycle is started.
QgsWkbTypes::GeometryType type
static QgsMapUnitScale decodeMapUnitScale(const QString &str)
static QgsFeatureRenderer * create(QDomElement &element, const QgsReadWriteContext &context)
Creates a new heatmap renderer instance from XML.