QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
qgsmapcanvasutils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmapcanvasutils.cpp
3  -------------------
4  begin : June 2020
5  copyright : (C) 2020 by Nyall Dawson
6  email : nyall dot dawson 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 #include "qgsmapcanvasutils.h"
17 #include "qgsmapcanvas.h"
18 #include "qgsvectorlayer.h"
20 
21 long QgsMapCanvasUtils::zoomToMatchingFeatures( QgsMapCanvas *canvas, QgsVectorLayer *layer, const QString &filter )
22 {
24 
25  const QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( filter )
26  .setExpressionContext( context )
27  .setNoAttributes();
28 
29  QgsFeatureIterator features = layer->getFeatures( request );
30 
31  QgsRectangle bbox;
32  bbox.setMinimal();
33  QgsFeature feat;
34  int featureCount = 0;
35  while ( features.nextFeature( feat ) )
36  {
37  const QgsGeometry geom = feat.geometry();
38  if ( geom.isNull() || geom.constGet()->isEmpty() )
39  continue;
40 
41  const QgsRectangle r = canvas->mapSettings().layerExtentToOutputExtent( layer, geom.boundingBox() );
42  bbox.combineExtentWith( r );
43  featureCount++;
44  }
45  features.close();
46 
47  if ( featureCount > 0 )
48  {
49  canvas->zoomToFeatureExtent( bbox );
50  }
51  return featureCount;
52 }
53 
54 long QgsMapCanvasUtils::flashMatchingFeatures( QgsMapCanvas *canvas, QgsVectorLayer *layer, const QString &filter )
55 {
57 
58  const QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( filter )
59  .setExpressionContext( context )
60  .setNoAttributes();
61 
62  QgsFeatureIterator features = layer->getFeatures( request );
63  QgsFeature feat;
64  QList< QgsGeometry > geoms;
65  while ( features.nextFeature( feat ) )
66  {
67  if ( feat.hasGeometry() )
68  geoms << feat.geometry();
69  }
70 
71  if ( !geoms.empty() )
72  {
73  canvas->flashGeometries( geoms, layer->crs() );
74  }
75  return geoms.size();
76 }
virtual bool isEmpty() const
Returns true if the geometry is empty.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsGeometry geometry
Definition: qgsfeature.h:67
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:223
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:125
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Q_GADGET bool isNull
Definition: qgsgeometry.h:127
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
static long zoomToMatchingFeatures(QgsMapCanvas *canvas, QgsVectorLayer *layer, const QString &filter)
Zooms a map canvas to features from the specified layer which match the given filter expression strin...
static long flashMatchingFeatures(QgsMapCanvas *canvas, QgsVectorLayer *layer, const QString &filter)
Flashes features from the specified layer which match the given filter expression string with a map c...
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
void flashGeometries(const QList< QgsGeometry > &geometries, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem(), const QColor &startColor=QColor(255, 0, 0, 255), const QColor &endColor=QColor(255, 0, 0, 0), int flashes=3, int duration=500)
Causes a set of geometries to flash within the canvas.
void zoomToFeatureExtent(QgsRectangle &rect)
Zooms to feature extent.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:79
QgsRectangle layerExtentToOutputExtent(const QgsMapLayer *layer, QgsRectangle extent) const
transform bounding box from layer's CRS to output CRS
A rectangle specified with double values.
Definition: qgsrectangle.h:42
void setMinimal() SIP_HOLDGIL
Set a rectangle so that min corner is at max and max corner is at min.
Definition: qgsrectangle.h:172
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
Definition: qgsrectangle.h:391
Represents a vector layer which manages a vector based data sets.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.