QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
qgsmaptoolidentify.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaptoolidentify.h - map tool for identifying features
3  ---------------------
4  begin : January 2006
5  copyright : (C) 2006 by Martin Dobias
6  email : wonder.sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSMAPTOOLIDENTIFY_H
17 #define QGSMAPTOOLIDENTIFY_H
18 
19 #include "qgsfeature.h"
20 #include "qgsfields.h"
21 #include "qgsidentifycontext.h"
22 #include "qgsmaptool.h"
23 #include "qgspointxy.h"
24 #include "qgsunittypes.h"
25 
26 #include <QObject>
27 #include <QPointer>
28 #include "qgis_gui.h"
29 
30 class QgsRasterLayer;
31 class QgsVectorLayer;
32 class QgsVectorTileLayer;
33 class QgsMapLayer;
34 class QgsMapCanvas;
35 class QgsMeshLayer;
36 class QgsHighlight;
37 class QgsIdentifyMenu;
38 class QgsPointCloudLayer;
40 
51 class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
52 {
53  Q_OBJECT
54 
55  public:
56 
58  {
59  DefaultQgsSetting = -1,
63  LayerSelection
64  };
65  Q_ENUM( IdentifyMode )
66 
67  enum Type
68  {
71  MeshLayer = 4,
75  };
76  Q_DECLARE_FLAGS( LayerType, Type )
77  Q_FLAG( LayerType )
78 
80  {
82  IdentifyResult() = default;
83 
84  IdentifyResult( QgsMapLayer *layer, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes )
85  : mLayer( layer ), mFeature( feature ), mDerivedAttributes( derivedAttributes ) {}
86 
87  IdentifyResult( QgsMapLayer *layer, const QString &label, const QMap< QString, QString > &attributes, const QMap< QString, QString > &derivedAttributes )
88  : mLayer( layer ), mLabel( label ), mAttributes( attributes ), mDerivedAttributes( derivedAttributes ) {}
89 
90  IdentifyResult( QgsMapLayer *layer, const QString &label, const QgsFields &fields, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes )
91  : mLayer( layer ), mLabel( label ), mFields( fields ), mFeature( feature ), mDerivedAttributes( derivedAttributes ) {}
92 
93  QgsMapLayer *mLayer = nullptr;
94  QString mLabel;
97  QMap< QString, QString > mAttributes;
98  QMap< QString, QString > mDerivedAttributes;
99  QMap< QString, QVariant > mParams;
100  };
101 
103  QgsMapToolIdentify( QgsMapCanvas *canvas );
104 
105  ~QgsMapToolIdentify() override;
106 
107  Flags flags() const override { return QgsMapTool::AllowZoomRect; }
108  void canvasMoveEvent( QgsMapMouseEvent *e ) override;
109  void canvasPressEvent( QgsMapMouseEvent *e ) override;
110  void canvasReleaseEvent( QgsMapMouseEvent *e ) override;
111  void activate() override;
112  void deactivate() override;
113 
123  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, const QList<QgsMapLayer *> &layerList = QList<QgsMapLayer *>(), IdentifyMode mode = DefaultQgsSetting, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
124 
136  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, LayerType layerType = AllLayers, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
137 
139  QList<QgsMapToolIdentify::IdentifyResult> identify( const QgsGeometry &geometry, IdentifyMode mode, LayerType layerType, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
141  QList<QgsMapToolIdentify::IdentifyResult> identify( const QgsGeometry &geometry, IdentifyMode mode, const QList<QgsMapLayer *> &layerList, LayerType layerType, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
142 
143 
148  QgsIdentifyMenu *identifyMenu() { return mIdentifyMenu; }
149 
155  static void fromPointCloudIdentificationToIdentifyResults( QgsPointCloudLayer *layer, const QVector<QVariantMap> &identified, QList<QgsMapToolIdentify::IdentifyResult> &results ) SIP_SKIP;
156 
157  public slots:
158  void formatChanged( QgsRasterLayer *layer );
159 
160  signals:
161  void identifyProgress( int, int );
162  void identifyMessage( const QString & );
163  void changedRasterResults( QList<QgsMapToolIdentify::IdentifyResult> & );
164 
165  protected:
166 
179  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, const QList<QgsMapLayer *> &layerList, LayerType layerType = AllLayers, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
180 
181  QgsIdentifyMenu *mIdentifyMenu = nullptr;
182 
184  bool identifyLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMapLayer *layer, const QgsPointXY &point, const QgsRectangle &viewExtent, double mapUnitsPerPixel, QgsMapToolIdentify::LayerType layerType = AllLayers, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
185 
195  bool identifyRasterLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsRasterLayer *layer, QgsPointXY point, const QgsRectangle &viewExtent, double mapUnitsPerPixel, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
196 
204  bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsPointXY &point, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
205 
212  bool identifyMeshLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMeshLayer *layer, const QgsPointXY &point, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
213 
215  QMap< QString, QString > derivedAttributesForPoint( const QgsPoint &point );
216 
229  void setCanvasPropertiesOverrides( double searchRadiusMapUnits );
230 
236  void restoreCanvasPropertiesOverrides();
237 
238  private:
239 
240  bool identifyLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMapLayer *layer, const QgsGeometry &geometry, const QgsRectangle &viewExtent, double mapUnitsPerPixel, QgsMapToolIdentify::LayerType layerType = AllLayers, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
241  bool identifyRasterLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsRasterLayer *layer, const QgsGeometry &geometry, const QgsRectangle &viewExtent, double mapUnitsPerPixel, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
242  bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsGeometry &geometry, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
243  bool identifyMeshLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMeshLayer *layer, const QgsGeometry &geometry, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
244  bool identifyVectorTileLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorTileLayer *layer, const QgsGeometry &geometry, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
245  bool identifyPointCloudLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsPointCloudLayer *layer, const QgsGeometry &geometry, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
246 
252  virtual QgsUnitTypes::DistanceUnit displayDistanceUnits() const;
253 
259  virtual QgsUnitTypes::AreaUnit displayAreaUnits() const;
260 
266  QString formatDistance( double distance ) const;
267 
273  QString formatArea( double area ) const;
274 
279  QString formatDistance( double distance, QgsUnitTypes::DistanceUnit unit ) const;
280 
285  QString formatArea( double area, QgsUnitTypes::AreaUnit unit ) const;
286 
287  QMap< QString, QString > featureDerivedAttributes( const QgsFeature &feature, QgsMapLayer *layer, const QgsPointXY &layerPoint = QgsPointXY() );
288 
292  void closestVertexAttributes( const QgsAbstractGeometry &geometry, QgsVertexId vId, QgsMapLayer *layer, QMap< QString, QString > &derivedAttributes );
293 
297  void closestPointAttributes( const QgsAbstractGeometry &geometry, const QgsPointXY &layerPoint, QMap< QString, QString > &derivedAttributes );
298 
299  QString formatCoordinate( const QgsPointXY &canvasPoint ) const;
300  QString formatXCoordinate( const QgsPointXY &canvasPoint ) const;
301  QString formatYCoordinate( const QgsPointXY &canvasPoint ) const;
302 
303  // Last geometry (point or polygon) in map CRS
304  QgsGeometry mLastGeometry;
305 
306  double mLastMapUnitsPerPixel;
307 
308  QgsRectangle mLastExtent;
309 
310  int mCoordinatePrecision;
311 
312  double mOverrideCanvasSearchRadius = -1;
313 };
314 
315 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapToolIdentify::LayerType )
316 
317 #endif
Abstract base class for all geometries.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Container of fields for a vector layer.
Definition: qgsfields.h:45
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:125
A class for highlight features on the map.
Definition: qgshighlight.h:62
Identify contexts are used to encapsulate the settings to be used to perform an identify action.
The QgsIdentifyMenu class builds a menu to be used with identify results (.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
Base class for all map layer types.
Definition: qgsmaplayer.h:73
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
Map tool for identifying features in layers.
void changedRasterResults(QList< QgsMapToolIdentify::IdentifyResult > &)
void identifyProgress(int, int)
void identifyMessage(const QString &)
QgsIdentifyMenu * identifyMenu()
Returns a pointer to the identify menu which will be used in layer selection mode this menu can also ...
Flags flags() const override
Returns the flags for the map tool.
Abstract base class for all map tools.
Definition: qgsmaptool.h:71
virtual void canvasPressEvent(QgsMapMouseEvent *e)
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:178
virtual void canvasMoveEvent(QgsMapMouseEvent *e)
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:168
virtual void canvasReleaseEvent(QgsMapMouseEvent *e)
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:183
@ AllowZoomRect
Allow zooming by rectangle (by holding shift and dragging) while the tool is active.
Definition: qgsmaptool.h:114
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:94
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:110
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:97
Point cloud layer specific subclass of QgsMapLayerElevationProperties.
Represents a map layer supporting display of point clouds.
A class to represent a 2D point.
Definition: qgspointxy.h:59
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
Represents a raster layer.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:68
AreaUnit
Units of area.
Definition: qgsunittypes.h:94
Represents a vector layer which manages a vector based data sets.
Implements a map layer that is dedicated to rendering of vector tiles.
@ PointCloudLayer
Added in 3.18.
@ MeshLayer
Added in 3.2.
@ VectorTileLayer
Added in 3.14.
#define SIP_SKIP
Definition: qgis_sip.h:126
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
QMap< QString, QString > mDerivedAttributes
IdentifyResult(QgsMapLayer *layer, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes)
IdentifyResult(QgsMapLayer *layer, const QString &label, const QgsFields &fields, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes)
QMap< QString, QString > mAttributes
QMap< QString, QVariant > mParams
IdentifyResult(QgsMapLayer *layer, const QString &label, const QMap< QString, QString > &attributes, const QMap< QString, QString > &derivedAttributes)
IdentifyResult()=default
Constructor for IdentifyResult.
Utility class for identifying a unique vertex within a geometry.
Definition: qgsvertexid.h:31