QGIS API Documentation  3.27.0-Master (597e8eebd4)
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
Abstract base class for all geometries.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
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:90
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:99
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
Contains information about the context of a rendering operation.
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
Point cloud layer. Added in QGIS 3.18.
@ MeshLayer
Mesh layer. Added in QGIS 3.2.
@ VectorLayer
Vector layer.
@ RasterLayer
Raster layer.
@ VectorTileLayer
Vector tile layer. Added in QGIS 3.14.
#define SIP_SKIP
Definition: qgis_sip.h:126
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
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