QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 class QgsFeatureRenderer;
42 
53 class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
54 {
55  Q_OBJECT
56 
57  public:
58 
60  {
61  DefaultQgsSetting = -1,
65  LayerSelection
66  };
67  Q_ENUM( IdentifyMode )
68 
69  enum Type
70  {
73  MeshLayer = 4,
77  };
78  Q_DECLARE_FLAGS( LayerType, Type )
79  Q_FLAG( LayerType )
80 
82  {
84  IdentifyResult() = default;
85 
86  IdentifyResult( QgsMapLayer *layer, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes )
87  : mLayer( layer ), mFeature( feature ), mDerivedAttributes( derivedAttributes ) {}
88 
89  IdentifyResult( QgsMapLayer *layer, const QString &label, const QMap< QString, QString > &attributes, const QMap< QString, QString > &derivedAttributes )
90  : mLayer( layer ), mLabel( label ), mAttributes( attributes ), mDerivedAttributes( derivedAttributes ) {}
91 
92  IdentifyResult( QgsMapLayer *layer, const QString &label, const QgsFields &fields, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes )
93  : mLayer( layer ), mLabel( label ), mFields( fields ), mFeature( feature ), mDerivedAttributes( derivedAttributes ) {}
94 
95  QgsMapLayer *mLayer = nullptr;
96  QString mLabel;
99  QMap< QString, QString > mAttributes;
100  QMap< QString, QString > mDerivedAttributes;
101  QMap< QString, QVariant > mParams;
102  };
103 
105  QgsMapToolIdentify( QgsMapCanvas *canvas );
106 
107  ~QgsMapToolIdentify() override;
108 
109  Flags flags() const override { return QgsMapTool::AllowZoomRect; }
110  void canvasMoveEvent( QgsMapMouseEvent *e ) override;
111  void canvasPressEvent( QgsMapMouseEvent *e ) override;
112  void canvasReleaseEvent( QgsMapMouseEvent *e ) override;
113  void activate() override;
114  void deactivate() override;
115 
125  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, const QList<QgsMapLayer *> &layerList = QList<QgsMapLayer *>(), IdentifyMode mode = DefaultQgsSetting, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
126 
138  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, LayerType layerType = AllLayers, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
139 
141  QList<QgsMapToolIdentify::IdentifyResult> identify( const QgsGeometry &geometry, IdentifyMode mode, LayerType layerType, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
143  QList<QgsMapToolIdentify::IdentifyResult> identify( const QgsGeometry &geometry, IdentifyMode mode, const QList<QgsMapLayer *> &layerList, LayerType layerType, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
144 
145 
150  QgsIdentifyMenu *identifyMenu() { return mIdentifyMenu; }
151 
157  static void fromPointCloudIdentificationToIdentifyResults( QgsPointCloudLayer *layer, const QVector<QVariantMap> &identified, QList<QgsMapToolIdentify::IdentifyResult> &results ) SIP_SKIP;
158 
165  void fromElevationProfileLayerIdentificationToIdentifyResults( QgsMapLayer *layer, const QVector<QVariantMap> &identified, QList<QgsMapToolIdentify::IdentifyResult> &results ) SIP_SKIP;
166 
167  public slots:
168  void formatChanged( QgsRasterLayer *layer );
169 
170  signals:
171  void identifyProgress( int, int );
172  void identifyMessage( const QString & );
173  void changedRasterResults( QList<QgsMapToolIdentify::IdentifyResult> & );
174 
175  protected:
176 
189  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, const QList<QgsMapLayer *> &layerList, LayerType layerType = AllLayers, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
190 
191  QgsIdentifyMenu *mIdentifyMenu = nullptr;
192 
194  bool identifyLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMapLayer *layer, const QgsPointXY &point, const QgsRectangle &viewExtent, double mapUnitsPerPixel, QgsMapToolIdentify::LayerType layerType = AllLayers, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
195 
205  bool identifyRasterLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsRasterLayer *layer, QgsPointXY point, const QgsRectangle &viewExtent, double mapUnitsPerPixel, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
206 
214  bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsPointXY &point, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
215 
222  bool identifyMeshLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMeshLayer *layer, const QgsPointXY &point, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
223 
225  QMap< QString, QString > derivedAttributesForPoint( const QgsPoint &point );
226 
239  void setCanvasPropertiesOverrides( double searchRadiusMapUnits );
240 
246  void restoreCanvasPropertiesOverrides();
247 
248  private:
249 
250  bool identifyLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMapLayer *layer, const QgsGeometry &geometry, const QgsRectangle &viewExtent, double mapUnitsPerPixel, QgsMapToolIdentify::LayerType layerType = AllLayers, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
251  bool identifyRasterLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsRasterLayer *layer, const QgsGeometry &geometry, const QgsRectangle &viewExtent, double mapUnitsPerPixel, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
252  bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsGeometry &geometry, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
253  int identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsFeatureList &features, QgsFeatureRenderer *renderer, const QMap< QString, QString > &commonDerivedAttributes, const std::function< QMap< QString, QString > ( const QgsFeature & ) > &derivedAttributes, QgsRenderContext &context );
254  bool identifyMeshLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMeshLayer *layer, const QgsGeometry &geometry, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
255  bool identifyVectorTileLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorTileLayer *layer, const QgsGeometry &geometry, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
256  bool identifyPointCloudLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsPointCloudLayer *layer, const QgsGeometry &geometry, const QgsIdentifyContext &identifyContext = QgsIdentifyContext() );
257 
263  virtual QgsUnitTypes::DistanceUnit displayDistanceUnits() const;
264 
270  virtual QgsUnitTypes::AreaUnit displayAreaUnits() const;
271 
277  QString formatDistance( double distance ) const;
278 
284  QString formatArea( double area ) const;
285 
290  QString formatDistance( double distance, QgsUnitTypes::DistanceUnit unit ) const;
291 
296  QString formatArea( double area, QgsUnitTypes::AreaUnit unit ) const;
297 
298  QMap< QString, QString > featureDerivedAttributes( const QgsFeature &feature, QgsMapLayer *layer, const QgsPointXY &layerPoint = QgsPointXY() );
299 
303  void closestVertexAttributes( const QgsAbstractGeometry &geometry, QgsVertexId vId, QgsMapLayer *layer, QMap< QString, QString > &derivedAttributes );
304 
308  void closestPointAttributes( const QgsAbstractGeometry &geometry, const QgsPointXY &layerPoint, QMap< QString, QString > &derivedAttributes );
309 
310  QString formatCoordinate( const QgsPointXY &canvasPoint ) const;
311  QString formatXCoordinate( const QgsPointXY &canvasPoint ) const;
312  QString formatYCoordinate( const QgsPointXY &canvasPoint ) const;
313 
314  // Last geometry (point or polygon) in map CRS
315  QgsGeometry mLastGeometry;
316 
317  double mLastMapUnitsPerPixel;
318 
319  QgsRectangle mLastExtent;
320 
321  int mCoordinatePrecision;
322 
323  double mOverrideCanvasSearchRadius = -1;
324 };
325 
326 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapToolIdentify::LayerType )
327 
328 #endif
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
qgsfields.h
QgsMapToolIdentify::Type
Type
Definition: qgsmaptoolidentify.h:69
QgsVectorTileLayer
Implements a map layer that is dedicated to rendering of vector tiles. Vector tiles compared to "ordi...
Definition: qgsvectortilelayer.h:84
QgsMapLayerType::MeshLayer
@ MeshLayer
Mesh layer. Added in QGIS 3.2.
QgsMapLayerType::VectorLayer
@ VectorLayer
Vector layer.
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:48
QgsMapToolIdentify::ActiveLayer
@ ActiveLayer
Definition: qgsmaptoolidentify.h:62
QgsPointCloudLayerElevationProperties
Point cloud layer specific subclass of QgsMapLayerElevationProperties.
Definition: qgspointcloudlayerelevationproperties.h:33
QgsPointCloudLayer
Represents a map layer supporting display of point clouds.
Definition: qgspointcloudlayer.h:45
qgsidentifycontext.h
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:150
QgsMapToolIdentify
Map tool for identifying features in layers.
Definition: qgsmaptoolidentify.h:53
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
QgsMapTool::deactivate
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:110
qgsfeature.h
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
qgsunittypes.h
QgsMapToolIdentify::IdentifyResult::mFields
QgsFields mFields
Definition: qgsmaptoolidentify.h:97
QgsMapToolIdentify::IdentifyResult::mLabel
QString mLabel
Definition: qgsmaptoolidentify.h:96
QgsUnitTypes::DistanceUnit
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:67
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsMapToolIdentify::TopDownStopAtFirst
@ TopDownStopAtFirst
Definition: qgsmaptoolidentify.h:63
QgsHighlight
A class for highlight features on the map.
Definition: qgshighlight.h:61
QgsMapToolIdentify::TopDownAll
@ TopDownAll
Definition: qgsmaptoolidentify.h:64
QgsMapToolIdentify::IdentifyResult::IdentifyResult
IdentifyResult(QgsMapLayer *layer, const QString &label, const QMap< QString, QString > &attributes, const QMap< QString, QString > &derivedAttributes)
Definition: qgsmaptoolidentify.h:89
QgsMapTool
Abstract base class for all map tools. Map tools are user interactive tools for manipulating the map ...
Definition: qgsmaptool.h:70
QgsMapToolIdentify::IdentifyResult::IdentifyResult
IdentifyResult(QgsMapLayer *layer, const QString &label, const QgsFields &fields, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes)
Definition: qgsmaptoolidentify.h:92
QgsMapToolIdentify::IdentifyMode
IdentifyMode
Definition: qgsmaptoolidentify.h:59
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsMapToolIdentify::IdentifyResult::mFeature
QgsFeature mFeature
Definition: qgsmaptoolidentify.h:98
QgsMapToolIdentify::IdentifyResult::mAttributes
QMap< QString, QString > mAttributes
Definition: qgsmaptoolidentify.h:99
QgsMeshLayer
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:98
QgsMapLayerType::RasterLayer
@ RasterLayer
Raster layer.
qgsmaptool.h
QgsIdentifyContext
Identify contexts are used to encapsulate the settings to be used to perform an identify action.
Definition: qgsidentifycontext.h:31
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.
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
QgsMapToolIdentify::flags
Flags flags() const override
Returns the flags for the map tool.
Definition: qgsmaptoolidentify.h:109
QgsUnitTypes::AreaUnit
AreaUnit
Units of area.
Definition: qgsunittypes.h:93
QgsRasterLayer
Represents a raster layer.
Definition: qgsrasterlayer.h:76
QgsMapTool::activate
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:94
QgsAbstractGeometry
Abstract base class for all geometries.
Definition: qgsabstractgeometry.h:79
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:58
QgsMapTool::canvasPressEvent
virtual void canvasPressEvent(QgsMapMouseEvent *e)
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:178
QgsMapMouseEvent
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas....
Definition: qgsmapmouseevent.h:35
QgsMapTool::canvasMoveEvent
virtual void canvasMoveEvent(QgsMapMouseEvent *e)
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:168
QgsFeatureRenderer
Definition: qgsrenderer.h:101
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:391
QgsMapTool::canvasReleaseEvent
virtual void canvasReleaseEvent(QgsMapMouseEvent *e)
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:183
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsVertexId
Utility class for identifying a unique vertex within a geometry.
Definition: qgsvertexid.h:30
QgsMapLayerType::VectorTileLayer
@ VectorTileLayer
Vector tile layer. Added in QGIS 3.14.
QgsMapToolIdentify::IdentifyResult::IdentifyResult
IdentifyResult(QgsMapLayer *layer, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes)
Definition: qgsmaptoolidentify.h:86
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsMapTool::AllowZoomRect
@ AllowZoomRect
Allow zooming by rectangle (by holding shift and dragging) while the tool is active.
Definition: qgsmaptool.h:114
QgsMapToolIdentify::IdentifyResult::mParams
QMap< QString, QVariant > mParams
Definition: qgsmaptoolidentify.h:101
QgsIdentifyMenu
The QgsIdentifyMenu class builds a menu to be used with identify results (.
Definition: qgsidentifymenu.h:49
QgsMapLayerType::PointCloudLayer
@ PointCloudLayer
Point cloud layer. Added in QGIS 3.18.
qgspointxy.h
QgsMapToolIdentify::IdentifyResult
Definition: qgsmaptoolidentify.h:81
QgsMapToolIdentify::IdentifyResult::mDerivedAttributes
QMap< QString, QString > mDerivedAttributes
Definition: qgsmaptoolidentify.h:100