QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgsmeshdataprovidertemporalcapabilities.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmeshdataprovidertemporalcapabilities.cpp
3 -----------------------
4 begin : March 2020
5 copyright : (C) 2020 by Vincent Cloarec
6 email : vcloarec at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "qgsunittypes.h"
20
22{}
23
25{
26 // No time --> non temporal dataset, so return the dataset that has to be the only one
27 const QList<qint64> &datasetTimes = mDatasetTimeSinceGroupReference[group];
28 if ( datasetTimes.isEmpty() )
29 return QgsMeshDatasetIndex( group, 0 );
30 const QDateTime groupReference = mGroupsReferenceDateTime[group];
31 const qint64 timeSinceGroupReference =
32 timeSinceGlobalReference - mGlobalReferenceDateTime.msecsTo( groupReference );
33
34 if ( timeSinceGroupReference > datasetTimes.last() // after last time
35 || timeSinceGroupReference < datasetTimes.first() ) // before first time
36 return QgsMeshDatasetIndex();
37
38 for ( int i = 1 ; i < datasetTimes.count(); ++i )
39 {
40 const qint64 time1 = datasetTimes.at( i - 1 );
41 const qint64 time2 = datasetTimes.at( i );
42 if ( time1 <= timeSinceGroupReference && timeSinceGroupReference <= time2 )
43 {
44 if ( abs( timeSinceGroupReference - time2 ) < abs( timeSinceGroupReference - time1 ) )
45 return QgsMeshDatasetIndex( group, i );
46 else
47 return QgsMeshDatasetIndex( group, i - 1 );
48 }
49 }
50
51 return QgsMeshDatasetIndex( QgsMeshDatasetIndex( group, datasetTimes.count() - 1 ) );
52}
53
55{
56 // No time --> non temporal dataset, so return the dataset that has to be the only one
57 const QList<qint64> &datasetTimes = mDatasetTimeSinceGroupReference[group];
58 if ( datasetTimes.isEmpty() )
59 return QgsMeshDatasetIndex( group, 0 );
60 const QDateTime groupReference = mGroupsReferenceDateTime[group];
61 const qint64 timeSinceGroupReference =
62 timeSinceGlobalReference - mGlobalReferenceDateTime.msecsTo( groupReference );
63
64 if ( timeSinceGroupReference > datasetTimes.last() // after last time
65 || timeSinceGroupReference < datasetTimes.first() ) // before first time
66 return QgsMeshDatasetIndex();
67
68 for ( int i = 1; i < datasetTimes.count(); ++i )
69 {
70 const qint64 time = datasetTimes.at( i );
71 if ( timeSinceGroupReference < time )
72 return QgsMeshDatasetIndex( group, i - 1 );
73 }
74
75 return QgsMeshDatasetIndex( QgsMeshDatasetIndex( group, datasetTimes.count() - 1 ) );
76}
77
78void QgsMeshDataProviderTemporalCapabilities::addGroupReferenceDateTime( int group, const QDateTime &reference )
79{
80 if ( ( !mGlobalReferenceDateTime.isValid() && reference.isValid() ) ||
81 ( reference.isValid() && mGlobalReferenceDateTime.isValid() && reference < mGlobalReferenceDateTime ) )
82 mGlobalReferenceDateTime = reference;
83
84 mGroupsReferenceDateTime[group] = reference;
85}
86
88{
89 QList<qint64> &datasetTimes = mDatasetTimeSinceGroupReference[group];
90 datasetTimes.append( time );
91}
92
94{
95 const qint64 unitTimeFactor = QgsUnitTypes::fromUnitToUnitFactor( mTemporalUnit, Qgis::TemporalUnit::Milliseconds );
96 addDatasetTimeInMilliseconds( group, time * unitTimeFactor );
97}
98
100{
101 return mGlobalReferenceDateTime.isValid();
102}
103
105{
106 return mGlobalReferenceDateTime;
107}
108
110{
111
112 return timeExtent( mGlobalReferenceDateTime );
113}
114
115QgsDateTimeRange QgsMeshDataProviderTemporalCapabilities::timeExtent( const QDateTime &reference ) const
116{
117 QDateTime end;
118 QDateTime begin;
119 for ( QHash<int, QDateTime>::const_iterator it = mGroupsReferenceDateTime.constBegin() ;
120 it != mGroupsReferenceDateTime.constEnd(); ++it )
121 {
122 QDateTime groupReference = it.value();
123 if ( !groupReference.isValid() ) //the dataset group has not a valid reference time -->take global reference
124 groupReference = mGlobalReferenceDateTime;
125
126 if ( !groupReference.isValid() )
127 groupReference = reference;
128
129 const QList<qint64> times = mDatasetTimeSinceGroupReference[it.key()];
130 qint64 durationSinceFirst = groupReference.msecsTo( reference );
131 qint64 durationSinceLast = groupReference.msecsTo( reference );
132 if ( !times.isEmpty() )
133 {
134 durationSinceFirst += times.first();
135 durationSinceLast += times.last();
136 }
137
138 if ( !end.isValid() || groupReference.addMSecs( durationSinceLast ) > end )
139 end = groupReference.addMSecs( durationSinceLast );
140
141 if ( !begin.isValid() || groupReference.addMSecs( durationSinceFirst ) > begin )
142 begin = groupReference.addMSecs( durationSinceFirst );
143 }
144
145 return QgsDateTimeRange( begin, end );
146}
147
149{
150 mTemporalUnit = timeUnit;
151}
152
154{
155 return mTemporalUnit;
156}
157
159{
160 if ( !index.isValid() )
162
163 const QList<qint64> &timesList = mDatasetTimeSinceGroupReference[index.group()];
164 if ( index.dataset() < timesList.count() )
165 return timesList.at( index.dataset() );
166 else
168}
169
171{
172 mGlobalReferenceDateTime = QDateTime();
173 mGroupsReferenceDateTime.clear();
174 mDatasetTimeSinceGroupReference.clear();
175}
176
178{
179 qint64 ret = -1;
180 if ( mDatasetTimeSinceGroupReference.contains( group ) )
181 {
182 const QList<qint64> times = mDatasetTimeSinceGroupReference[group];
183 if ( times.count() > 1 )
184 ret = times.at( 1 ) - times.at( 0 );
185 }
186 return ret;
187}
TemporalUnit
Temporal units.
Definition: qgis.h:3417
Base class for handling properties relating to a data provider's temporal capabilities.
void setTemporalUnit(Qgis::TemporalUnit temporalUnit)
Sets the temporal unit (temporalUnit) used to read data by the data provider.
void addDatasetTime(int group, double time)
Adds a time (in provider unit) from a dataset contained in group.
QDateTime referenceTime() const
Returns the reference time.
QgsMeshDatasetIndex datasetIndexClosestFromRelativeTime(int group, qint64 timeSinceGlobalReference) const
Returns the closest dataset index from the timeSinceGlobalReference.
bool hasReferenceTime() const
Returns whether the reference time is set.
QgsMeshDataProviderTemporalCapabilities()
Constructor for QgsMeshDataProviderTemporalCapabilities.
QgsDateTimeRange timeExtent() const
Returns the time extent using the internal reference time and the first and last times available from...
Qgis::TemporalUnit temporalUnit() const
Returns the temporal unit used to read data by the data provider.
QgsMeshDatasetIndex datasetIndexClosestBeforeRelativeTime(int group, qint64 timeSinceGlobalReference) const
Returns the last dataset with time less than or equal to timeSinceGlobalReference.
void addDatasetTimeInMilliseconds(int group, qint64 time)
Adds a time (in milliseconds) from a dataset contained in group.
void clear()
Clears alls stored reference times and dataset times.
qint64 datasetTime(const QgsMeshDatasetIndex &index) const
Returns the relative time in milliseconds of the dataset.
void addGroupReferenceDateTime(int group, const QDateTime &reference)
Adds a reference date/time from a dataset group.
qint64 firstTimeStepDuration(int group) const
Returns the duration of the first time step of the dataset group with index group.
QgsMeshDatasetIndex is index that identifies the dataset group (e.g.
bool isValid() const
Returns whether index is valid, ie at least groups is set.
int group() const
Returns a group index.
int dataset() const
Returns a dataset index within group()
static Q_INVOKABLE double fromUnitToUnitFactor(Qgis::DistanceUnit fromUnit, Qgis::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.