Skip to content
Permalink
Browse files

Cleanup QgsTemporalUtils::calculateTemporalRangeForProject

Move logic to subclasses instead
  • Loading branch information
nyalldawson committed May 10, 2020
1 parent 702675c commit db7108b26f8092b9bf8871e831add8d73847f422
@@ -72,6 +72,11 @@ void QgsMeshLayerTemporalProperties::setDefaultsFromDataProviderTemporalCapabili
mTimeExtent = temporalCapabilities->timeExtent();
}

QgsDateTimeRange QgsMeshLayerTemporalProperties::calculateTemporalExtent( QgsMapLayer * ) const
{
return mTimeExtent;
}

QgsDateTimeRange QgsMeshLayerTemporalProperties::timeExtent() const
{
return mTimeExtent;
@@ -64,6 +64,7 @@ class CORE_EXPORT QgsMeshLayerTemporalProperties : public QgsMapLayerTemporalPro
QDomElement writeXml( QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context ) override;
bool readXml( const QDomElement &element, const QgsReadWriteContext &context ) override;
void setDefaultsFromDataProviderTemporalCapabilities( const QgsDataProviderTemporalCapabilities *capabilities ) override;
QgsDateTimeRange calculateTemporalExtent( QgsMapLayer *layer ) const override SIP_SKIP;

/**
* Returns the time extent
@@ -26,3 +26,8 @@ bool QgsMapLayerTemporalProperties::isVisibleInTemporalRange( const QgsDateTimeR
{
return true;
}

QgsDateTimeRange QgsMapLayerTemporalProperties::calculateTemporalExtent( QgsMapLayer * ) const
{
return QgsDateTimeRange();
}
@@ -104,6 +104,19 @@ class CORE_EXPORT QgsMapLayerTemporalProperties : public QgsTemporalProperty
*/
virtual void setDefaultsFromDataProviderTemporalCapabilities( const QgsDataProviderTemporalCapabilities *capabilities ) = 0;

#ifndef SIP_RUN
// sip gets confused with this, refuses to compile

/**
* Attempts to calculate the overall temporal extent for the specified \a layer, using
* the settings defined by the temporal properties object.
*
* May return an infinite range if the extent could not be calculated.
*
* \note Not available in Python bindings
*/
virtual QgsDateTimeRange calculateTemporalExtent( QgsMapLayer *layer ) const;
#endif
};

#endif // QGSMAPLAYERTEMPORALPROPERTIES_H
@@ -37,100 +37,12 @@ QgsDateTimeRange QgsTemporalUtils::calculateTemporalRangeForProject( QgsProject

if ( !currentLayer->temporalProperties() || !currentLayer->temporalProperties()->isActive() )
continue;
QgsDateTimeRange layerRange;
QgsDateTimeRange layerRange = currentLayer->temporalProperties()->calculateTemporalExtent( currentLayer );

switch ( currentLayer->type() )
{
case QgsMapLayerType::RasterLayer:
{
QgsRasterLayer *rasterLayer = qobject_cast< QgsRasterLayer *>( currentLayer );
const QgsRasterLayerTemporalProperties *temporalProperties = qobject_cast< const QgsRasterLayerTemporalProperties * >( rasterLayer->temporalProperties() );

switch ( temporalProperties->mode() )
{
case QgsRasterLayerTemporalProperties::ModeFixedTemporalRange:
layerRange = temporalProperties->fixedTemporalRange();
break;

case QgsRasterLayerTemporalProperties::ModeTemporalRangeFromDataProvider:
layerRange = rasterLayer->dataProvider()->temporalCapabilities()->availableTemporalRange();
break;
}
break;
}

case QgsMapLayerType::VectorLayer:
{
QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( currentLayer );

QgsVectorLayerTemporalProperties *properties = static_cast< QgsVectorLayerTemporalProperties * >( vectorLayer->temporalProperties() );
switch ( properties->mode() )
{
case QgsVectorLayerTemporalProperties::ModeFixedTemporalRange:
layerRange = properties->fixedTemporalRange();
break;

case QgsVectorLayerTemporalProperties::ModeFeatureDateTimeInstantFromField:
{
const int fieldIndex = vectorLayer->fields().lookupField( properties->startField() );
if ( fieldIndex >= 0 )
{
layerRange = QgsDateTimeRange( vectorLayer->minimumValue( fieldIndex ).toDateTime(),
vectorLayer->maximumValue( fieldIndex ).toDateTime() );
}
break;
}

case QgsVectorLayerTemporalProperties::ModeFeatureDateTimeStartAndEndFromFields:
{
const int startFieldIndex = vectorLayer->fields().lookupField( properties->startField() );
const int endFieldIndex = vectorLayer->fields().lookupField( properties->endField() );
if ( startFieldIndex >= 0 && endFieldIndex >= 0 )
{
layerRange = QgsDateTimeRange( std::min( vectorLayer->minimumValue( startFieldIndex ).toDateTime(),
vectorLayer->minimumValue( endFieldIndex ).toDateTime() ),
std::max( vectorLayer->maximumValue( startFieldIndex ).toDateTime(),
vectorLayer->maximumValue( endFieldIndex ).toDateTime() ) );
}
else if ( startFieldIndex >= 0 )
{
layerRange = QgsDateTimeRange( vectorLayer->minimumValue( startFieldIndex ).toDateTime(),
vectorLayer->maximumValue( startFieldIndex ).toDateTime() );
}
else if ( endFieldIndex >= 0 )
{
layerRange = QgsDateTimeRange( vectorLayer->minimumValue( endFieldIndex ).toDateTime(),
vectorLayer->maximumValue( endFieldIndex ).toDateTime() );
}
break;
}

case QgsVectorLayerTemporalProperties::ModeRedrawLayerOnly:
break;
}
break;
}

case QgsMapLayerType::MeshLayer:
{
QgsMeshLayer *meshLayer = qobject_cast<QgsMeshLayer *>( currentLayer );
const QgsMeshLayerTemporalProperties *temporalProperties = qobject_cast< const QgsMeshLayerTemporalProperties * >( meshLayer->temporalProperties() );
layerRange = temporalProperties->timeExtent();
break;
}

case QgsMapLayerType::PluginLayer:
case QgsMapLayerType::VectorTileLayer:
break;
}


if ( !minDate.isValid() || layerRange.begin() < minDate )
if ( layerRange.begin().isValid() && ( !minDate.isValid() || layerRange.begin() < minDate ) )
minDate = layerRange.begin();
if ( !maxDate.isValid() || layerRange.end() > maxDate )
if ( layerRange.end().isValid() && ( !maxDate.isValid() || layerRange.end() > maxDate ) )
maxDate = layerRange.end();


}

