35 rasterLayer ? rasterLayer->dataProvider() : nullptr,
37 rasterLayer ? rasterLayer->rasterUnitsPerPixelX() : 0,
38 rasterLayer ? rasterLayer->rasterUnitsPerPixelY() : 0,
46 const QgsCoordinateReferenceSystem &rasterCrs,
double rasterUnitsPerPixelX,
double rasterUnitsPerPixelY,
const QString &attributePrefix,
int rasterBand, QgsZonalStatistics::Statistics stats )
47 : mRasterInterface( rasterInterface )
48 , mRasterCrs( rasterCrs )
49 , mCellSizeX( std::fabs( rasterUnitsPerPixelX ) )
50 , mCellSizeY( std::fabs( rasterUnitsPerPixelY ) )
51 , mRasterBand( rasterBand )
52 , mPolygonLayer( polygonLayer )
53 , mAttributePrefix( attributePrefix )
54 , mStatistics( stats )
60 if ( !mRasterInterface )
65 if ( mRasterInterface->
bandCount() < mRasterBand )
76 if ( !vectorProvider )
81 QMap<QgsZonalStatistics::Statistic, int> statFieldIndexes;
84 int oldFieldCount = vectorProvider->
fields().
count();
85 QList<QgsField> newFieldList;
102 if ( mStatistics & stat )
105 QgsField field( fieldName, QVariant::Double, QStringLiteral(
"double precision" ) );
106 newFieldList.push_back(
field );
107 statFieldIndexes.insert( stat, oldFieldCount + newFieldList.count() - 1 );
122 int featureCounter = 0;
135 feedback->
setProgress( 100.0 *
static_cast< double >( featureCounter ) / featureCount );
140 QMap<QgsZonalStatistics::Statistic, QVariant> results =
calculateStatistics( mRasterInterface, featureGeometry, mCellSizeX, mCellSizeY, mRasterBand, mStatistics );
142 if ( results.empty() )
146 for (
const auto &result : results.toStdMap() )
148 changeAttributeMap.insert( statFieldIndexes.value( result.first ), result.second );
151 changeMap.insert( feature.
id(), changeAttributeMap );
168 QString QgsZonalStatistics::getUniqueFieldName(
const QString &fieldName,
const QList<QgsField> &newFields )
172 if ( !dp->
storageType().contains( QLatin1String(
"ESRI Shapefile" ) ) )
178 allFields.append( newFields );
179 QString
shortName = fieldName.mid( 0, 10 );
197 shortName = QStringLiteral(
"%1_%2" ).arg( fieldName.mid( 0, 8 ) ).arg( n );
209 shortName = QStringLiteral(
"%1_%2" ).arg( fieldName.mid( 0, 8 ) ).arg( n );
213 shortName = QStringLiteral(
"%1_%2" ).arg( fieldName.mid( 0, 7 ) ).arg( n );
227 return QObject::tr(
"Count" );
229 return QObject::tr(
"Sum" );
231 return QObject::tr(
"Mean" );
233 return QObject::tr(
"Median" );
235 return QObject::tr(
"St dev" );
237 return QObject::tr(
"Minimum" );
239 return QObject::tr(
"Maximum" );
241 return QObject::tr(
"Range" );
243 return QObject::tr(
"Minority" );
245 return QObject::tr(
"Majority" );
247 return QObject::tr(
"Variety" );
249 return QObject::tr(
"Variance" );
261 return QStringLiteral(
"count" );
263 return QStringLiteral(
"sum" );
265 return QStringLiteral(
"mean" );
267 return QStringLiteral(
"median" );
269 return QStringLiteral(
"stdev" );
271 return QStringLiteral(
"min" );
273 return QStringLiteral(
"max" );
275 return QStringLiteral(
"range" );
277 return QStringLiteral(
"minority" );
279 return QStringLiteral(
"majority" );
281 return QStringLiteral(
"variety" );
283 return QStringLiteral(
"variance" );
292 QMap<QgsZonalStatistics::Statistic, QVariant> results;
310 FeatureStats featureStats( statsStoreValues, statsStoreValueCount );
312 int nCellsXProvider = rasterInterface->
xSize();
313 int nCellsYProvider = rasterInterface->
ySize();
315 int nCellsX, nCellsY;
317 QgsRasterAnalysisUtils::cellInfoForBBox( rasterBBox, featureRect, cellSizeX, cellSizeY, nCellsX, nCellsY, nCellsXProvider, nCellsYProvider, rasterBlockExtent );
319 featureStats.reset();
320 QgsRasterAnalysisUtils::statisticsFromMiddlePointTest( rasterInterface, rasterBand, geometry, nCellsX, nCellsY, cellSizeX, cellSizeY, rasterBlockExtent, [ &featureStats ](
double value ) { featureStats.addValue( value ); } );
322 if ( featureStats.count <= 1 )
325 featureStats.reset();
326 QgsRasterAnalysisUtils::statisticsFromPreciseIntersection( rasterInterface, rasterBand, geometry, nCellsX, nCellsY, cellSizeX, cellSizeY, rasterBlockExtent, [ &featureStats ](
double value,
double weight ) { featureStats.addValue( value, weight ); } );
335 if ( featureStats.count > 0 )
337 double mean = featureStats.sum / featureStats.count;
342 std::sort( featureStats.values.begin(), featureStats.values.end() );
343 int size = featureStats.values.count();
344 bool even = ( size % 2 ) < 1;
348 medianValue = ( featureStats.values.at( size / 2 - 1 ) + featureStats.values.at( size / 2 ) ) / 2;
352 medianValue = featureStats.values.at( ( size + 1 ) / 2 - 1 );
358 double sumSquared = 0;
359 for (
int i = 0; i < featureStats.values.count(); ++i )
361 double diff = featureStats.values.at( i ) - mean;
362 sumSquared += diff * diff;
364 double variance = sumSquared / featureStats.values.count();
367 double stdev = std::pow( variance, 0.5 );
381 QList<int> vals = featureStats.valueCount.values();
382 std::sort( vals.begin(), vals.end() );
385 double minorityKey = featureStats.valueCount.key( vals.first() );
390 double majKey = featureStats.valueCount.key( vals.last() );