QGIS API Documentation  3.23.0-Master (22c16f2067)
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 }
TemporalIntervalMatchMethod
Method to use when resolving a temporal range to a data provider layer or band.
Definition: qgis.h:895
RasterTemporalMode
Raster layer temporal modes.
Definition: qgis.h:882
Base class for handling properties relating to a data provider's temporal capabilities.
Base class for storage of map layer temporal properties.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
Implementation of data provider temporal properties for QgsRasterDataProviders.
QList< QgsDateTimeRange > allAvailableTemporalRanges() const
Returns a list of all valid datetime ranges for which temporal data is available from the provider.
const QgsDateTimeRange & availableTemporalRange() const
Returns the overall datetime range extent from which temporal data is available from the provider.
QgsRasterDataProviderTemporalCapabilities * temporalCapabilities() override
Returns the provider's temporal capabilities.
QDomElement writeXml(QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context) override
Writes the properties to a DOM element, to be used later with readXml().
void setDefaultsFromDataProviderTemporalCapabilities(const QgsDataProviderTemporalCapabilities *capabilities) override
Sets the layers temporal settings to appropriate defaults based on a provider's temporal capabilities...
bool isVisibleInTemporalRange(const QgsDateTimeRange &range) const override
Returns true if the layer should be visible and rendered for the specified time range.
QgsTemporalProperty::Flags flags() const override
Returns flags associated to the temporal property.
Qgis::TemporalIntervalMatchMethod intervalHandlingMethod() const
Returns the desired method to use when resolving a temporal interval to matching layers or bands in t...
void setIntervalHandlingMethod(Qgis::TemporalIntervalMatchMethod method)
Sets the desired method to use when resolving a temporal interval to matching layers or bands in the ...
Qgis::RasterTemporalMode mode() const
Returns the temporal properties mode.
QList< QgsDateTimeRange > allTemporalRanges(QgsMapLayer *layer) const override
Attempts to calculate the overall list of all temporal extents which are contained in the specified l...
void setMode(Qgis::RasterTemporalMode mode)
Sets the temporal properties mode.
QgsRasterLayerTemporalProperties(QObject *parent=nullptr, bool enabled=false)
Constructor for QgsRasterLayerTemporalProperties, with the specified parent object.
void setFixedTemporalRange(const QgsDateTimeRange &range)
Sets a temporal range to apply to the whole layer.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads temporal properties from a DOM element previously written by writeXml().
const QgsDateTimeRange & fixedTemporalRange() const
Returns the fixed temporal range for the layer.
QgsDateTimeRange calculateTemporalExtent(QgsMapLayer *layer) const override
Attempts to calculate the overall temporal extent for the specified layer, using the settings defined...
Represents a raster layer.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
The class is used as a container of context for various read/write operations on other objects.
bool isActive() const
Returns true if the temporal property is active.
void setIsActive(bool active)
Sets whether the temporal property is active.
@ FlagDontInvalidateCachedRendersWhenRangeChanges
Any cached rendering will not be invalidated when temporal range context is modified.