QGIS API Documentation  3.20.0-Odense (decaadbb31)
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  {
35  return range.isInfinite() || mFixedRange.isInfinite() || mFixedRange.overlaps( range );
36 
38  return true;
39  }
40  return true;
41 }
42 
44 {
45  QgsRasterLayer *rasterLayer = qobject_cast< QgsRasterLayer *>( layer );
46  if ( !rasterLayer )
47  return QgsDateTimeRange();
48 
49  switch ( mMode )
50  {
52  return mFixedRange;
53 
55  return rasterLayer->dataProvider()->temporalCapabilities()->availableTemporalRange();
56  }
57 
58  return QgsDateTimeRange();
59 }
60 
62 {
63  QgsRasterLayer *rasterLayer = qobject_cast< QgsRasterLayer *>( layer );
64  if ( !rasterLayer )
65  return {};
66 
67  switch ( mMode )
68  {
70  return { mFixedRange };
71 
73  {
74  QList< QgsDateTimeRange > ranges = rasterLayer->dataProvider()->temporalCapabilities()->allAvailableTemporalRanges();
75  return ranges.empty() ? QList< QgsDateTimeRange > { rasterLayer->dataProvider()->temporalCapabilities()->availableTemporalRange() } : ranges;
76  }
77  }
78 
79  return {};
80 }
81 
83 {
84  return mMode;
85 }
86 
88 {
89  if ( mMode == mode )
90  return;
91  mMode = mode;
92 }
93 
94 QgsTemporalProperty::Flags QgsRasterLayerTemporalProperties::flags() const
95 {
97 }
98 
100 {
101  return mIntervalHandlingMethod;
102 }
103 
105 {
106  if ( mIntervalHandlingMethod == method )
107  return;
108  mIntervalHandlingMethod = method;
109 }
110 
111 void QgsRasterLayerTemporalProperties::setFixedTemporalRange( const QgsDateTimeRange &range )
112 {
113  mFixedRange = range;
114 }
115 
117 {
118  return mFixedRange;
119 }
120 
121 bool QgsRasterLayerTemporalProperties::readXml( const QDomElement &element, const QgsReadWriteContext &context )
122 {
123  Q_UNUSED( context )
124  // TODO add support for raster layers with multi-temporal properties.
125 
126  QDomElement temporalNode = element.firstChildElement( QStringLiteral( "temporal" ) );
127 
128  setIsActive( temporalNode.attribute( QStringLiteral( "enabled" ), QStringLiteral( "0" ) ).toInt() );
129 
130  mMode = static_cast< TemporalMode >( temporalNode.attribute( QStringLiteral( "mode" ), QStringLiteral( "0" ) ). toInt() );
131  mIntervalHandlingMethod = static_cast< QgsRasterDataProviderTemporalCapabilities::IntervalHandlingMethod >( temporalNode.attribute( QStringLiteral( "fetchMode" ), QStringLiteral( "0" ) ). toInt() );
132 
133  QDomNode rangeElement = temporalNode.namedItem( QStringLiteral( "fixedRange" ) );
134 
135  QDomNode begin = rangeElement.namedItem( QStringLiteral( "start" ) );
136  QDomNode end = rangeElement.namedItem( QStringLiteral( "end" ) );
137 
138  QDateTime beginDate = QDateTime::fromString( begin.toElement().text(), Qt::ISODate );
139  QDateTime endDate = QDateTime::fromString( end.toElement().text(), Qt::ISODate );
140 
141  QgsDateTimeRange range = QgsDateTimeRange( beginDate, endDate );
142  setFixedTemporalRange( range );
143 
144  return true;
145 }
146 
147 QDomElement QgsRasterLayerTemporalProperties::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context )
148 {
149  Q_UNUSED( context )
150  if ( element.isNull() )
151  return QDomElement();
152 
153  QDomElement temporalElement = document.createElement( QStringLiteral( "temporal" ) );
154  temporalElement.setAttribute( QStringLiteral( "enabled" ), isActive() ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
155  temporalElement.setAttribute( QStringLiteral( "mode" ), QString::number( mMode ) );
156  temporalElement.setAttribute( QStringLiteral( "fetchMode" ), QString::number( mIntervalHandlingMethod ) );
157 
158  QDomElement rangeElement = document.createElement( QStringLiteral( "fixedRange" ) );
159 
160  QDomElement startElement = document.createElement( QStringLiteral( "start" ) );
161  QDomElement endElement = document.createElement( QStringLiteral( "end" ) );
162 
163  QDomText startText = document.createTextNode( mFixedRange.begin().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate ) );
164  QDomText endText = document.createTextNode( mFixedRange.end().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate ) );
165  startElement.appendChild( startText );
166  endElement.appendChild( endText );
167  rangeElement.appendChild( startElement );
168  rangeElement.appendChild( endElement );
169 
170  temporalElement.appendChild( rangeElement );
171 
172  element.appendChild( temporalElement );
173 
174  return element;
175 }
176 
178 {
179  if ( const QgsRasterDataProviderTemporalCapabilities *rasterCaps = dynamic_cast< const QgsRasterDataProviderTemporalCapabilities *>( capabilities ) )
180  {
181  setIsActive( rasterCaps->hasTemporalCapabilities() );
182  setFixedTemporalRange( rasterCaps->availableTemporalRange() );
183 
184  if ( rasterCaps->hasTemporalCapabilities() )
185  {
187  }
188 
189  mIntervalHandlingMethod = rasterCaps->intervalHandlingMethod();
190  }
191 }
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:70
Implementation of data provider temporal properties for QgsRasterDataProviders.
IntervalHandlingMethod
Method to use when resolving a temporal range to a data provider layer or band.
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 setMode(TemporalMode mode)
Sets the temporal properties mode.
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.
void setIntervalHandlingMethod(QgsRasterDataProviderTemporalCapabilities::IntervalHandlingMethod method)
Sets the desired method to use when resolving a temporal interval to matching layers or bands in the ...
TemporalMode
Mode of the raster temporal properties.
@ ModeFixedTemporalRange
Mode when temporal properties have fixed start and end datetimes.
@ ModeTemporalRangeFromDataProvider
Mode when raster layer delegates temporal range handling to the dataprovider.
QList< QgsDateTimeRange > allTemporalRanges(QgsMapLayer *layer) const override
Attempts to calculate the overall list of all temporal extents which are contained in the specified l...
TemporalMode mode() const
Returns 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().
QgsRasterDataProviderTemporalCapabilities::IntervalHandlingMethod intervalHandlingMethod() const
Returns the desired method to use when resolving a temporal interval to matching layers or bands in t...
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.