/
qgsalgorithmcellstatistics.cpp
333 lines (293 loc) · 15.1 KB
/
qgsalgorithmcellstatistics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/***************************************************************************
qgsalgorithmcellstatistics.cpp
---------------------
begin : May 2020
copyright : (C) 2020 by Clemens Raffler
email : clemens dot raffler at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsalgorithmcellstatistics.h"
#include "qgsrasterprojector.h"
#include "qgsrasterfilewriter.h"
#include "qgsrasteranalysisutils.h"
///@cond PRIVATE
QString QgsCellStatisticsAlgorithm::displayName() const
{
return QObject::tr( "Cell statistics" );
}
QString QgsCellStatisticsAlgorithm::name() const
{
return QObject::tr( "cellstatistics" );
}
QStringList QgsCellStatisticsAlgorithm::tags() const
{
return QObject::tr( "cell,pixel,statistic,count,mean,sum,majority,minority,variance,variety,range,median,minimum,maximum" ).split( ',' );
}
QString QgsCellStatisticsAlgorithm::group() const
{
return QObject::tr( "Raster analysis" );
}
QString QgsCellStatisticsAlgorithm::groupId() const
{
return QStringLiteral( "rasteranalysis" );
}
QString QgsCellStatisticsAlgorithm::shortHelpString() const
{
return QObject::tr( "The Cell statistics algorithm computes a value for each cell of the "
"output raster. At each cell location, "
"the output value is defined as a function of all overlaid cell values of the "
"input rasters.\n "
"The output rasters extent and resolution is defined by a reference "
"raster. The following functions can be applied on the input "
"raster cells per output raster cell location: "
"<ul> "
" <li>Sum</li>"
" <li>Count</li>"
" <li>Mean</li>"
" <li>Median</li>"
" <li>Standard deviation</li>"
" <li>Variance</li>"
" <li>Minimum</li>"
" <li>Maximum</li>"
" <li>Minority (most frequent value)</li>"
" <li>Majority (least frequent value)</li>"
" <li>Range (max-min)</li>"
" <li>Variety (count of unique values)</li>"
"</ul> "
"Input raster layers that do not match the cell size of the reference raster layer will be "
"resampled using nearest neighbor resampling. The output raster data type will be set to "
"the most complex data type present in the input datasets except when using the functions "
"Mean and Standard deviation (data type is always Float32) or Count and Variety (data type is always Int32).\n"
"<i>Calculation details - general:</i> NoData values in any of the input layers will result in a NoData cell output if the Ignore NoData parameter is not set.\n"
"<i>Calculation details - Count:</i> Count will always result in the number of cells without NoData values at the current cell location."
"<i>Calculation details - Median:</i> If the number of input layers is even, the median will be calculated as the "
"arithmetic mean of the two middle values of the ordered cell input values.\n"
"<i>Calculation details - Minority/Majority:</i> If no unique minority or majority could be found, the result is NoData, except all "
"input cell values are equal." );
}
QgsCellStatisticsAlgorithm *QgsCellStatisticsAlgorithm::createInstance() const
{
return new QgsCellStatisticsAlgorithm();
}
void QgsCellStatisticsAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterMultipleLayers( QStringLiteral( "INPUTS" ),
QObject::tr( "Input layers" ), QgsProcessing::TypeRaster ) );
QStringList statistics = QStringList();
statistics << QStringLiteral( "Sum" )
<< QStringLiteral( "Count" )
<< QStringLiteral( "Mean" )
<< QStringLiteral( "Median" )
<< QStringLiteral( "Standard deviation" )
<< QStringLiteral( "Variance" )
<< QStringLiteral( "Minimum" )
<< QStringLiteral( "Maximum" )
<< QStringLiteral( "Minority" )
<< QStringLiteral( "Majority" )
<< QStringLiteral( "Range" )
<< QStringLiteral( "Variety" );
addParameter( new QgsProcessingParameterEnum( QStringLiteral( "STATISTIC" ), QObject::tr( "Statistic" ), statistics, false, 0, false ) );
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "IGNORE_NODATA" ), QObject::tr( "Ignore NoData values" ), true) );
addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "REF_LAYER" ), QObject::tr( "Reference layer" ) ) );
addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ),
QObject::tr( "Output layer" ) ) );
addOutput( new QgsProcessingOutputString( QStringLiteral( "EXTENT" ), QObject::tr( "Extent" ) ) );
addOutput( new QgsProcessingOutputString( QStringLiteral( "CRS_AUTHID" ), QObject::tr( "CRS authority identifier" ) ) );
addOutput( new QgsProcessingOutputNumber( QStringLiteral( "WIDTH_IN_PIXELS" ), QObject::tr( "Width in pixels" ) ) );
addOutput( new QgsProcessingOutputNumber( QStringLiteral( "HEIGHT_IN_PIXELS" ), QObject::tr( "Height in pixels" ) ) );
addOutput( new QgsProcessingOutputNumber( QStringLiteral( "TOTAL_PIXEL_COUNT" ), QObject::tr( "Total pixel count" ) ) );
}
bool QgsCellStatisticsAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * feedback )
{
QgsRasterLayer *referenceLayer = parameterAsRasterLayer( parameters, QStringLiteral( "REF_LAYER" ), context );
if ( !referenceLayer )
throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "REF_LAYER" ) ) );
mIgnoreNoData = parameterAsBool( parameters, QStringLiteral( "IGNORE_NODATA" ), context);
mCrs = referenceLayer->crs();
mRasterUnitsPerPixelX = referenceLayer->rasterUnitsPerPixelX();
mRasterUnitsPerPixelY = referenceLayer->rasterUnitsPerPixelY();
mLayerWidth = referenceLayer->width();
mLayerHeight = referenceLayer->height();
mExtent = referenceLayer->extent();
const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral( "INPUTS" ), context );
QList< QgsRasterLayer * > rasterLayers;
rasterLayers.reserve( layers.count() );
for ( QgsMapLayer *l : layers )
{
if(feedback->isCanceled())
break; //in case some slow data sources are loaded
if ( l->type() == QgsMapLayerType::RasterLayer )
{
QgsRasterLayer *layer = qobject_cast< QgsRasterLayer * >( l );
QgsRasterAnalysisUtils::RasterLogicInput input;
const int band = 1; //could be made dynamic
input.hasNoDataValue = layer->dataProvider()->sourceHasNoDataValue( band );
input.sourceDataProvider.reset( layer->dataProvider()->clone() );
input.interface = input.sourceDataProvider.get();
// add projector if necessary
if ( layer->crs() != mCrs )
{
input.projector = qgis::make_unique< QgsRasterProjector >();
input.projector->setInput( input.sourceDataProvider.get() );
input.projector->setCrs( layer->crs(), mCrs, context.transformContext() );
input.interface = input.projector.get();
}
mInputs.emplace_back( std::move( input ) );
}
}
return true;
}
QVariantMap QgsCellStatisticsAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
//obtain statistic method
int statisticMethodIdx = parameterAsInt( parameters, QStringLiteral( "STATISTIC" ), context );
//determine output raster data type
//initially raster data type to most primitive data type that is possible
mDataType = Qgis::Byte;
for ( const QgsRasterAnalysisUtils::RasterLogicInput &i : mInputs )
{
for ( int band : i.bands )
{
Qgis::DataType inputDataType = i.interface->dataType( band );
if( static_cast<int>(mDataType) < static_cast<int>(inputDataType) )
mDataType = inputDataType; //if raster data type is more potent, set it as new data type
}
}
//force data types on specific functions if input data types don't match
if( statisticMethodIdx == 2 || statisticMethodIdx == 4 ) //mean, stddev
{
if( static_cast<int>(mDataType) < 6 )
mDataType = Qgis::Float32; //force float on mean and stddev if all inputs are integer
}
else if ( statisticMethodIdx == 1 || statisticMethodIdx == 11 ) //count, variety
{
if( static_cast<int>(mDataType) > 5 ) //if is floating point type
mDataType = Qgis::Int32; //force integer on variety if all inputs are float or complex
}
const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
QFileInfo fi( outputFile );
const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );
std::unique_ptr< QgsRasterFileWriter > writer = qgis::make_unique< QgsRasterFileWriter >( outputFile );
writer->setOutputProviderKey( QStringLiteral( "gdal" ) );
writer->setOutputFormat( outputFormat );
std::unique_ptr<QgsRasterDataProvider > provider( writer->createOneBandRaster( mDataType, mLayerWidth, mLayerHeight, mExtent, mCrs ) );
if ( !provider )
throw QgsProcessingException( QObject::tr( "Could not create raster output: %1" ).arg( outputFile ) );
if ( !provider->isValid() )
throw QgsProcessingException( QObject::tr( "Could not create raster output %1: %2" ).arg( outputFile, provider->error().message( QgsErrorMessage::Text ) ) );
provider->setNoDataValue( 1, mNoDataValue );
qgssize layerSize = static_cast< qgssize >( mLayerWidth ) * static_cast< qgssize >( mLayerHeight );
int maxWidth = QgsRasterIterator::DEFAULT_MAXIMUM_TILE_WIDTH;
int maxHeight = QgsRasterIterator::DEFAULT_MAXIMUM_TILE_HEIGHT;
int nbBlocksWidth = static_cast< int>( std::ceil( 1.0 * mLayerWidth / maxWidth ) );
int nbBlocksHeight = static_cast< int >( std::ceil( 1.0 * mLayerHeight / maxHeight ) );
int nbBlocks = nbBlocksWidth * nbBlocksHeight;
provider->setEditable( true );
QgsRasterIterator outputIter( provider.get() );
outputIter.startRasterRead( 1, mLayerWidth, mLayerHeight, mExtent );
int iterLeft = 0;
int iterTop = 0;
int iterCols = 0;
int iterRows = 0;
QgsRectangle blockExtent;
std::unique_ptr< QgsRasterBlock > outputBlock;
while ( outputIter.readNextRasterPart( 1, iterCols, iterRows, outputBlock, iterLeft, iterTop, &blockExtent ) )
{
std::vector< std::unique_ptr< QgsRasterBlock > > inputBlocks;
for ( const QgsRasterAnalysisUtils::RasterLogicInput &i : mInputs )
{
for ( int band : i.bands )
{
std::unique_ptr< QgsRasterBlock > b( i.interface->block( band, blockExtent, iterCols, iterRows ) );
inputBlocks.emplace_back( std::move( b ) );
}
}
feedback->setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
for ( int row = 0; row < iterRows; row++ )
{
if ( feedback->isCanceled() )
break;
for ( int col = 0; col < iterCols; col++ )
{
double result = 0;
bool noDataInStack = false;
std::vector<double> cellValues = QgsRasterAnalysisUtils::getCellValuesFromBlockStack( inputBlocks, row, col, noDataInStack );
int cellValueStackSize = cellValues.size();
if ( noDataInStack && !mIgnoreNoData )
{
//output cell will always be NoData if NoData occurs in cellValueStack and NoData is not ignored
//this saves unnecessary iterations on the cellValueStack
if( statisticMethodIdx == 1)
outputBlock->setValue( row, col, cellValueStackSize);
else
{
outputBlock->setValue( row, col, mNoDataValue );
}
}
else if ( !noDataInStack || (noDataInStack && mIgnoreNoData) )
{
switch ( statisticMethodIdx )
{
case 0: //sum
result = std::accumulate( cellValues.begin(), cellValues.end(), 0.0 );
break;
case 1: //count
result = cellValueStackSize;
break;
case 2: //mean
result = QgsRasterAnalysisUtils::meanFromCellValues( cellValues, cellValueStackSize );
break;
case 3: //median
result = QgsRasterAnalysisUtils::medianFromCellValues( cellValues, cellValueStackSize );
break;
case 4: //stddev
result = QgsRasterAnalysisUtils::stddevFromCellValues( cellValues, cellValueStackSize );
break;
case 5: //variance
result = QgsRasterAnalysisUtils::varianceFromCellValues( cellValues, cellValueStackSize );
break;
case 6: //min
result = QgsRasterAnalysisUtils::minimumFromCellValues( cellValues );
break;
case 7: //max
result = QgsRasterAnalysisUtils::maximumFromCellValues( cellValues );
break;
case 8: //minority
result = QgsRasterAnalysisUtils::minorityFromCellValues( cellValues, mNoDataValue, cellValueStackSize );
break;
case 9: //majority
result = QgsRasterAnalysisUtils::majorityFromCellValues( cellValues, mNoDataValue, cellValueStackSize );
break;
case 10: //range
result = QgsRasterAnalysisUtils::rangeFromCellValues( cellValues );
break;
case 11: //variety
result = QgsRasterAnalysisUtils::varietyFromCellValues( cellValues );
break;
default:
break;
}
outputBlock->setValue( row, col, result );
}
}
}
provider->writeBlock( outputBlock.get(), 1, iterLeft, iterTop );
}
provider->setEditable( false );
QVariantMap outputs;
outputs.insert( QStringLiteral( "EXTENT" ), mExtent.toString() );
outputs.insert( QStringLiteral( "CRS_AUTHID" ), mCrs.authid() );
outputs.insert( QStringLiteral( "WIDTH_IN_PIXELS" ), mLayerWidth );
outputs.insert( QStringLiteral( "HEIGHT_IN_PIXELS" ), mLayerHeight );
outputs.insert( QStringLiteral( "TOTAL_PIXEL_COUNT" ), layerSize );
outputs.insert( QStringLiteral( "OUTPUT" ), outputFile );
return outputs;
}
///@endcond