Skip to content

Commit f52954f

Browse files
committedMay 14, 2020
[temporal] Fix computation of temporal extent for start/end expression mode
(fixes #36408)
1 parent cfc202a commit f52954f

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed
 

‎src/core/qgsvectorlayertemporalproperties.cpp

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,38 @@ QgsDateTimeRange QgsVectorLayerTemporalProperties::calculateTemporalExtent( QgsM
129129

130130
case QgsVectorLayerTemporalProperties::ModeFeatureDateTimeStartAndEndFromExpressions:
131131
{
132+
bool hasStartExpression = !mStartExpression.isEmpty();
133+
bool hasEndExpression = !mEndExpression.isEmpty();
134+
if ( !hasStartExpression && !hasEndExpression )
135+
return QgsDateTimeRange();
136+
132137
QDateTime minTime;
133138
QDateTime maxTime;
134139

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

139-
QgsExpression startExpression( mStartExpression );
140-
QgsExpression endExpression( mEndExpression );
141-
startExpression.prepare( &context );
142-
endExpression.prepare( &context );
144+
QgsExpression startExpression;
145+
if ( hasStartExpression )
146+
{
147+
startExpression.setExpression( mStartExpression );
148+
startExpression.prepare( &context );
149+
}
150+
151+
QgsExpression endExpression;
152+
if ( hasEndExpression )
153+
{
154+
endExpression.setExpression( mEndExpression );
155+
endExpression.prepare( &context );
156+
}
157+
158+
QSet< QString > fields;
159+
if ( hasStartExpression )
160+
fields.unite( startExpression.referencedColumns() );
161+
if ( hasEndExpression )
162+
fields.unite( endExpression.referencedColumns() );
143163

144-
QSet< QString > fields = startExpression.referencedColumns();
145-
fields.unite( endExpression.referencedColumns() );
146164
const bool needsGeom = startExpression.needsGeometry() || endExpression.needsGeometry();
147165

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

162180
if ( start.isValid() )
181+
{
163182
minTime = minTime.isValid() ? std::min( minTime, start ) : start;
183+
if ( !hasEndExpression )
184+
maxTime = maxTime.isValid() ? std::max( maxTime, start ) : start;
185+
}
164186
if ( end.isValid() )
187+
{
165188
maxTime = maxTime.isValid() ? std::max( maxTime, end ) : end;
189+
if ( !hasStartExpression )
190+
minTime = minTime.isValid() ? std::min( minTime, end ) : end;
191+
}
166192
}
167193
return QgsDateTimeRange( minTime, maxTime );
168194
}

0 commit comments

Comments
 (0)
Please sign in to comment.