QGIS API Documentation  3.6.0-Noosa (5873452)
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 QgsMapLayer;
32 class QgsMapCanvas;
33 class QgsMeshLayer;
34 class QgsHighlight;
35 class QgsIdentifyMenu;
36 class QgsDistanceArea;
37 
47 class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
48 {
49  Q_OBJECT
50 
51  public:
52 
54  {
55  DefaultQgsSetting = -1,
59  LayerSelection
60  };
61  Q_ENUM( IdentifyMode )
62 
63  enum Type
64  {
65  VectorLayer = 1,
66  RasterLayer = 2,
67  MeshLayer = 4,
68  AllLayers = VectorLayer | RasterLayer | MeshLayer
69  };
70  Q_DECLARE_FLAGS( LayerType, Type )
71  Q_FLAG( LayerType )
72 
74  {
76  IdentifyResult() = default;
77 
78  IdentifyResult( QgsMapLayer *layer, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes )
79  : mLayer( layer ), mFeature( feature ), mDerivedAttributes( derivedAttributes ) {}
80 
81  IdentifyResult( QgsMapLayer *layer, const QString &label, const QMap< QString, QString > &attributes, const QMap< QString, QString > &derivedAttributes )
82  : mLayer( layer ), mLabel( label ), mAttributes( attributes ), mDerivedAttributes( derivedAttributes ) {}
83 
84  IdentifyResult( QgsMapLayer *layer, const QString &label, const QgsFields &fields, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes )
85  : mLayer( layer ), mLabel( label ), mFields( fields ), mFeature( feature ), mDerivedAttributes( derivedAttributes ) {}
86 
87  QgsMapLayer *mLayer = nullptr;
88  QString mLabel;
91  QMap< QString, QString > mAttributes;
92  QMap< QString, QString > mDerivedAttributes;
93  QMap< QString, QVariant > mParams;
94  };
95 
98 
99  ~QgsMapToolIdentify() override;
100 
101  Flags flags() const override { return QgsMapTool::AllowZoomRect; }
102  void canvasMoveEvent( QgsMapMouseEvent *e ) override;
103  void canvasPressEvent( QgsMapMouseEvent *e ) override;
104  void canvasReleaseEvent( QgsMapMouseEvent *e ) override;
105  void activate() override;
106  void deactivate() override;
107 
115  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, const QList<QgsMapLayer *> &layerList = QList<QgsMapLayer *>(), IdentifyMode mode = DefaultQgsSetting );
116 
127  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, LayerType layerType = AllLayers );
128 
130  QList<QgsMapToolIdentify::IdentifyResult> identify( const QgsGeometry &geometry, IdentifyMode mode, LayerType layerType );
132  QList<QgsMapToolIdentify::IdentifyResult> identify( const QgsGeometry &geometry, IdentifyMode mode, const QList<QgsMapLayer *> &layerList, LayerType layerType );
133 
134 
139  QgsIdentifyMenu *identifyMenu() {return mIdentifyMenu;}
140 
141  public slots:
142  void formatChanged( QgsRasterLayer *layer );
143 
144  signals:
145  void identifyProgress( int, int );
146  void identifyMessage( const QString & );
147  void changedRasterResults( QList<QgsMapToolIdentify::IdentifyResult> & );
148 
149  protected:
150 
162  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, const QList<QgsMapLayer *> &layerList, LayerType layerType = AllLayers );
163 
164  QgsIdentifyMenu *mIdentifyMenu = nullptr;
165 
167  bool identifyLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMapLayer *layer, const QgsPointXY &point, const QgsRectangle &viewExtent, double mapUnitsPerPixel, QgsMapToolIdentify::LayerType layerType = AllLayers );
168 
169  bool identifyRasterLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsRasterLayer *layer, QgsPointXY point, const QgsRectangle &viewExtent, double mapUnitsPerPixel );
170  bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsPointXY &point );
171 
178  bool identifyMeshLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMeshLayer *layer, const QgsPointXY &point );
179 
181  QMap< QString, QString > derivedAttributesForPoint( const QgsPoint &point );
182 
195  void setCanvasPropertiesOverrides( double searchRadiusMapUnits );
196 
202  void restoreCanvasPropertiesOverrides();
203 
204  private:
205 
206  bool identifyLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMapLayer *layer, const QgsGeometry &geometry, const QgsRectangle &viewExtent, double mapUnitsPerPixel, QgsMapToolIdentify::LayerType layerType = AllLayers );
207  bool identifyRasterLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsRasterLayer *layer, const QgsGeometry &geometry, const QgsRectangle &viewExtent, double mapUnitsPerPixel );
208  bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsGeometry &geometry );
209  bool identifyMeshLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMeshLayer *layer, const QgsGeometry &geometry );
210 
216  virtual QgsUnitTypes::DistanceUnit displayDistanceUnits() const;
217 
223  virtual QgsUnitTypes::AreaUnit displayAreaUnits() const;
224 
230  QString formatDistance( double distance ) const;
231 
237  QString formatArea( double area ) const;
238 
243  QString formatDistance( double distance, QgsUnitTypes::DistanceUnit unit ) const;
244 
249  QString formatArea( double area, QgsUnitTypes::AreaUnit unit ) const;
250 
251  QMap< QString, QString > featureDerivedAttributes( const QgsFeature &feature, QgsMapLayer *layer, const QgsPointXY &layerPoint = QgsPointXY() );
252 
256  void closestVertexAttributes( const QgsAbstractGeometry &geometry, QgsVertexId vId, QgsMapLayer *layer, QMap< QString, QString > &derivedAttributes );
257 
261  void closestPointAttributes( const QgsAbstractGeometry &geometry, const QgsPointXY &layerPoint, QMap< QString, QString > &derivedAttributes );
262 
263  QString formatCoordinate( const QgsPointXY &canvasPoint ) const;
264  QString formatXCoordinate( const QgsPointXY &canvasPoint ) const;
265  QString formatYCoordinate( const QgsPointXY &canvasPoint ) const;
266 
267  // Last geometry (point or polygon) in map CRS
268  QgsGeometry mLastGeometry;
269 
270  double mLastMapUnitsPerPixel;
271 
272  QgsRectangle mLastExtent;
273 
274  int mCoordinatePrecision;
275 
276  double mOverrideCanvasSearchRadius = -1;
277 };
278 
279 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapToolIdentify::LayerType )
280 
281 #endif
IdentifyResult(QgsMapLayer *layer, const QString &label, const QgsFields &fields, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes)
IdentifyResult(QgsMapLayer *layer, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes)
A rectangle specified with double values.
Definition: qgsrectangle.h:41
Base class for all map layer types.
Definition: qgsmaplayer.h:64
virtual void canvasMoveEvent(QgsMapMouseEvent *e)
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:156
QMap< QString, QString > mAttributes
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
A class to represent a 2D point.
Definition: qgspointxy.h:43
Allow zooming by rectangle (by holding shift and dragging) while the tool is active.
Definition: qgsmaptool.h:94
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
Container of fields for a vector layer.
Definition: qgsfields.h:42
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:106
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:73
IdentifyResult(QgsMapLayer *layer, const QString &label, const QMap< QString, QString > &attributes, const QMap< QString, QString > &derivedAttributes)
virtual void canvasPressEvent(QgsMapMouseEvent *e)
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:166
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:82
Map tool for identifying features in layers.
Utility class for identifying a unique vertex within a geometry.
A class for highlight features on the map.
Definition: qgshighlight.h:49
The QgsIdentifyMenu class builds a menu to be used with identify results (.
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:98
Abstract base class for all geometries.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:54
Abstract base class for all map tools.
Definition: qgsmaptool.h:62
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
QMap< QString, QVariant > mParams
Flags flags() const override
Returns the flags for the map tool.
QgsIdentifyMenu * identifyMenu()
Returns a pointer to the identify menu which will be used in layer selection mode this menu can also ...
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:89
Represents a vector layer which manages a vector based data sets.
AreaUnit
Units of area.
Definition: qgsunittypes.h:80
QMap< QString, QString > mDerivedAttributes
virtual void canvasReleaseEvent(QgsMapMouseEvent *e)
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:171