21 #include <QJsonObject>
22 #include <QJsonDocument>
35 double delta1 = newMean -
mean;
37 double delta2 = newMean - stats.
mean;
38 double variance2 = stats.
stDev * stats.
stDev + delta2 * delta2 - 2 * stats.
count * delta2 * stats.
mean;
61 : mSampledPointsCount( sampledPointsCount ), mStatisticsMap( stats )
68 mStatisticsMap.clear();
75 mStatisticsMap.remove( attribute.name() );
82 defaultVal.
minimum = std::numeric_limits<double>::max();
83 defaultVal.
maximum = std::numeric_limits<double>::lowest();
85 return mStatisticsMap.value( attribute, defaultVal );
90 if ( !mStatisticsMap.contains( attribute ) )
98 if ( !mStatisticsMap.contains( attribute ) )
99 return QMap<int, int>();
100 return mStatisticsMap[ attribute ].classCount;
105 if ( !mStatisticsMap.contains( attribute ) )
106 return std::numeric_limits<double>::quiet_NaN();
107 return mStatisticsMap[ attribute ].minimum;
112 if ( !mStatisticsMap.contains( attribute ) )
113 return std::numeric_limits<double>::quiet_NaN();
114 return mStatisticsMap[ attribute ].maximum;
119 if ( !mStatisticsMap.contains( attribute ) )
120 return std::numeric_limits<double>::quiet_NaN();
121 return mStatisticsMap[ attribute ].mean;
126 if ( !mStatisticsMap.contains( attribute ) )
127 return std::numeric_limits<double>::quiet_NaN();
128 return mStatisticsMap[ attribute ].stDev;
134 for (
const QString &attribute : stats.mStatisticsMap.keys() )
137 if ( mStatisticsMap.contains( attribute ) )
141 mStatisticsMap[ attribute ] = s;
143 mSampledPointsCount += stats.mSampledPointsCount;
149 obj.insert( QStringLiteral(
"sampled-points" ), QJsonValue::fromVariant(
sampledPointsCount() ) );
151 for (
const QString &attr : mStatisticsMap.keys() )
154 stats.insert( attr, attributeStatisticsToJson( stat ) );
156 obj.insert( QStringLiteral(
"stats" ), stats );
158 QJsonDocument statsDoc( obj );
159 return statsDoc.toJson( QJsonDocument::Compact );
164 QJsonParseError error;
165 QJsonDocument document = QJsonDocument::fromJson( statsByteArray, &error );
166 if ( error.error != QJsonParseError::NoError )
168 QgsMessageLog::logMessage( QObject::tr(
"Failed to load statistics JSON from COPC file, reason: %1" ).arg( error.errorString() ) );
172 QJsonObject statsJson = document.object();
175 stats.mSampledPointsCount = statsJson.value( QStringLiteral(
"sampled-points" ) ).toInt();
176 if ( statsJson.contains( QStringLiteral(
"stats" ) ) )
178 QJsonObject statsObj = statsJson.value( QStringLiteral(
"stats" ) ).toObject();
179 for (
const QString &attr : statsObj.keys() )
181 QJsonObject obj = statsObj.value( attr ).toObject();
183 attrStats.
count = stats.mSampledPointsCount;
184 stats.mStatisticsMap.insert( attr, attrStats );
193 obj.insert( QStringLiteral(
"minimum" ), stats.
minimum );
194 obj.insert( QStringLiteral(
"maximum" ), stats.
maximum );
195 obj.insert( QStringLiteral(
"mean" ), stats.
mean );
196 if ( !std::isnan( stats.
stDev ) )
198 obj.insert( QStringLiteral(
"standard-deviation" ), stats.
stDev );
200 QJsonObject classCount;
203 classCount.insert( QString::number(
c ), stats.
classCount[
c] );
205 obj.insert( QStringLiteral(
"class-count" ), classCount );
212 QVariantMap m = statsJson.toVariantMap();
213 statsObj.
minimum = m.value( QStringLiteral(
"minimum" ), std::numeric_limits<double>::max() ).toDouble();
214 statsObj.
maximum = m.value( QStringLiteral(
"maximum" ), std::numeric_limits<double>::lowest() ).toDouble();
215 statsObj.
mean = m.value( QStringLiteral(
"mean" ), 0 ).toDouble();
216 statsObj.
stDev = m.value( QStringLiteral(
"standard-deviation" ), std::numeric_limits<double>::quiet_NaN() ).toDouble();
217 QJsonObject classCountJson = statsJson.value( QStringLiteral(
"class-count" ) ).toObject();
218 for (
const QString &key : classCountJson.keys() )
220 statsObj.
classCount.insert( key.toInt(), classCountJson.value( key ).toInt() );