QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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 {
68 {
69 temporalBands << temporalProperties->filteredBandsForTemporalRange( layer, temporalRange );
70 break;
71 }
72
74 {
75 temporalBands << temporalProperties->bandNumber();
76 break;
77 }
78 }
79
80 if ( temporalBands.empty() )
81 {
82 matched = false;
83 return -1;
84 }
85
86 switch ( elevationProperties->mode() )
87 {
90 return temporalBands.at( 0 );
91
93 {
94 // find the top-most band which matches the map range
95 int currentMatchingBand = -1;
96 matched = false;
97 QgsDoubleRange currentMatchingRange;
98 const QMap<int, QgsDoubleRange> rangePerBand = elevationProperties->fixedRangePerBand();
99 for ( int band : temporalBands )
100 {
101 const QgsDoubleRange rangeForBand = rangePerBand.value( band );
102 if ( rangeForBand.overlaps( elevationRange ) )
103 {
104 if ( currentMatchingRange.isInfinite()
105 || ( rangeForBand.includeUpper() && rangeForBand.upper() >= currentMatchingRange.upper() )
106 || ( !currentMatchingRange.includeUpper() && rangeForBand.upper() >= currentMatchingRange.upper() ) )
107 {
108 matched = true;
109 currentMatchingBand = band;
110 currentMatchingRange = rangeForBand;
111 }
112 }
113 }
114 return currentMatchingBand;
115 }
116
118 {
119 if ( layer )
120 {
121 QgsExpressionContext context;
124 context.appendScope( bandScope );
125
128 lowerProperty.prepare( context );
129 upperProperty.prepare( context );
130
131 int currentMatchingBand = -1;
132 matched = false;
133 QgsDoubleRange currentMatchingRange;
134
135 for ( int band : temporalBands )
136 {
137 bandScope->setVariable( QStringLiteral( "band" ), band );
138 bandScope->setVariable( QStringLiteral( "band_name" ), layer->dataProvider()->displayBandName( band ) );
139 bandScope->setVariable( QStringLiteral( "band_description" ), layer->dataProvider()->bandDescription( band ) );
140
141 bool ok = false;
142 const double lower = lowerProperty.valueAsDouble( context, 0, &ok );
143 if ( !ok )
144 continue;
145 const double upper = upperProperty.valueAsDouble( context, 0, &ok );
146 if ( !ok )
147 continue;
148
149 const QgsDoubleRange bandRange = QgsDoubleRange( lower, upper );
150 if ( bandRange.overlaps( elevationRange ) )
151 {
152 if ( currentMatchingRange.isInfinite()
153 || ( bandRange.includeUpper() && bandRange.upper() >= currentMatchingRange.upper() )
154 || ( !currentMatchingRange.includeUpper() && bandRange.upper() >= currentMatchingRange.upper() ) )
155 {
156 currentMatchingBand = band;
157 currentMatchingRange = bandRange;
158 matched = true;
159 }
160 }
161 }
162 return currentMatchingBand;
163 }
164 return -1;
165 }
166 }
168}
@ 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.
QgsRange which stores a range of double values.
Definition qgsrange.h:231
bool isInfinite() const
Returns true if the range consists of all possible values.
Definition qgsrange.h:285
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:480
#define BUILTIN_UNREACHABLE
Definition qgis.h:6612