QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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 if ( value.userType() == QMetaType::Type::QDateTime )
66 {
67 testDateTime( value.toDateTime(), QgsVariantUtils::isNull( value ) );
68 }
69 else if ( value.userType() == QMetaType::Type::QDate )
70 {
71 const QDate date = value.toDate();
72 testDateTime( date.isValid() ? QDateTime( date, QTime( 0, 0, 0 ) ) : QDateTime(), QgsVariantUtils::isNull( value ) );
73 }
74 else if ( value.userType() == QMetaType::Type::QTime )
75 {
76 mIsTimes = true;
77 const QTime time = value.toTime();
78 testDateTime( time.isValid() ? QDateTime( QDate::fromJulianDay( 0 ), time ) : 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 return mIsTimes ? QVariant::fromValue( mMax.time() - mMin.time() ) : QVariant::fromValue( QgsInterval( static_cast< double >( ( mMax - mMin ).count() ) / 1000.0 ) );
147 return 0;
148 }
149 return 0;
150}
151
153{
154 return QgsInterval( static_cast< double >( ( mMax - mMin ).count() ) / 1000.0 );
155}
156
158{
159 switch ( statistic )
160 {
162 return QObject::tr( "Count" );
164 return QObject::tr( "Count (distinct)" );
166 return QObject::tr( "Count (missing)" );
168 return QObject::tr( "Minimum (earliest)" );
170 return QObject::tr( "Maximum (latest)" );
172 return QObject::tr( "Range (interval)" );
174 return QString();
175 }
176 return QString();
177}
QFlags< DateTimeStatistic > DateTimeStatistics
Statistics to be calculated for date/time values.
Definition qgis.h:6259
DateTimeStatistic
Available date/time statistics.
Definition qgis.h:6243
@ Max
Maximum (latest) datetime value.
Definition qgis.h:6248
@ Min
Minimum (earliest) datetime value.
Definition qgis.h:6247
@ Range
Interval between earliest and latest datetime value.
Definition qgis.h:6249
@ CountMissing
Number of missing (null) values.
Definition qgis.h:6246
@ All
All statistics.
Definition qgis.h:6250
@ CountDistinct
Number of distinct datetime values.
Definition qgis.h:6245
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:52
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.