QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
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  QgsGeometry geom = feat.geometry();
38  if ( geom.isNull() || geom.constGet()->isEmpty() )
39  continue;
40 
41  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 
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 }
QgsVectorLayer::getFeatures
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Definition: qgsvectorlayer.cpp:993
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
QgsMapLayer::crs
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:89
qgsexpressioncontextutils.h
QgsRectangle::combineExtentWith
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
Definition: qgsrectangle.h:359
qgsmapcanvas.h
QgsMapCanvas::mapSettings
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Definition: qgsmapcanvas.cpp:391
qgsmapcanvasutils.h
QgsGeometry::isNull
Q_GADGET bool isNull
Definition: qgsgeometry.h:126
QgsMapCanvas::flashGeometries
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.
Definition: qgsmapcanvas.cpp:1471
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:85
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:67
QgsRectangle::setMinimal
void setMinimal() SIP_HOLDGIL
Set a rectangle so that min corner is at max and max corner is at min.
Definition: qgsrectangle.h:151
QgsMapSettings::layerExtentToOutputExtent
QgsRectangle layerExtentToOutputExtent(const QgsMapLayer *layer, QgsRectangle extent) const
transform bounding box from layer's CRS to output CRS
Definition: qgsmapsettings.cpp:433
QgsExpressionContextUtils::globalProjectLayerScopes
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Definition: qgsexpressioncontextutils.cpp:307
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsFeatureRequest::setExpressionContext
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
Definition: qgsfeaturerequest.cpp:144
QgsMapCanvasUtils::zoomToMatchingFeatures
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...
Definition: qgsmapcanvasutils.cpp:21
QgsFeatureRequest::setFilterExpression
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
Definition: qgsfeaturerequest.cpp:124
QgsMapCanvasUtils::flashMatchingFeatures
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...
Definition: qgsmapcanvasutils.cpp:54
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:76
QgsFeatureIterator::close
bool close()
Definition: qgsfeatureiterator.h:387
QgsFeatureRequest::setNoAttributes
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
Definition: qgsfeaturerequest.cpp:192
QgsAbstractGeometry::isEmpty
virtual bool isEmpty() const
Returns true if the geometry is empty.
Definition: qgsabstractgeometry.cpp:298
QgsGeometry::constGet
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Definition: qgsgeometry.cpp:128
QgsMapCanvas::zoomToFeatureExtent
void zoomToFeatureExtent(QgsRectangle &rect)
Zooms to feature extent.
Definition: qgsmapcanvas.cpp:1319
qgsvectorlayer.h
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:374
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
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:199
QgsGeometry::boundingBox
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Definition: qgsgeometry.cpp:996
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:265