QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgspointdistancerenderer.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgspointdistancerenderer.cpp
3  ----------------------------
4  begin : January 26, 2010
5  copyright : (C) 2010 by Marco Hugentobler
6  email : marco at hugis dot net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSPOINTDISTANCERENDERER_H
19 #define QGSPOINTDISTANCERENDERER_H
20 
21 #include "qgis_core.h"
22 #include "qgis.h"
23 #include "qgsrenderer.h"
24 #include <QFont>
25 
26 class QgsSpatialIndex;
27 
39 {
40  public:
41 
44  {
45 
53  GroupedFeature( const QgsFeature &feature, QgsMarkerSymbol *symbol SIP_TRANSFER, bool isSelected, const QString &label = QString() )
54  : feature( feature )
55  , isSelected( isSelected )
56  , label( label )
57  , mSymbol( symbol )
58  {}
59 
62 
64  QgsMarkerSymbol *symbol() const { return mSymbol.get(); }
65 
67  bool isSelected;
68 
70  QString label;
71 
72  private:
73  std::shared_ptr< QgsMarkerSymbol > mSymbol;
74  };
75 
77  typedef QList< QgsPointDistanceRenderer::GroupedFeature > ClusteredGroup;
78 
84  QgsPointDistanceRenderer( const QString &rendererName, const QString &labelAttributeName = QString() );
85 
86  void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const override;
87  bool renderFeature( const QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) override SIP_THROW( QgsCsException );
88  QSet<QString> usedAttributes( const QgsRenderContext &context ) const override;
89  bool filterNeedsGeometry() const override;
90  QgsFeatureRenderer::Capabilities capabilities() override;
91  QgsSymbolList symbols( QgsRenderContext &context ) const override;
92  QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
93  QgsSymbol *originalSymbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
94  QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
95  QgsSymbolList originalSymbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
96  QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
97  bool willRenderFeature( const QgsFeature &feature, QgsRenderContext &context ) const override;
98  void startRender( QgsRenderContext &context, const QgsFields &fields ) override;
99  void stopRender( QgsRenderContext &context ) override;
100  QgsLegendSymbolList legendSymbolItems() const override;
101  void setEmbeddedRenderer( QgsFeatureRenderer *r SIP_TRANSFER ) override;
102  const QgsFeatureRenderer *embeddedRenderer() const override;
103  void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER ) override;
104  bool legendSymbolItemsCheckable() const override;
105  bool legendSymbolItemChecked( const QString &key ) override;
106  void checkLegendSymbolItem( const QString &key, bool state ) override;
107  QString filter( const QgsFields &fields = QgsFields() ) override;
108  bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
109 
118  void setLabelAttributeName( const QString &name ) { mLabelAttributeName = name; }
119 
128  QString labelAttributeName() const { return mLabelAttributeName; }
129 
137  void setLabelFont( const QFont &font ) { mLabelFont = font; }
138 
145  QFont labelFont() const { return mLabelFont;}
146 
153  void setMinimumLabelScale( double scale ) { mMinLabelScale = scale; }
154 
161  double minimumLabelScale() const { return mMinLabelScale; }
162 
170  void setLabelColor( const QColor &color ) { mLabelColor = color;}
171 
178  QColor labelColor() const { return mLabelColor; }
179 
187  void setTolerance( double distance ) { mTolerance = distance; }
188 
195  double tolerance() const { return mTolerance; }
196 
204  void setToleranceUnit( QgsUnitTypes::RenderUnit unit ) { mToleranceUnit = unit; }
205 
212  QgsUnitTypes::RenderUnit toleranceUnit() const { return mToleranceUnit; }
213 
221  void setToleranceMapUnitScale( const QgsMapUnitScale &scale ) { mToleranceMapUnitScale = scale; }
222 
229  const QgsMapUnitScale &toleranceMapUnitScale() const { return mToleranceMapUnitScale; }
230 
231  protected:
232 
234  std::unique_ptr< QgsFeatureRenderer > mRenderer;
235 
238 
241 
243  double mTolerance;
248 
250  QFont mLabelFont;
252  QColor mLabelColor;
256  double mMinLabelScale = 0;
257 
259  QList<ClusteredGroup> mClusteredGroups;
260 
262  QMap<QgsFeatureId, int> mGroupIndex;
263 
265  QMap<QgsFeatureId, QgsPointXY > mGroupLocations;
266 
268  QgsSpatialIndex *mSpatialIndex = nullptr;
269 
278  void drawLabels( QPointF centerPoint, QgsSymbolRenderContext &context, const QList<QPointF> &labelShifts, const ClusteredGroup &group );
279 
280  private:
281 
288  virtual void drawGroup( QPointF centerPoint, QgsRenderContext &context, const ClusteredGroup &group ) = 0 SIP_FORCE;
289 
291  QgsRectangle searchRect( const QgsPointXY &p, double distance ) const;
292 
294  void printGroupInfo() const;
295 
297  QString getLabel( const QgsFeature &feature ) const;
298 
300  void drawGroup( const ClusteredGroup &group, QgsRenderContext &context );
301 
307  QgsMarkerSymbol *firstSymbolForFeature( const QgsFeature &feature, QgsRenderContext &context );
308 
314  QgsExpressionContextScope *createGroupScope( const ClusteredGroup &group ) const;
315 
316 };
317 
318 #endif // QGSPOINTDISTANCERENDERER_H
QgsPointDistanceRenderer::labelAttributeName
QString labelAttributeName() const
Returns the attribute name used for labeling points, or an empty string if no labeling will be done b...
Definition: qgspointdistancerenderer.h:128
QgsUnitTypes::RenderUnit
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:166
QgsPointDistanceRenderer::mGroupLocations
QMap< QgsFeatureId, QgsPointXY > mGroupLocations
Mapping of feature ID to approximate group location.
Definition: qgspointdistancerenderer.h:265
QgsPointDistanceRenderer::mToleranceUnit
QgsUnitTypes::RenderUnit mToleranceUnit
Unit for distance tolerance.
Definition: qgspointdistancerenderer.h:245
QgsPointDistanceRenderer::mLabelAttributeName
QString mLabelAttributeName
Attribute name for labeling. An empty string indicates that no labels should be rendered.
Definition: qgspointdistancerenderer.h:237
QgsFields
Definition: qgsfields.h:44
qgis.h
QgsRenderContext
Definition: qgsrendercontext.h:57
QgsStyleEntityVisitorInterface
Definition: qgsstyleentityvisitor.h:33
QgsSymbol
Definition: qgssymbol.h:63
QgsPointDistanceRenderer::mLabelFont
QFont mLabelFont
Label font.
Definition: qgspointdistancerenderer.h:250
QgsFeatureRenderer::toSld
virtual void toSld(QDomDocument &doc, QDomElement &element, const QgsStringMap &props=QgsStringMap()) const
used from subclasses to create SLD Rule elements following SLD v1.1 specs
Definition: qgsrenderer.h:312
QgsPointDistanceRenderer::toleranceMapUnitScale
const QgsMapUnitScale & toleranceMapUnitScale() const
Returns the map unit scale object for the distance tolerance.
Definition: qgspointdistancerenderer.h:229
QgsRectangle
Definition: qgsrectangle.h:41
QgsSymbolRenderContext
Definition: qgssymbol.h:681
QgsPointDistanceRenderer::mRenderer
std::unique_ptr< QgsFeatureRenderer > mRenderer
Embedded base renderer. This can be used for rendering individual, isolated points.
Definition: qgspointdistancerenderer.h:234
QgsPointDistanceRenderer::mClusteredGroups
QList< ClusteredGroup > mClusteredGroups
Groups of features that are considered clustered together.
Definition: qgspointdistancerenderer.h:259
QgsPointDistanceRenderer::toleranceUnit
QgsUnitTypes::RenderUnit toleranceUnit() const
Returns the units for the tolerance distance.
Definition: qgspointdistancerenderer.h:212
QgsCsException
Definition: qgsexception.h:65
QgsPointDistanceRenderer::setLabelColor
void setLabelColor(const QColor &color)
Sets the color to use for for labeling points.
Definition: qgspointdistancerenderer.h:170
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:184
QgsPointDistanceRenderer::mLabelIndex
int mLabelIndex
Label attribute index (or -1 if none). This index is not stored, it is requested in the startRender()...
Definition: qgspointdistancerenderer.h:240
QgsSymbolList
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:45
QgsMarkerSymbol
Definition: qgssymbol.h:917
QgsPointDistanceRenderer::mLabelColor
QColor mLabelColor
Label text color.
Definition: qgspointdistancerenderer.h:252
QgsPointDistanceRenderer
Definition: qgspointdistancerenderer.h:38
QgsPointDistanceRenderer::GroupedFeature::symbol
QgsMarkerSymbol * symbol() const
Base symbol for rendering feature.
Definition: qgspointdistancerenderer.h:64
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsSpatialIndex
Definition: qgsspatialindex.h:67
QgsPointDistanceRenderer::mToleranceMapUnitScale
QgsMapUnitScale mToleranceMapUnitScale
Map unit scale for distance tolerance.
Definition: qgspointdistancerenderer.h:247
QgsMapUnitScale
Struct for storing maximum and minimum scales for measurements in map units.
Definition: qgsmapunitscale.h:37
qgsrenderer.h
QgsPointDistanceRenderer::labelColor
QColor labelColor() const
Returns the color used for for labeling points.
Definition: qgspointdistancerenderer.h:178
QgsPointDistanceRenderer::setToleranceMapUnitScale
void setToleranceMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale object for the distance tolerance.
Definition: qgspointdistancerenderer.h:221
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext....
Definition: qgsexpressioncontext.h:111
QgsPointDistanceRenderer::tolerance
double tolerance() const
Returns the tolerance distance for grouping points.
Definition: qgspointdistancerenderer.h:195
QgsPointDistanceRenderer::ClusteredGroup
QList< QgsPointDistanceRenderer::GroupedFeature > ClusteredGroup
A group of clustered points (ie features within the distance tolerance).
Definition: qgspointdistancerenderer.h:77
QgsPointXY
Definition: qgspointxy.h:43
QgsPointDistanceRenderer::setToleranceUnit
void setToleranceUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the tolerance distance.
Definition: qgspointdistancerenderer.h:204
QgsStringMap
QMap< QString, QString > QgsStringMap
Definition: qgis.h:714
QgsFeatureRenderer
Definition: qgsrenderer.h:102
SIP_FORCE
#define SIP_FORCE
Definition: qgis_sip.h:131
QgsPointDistanceRenderer::GroupedFeature::label
QString label
Optional label text.
Definition: qgspointdistancerenderer.h:70
QgsPointDistanceRenderer::mDrawLabels
bool mDrawLabels
Whether labels should be drawn for points. This is set internally from startRender() depending on sca...
Definition: qgspointdistancerenderer.h:254
QgsFeatureRenderer::renderFeature
virtual bool renderFeature(const QgsFeature &feature, QgsRenderContext &context, int layer=-1, bool selected=false, bool drawVertexMarker=false) SIP_THROW(QgsCsException)
Render a feature using this renderer in the given context.
Definition: qgsrenderer.cpp:119
QgsPointDistanceRenderer::setLabelFont
void setLabelFont(const QFont &font)
Sets the font used for labeling points.
Definition: qgspointdistancerenderer.h:137
QgsPointDistanceRenderer::minimumLabelScale
double minimumLabelScale() const
Returns the minimum map scale (i.e.
Definition: qgspointdistancerenderer.h:161
QgsPointDistanceRenderer::labelFont
QFont labelFont() const
Returns the font used for labeling points.
Definition: qgspointdistancerenderer.h:145
QgsPointDistanceRenderer::setMinimumLabelScale
void setMinimumLabelScale(double scale)
Sets the minimum map scale (i.e.
Definition: qgspointdistancerenderer.h:153
QgsPointDistanceRenderer::setTolerance
void setTolerance(double distance)
Sets the tolerance distance for grouping points.
Definition: qgspointdistancerenderer.h:187
QgsPointDistanceRenderer::GroupedFeature::GroupedFeature
GroupedFeature(const QgsFeature &feature, QgsMarkerSymbol *symbol, bool isSelected, const QString &label=QString())
Constructor for GroupedFeature.
Definition: qgspointdistancerenderer.h:53
QgsFeature
Definition: qgsfeature.h:55
QgsLegendSymbolList
QList< QgsLegendSymbolItem > QgsLegendSymbolList
Definition: qgslegendsymbolitem.h:144
QgsPointDistanceRenderer::mGroupIndex
QMap< QgsFeatureId, int > mGroupIndex
Mapping of feature ID to the feature's group index.
Definition: qgspointdistancerenderer.h:262
QgsPointDistanceRenderer::GroupedFeature::feature
QgsFeature feature
Feature.
Definition: qgspointdistancerenderer.h:61
QgsPointDistanceRenderer::mTolerance
double mTolerance
Distance tolerance. Points that are closer together than this distance are considered clustered.
Definition: qgspointdistancerenderer.h:243
QgsPointDistanceRenderer::GroupedFeature::isSelected
bool isSelected
True if feature is selected and should be rendered in a selected state.
Definition: qgspointdistancerenderer.h:67
QgsPointDistanceRenderer::GroupedFeature
Contains properties for a feature within a clustered group.
Definition: qgspointdistancerenderer.h:43