QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
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
22long QgsMapCanvasUtils::zoomToMatchingFeatures( QgsMapCanvas *canvas, QgsVectorLayer *layer, const QString &filter )
23{
25
27
28 QgsFeatureIterator features = layer->getFeatures( request );
29
30 QgsRectangle bbox;
31 bbox.setNull();
32 QgsFeature feat;
33 int featureCount = 0;
34 while ( features.nextFeature( feat ) )
35 {
36 const QgsGeometry geom = feat.geometry();
37 if ( geom.isNull() || geom.constGet()->isEmpty() )
38 continue;
39
40 const QgsRectangle r = canvas->mapSettings().layerExtentToOutputExtent( layer, geom.boundingBox() );
41 bbox.combineExtentWith( r );
42 featureCount++;
43 }
44 features.close();
45
46 if ( featureCount > 0 )
47 {
48 canvas->zoomToFeatureExtent( bbox );
49 }
50 return featureCount;
51}
52
53long QgsMapCanvasUtils::flashMatchingFeatures( QgsMapCanvas *canvas, QgsVectorLayer *layer, const QString &filter )
54{
56
58
59 QgsFeatureIterator features = layer->getFeatures( request );
60 QgsFeature feat;
61 QList<QgsGeometry> geoms;
62 while ( features.nextFeature( feat ) )
63 {
64 if ( feat.hasGeometry() )
65 geoms << feat.geometry();
66 }
67
68 if ( !geoms.empty() )
69 {
70 canvas->flashGeometries( geoms, layer->crs() );
71 }
72 return geoms.size();
73}
74
76{
77 if ( canvas->mapSettings().isTemporal() )
78 {
79 if ( !layer->temporalProperties()->isVisibleInTemporalRange( canvas->temporalRange() ) )
80 return QStringLiteral( "FALSE" );
81
82 QgsVectorLayerTemporalContext temporalContext;
83 temporalContext.setLayer( layer );
84 return qobject_cast<const QgsVectorLayerTemporalProperties *>( layer->temporalProperties() )->createFilterString( temporalContext, canvas->temporalRange() );
85 }
86 else
87 {
88 return QString();
89 }
90}
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)
Fetch next feature and stores in f, returns true on success.
bool close()
Call to end the iteration.
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:58
QgsGeometry geometry
Definition qgsfeature.h:69
bool hasGeometry() const
Returns true if the feature has an associated geometry.
A geometry is the spatial representation of a feature.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
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 QString filterForLayer(QgsMapCanvas *canvas, QgsVectorLayer *layer)
Constructs a filter to use for selecting features from the given layer, in order to apply filters whi...
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.
const QgsDateTimeRange & temporalRange() const
Returns map canvas datetime range.
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.
virtual bool isVisibleInTemporalRange(const QgsDateTimeRange &range) const
Returns true if the layer should be visible and rendered for the specified time range.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:83
QgsRectangle layerExtentToOutputExtent(const QgsMapLayer *layer, QgsRectangle extent) const
transform bounding box from layer's CRS to output CRS
A rectangle specified with double values.
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
void setNull()
Mark a rectangle as being null (holding no spatial information).
bool isTemporal() const
Returns true if the object's temporal range is enabled, and the object will be filtered when renderin...
Encapsulates the context in which a QgsVectorLayer's temporal capabilities will be applied.
void setLayer(QgsVectorLayer *layer)
Sets the associated layer.
Represents a vector layer which manages a vector based data sets.
QgsMapLayerTemporalProperties * temporalProperties() override
Returns the layer's temporal properties.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.