Skip to content
Permalink
Browse files

[temporal] Fix computation of temporal extent for start/end expressio…

…n mode

(fixes #36408)
  • Loading branch information
nirvn committed May 14, 2020
1 parent cfc202a commit f52954f71a493ea2e05620adabd59a8c6821d770
Showing with 34 additions and 8 deletions.
  1. +34 −8 src/core/qgsvectorlayertemporalproperties.cpp
@@ -129,20 +129,38 @@ QgsDateTimeRange QgsVectorLayerTemporalProperties::calculateTemporalExtent( QgsM

case QgsVectorLayerTemporalProperties::ModeFeatureDateTimeStartAndEndFromExpressions:
{
bool hasStartExpression = !mStartExpression.isEmpty();
bool hasEndExpression = !mEndExpression.isEmpty();
if ( !hasStartExpression && !hasEndExpression )
return QgsDateTimeRange();

QDateTime minTime;
QDateTime maxTime;

// no choice here but to loop through all features
QgsExpressionContext context;
context.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( vectorLayer ) );

QgsExpression startExpression( mStartExpression );
QgsExpression endExpression( mEndExpression );
startExpression.prepare( &context );
endExpression.prepare( &context );
QgsExpression startExpression;
if ( hasStartExpression )
{
startExpression.setExpression( mStartExpression );
startExpression.prepare( &context );
}

QgsExpression endExpression;
if ( hasEndExpression )
{
endExpression.setExpression( mEndExpression );
endExpression.prepare( &context );
}

QSet< QString > fields;
if ( hasStartExpression )
fields.unite( startExpression.referencedColumns() );
if ( hasEndExpression )
fields.unite( endExpression.referencedColumns() );

QSet< QString > fields = startExpression.referencedColumns();
fields.unite( endExpression.referencedColumns() );
const bool needsGeom = startExpression.needsGeometry() || endExpression.needsGeometry();

QgsFeatureRequest req;
@@ -156,13 +174,21 @@ QgsDateTimeRange QgsVectorLayerTemporalProperties::calculateTemporalExtent( QgsM
while ( it.nextFeature( f ) )
{
context.setFeature( f );
const QDateTime start = startExpression.evaluate( &context ).toDateTime();
const QDateTime end = endExpression.evaluate( &context ).toDateTime();
const QDateTime start = hasStartExpression ? startExpression.evaluate( &context ).toDateTime() : QDateTime();
const QDateTime end = hasEndExpression ? endExpression.evaluate( &context ).toDateTime() : QDateTime();

if ( start.isValid() )
{
minTime = minTime.isValid() ? std::min( minTime, start ) : start;
if ( !hasEndExpression )
maxTime = maxTime.isValid() ? std::max( maxTime, start ) : start;
}
if ( end.isValid() )
{
maxTime = maxTime.isValid() ? std::max( maxTime, end ) : end;
if ( !hasStartExpression )
minTime = minTime.isValid() ? std::min( minTime, end ) : end;
}
}
return QgsDateTimeRange( minTime, maxTime );
}

0 comments on commit f52954f

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