QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsrasterlayertemporalproperties.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrasterlayertemporalproperties.cpp
3  ---------------
4  begin : February 2020
5  copyright : (C) 2020 by Samweli Mwakisambwe
6  email : samweli at kartoza 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 
20 #include "qgsrasterlayer.h"
21 
23  : QgsMapLayerTemporalProperties( parent, enabled )
24 {
25 }
26 
27 bool QgsRasterLayerTemporalProperties::isVisibleInTemporalRange( const QgsDateTimeRange &range ) const
28 {
29  if ( !isActive() )
30  return true;
31 
32  switch ( mMode )
33  {
34  case Qgis::RasterTemporalMode::FixedTemporalRange:
35  return range.isInfinite() || mFixedRange.isInfinite() || mFixedRange.overlaps( range );
36 
37  case Qgis::RasterTemporalMode::TemporalRangeFromDataProvider:
38  case Qgis::RasterTemporalMode::RedrawLayerOnly:
39  return true;
40  }
41  return true;
42 }
43 
45 {
46  QgsRasterLayer *rasterLayer = qobject_cast< QgsRasterLayer *>( layer );
47  if ( !rasterLayer )
48  return QgsDateTimeRange();
49 
50  switch ( mMode )
51  {
52  case Qgis::RasterTemporalMode::FixedTemporalRange:
53  return mFixedRange;
54 
55  case Qgis::RasterTemporalMode::TemporalRangeFromDataProvider:
56  return rasterLayer->dataProvider()->temporalCapabilities()->availableTemporalRange();
57 
58  case Qgis::RasterTemporalMode::RedrawLayerOnly:
59  break;
60  }
61 
62  return QgsDateTimeRange();
63 }
64 
66 {
67  QgsRasterLayer *rasterLayer = qobject_cast< QgsRasterLayer *>( layer );
68  if ( !rasterLayer )
69  return {};
70 
71  switch ( mMode )
72  {
73  case Qgis::RasterTemporalMode::FixedTemporalRange:
74  return { mFixedRange };
75 
76  case Qgis::RasterTemporalMode::TemporalRangeFromDataProvider:
77  {
78  const QList< QgsDateTimeRange > ranges = rasterLayer->dataProvider()->temporalCapabilities()->allAvailableTemporalRanges();
79  return ranges.empty() ? QList< QgsDateTimeRange > { rasterLayer->dataProvider()->temporalCapabilities()->availableTemporalRange() } : ranges;
80  }
81 
82  case Qgis::RasterTemporalMode::RedrawLayerOnly:
83  break;
84  }
85 
86  return {};
87 }
88 
90 {
91  return mMode;
92 }
93 
95 {
96  if ( mMode == mode )
97  return;
98  mMode = mode;
99 }
100 
101 QgsTemporalProperty::Flags QgsRasterLayerTemporalProperties::flags() const
102 {
103  return mode() == Qgis::RasterTemporalMode::FixedTemporalRange ? QgsTemporalProperty::FlagDontInvalidateCachedRendersWhenRangeChanges : QgsTemporalProperty::Flags();
104 }
105 
107 {
108  return mIntervalHandlingMethod;
109 }
110 
112 {
113  if ( mIntervalHandlingMethod == method )
114  return;
115  mIntervalHandlingMethod = method;
116 }
117 
118 void QgsRasterLayerTemporalProperties::setFixedTemporalRange( const QgsDateTimeRange &range )
119 {
120  mFixedRange = range;
121 }
122 
124 {
125  return mFixedRange;
126 }
127 
128 bool QgsRasterLayerTemporalProperties::readXml( const QDomElement &element, const QgsReadWriteContext &context )
129 {
130  Q_UNUSED( context )
131  // TODO add support for raster layers with multi-temporal properties.
132 
133  const QDomElement temporalNode = element.firstChildElement( QStringLiteral( "temporal" ) );
134 
135  setIsActive( temporalNode.attribute( QStringLiteral( "enabled" ), QStringLiteral( "0" ) ).toInt() );
136 
137  mMode = static_cast< Qgis::RasterTemporalMode >( temporalNode.attribute( QStringLiteral( "mode" ), QStringLiteral( "0" ) ). toInt() );
138  mIntervalHandlingMethod = static_cast< Qgis::TemporalIntervalMatchMethod >( temporalNode.attribute( QStringLiteral( "fetchMode" ), QStringLiteral( "0" ) ). toInt() );
139 
140  const QDomNode rangeElement = temporalNode.namedItem( QStringLiteral( "fixedRange" ) );
141 
142  const QDomNode begin = rangeElement.namedItem( QStringLiteral( "start" ) );
143  const QDomNode end = rangeElement.namedItem( QStringLiteral( "end" ) );
144 
145  const QDateTime beginDate = QDateTime::fromString( begin.toElement().text(), Qt::ISODate );
146  const QDateTime endDate = QDateTime::fromString( end.toElement().text(), Qt::ISODate );
147 
148  const QgsDateTimeRange range = QgsDateTimeRange( beginDate, endDate );
149  setFixedTemporalRange( range );
150 
151  return true;
152 }
153 
154 QDomElement QgsRasterLayerTemporalProperties::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context )
155 {
156  Q_UNUSED( context )
157  if ( element.isNull() )
158  return QDomElement();
159 
160  QDomElement temporalElement = document.createElement( QStringLiteral( "temporal" ) );
161  temporalElement.setAttribute( QStringLiteral( "enabled" ), isActive() ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
162  temporalElement.setAttribute( QStringLiteral( "mode" ), QString::number( static_cast< int >( mMode ) ) );
163  temporalElement.setAttribute( QStringLiteral( "fetchMode" ), QString::number( static_cast< int >( mIntervalHandlingMethod ) ) );
164 
165  QDomElement rangeElement = document.createElement( QStringLiteral( "fixedRange" ) );
166 
167  QDomElement startElement = document.createElement( QStringLiteral( "start" ) );
168  QDomElement endElement = document.createElement( QStringLiteral( "end" ) );
169 
170  const QDomText startText = document.createTextNode( mFixedRange.begin().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate ) );
171  const QDomText endText = document.createTextNode( mFixedRange.end().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate ) );
172  startElement.appendChild( startText );
173  endElement.appendChild( endText );
174  rangeElement.appendChild( startElement );
175  rangeElement.appendChild( endElement );
176 
177  temporalElement.appendChild( rangeElement );
178 
179  element.appendChild( temporalElement );
180 
181  return element;
182 }
183 
185 {
186  if ( const QgsRasterDataProviderTemporalCapabilities *rasterCaps = dynamic_cast< const QgsRasterDataProviderTemporalCapabilities *>( capabilities ) )
187  {
188  setIsActive( rasterCaps->hasTemporalCapabilities() );
189  setFixedTemporalRange( rasterCaps->availableTemporalRange() );
190 
191  if ( rasterCaps->hasTemporalCapabilities() )
192  {
193  setMode( Qgis::RasterTemporalMode::TemporalRangeFromDataProvider );
194  }
195 
196  mIntervalHandlingMethod = rasterCaps->intervalHandlingMethod();
197  }
198 }
qgsrasterlayer.h
QgsRasterLayerTemporalProperties::setIntervalHandlingMethod
void setIntervalHandlingMethod(Qgis::TemporalIntervalMatchMethod method)
Sets the desired method to use when resolving a temporal interval to matching layers or bands in the ...
Definition: qgsrasterlayertemporalproperties.cpp:111
QgsRasterDataProviderTemporalCapabilities::allAvailableTemporalRanges
QList< QgsDateTimeRange > allAvailableTemporalRanges() const
Returns a list of all valid datetime ranges for which temporal data is available from the provider.
Definition: qgsrasterdataprovidertemporalcapabilities.cpp:43
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsRasterDataProviderTemporalCapabilities::availableTemporalRange
const QgsDateTimeRange & availableTemporalRange() const
Returns the overall datetime range extent from which temporal data is available from the provider.
Definition: qgsrasterdataprovidertemporalcapabilities.cpp:33
QgsRasterLayerTemporalProperties::setMode
void setMode(Qgis::RasterTemporalMode mode)
Sets the temporal properties mode.
Definition: qgsrasterlayertemporalproperties.cpp:94
QgsRasterLayerTemporalProperties::QgsRasterLayerTemporalProperties
QgsRasterLayerTemporalProperties(QObject *parent=nullptr, bool enabled=false)
Constructor for QgsRasterLayerTemporalProperties, with the specified parent object.
Definition: qgsrasterlayertemporalproperties.cpp:22
Qgis::RasterTemporalMode
RasterTemporalMode
Raster layer temporal modes.
Definition: qgis.h:1207
qgsrasterdataprovidertemporalcapabilities.h
QgsDataProviderTemporalCapabilities
Base class for handling properties relating to a data provider's temporal capabilities.
Definition: qgsdataprovidertemporalcapabilities.h:33
QgsRasterLayerTemporalProperties::readXml
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads temporal properties from a DOM element previously written by writeXml().
Definition: qgsrasterlayertemporalproperties.cpp:128
QgsRasterLayerTemporalProperties::allTemporalRanges
QList< QgsDateTimeRange > allTemporalRanges(QgsMapLayer *layer) const override
Attempts to calculate the overall list of all temporal extents which are contained in the specified l...
Definition: qgsrasterlayertemporalproperties.cpp:65
QgsTemporalProperty::isActive
bool isActive() const
Returns true if the temporal property is active.
Definition: qgstemporalproperty.cpp:36
QgsRasterLayerTemporalProperties::setFixedTemporalRange
void setFixedTemporalRange(const QgsDateTimeRange &range)
Sets a temporal range to apply to the whole layer.
Definition: qgsrasterlayertemporalproperties.cpp:118
Qgis::TemporalIntervalMatchMethod
TemporalIntervalMatchMethod
Method to use when resolving a temporal range to a data provider layer or band.
Definition: qgis.h:1220
QgsMapLayerTemporalProperties
Base class for storage of map layer temporal properties.
Definition: qgsmaplayertemporalproperties.h:42
QgsRasterLayerTemporalProperties::isVisibleInTemporalRange
bool isVisibleInTemporalRange(const QgsDateTimeRange &range) const override
Returns true if the layer should be visible and rendered for the specified time range.
Definition: qgsrasterlayertemporalproperties.cpp:27
QgsRasterLayer
Represents a raster layer.
Definition: qgsrasterlayer.h:76
QgsTemporalProperty::setIsActive
void setIsActive(bool active)
Sets whether the temporal property is active.
Definition: qgstemporalproperty.cpp:27
QgsRasterLayerTemporalProperties::setDefaultsFromDataProviderTemporalCapabilities
void setDefaultsFromDataProviderTemporalCapabilities(const QgsDataProviderTemporalCapabilities *capabilities) override
Sets the layers temporal settings to appropriate defaults based on a provider's temporal capabilities...
Definition: qgsrasterlayertemporalproperties.cpp:184
QgsRasterLayerTemporalProperties::mode
Qgis::RasterTemporalMode mode() const
Returns the temporal properties mode.
Definition: qgsrasterlayertemporalproperties.cpp:89
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsRasterLayerTemporalProperties::intervalHandlingMethod
Qgis::TemporalIntervalMatchMethod intervalHandlingMethod() const
Returns the desired method to use when resolving a temporal interval to matching layers or bands in t...
Definition: qgsrasterlayertemporalproperties.cpp:106
QgsRasterLayerTemporalProperties::flags
QgsTemporalProperty::Flags flags() const override
Returns flags associated to the temporal property.
Definition: qgsrasterlayertemporalproperties.cpp:101
QgsRasterLayerTemporalProperties::fixedTemporalRange
const QgsDateTimeRange & fixedTemporalRange() const
Returns the fixed temporal range for the layer.
Definition: qgsrasterlayertemporalproperties.cpp:123
qgsrasterlayertemporalproperties.h
QgsRasterLayerTemporalProperties::writeXml
QDomElement writeXml(QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context) override
Writes the properties to a DOM element, to be used later with readXml().
Definition: qgsrasterlayertemporalproperties.cpp:154
QgsRasterLayerTemporalProperties::calculateTemporalExtent
QgsDateTimeRange calculateTemporalExtent(QgsMapLayer *layer) const override
Attempts to calculate the overall temporal extent for the specified layer, using the settings defined...
Definition: qgsrasterlayertemporalproperties.cpp:44
QgsRasterDataProvider::temporalCapabilities
QgsRasterDataProviderTemporalCapabilities * temporalCapabilities() override
Returns the provider's temporal capabilities.
Definition: qgsrasterdataprovider.cpp:412
QgsTemporalProperty::FlagDontInvalidateCachedRendersWhenRangeChanges
@ FlagDontInvalidateCachedRendersWhenRangeChanges
Any cached rendering will not be invalidated when temporal range context is modified.
Definition: qgstemporalproperty.h:46
QgsRasterLayer::dataProvider
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
Definition: qgsrasterlayer.cpp:257
QgsRasterDataProviderTemporalCapabilities
Implementation of data provider temporal properties for QgsRasterDataProviders.
Definition: qgsrasterdataprovidertemporalcapabilities.h:38