Skip to content
Permalink
Browse files

Add API to determine if a layer should be rendered for a given time r…

…ange
  • Loading branch information
nyalldawson committed Mar 12, 2020
1 parent 4b1b4ab commit ff325027f7cd98c6b2f284f401b5a9f92cf8268b
@@ -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

};
@@ -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,
@@ -31,6 +31,11 @@ void QgsMapLayerTemporalProperties::setTemporalSource( QgsMapLayerTemporalProper
}
}

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

QgsMapLayerTemporalProperties::TemporalSource QgsMapLayerTemporalProperties::temporalSource() const
{
return mSource;
@@ -23,6 +23,7 @@
#include "qgis_sip.h"
#include "qgstemporalproperty.h"
#include "qgsreadwritecontext.h"
#include "qgsrange.h"

#include <QDomElement>

@@ -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
@@ -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;
@@ -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
**/
@@ -40,6 +40,7 @@ class TestQgsRasterLayerTemporalProperties : public QObject

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

private:
QgsRasterLayerTemporalProperties *temporalProperties = nullptr;
@@ -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.
You can’t perform that action at this time.