Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add API to determine if a layer should be rendered for a given time r…
…ange
  • Loading branch information
nyalldawson committed Mar 13, 2020
1 parent 4b1b4ab commit ff32502
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 0 deletions.
Expand Up @@ -66,6 +66,11 @@ Returns the temporal properties temporal range source, can be layer or project.
Sets the temporal properties temporal range ``source``.

.. seealso:: :py:func:`temporalSource`
%End

virtual bool isVisibleInTemporalRange( const QgsDateTimeRange &range ) const;
%Docstring
Returns ``True`` if the layer should be visible and rendered for the specified time ``range``.
%End

};
Expand Down
Expand Up @@ -30,6 +30,9 @@ Constructor for QgsRasterLayerTemporalProperties, with the specified ``parent``
The ``enabled`` argument specifies whether the temporal properties are initially enabled or not (see isActive()).
%End

virtual bool isVisibleInTemporalRange( const QgsDateTimeRange &range ) const;


enum TemporalMode
{
ModeFixedTemporalRange,
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsmaplayertemporalproperties.cpp
Expand Up @@ -31,6 +31,11 @@ void QgsMapLayerTemporalProperties::setTemporalSource( QgsMapLayerTemporalProper
}
}

bool QgsMapLayerTemporalProperties::isVisibleInTemporalRange( const QgsDateTimeRange & ) const
{
return true;
}

QgsMapLayerTemporalProperties::TemporalSource QgsMapLayerTemporalProperties::temporalSource() const
{
return mSource;
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsmaplayertemporalproperties.h
Expand Up @@ -23,6 +23,7 @@
#include "qgis_sip.h"
#include "qgstemporalproperty.h"
#include "qgsreadwritecontext.h"
#include "qgsrange.h"

#include <QDomElement>

Expand Down Expand Up @@ -86,6 +87,11 @@ class CORE_EXPORT QgsMapLayerTemporalProperties : public QgsTemporalProperty
**/
void setTemporalSource( TemporalSource source );

/**
* Returns TRUE if the layer should be visible and rendered for the specified time \a range.
*/
virtual bool isVisibleInTemporalRange( const QgsDateTimeRange &range ) const;

private:

//! Source of the properties temporal range
Expand Down
16 changes: 16 additions & 0 deletions src/core/raster/qgsrasterlayertemporalproperties.cpp
Expand Up @@ -23,6 +23,22 @@ QgsRasterLayerTemporalProperties::QgsRasterLayerTemporalProperties( QObject *par
{
}

bool QgsRasterLayerTemporalProperties::isVisibleInTemporalRange( const QgsDateTimeRange &range ) const
{
if ( !isActive() )
return true;

switch ( mMode )
{
case ModeFixedTemporalRange:
return range.isInfinite() || mFixedRange.isInfinite() || mFixedRange.overlaps( range );

case ModeTemporalRangeFromDataProvider:
return true;
}
return true;
}

QgsRasterLayerTemporalProperties::TemporalMode QgsRasterLayerTemporalProperties::mode() const
{
return mMode;
Expand Down
2 changes: 2 additions & 0 deletions src/core/raster/qgsrasterlayertemporalproperties.h
Expand Up @@ -45,6 +45,8 @@ class CORE_EXPORT QgsRasterLayerTemporalProperties : public QgsMapLayerTemporalP
*/
QgsRasterLayerTemporalProperties( QObject *parent SIP_TRANSFERTHIS = nullptr, bool enabled = false );

bool isVisibleInTemporalRange( const QgsDateTimeRange &range ) const override;

/**
* Mode of the raster temporal properties
**/
Expand Down
46 changes: 46 additions & 0 deletions tests/src/core/testqgsrasterlayertemporalproperties.cpp
Expand Up @@ -40,6 +40,7 @@ class TestQgsRasterLayerTemporalProperties : public QObject

void checkSettingTemporalRange();
void testChangedSignal();
void testVisibleInTimeRange();

private:
QgsRasterLayerTemporalProperties *temporalProperties = nullptr;
Expand Down Expand Up @@ -97,5 +98,50 @@ void TestQgsRasterLayerTemporalProperties::testChangedSignal()
QCOMPARE( spy.count(), 2 );
}

void TestQgsRasterLayerTemporalProperties::testVisibleInTimeRange()
{
QgsRasterLayerTemporalProperties props;
// by default, should be visible regardless of time range
QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange() ) );
QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2020, 1, 1 ) ),
QDateTime( QDate( 2020, 1, 1 ) ) ) ) );

// when in data provider time handling mode, we also should always render regardless of time range
props.setIsActive( true );
props.setMode( QgsRasterLayerTemporalProperties::ModeTemporalRangeFromDataProvider );
QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange() ) );
QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2020, 1, 1 ) ),
QDateTime( QDate( 2020, 1, 1 ) ) ) ) );
// fix temporal range should be ignored while in ModeTemporalRangeFromDataProvider
props.setFixedTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2020, 1, 1 ) ),
QDateTime( QDate( 2020, 1, 5 ) ) ) );
QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange() ) );
QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2019, 1, 1 ) ),
QDateTime( QDate( 2019, 1, 2 ) ) ) ) );

// switch to fixed time mode
props.setMode( QgsRasterLayerTemporalProperties::ModeFixedTemporalRange );
// should be visible in infinite time ranges
QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange() ) );
// should not be visible -- outside of fixed time range
QVERIFY( !props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2019, 1, 1 ) ),
QDateTime( QDate( 2019, 1, 2 ) ) ) ) );
// should be visible -- intersects fixed time range
QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2020, 1, 2 ) ),
QDateTime( QDate( 2020, 1, 3 ) ) ) ) );
QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2020, 1, 2 ) ),
QDateTime( ) ) ) );
QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime(),
QDateTime( QDate( 2020, 1, 3 ) ) ) ) );
QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2019, 1, 2 ) ),
QDateTime( QDate( 2020, 1, 3 ) ) ) ) );
QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2019, 1, 2 ) ),
QDateTime( QDate( 2021, 1, 3 ) ) ) ) );
QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2020, 1, 1 ) ),
QDateTime( QDate( 2020, 1, 1 ) ) ) ) );
QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2020, 1, 5 ) ),
QDateTime( QDate( 2020, 1, 5 ) ) ) ) );
}

QGSTEST_MAIN( TestQgsRasterLayerTemporalProperties )
#include "testqgsrasterlayertemporalproperties.moc"

0 comments on commit ff32502

Please sign in to comment.