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" );
291 QMap<int, QVariant> QgsZonalStatistics::calculateStatisticsInt(
QgsRasterInterface *rasterInterface,
const QgsGeometry &geometry,
double cellSizeX,
double cellSizeY,
int rasterBand, QgsZonalStatistics::Statistics statistics )
294 QMap<int, QVariant> pyResult;
295 for (
auto it = result.constBegin(); it != result.constEnd(); ++it )
297 pyResult.insert( it.key(), it.value() );
305 QMap<QgsZonalStatistics::Statistic, QVariant> results;
323 FeatureStats featureStats( statsStoreValues, statsStoreValueCount );
325 int nCellsXProvider = rasterInterface->
xSize();
326 int nCellsYProvider = rasterInterface->
ySize();
328 int nCellsX, nCellsY;
330 QgsRasterAnalysisUtils::cellInfoForBBox( rasterBBox, featureRect, cellSizeX, cellSizeY, nCellsX, nCellsY, nCellsXProvider, nCellsYProvider, rasterBlockExtent );
332 featureStats.reset();
333 QgsRasterAnalysisUtils::statisticsFromMiddlePointTest( rasterInterface, rasterBand, geometry, nCellsX, nCellsY, cellSizeX, cellSizeY, rasterBlockExtent, [ &featureStats ](
double value ) { featureStats.addValue( value ); } );
335 if ( featureStats.count <= 1 )
338 featureStats.reset();
339 QgsRasterAnalysisUtils::statisticsFromPreciseIntersection( rasterInterface, rasterBand, geometry, nCellsX, nCellsY, cellSizeX, cellSizeY, rasterBlockExtent, [ &featureStats ](
double value,
double weight ) { featureStats.addValue( value, weight ); } );
348 if ( featureStats.count > 0 )
350 double mean = featureStats.sum / featureStats.count;
355 std::sort( featureStats.values.begin(), featureStats.values.end() );
356 int size = featureStats.values.count();
357 bool even = ( size % 2 ) < 1;
361 medianValue = ( featureStats.values.at( size / 2 - 1 ) + featureStats.values.at( size / 2 ) ) / 2;
365 medianValue = featureStats.values.at( ( size + 1 ) / 2 - 1 );
371 double sumSquared = 0;
372 for (
int i = 0; i < featureStats.values.count(); ++i )
374 double diff = featureStats.values.at( i ) - mean;
375 sumSquared += diff * diff;
377 double variance = sumSquared / featureStats.values.count();
380 double stdev = std::pow( variance, 0.5 );
394 QList<int> vals = featureStats.valueCount.values();
395 std::sort( vals.begin(), vals.end() );
398 double minorityKey = featureStats.valueCount.key( vals.first() );
403 double majKey = featureStats.valueCount.key( vals.last() );