QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 "qgsmaptool.h"
22 #include "qgspointxy.h"
23 #include "qgsunittypes.h"
24 
25 #include <QObject>
26 #include <QPointer>
27 #include "qgis_gui.h"
28 
29 class QgsRasterLayer;
30 class QgsVectorLayer;
31 class QgsVectorTileLayer;
32 class QgsMapLayer;
33 class QgsMapCanvas;
34 class QgsMeshLayer;
35 class QgsHighlight;
36 class QgsIdentifyMenu;
37 
48 class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
49 {
50  Q_OBJECT
51 
52  public:
53 
55  {
56  DefaultQgsSetting = -1,
60  LayerSelection
61  };
62  Q_ENUM( IdentifyMode )
63 
64  enum Type
65  {
66  VectorLayer = 1,
67  RasterLayer = 2,
68  MeshLayer = 4,
69  VectorTileLayer = 8,
70  AllLayers = VectorLayer | RasterLayer | MeshLayer | VectorTileLayer
71  };
72  Q_DECLARE_FLAGS( LayerType, Type )
73  Q_FLAG( LayerType )
74 
76  {
78  IdentifyResult() = default;
79 
80  IdentifyResult( QgsMapLayer *layer, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes )
81  : mLayer( layer ), mFeature( feature ), mDerivedAttributes( derivedAttributes ) {}
82 
83  IdentifyResult( QgsMapLayer *layer, const QString &label, const QMap< QString, QString > &attributes, const QMap< QString, QString > &derivedAttributes )
84  : mLayer( layer ), mLabel( label ), mAttributes( attributes ), mDerivedAttributes( derivedAttributes ) {}
85 
86  IdentifyResult( QgsMapLayer *layer, const QString &label, const QgsFields &fields, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes )
87  : mLayer( layer ), mLabel( label ), mFields( fields ), mFeature( feature ), mDerivedAttributes( derivedAttributes ) {}
88 
89  QgsMapLayer *mLayer = nullptr;
90  QString mLabel;
93  QMap< QString, QString > mAttributes;
94  QMap< QString, QString > mDerivedAttributes;
95  QMap< QString, QVariant > mParams;
96  };
97 
100 
101  ~QgsMapToolIdentify() override;
102 
103  Flags flags() const override { return QgsMapTool::AllowZoomRect; }
104  void canvasMoveEvent( QgsMapMouseEvent *e ) override;
105  void canvasPressEvent( QgsMapMouseEvent *e ) override;
106  void canvasReleaseEvent( QgsMapMouseEvent *e ) override;
107  void activate() override;
108  void deactivate() override;
109 
118  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, const QList<QgsMapLayer *> &layerList = QList<QgsMapLayer *>(), IdentifyMode mode = DefaultQgsSetting );
119 
130  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, LayerType layerType = AllLayers );
131 
133  QList<QgsMapToolIdentify::IdentifyResult> identify( const QgsGeometry &geometry, IdentifyMode mode, LayerType layerType );
135  QList<QgsMapToolIdentify::IdentifyResult> identify( const QgsGeometry &geometry, IdentifyMode mode, const QList<QgsMapLayer *> &layerList, LayerType layerType );
136 
137 
142  QgsIdentifyMenu *identifyMenu() {return mIdentifyMenu;}
143 
144  public slots:
145  void formatChanged( QgsRasterLayer *layer );
146 
147  signals:
148  void identifyProgress( int, int );
149  void identifyMessage( const QString & );
150  void changedRasterResults( QList<QgsMapToolIdentify::IdentifyResult> & );
151 
152  protected:
153 
165  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, const QList<QgsMapLayer *> &layerList, LayerType layerType = AllLayers );
166 
167  QgsIdentifyMenu *mIdentifyMenu = nullptr;
168 
170  bool identifyLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMapLayer *layer, const QgsPointXY &point, const QgsRectangle &viewExtent, double mapUnitsPerPixel, QgsMapToolIdentify::LayerType layerType = AllLayers );
171 
172  bool identifyRasterLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsRasterLayer *layer, QgsPointXY point, const QgsRectangle &viewExtent, double mapUnitsPerPixel );
173  bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsPointXY &point );
174 
181  bool identifyMeshLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMeshLayer *layer, const QgsPointXY &point );
182 
184  QMap< QString, QString > derivedAttributesForPoint( const QgsPoint &point );
185 
198  void setCanvasPropertiesOverrides( double searchRadiusMapUnits );
199 
205  void restoreCanvasPropertiesOverrides();
206 
207  private:
208 
209  bool identifyLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMapLayer *layer, const QgsGeometry &geometry, const QgsRectangle &viewExtent, double mapUnitsPerPixel, QgsMapToolIdentify::LayerType layerType = AllLayers );
210  bool identifyRasterLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsRasterLayer *layer, const QgsGeometry &geometry, const QgsRectangle &viewExtent, double mapUnitsPerPixel );
211  bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsGeometry &geometry );
212  bool identifyMeshLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMeshLayer *layer, const QgsGeometry &geometry );
213  bool identifyVectorTileLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorTileLayer *layer, const QgsGeometry &geometry );
214 
220  virtual QgsUnitTypes::DistanceUnit displayDistanceUnits() const;
221 
227  virtual QgsUnitTypes::AreaUnit displayAreaUnits() const;
228 
234  QString formatDistance( double distance ) const;
235 
241  QString formatArea( double area ) const;
242 
247  QString formatDistance( double distance, QgsUnitTypes::DistanceUnit unit ) const;
248 
253  QString formatArea( double area, QgsUnitTypes::AreaUnit unit ) const;
254 
255  QMap< QString, QString > featureDerivedAttributes( const QgsFeature &feature, QgsMapLayer *layer, const QgsPointXY &layerPoint = QgsPointXY() );
256 
260  void closestVertexAttributes( const QgsAbstractGeometry &geometry, QgsVertexId vId, QgsMapLayer *layer, QMap< QString, QString > &derivedAttributes );
261 
265  void closestPointAttributes( const QgsAbstractGeometry &geometry, const QgsPointXY &layerPoint, QMap< QString, QString > &derivedAttributes );
266 
267  QString formatCoordinate( const QgsPointXY &canvasPoint ) const;
268  QString formatXCoordinate( const QgsPointXY &canvasPoint ) const;
269  QString formatYCoordinate( const QgsPointXY &canvasPoint ) const;
270 
271  // Last geometry (point or polygon) in map CRS
272  QgsGeometry mLastGeometry;
273 
274  double mLastMapUnitsPerPixel;
275 
276  QgsRectangle mLastExtent;
277 
278  int mCoordinatePrecision;
279 
280  double mOverrideCanvasSearchRadius = -1;
281 };
282 
283 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapToolIdentify::LayerType )
284 
285 #endif
qgsfields.h
QgsMapToolIdentify::Type
Type
Definition: qgsmaptoolidentify.h:65
QgsVectorTileLayer
Implements a map layer that is dedicated to rendering of vector tiles.
Definition: qgsvectortilelayer.h:84
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:38
QgsMapToolIdentify::ActiveLayer
@ ActiveLayer
Definition: qgsmaptoolidentify.h:57
QgsMapToolIdentify::identifyMenu
QgsIdentifyMenu * identifyMenu()
Returns a pointer to the identify menu which will be used in layer selection mode this menu can also ...
Definition: qgsmaptoolidentify.h:142
QgsMapToolIdentify
Map tool for identifying features in layers.
Definition: qgsmaptoolidentify.h:49
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:45
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:85
QgsMapTool::deactivate
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:96
qgsfeature.h
qgsunittypes.h
QgsMapToolIdentify::IdentifyResult::mFields
QgsFields mFields
Definition: qgsmaptoolidentify.h:91
QgsMapToolIdentify::IdentifyResult::mLabel
QString mLabel
Definition: qgsmaptoolidentify.h:90
QgsUnitTypes::DistanceUnit
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:68
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsMapToolIdentify::TopDownStopAtFirst
@ TopDownStopAtFirst
Definition: qgsmaptoolidentify.h:58
QgsHighlight
A class for highlight features on the map.
Definition: qgshighlight.h:58
QgsMapToolIdentify::TopDownAll
@ TopDownAll
Definition: qgsmaptoolidentify.h:59
QgsMapToolIdentify::IdentifyResult::IdentifyResult
IdentifyResult(QgsMapLayer *layer, const QString &label, const QMap< QString, QString > &attributes, const QMap< QString, QString > &derivedAttributes)
Definition: qgsmaptoolidentify.h:83
QgsMapTool
Abstract base class for all map tools.
Definition: qgsmaptool.h:64
QgsMapToolIdentify::IdentifyResult::IdentifyResult
IdentifyResult(QgsMapLayer *layer, const QString &label, const QgsFields &fields, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes)
Definition: qgsmaptoolidentify.h:86
QgsMapToolIdentify::IdentifyMode
IdentifyMode
Definition: qgsmaptoolidentify.h:55
QgsMapToolIdentify::IdentifyResult::IdentifyResult
IdentifyResult()=default
Constructor for IdentifyResult.
QgsMapToolIdentify::identifyProgress
void identifyProgress(int, int)
QgsMapToolIdentify::changedRasterResults
void changedRasterResults(QList< QgsMapToolIdentify::IdentifyResult > &)
QgsMapToolIdentify::IdentifyResult::mFeature
QgsFeature mFeature
Definition: qgsmaptoolidentify.h:92
QgsMapToolIdentify::IdentifyResult::mAttributes
QMap< QString, QString > mAttributes
Definition: qgsmaptoolidentify.h:93
QgsMeshLayer
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:95
qgsmaptool.h
Q_DECLARE_OPERATORS_FOR_FLAGS
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.
QgsMapToolIdentify::flags
Flags flags() const override
Returns the flags for the map tool.
Definition: qgsmaptoolidentify.h:103
QgsUnitTypes::AreaUnit
AreaUnit
Units of area.
Definition: qgsunittypes.h:94
QgsRasterLayer
Represents a raster layer.
Definition: qgsrasterlayer.h:71
QgsMapTool::activate
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:80
QgsAbstractGeometry
Abstract base class for all geometries.
Definition: qgsabstractgeometry.h:74
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:44
QgsMapTool::canvasPressEvent
virtual void canvasPressEvent(QgsMapMouseEvent *e)
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:164
QgsMapMouseEvent
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
Definition: qgsmapmouseevent.h:36
QgsMapTool::canvasMoveEvent
virtual void canvasMoveEvent(QgsMapMouseEvent *e)
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:154
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsMapTool::canvasReleaseEvent
virtual void canvasReleaseEvent(QgsMapMouseEvent *e)
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:169
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
QgsVertexId
Utility class for identifying a unique vertex within a geometry.
Definition: qgsabstractgeometry.h:1059
QgsMapToolIdentify::identifyMessage
void identifyMessage(const QString &)
QgsMapToolIdentify::IdentifyResult::IdentifyResult
IdentifyResult(QgsMapLayer *layer, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes)
Definition: qgsmaptoolidentify.h:80
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsMapTool::AllowZoomRect
@ AllowZoomRect
Allow zooming by rectangle (by holding shift and dragging) while the tool is active.
Definition: qgsmaptool.h:95
QgsMapToolIdentify::IdentifyResult::mParams
QMap< QString, QVariant > mParams
Definition: qgsmaptoolidentify.h:95
QgsIdentifyMenu
The QgsIdentifyMenu class builds a menu to be used with identify results (.
Definition: qgsidentifymenu.h:49
qgspointxy.h
QgsMapToolIdentify::IdentifyResult
Definition: qgsmaptoolidentify.h:76
QgsMapToolIdentify::IdentifyResult::mDerivedAttributes
QMap< QString, QString > mDerivedAttributes
Definition: qgsmaptoolidentify.h:94