QGIS API Documentation 3.43.0-Master (3ee7834ace6)
qgsrasterlayerutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrasterlayerutils.cpp
3 -------------------------
4 begin : March 2024
5 copyright : (C) 2024 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsrasterlayerutils.h"
19#include "qgsrasterlayer.h"
24
26 QgsRasterLayer *layer,
27 const QgsDateTimeRange &temporalRange,
28 const QgsDoubleRange &elevationRange,
29 bool &matched )
30{
31 matched = true;
32 const QgsRasterLayerElevationProperties *elevationProperties = qobject_cast< QgsRasterLayerElevationProperties * >( layer->elevationProperties() );
33 const QgsRasterLayerTemporalProperties *temporalProperties = qobject_cast< QgsRasterLayerTemporalProperties *>( layer->temporalProperties() );
34
35 // neither active
36 if ( ( !temporalProperties->isActive() || temporalRange.isInfinite() )
37 && ( !elevationProperties->hasElevation() || elevationRange.isInfinite() ) )
38 {
39 return -1;
40 }
41
42 // only elevation properties enabled
43 if ( !temporalProperties->isActive() || temporalRange.isInfinite() )
44 {
45 const int band = elevationProperties->bandForElevationRange( layer, elevationRange );
46 matched = band > 0;
47 return band;
48 }
49
50 // only temporal properties enabled
51 if ( !elevationProperties->hasElevation() || elevationRange.isInfinite() )
52 {
53 const int band = temporalProperties->bandForTemporalRange( layer, temporalRange );
54 matched = band > 0;
55 return band;
56 }
57
58 // both elevation and temporal properties enabled
59
60 // first find bands matching the temporal range
61 QList< int > temporalBands;
62 switch ( temporalProperties->mode() )
63 {
69 {
70 temporalBands << temporalProperties->filteredBandsForTemporalRange( layer, temporalRange );
71 break;
72 }
73
75 {
76 temporalBands << temporalProperties->bandNumber();
77 break;
78 }
79 }
80
81 if ( temporalBands.empty() )
82 {
83 matched = false;
84 return -1;
85 }
86
87 switch ( elevationProperties->mode() )
88 {
91 return temporalBands.at( 0 );
92
94 {
95 // find the top-most band which matches the map range
96 int currentMatchingBand = -1;
97 matched = false;
98 QgsDoubleRange currentMatchingRange;
99 const QMap<int, QgsDoubleRange> rangePerBand = elevationProperties->fixedRangePerBand();
100 for ( int band : temporalBands )
101 {
102 const QgsDoubleRange rangeForBand = rangePerBand.value( band );
103 if ( rangeForBand.overlaps( elevationRange ) )
104 {
105 if ( currentMatchingRange.isInfinite()
106 || ( rangeForBand.includeUpper() && rangeForBand.upper() >= currentMatchingRange.upper() )
107 || ( !currentMatchingRange.includeUpper() && rangeForBand.upper() >= currentMatchingRange.upper() ) )
108 {
109 matched = true;
110 currentMatchingBand = band;
111 currentMatchingRange = rangeForBand;
112 }
113 }
114 }
115 return currentMatchingBand;
116 }
117
119 {
120 if ( layer )
121 {
122 QgsExpressionContext context;
125 context.appendScope( bandScope );
126
129 lowerProperty.prepare( context );
130 upperProperty.prepare( context );
131
132 int currentMatchingBand = -1;
133 matched = false;
134 QgsDoubleRange currentMatchingRange;
135
136 for ( int band : temporalBands )
137 {
138 bandScope->setVariable( QStringLiteral( "band" ), band );
139 bandScope->setVariable( QStringLiteral( "band_name" ), layer->dataProvider()->displayBandName( band ) );
140 bandScope->setVariable( QStringLiteral( "band_description" ), layer->dataProvider()->bandDescription( band ) );
141
142 bool ok = false;
143 const double lower = lowerProperty.valueAsDouble( context, 0, &ok );
144 if ( !ok )
145 continue;
146 const double upper = upperProperty.valueAsDouble( context, 0, &ok );
147 if ( !ok )
148 continue;
149
150 const QgsDoubleRange bandRange = QgsDoubleRange( lower, upper );
151 if ( bandRange.overlaps( elevationRange ) )
152 {
153 if ( currentMatchingRange.isInfinite()
154 || ( bandRange.includeUpper() && bandRange.upper() >= currentMatchingRange.upper() )
155 || ( !currentMatchingRange.includeUpper() && bandRange.upper() >= currentMatchingRange.upper() ) )
156 {
157 currentMatchingBand = band;
158 currentMatchingRange = bandRange;
159 matched = true;
160 }
161 }
162 }
163 return currentMatchingBand;
164 }
165 return -1;
166 }
167 }
169}
@ FixedRangePerBand
Layer has a fixed (manually specified) elevation range per band.
@ FixedElevationRange
Layer has a fixed elevation range.
@ RepresentsElevationSurface
Pixel values represent an elevation surface.
@ DynamicRangePerBand
Layer has a elevation range per band, calculated dynamically from an expression.
@ RepresentsTemporalValues
Pixel values represent an datetime.
@ RedrawLayerOnly
Redraw the layer when temporal range changes, but don't apply any filtering. Useful when raster symbo...
@ FixedRangePerBand
Layer has a fixed temporal range per band.
@ TemporalRangeFromDataProvider
Mode when raster layer delegates temporal range handling to the dataprovider.
@ FixedTemporalRange
Mode when temporal properties have fixed start and end datetimes.
@ FixedDateTime
Layer has a fixed date time instant.
QgsRange which stores a range of double values.
Definition qgsrange.h:233
bool isInfinite() const
Returns true if the range consists of all possible values.
Definition qgsrange.h:287
Single scope for storing variables and functions for use within a QgsExpressionContext.
void setVariable(const QString &name, const QVariant &value, bool isStatic=false)
Convenience method for setting a variable in the context scope by name name and value.
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...
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the object's property collection, used for data defined overrides.
@ RasterPerBandUpperElevation
Upper elevation for each raster band.
@ RasterPerBandLowerElevation
Lower elevation for each raster band.
QgsProperty property(int key) const final
Returns a matching property from the collection, if one exists.
A store for object properties.
double valueAsDouble(const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a double.
bool prepare(const QgsExpressionContext &context=QgsExpressionContext()) const
Prepares the property against a specified expression context.
bool includeUpper() const
Returns true if the upper bound is inclusive, or false if the upper bound is exclusive.
Definition qgsrange.h:101
bool overlaps(const QgsRange< T > &other) const
Returns true if this range overlaps another range.
Definition qgsrange.h:176
T upper() const
Returns the upper bound of the range.
Definition qgsrange.h:85
virtual QString bandDescription(int bandNumber)
Returns the description for band bandNumber, or an empty string if the band is not valid or has not d...
QString displayBandName(int bandNumber) const
Generates a friendly, descriptive name for the specified bandNumber.
Raster layer specific subclass of QgsMapLayerElevationProperties.
Qgis::RasterElevationMode mode() const
Returns the elevation mode.
bool hasElevation() const override
Returns true if the layer has an elevation or z component.
int bandForElevationRange(QgsRasterLayer *layer, const QgsDoubleRange &range) const
Returns the band corresponding to the specified range.
QMap< int, QgsDoubleRange > fixedRangePerBand() const
Returns the fixed elevation range for each band.
Implementation of map layer temporal properties for raster layers.
QList< int > filteredBandsForTemporalRange(QgsRasterLayer *layer, const QgsDateTimeRange &range) const
Returns a filtered list of bands which match the specified range.
Qgis::RasterTemporalMode mode() const
Returns the temporal properties mode.
int bandForTemporalRange(QgsRasterLayer *layer, const QgsDateTimeRange &range) const
Returns the band corresponding to the specified range.
int bandNumber() const
Returns the band number from which temporal values should be taken.
static int renderedBandForElevationAndTemporalRange(QgsRasterLayer *layer, const QgsDateTimeRange &temporalRange, const QgsDoubleRange &elevationRange, bool &matched)
Given a raster layer, returns the band which should be used for rendering the layer for a specified t...
Represents a raster layer.
QgsMapLayerTemporalProperties * temporalProperties() override
Returns the layer's temporal properties.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
QgsMapLayerElevationProperties * elevationProperties() override
Returns the layer's elevation properties.
bool isActive() const
Returns true if the temporal property is active.
bool isInfinite() const
Returns true if the range consists of all possible values.
Definition qgsrange.h:482
#define BUILTIN_UNREACHABLE
Definition qgis.h:6873