QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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"
21 
22 long QgsMapCanvasUtils::zoomToMatchingFeatures( QgsMapCanvas *canvas, QgsVectorLayer *layer, const QString &filter )
23 {
25 
26  const QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( filter )
27  .setExpressionContext( context )
28  .setNoAttributes();
29 
30  QgsFeatureIterator features = layer->getFeatures( request );
31 
32  QgsRectangle bbox;
33  bbox.setMinimal();
34  QgsFeature feat;
35  int featureCount = 0;
36  while ( features.nextFeature( feat ) )
37  {
38  const QgsGeometry geom = feat.geometry();
39  if ( geom.isNull() || geom.constGet()->isEmpty() )
40  continue;
41 
42  const QgsRectangle r = canvas->mapSettings().layerExtentToOutputExtent( layer, geom.boundingBox() );
43  bbox.combineExtentWith( r );
44  featureCount++;
45  }
46  features.close();
47 
48  if ( featureCount > 0 )
49  {
50  canvas->zoomToFeatureExtent( bbox );
51  }
52  return featureCount;
53 }
54 
55 long QgsMapCanvasUtils::flashMatchingFeatures( QgsMapCanvas *canvas, QgsVectorLayer *layer, const QString &filter )
56 {
58 
59  const QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( filter )
60  .setExpressionContext( context )
61  .setNoAttributes();
62 
63  QgsFeatureIterator features = layer->getFeatures( request );
64  QgsFeature feat;
65  QList< QgsGeometry > geoms;
66  while ( features.nextFeature( feat ) )
67  {
68  if ( feat.hasGeometry() )
69  geoms << feat.geometry();
70  }
71 
72  if ( !geoms.empty() )
73  {
74  canvas->flashGeometries( geoms, layer->crs() );
75  }
76  return geoms.size();
77 }
78 
80 {
81  if ( canvas->mapSettings().isTemporal() )
82  {
83  if ( !layer->temporalProperties()->isVisibleInTemporalRange( canvas->temporalRange() ) )
84  return QStringLiteral( "FALSE" );
85 
86  QgsVectorLayerTemporalContext temporalContext;
87  temporalContext.setLayer( layer );
88  return qobject_cast< const QgsVectorLayerTemporalProperties * >( layer->temporalProperties() )->createFilterString( temporalContext, canvas->temporalRange() );
89  }
90  else
91  {
92  return QString();
93  }
94 }
QgsVectorLayer::getFeatures
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Definition: qgsvectorlayer.cpp:1052
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsMapLayer::crs
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:79
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:391
qgsmapcanvas.h
QgsMapCanvas::mapSettings
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Definition: qgsmapcanvas.cpp:437
QgsTemporalRangeObject::isTemporal
bool isTemporal() const
Returns true if the object's temporal range is enabled, and the object will be filtered when renderin...
Definition: qgstemporalrangeobject.cpp:30
qgsmapcanvasutils.h
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:1876
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
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:172
QgsMapSettings::layerExtentToOutputExtent
QgsRectangle layerExtentToOutputExtent(const QgsMapLayer *layer, QgsRectangle extent) const
transform bounding box from layer's CRS to output CRS
Definition: qgsmapsettings.cpp:528
QgsExpressionContextUtils::globalProjectLayerScopes
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Definition: qgsexpressioncontextutils.cpp:377
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsFeatureRequest::setExpressionContext
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
Definition: qgsfeaturerequest.cpp:187
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:22
QgsFeatureRequest::setFilterExpression
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
Definition: qgsfeaturerequest.cpp:167
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:55
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:83
qgsvectorlayertemporalproperties.h
QgsVectorLayer::temporalProperties
QgsMapLayerTemporalProperties * temporalProperties() override
Returns the layer's temporal properties.
Definition: qgsvectorlayer.cpp:686
QgsFeatureIterator::close
bool close()
Definition: qgsfeatureiterator.h:412
QgsVectorLayerTemporalContext::setLayer
void setLayer(QgsVectorLayer *layer)
Sets the associated layer.
Definition: qgsvectorlayertemporalproperties.cpp:704
QgsMapLayerTemporalProperties::isVisibleInTemporalRange
virtual bool isVisibleInTemporalRange(const QgsDateTimeRange &range) const
Returns true if the layer should be visible and rendered for the specified time range.
Definition: qgsmaplayertemporalproperties.cpp:25
QgsVectorLayerTemporalContext
Encapsulates the context in which a QgsVectorLayer's temporal capabilities will be applied.
Definition: qgsvectorlayertemporalproperties.h:40
QgsGeometry::isNull
bool isNull
Definition: qgsgeometry.h:127
QgsFeatureRequest::setNoAttributes
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
Definition: qgsfeaturerequest.cpp:235
QgsAbstractGeometry::isEmpty
virtual bool isEmpty() const
Returns true if the geometry is empty.
Definition: qgsabstractgeometry.cpp:378
QgsGeometry::constGet
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Definition: qgsgeometry.cpp:136
QgsMapCanvas::zoomToFeatureExtent
void zoomToFeatureExtent(QgsRectangle &rect)
Zooms to feature extent.
Definition: qgsmapcanvas.cpp:1687
qgsvectorlayer.h
QgsMapCanvas::temporalRange
const QgsDateTimeRange & temporalRange() const
Returns map canvas datetime range.
Definition: qgsmapcanvas.cpp:1215
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:399
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
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
QgsGeometry::boundingBox
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Definition: qgsgeometry.cpp:1080
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:289
QgsMapCanvasUtils::filterForLayer
static QString filterForLayer(QgsMapCanvas *canvas, QgsVectorLayer *layer)
Constructs a filter to use for selecting features from the given layer, in order to apply filters whi...
Definition: qgsmapcanvasutils.cpp:79