QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsdatetimestatisticalsummary.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdatetimestatisticalsummary.cpp
3 ---------------------------------
4 Date : May 2016
5 Copyright : (C) 2016 by Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include <limits>
19
20#include "qgsvariantutils.h"
21
22#include <QDateTime>
23#include <QObject>
24#include <QString>
25#include <QStringList>
26#include <QVariant>
27#include <QVariantList>
28
29/***************************************************************************
30 * This class is considered CRITICAL and any change MUST be accompanied with
31 * full unit tests in test_qgsdatetimestatisticalsummary.py.
32 * See details in QEP #17
33 ****************************************************************************/
34
40
42{
43 mCount = 0;
44 mValues.clear();
45 mCountMissing = 0;
46 mMin = QDateTime();
47 mMax = QDateTime();
48 mIsTimes = false;
49}
50
51void QgsDateTimeStatisticalSummary::calculate( const QVariantList &values )
52{
53 reset();
54
55 const auto constValues = values;
56 for ( const QVariant &variant : constValues )
57 {
58 addValue( variant );
59 }
60 finalize();
61}
62
63void QgsDateTimeStatisticalSummary::addValue( const QVariant &value )
64{
65
66 if ( value.userType() == QMetaType::Type::QDateTime )
67 {
68 testDateTime( value.toDateTime(), QgsVariantUtils::isNull( value ) );
69 }
70 else if ( value.userType() == QMetaType::Type::QDate )
71 {
72 const QDate date = value.toDate();
73 testDateTime( date.isValid() ? QDateTime( date, QTime( 0, 0, 0 ) )
74 : QDateTime(), QgsVariantUtils::isNull( value ) );
75 }
76 else if ( value.userType() == QMetaType::Type::QTime )
77 {
78 mIsTimes = true;
79 const QTime time = value.toTime();
80 testDateTime( time.isValid() ? QDateTime( QDate::fromJulianDay( 0 ), time )
81 : QDateTime(), QgsVariantUtils::isNull( value ) );
82 }
83 else //not a date
84 {
85 mCountMissing++;
86 mCount++;
87 }
88
89 // QTime?
90}
91
93{
94 //nothing to do for now - this method has been added for forward compatibility
95 //if statistics are implemented which require a post-calculation step
96}
97
98void QgsDateTimeStatisticalSummary::testDateTime( const QDateTime &dateTime, bool isNull )
99{
100 mCount++;
101
102 if ( !dateTime.isValid() || isNull )
103 mCountMissing++;
104
105 if ( mStatistics & Qgis::DateTimeStatistic::CountDistinct )
106 {
107 mValues << dateTime;
108 }
109 if ( mStatistics & Qgis::DateTimeStatistic::Min || mStatistics & Qgis::DateTimeStatistic::Range )
110 {
111 if ( mMin.isValid() && dateTime.isValid() )
112 {
113 mMin = std::min( mMin, dateTime );
114 }
115 else if ( !mMin.isValid() && dateTime.isValid() )
116 {
117 mMin = dateTime;
118 }
119 }
120 if ( mStatistics & Qgis::DateTimeStatistic::Max || mStatistics & Qgis::DateTimeStatistic::Range )
121 {
122 if ( mMax.isValid() && dateTime.isValid() )
123 {
124 mMax = std::max( mMax, dateTime );
125 }
126 else if ( !mMax.isValid() && dateTime.isValid() )
127 {
128 mMax = dateTime;
129 }
130 }
131}
132
134{
135 switch ( stat )
136 {
138 return mCount;
140 return mValues.count();
142 return mCountMissing;
144 return mIsTimes ? QVariant( mMin.time() ) : QVariant( mMin );
146 return mIsTimes ? QVariant( mMax.time() ) : QVariant( mMax );
148#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
149 return mIsTimes ? QVariant::fromValue( mMax.time() - mMin.time() ) : QVariant::fromValue( mMax - mMin );
150#else
151 return mIsTimes ? QVariant::fromValue( mMax.time() - mMin.time() ) : QVariant::fromValue( QgsInterval( static_cast< double >( ( mMax - mMin ).count() ) / 1000.0 ) );
152#endif
154 return 0;
155 }
156 return 0;
157}
158
160{
161#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
162 return mMax - mMin;
163#else
164 return QgsInterval( static_cast< double >( ( mMax - mMin ).count() ) / 1000.0 );
165#endif
166}
167
169{
170 switch ( statistic )
171 {
173 return QObject::tr( "Count" );
175 return QObject::tr( "Count (distinct)" );
177 return QObject::tr( "Count (missing)" );
179 return QObject::tr( "Minimum (earliest)" );
181 return QObject::tr( "Maximum (latest)" );
183 return QObject::tr( "Range (interval)" );
185 return QString();
186 }
187 return QString();
188}
189
QFlags< DateTimeStatistic > DateTimeStatistics
Statistics to be calculated for date/time values.
Definition qgis.h:5901
DateTimeStatistic
Available date/time statistics.
Definition qgis.h:5885
@ Max
Maximum (latest) datetime value.
Definition qgis.h:5890
@ Min
Minimum (earliest) datetime value.
Definition qgis.h:5889
@ Range
Interval between earliest and latest datetime value.
Definition qgis.h:5891
@ CountMissing
Number of missing (null) values.
Definition qgis.h:5888
@ All
All statistics.
Definition qgis.h:5892
@ CountDistinct
Number of distinct datetime values.
Definition qgis.h:5887
QVariant statistic(Qgis::DateTimeStatistic stat) const
Returns the value of a specified statistic.
QgsDateTimeStatisticalSummary(Qgis::DateTimeStatistics stats=Qgis::DateTimeStatistic::All)
Constructor for QgsDateTimeStatisticalSummary.
QgsInterval range() const
Returns the range (interval between earliest and latest non-null datetime values).
void calculate(const QVariantList &values)
Calculates summary statistics for a list of variants.
static QString displayName(Qgis::DateTimeStatistic statistic)
Returns the friendly display name for a statistic.
void addValue(const QVariant &value)
Adds a single datetime to the statistics calculation.
void finalize()
Must be called after adding all datetimes with addValue() and before retrieving any calculated dateti...
void reset()
Resets the calculated values.
int count() const
Returns the calculated count of values.
A representation of the interval between two datetime values.
Definition qgsinterval.h:47
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.