28 #include <QDomElement>
35 , mLabelAttributeName( labelAttributeName )
53 Q_UNUSED( drawVertexMarker )
84 label = getLabel( feature );
98 if ( intersectList.empty() )
113 double minDist =
mGroupLocations.value( minDistFeatureId ).distance( point );
114 for (
int i = 1; i < intersectList.count(); ++i )
117 double newDist =
mGroupLocations.value( candidateId ).distance( point );
118 if ( newDist < minDist )
121 minDistFeatureId = candidateId;
131 ( oldCenter.
y() * group.size() + point.
y() ) / ( group.size() + 1.0 ) );
142 void QgsPointDistanceRenderer::drawGroup(
const ClusteredGroup &group,
QgsRenderContext &context )
146 const auto constGroup = group;
147 for (
const GroupedFeature &f : constGroup )
149 groupMultiPoint->
addGeometry( f.feature.geometry().constGet()->clone() );
157 drawGroup( pt, context, group );
175 mRenderer->setLegendSymbolItem( key, symbol );
183 return mRenderer->legendSymbolItemsCheckable();
191 return mRenderer->legendSymbolItemChecked( key );
199 mRenderer->checkLegendSymbolItem( key, state );
221 QSet<QString> attributeList;
228 attributeList +=
mRenderer->usedAttributes( context );
230 return attributeList;
242 return Capabilities();
262 return mRenderer->symbolForFeature( feature, context );
269 return mRenderer->originalSymbolForFeature( feature, context );
278 return mRenderer->symbolsForFeature( feature, context );
285 return mRenderer->originalSymbolsForFeature( feature, context );
291 return QSet< QString >() << QString();
292 return mRenderer->legendKeysForFeature( feature, context );
301 return mRenderer->willRenderFeature( feature, context );
309 mRenderer->startRender( context, fields );
346 drawGroup( group, context );
370 return QgsRectangle( p.
x() - distance, p.
y() - distance, p.
x() + distance, p.
y() + distance );
373 void QgsPointDistanceRenderer::printGroupInfo()
const
377 QgsDebugMsgLevel(
"number of displacement groups:" + QString::number( nGroups ), 3 );
378 for (
int i = 0; i < nGroups; ++i )
380 QgsDebugMsgLevel(
"***************displacement group " + QString::number( i ), 3 );
382 for (
const GroupedFeature &feature : constAt )
390 QString QgsPointDistanceRenderer::getLabel(
const QgsFeature &feature )
const
410 p->setPen( labelPen );
416 pixelSizeFont.setPixelSize(
static_cast< int >( std::round( fontSizeInPixels ) ) );
417 QFont scaledFont = pixelSizeFont;
418 scaledFont.setPixelSize( pixelSizeFont.pixelSize() );
419 p->setFont( scaledFont );
421 QFontMetricsF fontMetrics( pixelSizeFont );
422 QPointF currentLabelShift;
424 QList<QPointF>::const_iterator labelPosIt = labelShifts.constBegin();
425 ClusteredGroup::const_iterator groupIt = group.constBegin();
427 for ( ; labelPosIt != labelShifts.constEnd() && groupIt != group.constEnd(); ++labelPosIt, ++groupIt )
429 currentLabelShift = *labelPosIt;
430 if ( currentLabelShift.x() < 0 )
432 currentLabelShift.setX( currentLabelShift.x() - fontMetrics.horizontalAdvance( groupIt->label ) );
434 if ( currentLabelShift.y() > 0 )
436 currentLabelShift.setY( currentLabelShift.y() + fontMetrics.ascent() );
439 QPointF drawingPoint( centerPoint + currentLabelShift );
441 p->translate( drawingPoint.x(), drawingPoint.y() );
442 p->drawText( QPointF( 0, 0 ), groupIt->label );
449 if ( group.size() > 1 )
453 ClusteredGroup::const_iterator groupIt = group.constBegin();
454 for ( ; groupIt != group.constEnd(); ++groupIt )
456 if ( !groupIt->symbol() )
459 if ( !groupColor.isValid() )
461 groupColor = groupIt->symbol()->color();
465 if ( groupColor != groupIt->symbol()->color() )
467 groupColor = QColor();
473 if ( groupColor.isValid() )
485 if ( !group.empty() )
488 clusterScope->
setFeature( group.at( 0 ).feature );
501 if ( symbolList.isEmpty() )
511 , isSelected( isSelected )
RAII class to pop scope from an expression context on destruction.
Single scope for storing variables and functions for use within a QgsExpressionContext.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the scope.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
static const QString EXPR_CLUSTER_SIZE
Inbuilt variable name for cluster size variable.
static const QString EXPR_CLUSTER_COLOR
Inbuilt variable name for cluster color variable.
static QgsFeatureRenderer * defaultRenderer(QgsWkbTypes::GeometryType geomType)
Returns a new renderer - used by default in vector layers.
virtual QString filter(const QgsFields &fields=QgsFields())
If a renderer does not require all the features this method may be overridden and return an expressio...
virtual void stopRender(QgsRenderContext &context)
Must be called when a render cycle has finished, to allow the renderer to clean up.
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)
Must be called when a new render cycle is started.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Container of fields for a vector layer.
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
A geometry is the spatial representation of a feature.
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
QPointF asQPointF() const SIP_HOLDGIL
Returns contents of the geometry as a QPointF if wkbType is WKBPoint, otherwise returns a null QPoint...
QgsGeometry centroid() const
Returns the center of mass of a geometry.
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.
void transformInPlace(double &x, double &y) const
Transforms device coordinates to map coordinates.
A marker symbol type, for rendering Point and MultiPoint geometries.
QgsMarkerSymbol * clone() const override
Returns a deep copy of this symbol.
Multi point geometry collection.
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
QMap< QgsFeatureId, QgsPointXY > mGroupLocations
Mapping of feature ID to approximate group location.
double mMinLabelScale
Maximum scale denominator for label display. A zero value indicates no scale limitation.
int mLabelIndex
Label attribute index (or -1 if none). This index is not stored, it is requested in the startRender()...
bool legendSymbolItemChecked(const QString &key) override
items of symbology items in legend is checked
QgsSpatialIndex * mSpatialIndex
Spatial index for fast lookup of nearby points.
QgsSymbolList symbols(QgsRenderContext &context) const override
Returns list of symbols used by the renderer.
QColor mLabelColor
Label text color.
QgsMapUnitScale mToleranceMapUnitScale
Map unit scale for distance tolerance.
QString filter(const QgsFields &fields=QgsFields()) override
If a renderer does not require all the features this method may be overridden and return an expressio...
QList< ClusteredGroup > mClusteredGroups
Groups of features that are considered clustered together.
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.
QMap< QgsFeatureId, int > mGroupIndex
Mapping of feature ID to the feature's group index.
void toSld(QDomDocument &doc, QDomElement &element, const QVariantMap &props=QVariantMap()) const override
used from subclasses to create SLD Rule elements following SLD v1.1 specs
QgsPointDistanceRenderer(const QString &rendererName, const QString &labelAttributeName=QString())
Constructor for QgsPointDistanceRenderer.
void stopRender(QgsRenderContext &context) override
Must be called when a render cycle has finished, to allow the renderer to clean up.
QgsSymbol * symbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const override
To be overridden.
QgsLegendSymbolList legendSymbolItems() const override
Returns a list of symbology items for the legend.
bool legendSymbolItemsCheckable() const override
items of symbology items in legend should be checkable
QgsSymbolList originalSymbolsForFeature(const QgsFeature &feature, QgsRenderContext &context) const override
Equivalent of originalSymbolsForFeature() call extended to support renderers that may use more symbol...
void startRender(QgsRenderContext &context, const QgsFields &fields) override
Must be called when a new render cycle is started.
void setEmbeddedRenderer(QgsFeatureRenderer *r) override
Sets an embedded renderer (subrenderer) for this feature renderer.
QgsFeatureRenderer::Capabilities capabilities() override
Returns details about internals of this renderer.
const QgsFeatureRenderer * embeddedRenderer() const override
Returns the current embedded renderer (subrenderer) for this feature renderer.
QString mLabelAttributeName
Attribute name for labeling. An empty string indicates that no labels should be rendered.
void checkLegendSymbolItem(const QString &key, bool state) override
item in symbology was checked
void drawLabels(QPointF centerPoint, QgsSymbolRenderContext &context, const QList< QPointF > &labelShifts, const ClusteredGroup &group)
Renders the labels for a group.
QgsSymbolList symbolsForFeature(const QgsFeature &feature, QgsRenderContext &context) const override
Returns list of symbols used for rendering the feature.
double mTolerance
Distance tolerance. Points that are closer together than this distance are considered clustered.
QSet< QString > usedAttributes(const QgsRenderContext &context) const override
Returns a list of attributes required by this renderer.
bool mDrawLabels
Whether labels should be drawn for points. This is set internally from startRender() depending on sca...
QFont mLabelFont
Label font.
std::unique_ptr< QgsFeatureRenderer > mRenderer
Embedded base renderer. This can be used for rendering individual, isolated points.
QgsUnitTypes::RenderUnit mToleranceUnit
Unit for distance tolerance.
QgsSymbol * originalSymbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const override
Returns symbol for feature.
void setLegendSymbolItem(const QString &key, QgsSymbol *symbol) override
Sets the symbol to be used for a legend symbol item.
QList< QgsPointDistanceRenderer::GroupedFeature > ClusteredGroup
A group of clustered points (ie features within the distance tolerance).
bool willRenderFeature(const QgsFeature &feature, QgsRenderContext &context) const override
Returns whether the renderer will render a feature or not.
bool filterNeedsGeometry() const override
Returns true if this renderer requires the geometry to apply the filter.
QSet< QString > legendKeysForFeature(const QgsFeature &feature, QgsRenderContext &context) const override
Returns legend keys matching a specified feature.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
A class to represent a 2D point.
A rectangle specified with double values.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
double rendererScale() const
Returns the renderer map scale.
QgsExpressionContext & expressionContext()
Gets the expression context.
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
double convertToMapUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to map units.
bool renderingStopped() const
Returns true if the rendering operation has been stopped and any ongoing rendering should be canceled...
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
Scoped object for saving and restoring a QPainter object's state.
A spatial index for QgsFeature objects.
QList< QgsFeatureId > intersects(const QgsRectangle &rectangle) const
Returns a list of features with a bounding box which intersects the specified rectangle.
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a feature to the index.
An interface for classes which can visit style entity (e.g.
static QString encodeColor(const QColor &color)
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
Abstract base class for all rendered symbols.
Helper functions for various unit types.
@ RenderPoints
Points (e.g., for font sizes)
Type
The WKB type describes the number of dimensions a geometry has.
static Type flatType(Type type) SIP_HOLDGIL
Returns the flat type for a WKB type.
#define FID_TO_STRING(fid)
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
QList< QgsLegendSymbolItem > QgsLegendSymbolList
#define QgsDebugMsgLevel(str, level)
QList< QgsSymbol * > QgsSymbolList
Single variable definition for use within a QgsExpressionContextScope.
Contains properties for a feature within a clustered group.
GroupedFeature(const QgsFeature &feature, QgsMarkerSymbol *symbol, bool isSelected, const QString &label=QString())
Constructor for GroupedFeature.