QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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  return mMode;
64 }
65 
67 {
68  if ( mMode == mode )
69  return;
70  mMode = mode;
71 }
72 
73 QgsTemporalProperty::Flags QgsRasterLayerTemporalProperties::flags() const
74 {
75  return mode() == ModeFixedTemporalRange ? QgsTemporalProperty::FlagDontInvalidateCachedRendersWhenRangeChanges : QgsTemporalProperty::Flags( nullptr );
76 }
77 
79 {
80  return mIntervalHandlingMethod;
81 }
82 
84 {
85  if ( mIntervalHandlingMethod == method )
86  return;
87  mIntervalHandlingMethod = method;
88 }
89 
90 void QgsRasterLayerTemporalProperties::setFixedTemporalRange( const QgsDateTimeRange &range )
91 {
92  mFixedRange = range;
93 }
94 
96 {
97  return mFixedRange;
98 }
99 
100 bool QgsRasterLayerTemporalProperties::readXml( const QDomElement &element, const QgsReadWriteContext &context )
101 {
102  Q_UNUSED( context )
103  // TODO add support for raster layers with multi-temporal properties.
104 
105  QDomElement temporalNode = element.firstChildElement( QStringLiteral( "temporal" ) );
106 
107  setIsActive( temporalNode.attribute( QStringLiteral( "enabled" ), QStringLiteral( "0" ) ).toInt() );
108 
109  mMode = static_cast< TemporalMode >( temporalNode.attribute( QStringLiteral( "mode" ), QStringLiteral( "0" ) ). toInt() );
110  mIntervalHandlingMethod = static_cast< QgsRasterDataProviderTemporalCapabilities::IntervalHandlingMethod >( temporalNode.attribute( QStringLiteral( "fetchMode" ), QStringLiteral( "0" ) ). toInt() );
111 
112  QDomNode rangeElement = temporalNode.namedItem( QStringLiteral( "fixedRange" ) );
113 
114  QDomNode begin = rangeElement.namedItem( QStringLiteral( "start" ) );
115  QDomNode end = rangeElement.namedItem( QStringLiteral( "end" ) );
116 
117  QDateTime beginDate = QDateTime::fromString( begin.toElement().text(), Qt::ISODate );
118  QDateTime endDate = QDateTime::fromString( end.toElement().text(), Qt::ISODate );
119 
120  QgsDateTimeRange range = QgsDateTimeRange( beginDate, endDate );
121  setFixedTemporalRange( range );
122 
123  return true;
124 }
125 
126 QDomElement QgsRasterLayerTemporalProperties::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context )
127 {
128  Q_UNUSED( context )
129  if ( element.isNull() )
130  return QDomElement();
131 
132  QDomElement temporalElement = document.createElement( QStringLiteral( "temporal" ) );
133  temporalElement.setAttribute( QStringLiteral( "enabled" ), isActive() ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
134  temporalElement.setAttribute( QStringLiteral( "mode" ), QString::number( mMode ) );
135  temporalElement.setAttribute( QStringLiteral( "fetchMode" ), QString::number( mIntervalHandlingMethod ) );
136 
137  QDomElement rangeElement = document.createElement( QStringLiteral( "fixedRange" ) );
138 
139  QDomElement startElement = document.createElement( QStringLiteral( "start" ) );
140  QDomElement endElement = document.createElement( QStringLiteral( "end" ) );
141 
142  QDomText startText = document.createTextNode( mFixedRange.begin().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate ) );
143  QDomText endText = document.createTextNode( mFixedRange.end().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate ) );
144  startElement.appendChild( startText );
145  endElement.appendChild( endText );
146  rangeElement.appendChild( startElement );
147  rangeElement.appendChild( endElement );
148 
149  temporalElement.appendChild( rangeElement );
150 
151  element.appendChild( temporalElement );
152 
153  return element;
154 }
155 
157 {
158  if ( const QgsRasterDataProviderTemporalCapabilities *rasterCaps = dynamic_cast< const QgsRasterDataProviderTemporalCapabilities *>( capabilities ) )
159  {
160  setIsActive( rasterCaps->hasTemporalCapabilities() );
161  setFixedTemporalRange( rasterCaps->availableTemporalRange() );
162 
163  if ( rasterCaps->hasTemporalCapabilities() )
164  {
166  }
167 
168  mIntervalHandlingMethod = rasterCaps->intervalHandlingMethod();
169  }
170 }
qgsrasterlayer.h
QgsReadWriteContext
Definition: qgsreadwritecontext.h:34
QgsRasterDataProviderTemporalCapabilities::availableTemporalRange
const QgsDateTimeRange & availableTemporalRange() const
Returns the datetime range extent from which temporal data is available from the provider.
Definition: qgsrasterdataprovidertemporalcapabilities.cpp:33
QgsRasterLayerTemporalProperties::TemporalMode
TemporalMode
Mode of the raster temporal properties.
Definition: qgsrasterlayertemporalproperties.h:54
QgsRasterLayerTemporalProperties::QgsRasterLayerTemporalProperties
QgsRasterLayerTemporalProperties(QObject *parent=nullptr, bool enabled=false)
Constructor for QgsRasterLayerTemporalProperties, with the specified parent object.
Definition: qgsrasterlayertemporalproperties.cpp:22
QgsRasterDataProviderTemporalCapabilities::IntervalHandlingMethod
IntervalHandlingMethod
Method to use when resolving a temporal range to a data provider layer or band.
Definition: qgsrasterdataprovidertemporalcapabilities.h:51
QgsRasterLayerTemporalProperties::setIntervalHandlingMethod
void setIntervalHandlingMethod(QgsRasterDataProviderTemporalCapabilities::IntervalHandlingMethod method)
Sets the desired method to use when resolving a temporal interval to matching layers or bands in the ...
Definition: qgsrasterlayertemporalproperties.cpp:83
QgsRasterLayerTemporalProperties::ModeTemporalRangeFromDataProvider
@ ModeTemporalRangeFromDataProvider
Mode when raster layer delegates temporal range handling to the dataprovider.
Definition: qgsrasterlayertemporalproperties.h:57
qgsrasterdataprovidertemporalcapabilities.h
QgsDataProviderTemporalCapabilities
Definition: qgsdataprovidertemporalcapabilities.h:33
QgsRasterLayerTemporalProperties::ModeFixedTemporalRange
@ ModeFixedTemporalRange
Mode when temporal properties have fixed start and end datetimes.
Definition: qgsrasterlayertemporalproperties.h:56
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:100
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:90
QgsRasterLayerTemporalProperties::mode
TemporalMode mode() const
Returns the temporal properties mode.
Definition: qgsrasterlayertemporalproperties.cpp:61
QgsMapLayerTemporalProperties
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
Definition: qgsrasterlayer.h:72
QgsTemporalProperty::setIsActive
void setIsActive(bool active)
Sets whether the temporal property is active.
Definition: qgstemporalproperty.cpp:27
QgsRasterLayerTemporalProperties::intervalHandlingMethod
QgsRasterDataProviderTemporalCapabilities::IntervalHandlingMethod intervalHandlingMethod() const
Returns the desired method to use when resolving a temporal interval to matching layers or bands in t...
Definition: qgsrasterlayertemporalproperties.cpp:78
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:156
QgsRasterLayerTemporalProperties::setMode
void setMode(TemporalMode mode)
Sets the temporal properties mode.
Definition: qgsrasterlayertemporalproperties.cpp:66
QgsMapLayer
Definition: qgsmaplayer.h:81
QgsRasterLayerTemporalProperties::flags
QgsTemporalProperty::Flags flags() const override
Returns flags associated to the temporal property.
Definition: qgsrasterlayertemporalproperties.cpp:73
QgsRasterLayerTemporalProperties::fixedTemporalRange
const QgsDateTimeRange & fixedTemporalRange() const
Returns the fixed temporal range for the layer.
Definition: qgsrasterlayertemporalproperties.cpp:95
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:126
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:43
QgsRasterDataProvider::temporalCapabilities
QgsRasterDataProviderTemporalCapabilities * temporalCapabilities() override
Returns the provider's temporal capabilities.
Definition: qgsrasterdataprovider.cpp:413
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:233
QgsRasterDataProviderTemporalCapabilities
Definition: qgsrasterdataprovidertemporalcapabilities.h:37