return QgsDateTimeRange( minDate, maxDate );
@@ -43,6 +43,59 @@ bool QgsVectorLayerTemporalProperties::isVisibleInTemporalRange( const QgsDateTi
return true;
}

QgsDateTimeRange QgsVectorLayerTemporalProperties::calculateTemporalExtent( QgsMapLayer *layer ) const
{
QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( layer );
if ( !layer )
return QgsDateTimeRange();

switch ( mMode )
{
case QgsVectorLayerTemporalProperties::ModeFixedTemporalRange:
return mFixedRange;

case QgsVectorLayerTemporalProperties::ModeFeatureDateTimeInstantFromField:
{
const int fieldIndex = vectorLayer->fields().lookupField( mStartFieldName );
if ( fieldIndex >= 0 )
{
return QgsDateTimeRange( vectorLayer->minimumValue( fieldIndex ).toDateTime(),
vectorLayer->maximumValue( fieldIndex ).toDateTime() );
}
break;
}

case QgsVectorLayerTemporalProperties::ModeFeatureDateTimeStartAndEndFromFields:
{
const int startFieldIndex = vectorLayer->fields().lookupField( mStartFieldName );
const int endFieldIndex = vectorLayer->fields().lookupField( mEndFieldName );
if ( startFieldIndex >= 0 && endFieldIndex >= 0 )
{
return QgsDateTimeRange( std::min( vectorLayer->minimumValue( startFieldIndex ).toDateTime(),
vectorLayer->minimumValue( endFieldIndex ).toDateTime() ),
std::max( vectorLayer->maximumValue( startFieldIndex ).toDateTime(),
vectorLayer->maximumValue( endFieldIndex ).toDateTime() ) );
}
else if ( startFieldIndex >= 0 )
{
return QgsDateTimeRange( vectorLayer->minimumValue( startFieldIndex ).toDateTime(),
vectorLayer->maximumValue( startFieldIndex ).toDateTime() );
}
else if ( endFieldIndex >= 0 )
{
return QgsDateTimeRange( vectorLayer->minimumValue( endFieldIndex ).toDateTime(),
vectorLayer->maximumValue( endFieldIndex ).toDateTime() );
}
break;
}

case QgsVectorLayerTemporalProperties::ModeRedrawLayerOnly:
break;
}

return QgsDateTimeRange();
}

QgsVectorLayerTemporalProperties::TemporalMode QgsVectorLayerTemporalProperties::mode() const
{
return mMode;
@@ -48,6 +48,7 @@ class CORE_EXPORT QgsVectorLayerTemporalProperties : public QgsMapLayerTemporalP
QgsVectorLayerTemporalProperties( QObject *parent SIP_TRANSFERTHIS = nullptr, bool enabled = false );

bool isVisibleInTemporalRange( const QgsDateTimeRange &range ) const override;
QgsDateTimeRange calculateTemporalExtent( QgsMapLayer *layer ) const override SIP_SKIP;

/**
* Mode of the vector temporal properties
@@ -40,6 +40,24 @@ bool QgsRasterLayerTemporalProperties::isVisibleInTemporalRange( const QgsDateTi
return true;
}

QgsDateTimeRange QgsRasterLayerTemporalProperties::calculateTemporalExtent( QgsMapLayer *layer ) const
{
QgsRasterLayer *rasterLayer = qobject_cast< QgsRasterLayer *>( layer );
if ( !rasterLayer )
return QgsDateTimeRange();

switch ( mMode )
{
case QgsRasterLayerTemporalProperties::ModeFixedTemporalRange:
return mFixedRange;

case QgsRasterLayerTemporalProperties::ModeTemporalRangeFromDataProvider:
return rasterLayer->dataProvider()->temporalCapabilities()->availableTemporalRange();
}

return QgsDateTimeRange();
}

QgsRasterLayerTemporalProperties::TemporalMode QgsRasterLayerTemporalProperties::mode() const
{
return mMode;
@@ -46,6 +46,7 @@ class CORE_EXPORT QgsRasterLayerTemporalProperties : public QgsMapLayerTemporalP
QgsRasterLayerTemporalProperties( QObject *parent SIP_TRANSFERTHIS = nullptr, bool enabled = false );

bool isVisibleInTemporalRange( const QgsDateTimeRange &range ) const override;
QgsDateTimeRange calculateTemporalExtent( QgsMapLayer *layer ) const override SIP_SKIP;

/**
* Mode of the raster temporal properties

0 comments on commit db7108b

Please sign in to comment.
You can’t perform that action at this time.