QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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#include "qgsvariantutils.h"
18#include <QString>
19#include <QDateTime>
20#include <QStringList>
21#include <QObject>
22#include <QVariant>
23#include <QVariantList>
24#include <limits>
25
26/***************************************************************************
27 * This class is considered CRITICAL and any change MUST be accompanied with
28 * full unit tests in test_qgsdatetimestatisticalsummary.py.
29 * See details in QEP #17
30 ****************************************************************************/
31
37
39{
40 mCount = 0;
41 mValues.clear();
42 mCountMissing = 0;
43 mMin = QDateTime();
44 mMax = QDateTime();
45 mIsTimes = false;
46}
47
48void QgsDateTimeStatisticalSummary::calculate( const QVariantList &values )
49{
50 reset();
51
52 const auto constValues = values;
53 for ( const QVariant &variant : constValues )
54 {
55 addValue( variant );
56 }
57 finalize();
58}
59
60void QgsDateTimeStatisticalSummary::addValue( const QVariant &value )
61{
62
63 if ( value.userType() == QMetaType::Type::QDateTime )
64 {
65 testDateTime( value.toDateTime(), QgsVariantUtils::isNull( value ) );
66 }
67 else if ( value.userType() == QMetaType::Type::QDate )
68 {
69 const QDate date = value.toDate();
70 testDateTime( date.isValid() ? QDateTime( date, QTime( 0, 0, 0 ) )
71 : QDateTime(), QgsVariantUtils::isNull( value ) );
72 }
73 else if ( value.userType() == QMetaType::Type::QTime )
74 {
75 mIsTimes = true;
76 const QTime time = value.toTime();
77 testDateTime( time.isValid() ? QDateTime( QDate::fromJulianDay( 0 ), time )
78 : QDateTime(), QgsVariantUtils::isNull( value ) );
79 }
80 else //not a date
81 {
82 mCountMissing++;
83 mCount++;
84 }
85
86 // QTime?
87}
88
90{
91 //nothing to do for now - this method has been added for forward compatibility
92 //if statistics are implemented which require a post-calculation step
93}
94
95void QgsDateTimeStatisticalSummary::testDateTime( const QDateTime &dateTime, bool isNull )
96{
97 mCount++;
98
99 if ( !dateTime.isValid() || isNull )
100 mCountMissing++;
101
102 if ( mStatistics & Qgis::DateTimeStatistic::CountDistinct )
103 {
104 mValues << dateTime;
105 }
106 if ( mStatistics & Qgis::DateTimeStatistic::Min || mStatistics & Qgis::DateTimeStatistic::Range )
107 {
108 if ( mMin.isValid() && dateTime.isValid() )
109 {
110 mMin = std::min( mMin, dateTime );
111 }
112 else if ( !mMin.isValid() && dateTime.isValid() )
113 {
114 mMin = dateTime;
115 }
116 }
117 if ( mStatistics & Qgis::DateTimeStatistic::Max || mStatistics & Qgis::DateTimeStatistic::Range )
118 {
119 if ( mMax.isValid() && dateTime.isValid() )
120 {
121 mMax = std::max( mMax, dateTime );
122 }
123 else if ( !mMax.isValid() && dateTime.isValid() )
124 {
125 mMax = dateTime;
126 }
127 }
128}
129
131{
132 switch ( stat )
133 {
135 return mCount;
137 return mValues.count();
139 return mCountMissing;
141 return mIsTimes ? QVariant( mMin.time() ) : QVariant( mMin );
143 return mIsTimes ? QVariant( mMax.time() ) : QVariant( mMax );
145#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
146 return mIsTimes ? QVariant::fromValue( mMax.time() - mMin.time() ) : QVariant::fromValue( mMax - mMin );
147#else
148 return mIsTimes ? QVariant::fromValue( mMax.time() - mMin.time() ) : QVariant::fromValue( QgsInterval( static_cast< double >( ( mMax - mMin ).count() ) / 1000.0 ) );
149#endif
151 return 0;
152 }
153 return 0;
154}
155
157{
158#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
159 return mMax - mMin;
160#else
161 return QgsInterval( static_cast< double >( ( mMax - mMin ).count() ) / 1000.0 );
162#endif
163}
164
166{
167 switch ( statistic )
168 {
170 return QObject::tr( "Count" );
172 return QObject::tr( "Count (distinct)" );
174 return QObject::tr( "Count (missing)" );
176 return QObject::tr( "Minimum (earliest)" );
178 return QObject::tr( "Maximum (latest)" );
180 return QObject::tr( "Range (interval)" );
182 return QString();
183 }
184 return QString();
185}
186
QFlags< DateTimeStatistic > DateTimeStatistics
Statistics to be calculated for date/time values.
Definition qgis.h:5485
DateTimeStatistic
Available date/time statistics.
Definition qgis.h:5469
@ Max
Maximum (latest) datetime value.
@ Min
Minimum (earliest) datetime value.
@ Range
Interval between earliest and latest datetime value.
@ CountMissing
Number of missing (null) values.
@ All
All statistics.
@ CountDistinct
Number of distinct datetime values.
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:46
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.