QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
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  qint64 time1 = datasetTimes.at( i - 1 );
41  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  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 
78 void 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  qint64 unitTimeFactor = QgsUnitTypes::fromUnitToUnitFactor( mTemporalUnit, QgsUnitTypes::TemporalMilliseconds );
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 
115 QgsDateTimeRange 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 
127  if ( !groupReference.isValid() )
128  groupReference = reference;
129 
130  const QList<qint64> times = mDatasetTimeSinceGroupReference[it.key()];
131  qint64 durationSinceFirst = groupReference.msecsTo( reference );
132  qint64 durationSinceLast = groupReference.msecsTo( reference );
133  if ( !times.isEmpty() )
134  {
135  durationSinceFirst += times.first();
136  durationSinceLast += times.last();
137  }
138 
139  if ( !end.isValid() || groupReference.addMSecs( durationSinceLast ) > end )
140  end = groupReference.addMSecs( durationSinceLast );
141 
142  if ( !begin.isValid() || groupReference.addMSecs( durationSinceFirst ) > begin )
143  begin = groupReference.addMSecs( durationSinceFirst );
144  }
145 
146  return QgsDateTimeRange( begin, end );
147 }
148 
150 {
151  mTemporalUnit = timeUnit;
152 }
153 
155 {
156  return mTemporalUnit;
157 }
158 
160 {
161  if ( !index.isValid() )
162  return INVALID_MESHLAYER_TIME;
163 
164  const QList<qint64> &timesList = mDatasetTimeSinceGroupReference[index.group()];
165  if ( index.dataset() < timesList.count() )
166  return timesList.at( index.dataset() );
167  else
168  return INVALID_MESHLAYER_TIME;
169 }
170 
172 {
173  mGlobalReferenceDateTime = QDateTime();
174  mGroupsReferenceDateTime.clear();
175  mDatasetTimeSinceGroupReference.clear();
176 }
177 
179 {
180  qint64 ret = -1;
181  if ( mDatasetTimeSinceGroupReference.contains( group ) )
182  {
183  const QList<qint64> times = mDatasetTimeSinceGroupReference[group];
184  if ( times.count() > 1 )
185  ret = times.at( 1 ) - times.at( 0 );
186  }
187  return ret;
188 }
QgsMeshDatasetIndex::dataset
int dataset() const
Returns a dataset index within group()
Definition: qgsmeshdataset.cpp:31
QgsMeshDataProviderTemporalCapabilities::temporalUnit
QgsUnitTypes::TemporalUnit temporalUnit() const
Returns the temporal unit used to read data by the data provider.
Definition: qgsmeshdataprovidertemporalcapabilities.cpp:154
QgsMeshDataProviderTemporalCapabilities::setTemporalUnit
void setTemporalUnit(QgsUnitTypes::TemporalUnit temporalUnit)
Sets the temporal unit (temporalUnit) used to read data by the data provider.
Definition: qgsmeshdataprovidertemporalcapabilities.cpp:149
QgsMeshDataProviderTemporalCapabilities::hasReferenceTime
bool hasReferenceTime() const
Returns whether the reference time is set.
Definition: qgsmeshdataprovidertemporalcapabilities.cpp:99
QgsMeshDataProviderTemporalCapabilities::QgsMeshDataProviderTemporalCapabilities
QgsMeshDataProviderTemporalCapabilities()
Constructor for QgsMeshDataProviderTemporalCapabilities.
Definition: qgsmeshdataprovidertemporalcapabilities.cpp:21
QgsMeshDataProviderTemporalCapabilities::datasetTime
qint64 datasetTime(const QgsMeshDatasetIndex &index) const
Returns the relative time in milliseconds of the dataset.
Definition: qgsmeshdataprovidertemporalcapabilities.cpp:159
QgsUnitTypes::TemporalUnit
TemporalUnit
Temporal units.
Definition: qgsunittypes.h:150
QgsMeshDataProviderTemporalCapabilities::addDatasetTime
void addDatasetTime(int group, double time)
Adds a time (in provider unit) from a dataset contained in group.
Definition: qgsmeshdataprovidertemporalcapabilities.cpp:93
QgsMeshDataProviderTemporalCapabilities::datasetIndexClosestFromRelativeTime
QgsMeshDatasetIndex datasetIndexClosestFromRelativeTime(int group, qint64 timeSinceGlobalReference) const
Returns the closest dataset index from the timeSinceGlobalReference.
Definition: qgsmeshdataprovidertemporalcapabilities.cpp:24
QgsDataProviderTemporalCapabilities
Base class for handling properties relating to a data provider's temporal capabilities.
Definition: qgsdataprovidertemporalcapabilities.h:34
QgsUnitTypes::fromUnitToUnitFactor
static Q_INVOKABLE double fromUnitToUnitFactor(QgsUnitTypes::DistanceUnit fromUnit, QgsUnitTypes::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
Definition: qgsunittypes.cpp:352
QgsMeshDatasetIndex::group
int group() const
Returns a group index.
Definition: qgsmeshdataset.cpp:26
QgsMeshDataProviderTemporalCapabilities::addDatasetTimeInMilliseconds
void addDatasetTimeInMilliseconds(int group, qint64 time)
Adds a time (in milliseconds) from a dataset contained in group.
Definition: qgsmeshdataprovidertemporalcapabilities.cpp:87
QgsMeshDatasetIndex
QgsMeshDatasetIndex is index that identifies the dataset group (e.g.
Definition: qgsmeshdataset.h:47
QgsMeshDatasetIndex::isValid
bool isValid() const
Returns whether index is valid, ie at least groups is set.
Definition: qgsmeshdataset.cpp:36
QgsMeshDataProviderTemporalCapabilities::firstTimeStepDuration
qint64 firstTimeStepDuration(int group) const
Returns the duration of the first time step of the dataset group with index group.
Definition: qgsmeshdataprovidertemporalcapabilities.cpp:178
QgsUnitTypes::TemporalMilliseconds
@ TemporalMilliseconds
Milliseconds.
Definition: qgsunittypes.h:151
QgsMeshDataProviderTemporalCapabilities::datasetIndexClosestBeforeRelativeTime
QgsMeshDatasetIndex datasetIndexClosestBeforeRelativeTime(int group, qint64 timeSinceGlobalReference) const
Returns the last dataset with time less than or equal to timeSinceGlobalReference.
Definition: qgsmeshdataprovidertemporalcapabilities.cpp:54
qgsmeshdataprovidertemporalcapabilities.h
QgsMeshDataProviderTemporalCapabilities::clear
void clear()
Clears alls stored reference times and dataset times.
Definition: qgsmeshdataprovidertemporalcapabilities.cpp:171
QgsMeshDataProviderTemporalCapabilities::addGroupReferenceDateTime
void addGroupReferenceDateTime(int group, const QDateTime &reference)
Adds a reference date/time from a dataset group.
Definition: qgsmeshdataprovidertemporalcapabilities.cpp:78
QgsMeshDataProviderTemporalCapabilities::timeExtent
QgsDateTimeRange timeExtent() const
Returns the time extent using the internal reference time and the first and last times available from...
Definition: qgsmeshdataprovidertemporalcapabilities.cpp:109
QgsMeshDataProviderTemporalCapabilities::referenceTime
QDateTime referenceTime() const
Returns the reference time.
Definition: qgsmeshdataprovidertemporalcapabilities.cpp:104
INVALID_MESHLAYER_TIME
#define INVALID_MESHLAYER_TIME
Definition: qgsmeshdataprovidertemporalcapabilities.h:25