19#include "moc_qgsvectorlayertemporalproperties.cpp"
54 QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( layer );
66 if ( fieldIndex >= 0 )
72 const QDateTime min = minVal.toDateTime();
73 const QDateTime maxStartTime = maxVal.toDateTime();
83 const int durationFieldIndex = vectorLayer->
fields().
lookupField( mDurationFieldName );
84 if ( fieldIndex >= 0 && durationFieldIndex >= 0 )
86 const QDateTime minTime = vectorLayer->
minimumValue( fieldIndex ).toDateTime();
94 const QDateTime start = f.
attribute( fieldIndex ).toDateTime();
95 if ( start.isValid() )
97 const QVariant durationValue = f.
attribute( durationFieldIndex );
98 if ( durationValue.isValid() )
100 const double duration = durationValue.toDouble();
101 const QDateTime end = start.addMSecs(
QgsInterval( duration, mDurationUnit ).seconds() * 1000.0 );
103 maxTime = maxTime.isValid() ? std::max( maxTime, end ) : end;
114 const int startFieldIndex = vectorLayer->
fields().
lookupField( mStartFieldName );
116 if ( startFieldIndex >= 0 && endFieldIndex >= 0 )
118 QVariant startMinVal;
119 QVariant startMaxVal;
126 endMinVal.toDateTime() ),
127 std::max( startMaxVal.toDateTime(),
128 endMaxVal.toDateTime() ) );
130 else if ( startFieldIndex >= 0 )
132 QVariant startMinVal;
133 QVariant startMaxVal;
136 startMaxVal.toDateTime() );
138 else if ( endFieldIndex >= 0 )
144 endMaxVal.toDateTime() );
151 const bool hasStartExpression = !mStartExpression.isEmpty();
152 const bool hasEndExpression = !mEndExpression.isEmpty();
153 if ( !hasStartExpression && !hasEndExpression )
164 if ( hasStartExpression )
171 if ( hasEndExpression )
177 QSet< QString > fields;
178 if ( hasStartExpression )
180 if ( hasEndExpression )
196 const QDateTime start = hasStartExpression ?
startExpression.evaluate( &context ).toDateTime() : QDateTime();
197 const QDateTime end = hasEndExpression ?
endExpression.evaluate( &context ).toDateTime() : QDateTime();
199 if ( start.isValid() )
201 minTime = minTime.isValid() ? std::min( minTime, start ) : start;
202 if ( !hasEndExpression )
203 maxTime = maxTime.isValid() ? std::max( maxTime, start ) : start;
207 maxTime = maxTime.isValid() ? std::max( maxTime, end ) : end;
208 if ( !hasStartExpression )
209 minTime = minTime.isValid() ? std::min( minTime, end ) : end;
265 const QDomElement temporalNode = element.firstChildElement( QStringLiteral(
"temporal" ) );
267 setIsActive( temporalNode.attribute( QStringLiteral(
"enabled" ), QStringLiteral(
"0" ) ).toInt() );
269 mMode =
static_cast< Qgis::VectorTemporalMode >( temporalNode.attribute( QStringLiteral(
"mode" ), QStringLiteral(
"0" ) ). toInt() );
271 mLimitMode =
static_cast< Qgis::VectorTemporalLimitMode >( temporalNode.attribute( QStringLiteral(
"limitMode" ), QStringLiteral(
"0" ) ). toInt() );
272 mStartFieldName = temporalNode.attribute( QStringLiteral(
"startField" ) );
273 mEndFieldName = temporalNode.attribute( QStringLiteral(
"endField" ) );
274 mStartExpression = temporalNode.attribute( QStringLiteral(
"startExpression" ) );
275 mEndExpression = temporalNode.attribute( QStringLiteral(
"endExpression" ) );
276 mDurationFieldName = temporalNode.attribute( QStringLiteral(
"durationField" ) );
278 mFixedDuration = temporalNode.attribute( QStringLiteral(
"fixedDuration" ) ).toDouble();
279 mAccumulateFeatures = temporalNode.attribute( QStringLiteral(
"accumulate" ), QStringLiteral(
"0" ) ).toInt();
281 const QDomNode rangeElement = temporalNode.namedItem( QStringLiteral(
"fixedRange" ) );
283 const QDomNode begin = rangeElement.namedItem( QStringLiteral(
"start" ) );
284 const QDomNode end = rangeElement.namedItem( QStringLiteral(
"end" ) );
286 const QDateTime beginDate = QDateTime::fromString( begin.toElement().text(), Qt::ISODate );
287 const QDateTime endDate = QDateTime::fromString( end.toElement().text(), Qt::ISODate );
298 if ( element.isNull() )
299 return QDomElement();
301 QDomElement temporalElement = document.createElement( QStringLiteral(
"temporal" ) );
302 temporalElement.setAttribute( QStringLiteral(
"enabled" ),
isActive() ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
303 temporalElement.setAttribute( QStringLiteral(
"mode" ), QString::number(
static_cast< int >( mMode ) ) );
305 temporalElement.setAttribute( QStringLiteral(
"limitMode" ), QString::number(
static_cast< int >( mLimitMode ) ) );
306 temporalElement.setAttribute( QStringLiteral(
"startField" ), mStartFieldName );
307 temporalElement.setAttribute( QStringLiteral(
"endField" ), mEndFieldName );
308 temporalElement.setAttribute( QStringLiteral(
"startExpression" ), mStartExpression );
309 temporalElement.setAttribute( QStringLiteral(
"endExpression" ), mEndExpression );
310 temporalElement.setAttribute( QStringLiteral(
"durationField" ), mDurationFieldName );
312 temporalElement.setAttribute( QStringLiteral(
"fixedDuration" ),
qgsDoubleToString( mFixedDuration ) );
313 temporalElement.setAttribute( QStringLiteral(
"accumulate" ), mAccumulateFeatures ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
315 QDomElement rangeElement = document.createElement( QStringLiteral(
"fixedRange" ) );
317 QDomElement startElement = document.createElement( QStringLiteral(
"start" ) );
318 QDomElement endElement = document.createElement( QStringLiteral(
"end" ) );
320 const QDomText startText = document.createTextNode( mFixedRange.
begin().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate ) );
321 const QDomText endText = document.createTextNode( mFixedRange.
end().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate ) );
322 startElement.appendChild( startText );
323 endElement.appendChild( endText );
324 rangeElement.appendChild( startElement );
325 rangeElement.appendChild( endElement );
327 temporalElement.appendChild( rangeElement );
329 element.appendChild( temporalElement );
338 setIsActive( vectorCaps->hasTemporalCapabilities() );
342 switch ( vectorCaps->mode() )
359 return mStartExpression;
369 return mEndExpression;
379 return mAccumulateFeatures;
389 return mFixedDuration;
399 return mStartFieldName;
404 mStartFieldName = startFieldName;
409 return mEndFieldName;
414 mEndFieldName = field;
419 return mDurationFieldName;
424 mDurationFieldName = field;
429 return mDurationUnit;
434 mDurationUnit = units;
439 return QStringLiteral(
"make_datetime(%1,%2,%3,%4,%5,%6)" ).arg( datetime.date().year() )
440 .arg( datetime.date().month() )
441 .arg( datetime.date().day() )
442 .arg( datetime.time().hour() )
443 .arg( datetime.time().minute() )
444 .arg( datetime.time().second() + datetime.time().msec() / 1000.0 );
452 auto dateTimefieldCast = [ &context ](
const QString & fieldName ) -> QString
471 if ( mAccumulateFeatures )
473 return QStringLiteral(
"(%1 %2 %3) OR %1 IS NULL" ).arg( dateTimefieldCast( mStartFieldName ),
474 filterRange.
includeEnd() ? QStringLiteral(
"<=" ) : QStringLiteral(
"<" ),
479 return QStringLiteral(
"(%1 %2 %3 AND %1 %4 %5) OR %1 IS NULL" ).arg( dateTimefieldCast( mStartFieldName ),
480 filterRange.
includeBeginning() ? QStringLiteral(
">=" ) : QStringLiteral(
">" ),
482 filterRange.
includeEnd() ? QStringLiteral(
"<=" ) : QStringLiteral(
"<" ),
488 return QStringLiteral(
"(%1 %2 %3 AND %1 %4 %5) OR %1 IS NULL" ).arg( dateTimefieldCast( mStartFieldName ),
491 filterRange.
includeEnd() ? QStringLiteral(
"<=" ) : QStringLiteral(
"<" ),
498 QString intervalExpression;
499 switch ( mDurationUnit )
545 return QStringLiteral(
"(%1 %2 %3 OR %1 IS NULL) AND ((%1 + %4 %5 %6) OR %7 IS NULL)" ).arg( dateTimefieldCast( mStartFieldName ),
546 filterRange.
includeEnd() ? QStringLiteral(
"<=" ) : QStringLiteral(
"<" ),
556 if ( !mStartFieldName.isEmpty() && !mEndFieldName.isEmpty() )
558 return QStringLiteral(
"(%1 %2 %3 OR %1 IS NULL) AND (%4 %5 %6 OR %4 IS NULL)" ).arg( dateTimefieldCast( mStartFieldName ),
559 filterRange.
includeEnd() ? QStringLiteral(
"<=" ) : QStringLiteral(
"<" ),
561 dateTimefieldCast( mEndFieldName ),
566 else if ( !mStartFieldName.isEmpty() )
568 return QStringLiteral(
"%1 %2 %3 OR %1 IS NULL" ).arg( dateTimefieldCast( mStartFieldName ),
569 filterRange.
includeEnd() ? QStringLiteral(
"<=" ) : QStringLiteral(
"<" ),
572 else if ( !mEndFieldName.isEmpty() )
574 return QStringLiteral(
"%1 %2 %3 OR %1 IS NULL" ).arg( dateTimefieldCast( mEndFieldName ),
583 if ( !mStartExpression.isEmpty() && !mEndExpression.isEmpty() )
585 return QStringLiteral(
"((%1) %2 %3) AND ((%4) %5 %6)" ).arg( mStartExpression,
586 filterRange.
includeEnd() ? QStringLiteral(
"<=" ) : QStringLiteral(
"<" ),
592 else if ( !mStartExpression.isEmpty() )
594 return QStringLiteral(
"(%1) %2 %3" ).arg( mStartExpression,
595 filterRange.
includeEnd() ? QStringLiteral(
"<=" ) : QStringLiteral(
"<" ),
598 else if ( !mEndExpression.isEmpty() )
600 return QStringLiteral(
"(%1) %2 %3" ).arg( mEndExpression,
620 static const QStringList sStartCandidates{ QStringLiteral(
"start" ),
621 QStringLiteral(
"begin" ),
622 QStringLiteral(
"from" ),
623 QStringLiteral(
"since" ),
625 QStringLiteral(
"anfang" ),
626 QStringLiteral(
"von" ),
627 QStringLiteral(
"ab" ),
628 QStringLiteral(
"seit" ) };
630 static const QStringList sEndCandidates{ QStringLiteral(
"end" ),
631 QStringLiteral(
"last" ),
632 QStringLiteral(
"to" ),
633 QStringLiteral(
"stop" ),
635 QStringLiteral(
"ende" ),
636 QStringLiteral(
"bis" ) };
639 static const QStringList sSingleFieldCandidates{ QStringLiteral(
"event" ) };
642 bool foundStart =
false;
643 bool foundEnd =
false;
645 for (
const QgsField &field : fields )
647 if ( field.type() != QMetaType::Type::QDate && field.type() != QMetaType::Type::QDateTime )
652 for (
const QString &candidate : sStartCandidates )
654 const QString fldName = field.name();
655 if ( fldName.indexOf( candidate, 0, Qt::CaseInsensitive ) > -1 )
657 mStartFieldName = fldName;
665 for (
const QString &candidate : sEndCandidates )
667 const QString fldName = field.name();
668 if ( fldName.indexOf( candidate, 0, Qt::CaseInsensitive ) > -1 )
670 mEndFieldName = fldName;
676 if ( foundStart && foundEnd )
683 for (
const QgsField &field : fields )
685 if ( field.type() != QMetaType::Type::QDate && field.type() != QMetaType::Type::QDateTime )
688 for (
const QString &candidate : sSingleFieldCandidates )
690 const QString fldName = field.name();
691 if ( fldName.indexOf( candidate, 0, Qt::CaseInsensitive ) > -1 )
693 mStartFieldName = fldName;
703 if ( foundStart && foundEnd )
705 else if ( foundStart )
VectorTemporalMode
Vector layer temporal feature modes.
@ FeatureDateTimeStartAndDurationFromFields
Mode when features have a field for start time and a field for event duration.
@ RedrawLayerOnly
Redraw the layer when temporal range changes, but don't apply any filtering. Useful when symbology or...
@ FeatureDateTimeStartAndEndFromExpressions
Mode when features use expressions for start and end times.
@ FeatureDateTimeInstantFromField
Mode when features have a datetime instant taken from a single field.
@ FixedTemporalRange
Mode when temporal properties have fixed start and end datetimes.
@ FeatureDateTimeStartAndEndFromFields
Mode when features have separate fields for start and end times.
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
TemporalUnit
Temporal units.
@ IrregularStep
Special 'irregular step' time unit, used for temporal data which uses irregular, non-real-world unit ...
@ Milliseconds
Milliseconds.
@ Unknown
Unknown time unit.
@ StoresFeatureDateTimeInstantInField
Dataset has feature datetime instants stored in a single field.
@ StoresFeatureDateTimeStartAndEndInSeparateFields
Dataset stores feature start and end datetimes in separate fields.
@ HasFixedTemporalRange
Entire dataset from provider has a fixed start and end datetime.
VectorTemporalLimitMode
Mode for the handling of the limits of the filtering timeframe for vector features.
@ IncludeBeginIncludeEnd
Mode to include both limits of the filtering timeframe.
Base class for handling properties relating to a data provider's temporal capabilities.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
Class for parsing and evaluation of expressions (formerly called "search strings").
void setExpression(const QString &expression)
Set the expression string, will reset the whole internal structure.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
A representation of the interval between two datetime values.
Base class for storage of map layer temporal properties.
Base class for all map layer types.
The class is used as a container of context for various read/write operations on other objects.
bool isActive() const
Returns true if the temporal property is active.
void setIsActive(bool active)
Sets whether the temporal property is active.
@ FlagDontInvalidateCachedRendersWhenRangeChanges
Any cached rendering will not be invalidated when temporal range context is modified.
T begin() const
Returns the beginning of the range.
T end() const
Returns the upper bound of the range.
bool overlaps(const QgsTemporalRange< T > &other) const
Returns true if this range overlaps another range.
bool includeEnd() const
Returns true if the end is inclusive, or false if the end is exclusive.
bool includeBeginning() const
Returns true if the beginning is inclusive, or false if the beginning is exclusive.
bool isInfinite() const
Returns true if the range consists of all possible values.
static Q_INVOKABLE Qgis::TemporalUnit decodeTemporalUnit(const QString &string, bool *ok=nullptr)
Decodes a temporal unit from a string.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
Implementation of data provider temporal properties for QgsVectorDataProviders.
Encapsulates the context in which a QgsVectorLayer's temporal capabilities will be applied.
QgsVectorLayer * layer() const
Returns the associated layer.
void setLayer(QgsVectorLayer *layer)
Sets the associated layer.
void guessDefaultsFromFields(const QgsFields &fields)
Attempts to setup the temporal properties by scanning a set of fields and looking for standard naming...
QString endExpression() const
Returns the expression for the end time for the feature's time spans.
void setDurationField(const QString &field)
Sets the name of the duration field, which contains the duration of the event.
void setMode(Qgis::VectorTemporalMode mode)
Sets the temporal properties mode.
QgsVectorLayerTemporalProperties(QObject *parent=nullptr, bool enabled=false)
Constructor for QgsVectorLayerTemporalProperties, with the specified parent object.
void setStartExpression(const QString &expression)
Sets the expression to use for the start time for the feature's time spans.
bool isVisibleInTemporalRange(const QgsDateTimeRange &range) const override
Returns true if the layer should be visible and rendered for the specified time range.
Qgis::VectorTemporalLimitMode limitMode() const
Returns the temporal limit mode (to include or exclude begin/end limits).
void setLimitMode(Qgis::VectorTemporalLimitMode mode)
Sets the temporal limit mode (to include or exclude begin/end limits).
const QgsDateTimeRange & fixedTemporalRange() const
Returns the fixed temporal range for the layer.
double fixedDuration() const
Returns the fixed duration length, which contains the duration of the event.
bool accumulateFeatures() const
Returns true if features will be accumulated over time (i.e.
QgsTemporalProperty::Flags flags() const override
Returns flags associated to the temporal property.
void setFixedTemporalRange(const QgsDateTimeRange &range)
Sets a temporal range to apply to the whole layer.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads temporal properties from a DOM element previously written by writeXml().
void setEndExpression(const QString &endExpression)
Sets the expression to use for the end time for the feature's time spans.
QString durationField() const
Returns the name of the duration field, which contains the duration of the event.
void setDurationUnits(Qgis::TemporalUnit units)
Sets the units of the event's duration.
QString endField() const
Returns the name of the end datetime field, which contains the end time for the feature's time spans.
QString createFilterString(const QgsVectorLayerTemporalContext &context, const QgsDateTimeRange &range) const
Creates a QGIS expression filter string for filtering features within the specified context to those ...
QDomElement writeXml(QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context) override
Writes the properties to a DOM element, to be used later with readXml().
Qgis::TemporalUnit durationUnits() const
Returns the units of the event's duration.
void setAccumulateFeatures(bool accumulate)
Sets whether features will be accumulated over time (i.e.
void setFixedDuration(double duration)
Sets the fixed event duration, which contains the duration of the event.
void setEndField(const QString &field)
Sets the name of the end datetime field, which contains the end time for the feature's time spans.
QgsDateTimeRange calculateTemporalExtent(QgsMapLayer *layer) const override
Attempts to calculate the overall temporal extent for the specified layer, using the settings defined...
void setDefaultsFromDataProviderTemporalCapabilities(const QgsDataProviderTemporalCapabilities *capabilities) override
Sets the layers temporal settings to appropriate defaults based on a provider's temporal capabilities...
Qgis::VectorTemporalMode mode() const
Returns the temporal properties mode.
QString startField() const
Returns the name of the start datetime field, which contains the start time for the feature's time sp...
void setStartField(const QString &field)
Sets the name of the start datetime field, which contains the start time for the feature's time spans...
QString startExpression() const
Returns the expression for the start time for the feature's time spans.
Represents a vector layer which manages a vector based data sets.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QVariant minimumValue(int index) const FINAL
Returns the minimum value for an attribute column or an invalid variant in case of error.
void minimumAndMaximumValue(int index, QVariant &minimum, QVariant &maximum) const
Calculates both the minimum and maximum value for an attribute column.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
QList< int > QgsAttributeList
QgsTemporalRange< QDateTime > QgsDateTimeRange
QgsRange which stores a range of date times.
QString dateTimeExpressionLiteral(const QDateTime &datetime)