24 #include <unordered_map>
25 #include <unordered_set>
29 void QgsRasterAnalysisUtils::cellInfoForBBox(
const QgsRectangle &rasterBBox,
const QgsRectangle &featureBBox,
double cellSizeX,
double cellSizeY,
30 int &nCellsX,
int &nCellsY,
int rasterWidth,
int rasterHeight,
QgsRectangle &rasterBlockExtent )
43 int offsetX =
static_cast< int >( std::floor( ( intersectBox.
xMinimum() - rasterBBox.
xMinimum() ) / cellSizeX ) );
44 int offsetY =
static_cast< int >( std::floor( ( rasterBBox.
yMaximum() - intersectBox.
yMaximum() ) / cellSizeY ) );
46 int maxColumn =
static_cast< int >( std::floor( ( intersectBox.
xMaximum() - rasterBBox.
xMinimum() ) / cellSizeX ) ) + 1;
47 int maxRow =
static_cast< int >( std::floor( ( rasterBBox.
yMaximum() - intersectBox.
yMinimum() ) / cellSizeY ) ) + 1;
49 nCellsX = maxColumn - offsetX;
50 nCellsY = maxRow - offsetY;
53 nCellsX = std::min( offsetX + nCellsX, rasterWidth ) - offsetX;
54 nCellsY = std::min( offsetY + nCellsY, rasterHeight ) - offsetY;
57 rasterBBox.
yMaximum() - offsetY * cellSizeY,
58 rasterBBox.
xMinimum() + ( nCellsX + offsetX ) * cellSizeX,
59 rasterBBox.
yMaximum() - ( nCellsY + offsetY ) * cellSizeY );
62 void QgsRasterAnalysisUtils::statisticsFromMiddlePointTest(
QgsRasterInterface *rasterInterface,
int rasterBand,
const QgsGeometry &poly,
int nCellsX,
int nCellsY,
double cellSizeX,
double cellSizeY,
const QgsRectangle &rasterBBox,
const std::function<
void(
double )> &addValue,
bool skipNodata )
69 polyEngine->prepareGeometry();
72 iter.startRasterRead( rasterBand, nCellsX, nCellsY, rasterBBox );
74 std::unique_ptr< QgsRasterBlock > block;
80 bool isNoData =
false;
81 while ( iter.readNextRasterPart( rasterBand, iterCols, iterRows, block, iterLeft, iterTop, &blockExtent ) )
83 double cellCenterY = blockExtent.
yMaximum() - 0.5 * cellSizeY;
85 for (
int row = 0; row < iterRows; ++row )
87 double cellCenterX = blockExtent.
xMinimum() + 0.5 * cellSizeX;
88 for (
int col = 0; col < iterCols; ++col )
90 const double pixelValue = block->valueAndNoData( row, col, isNoData );
91 if ( validPixel( pixelValue ) && ( !skipNodata || !isNoData ) )
93 QgsPoint cellCenter( cellCenterX, cellCenterY );
94 if ( polyEngine->contains( &cellCenter ) )
96 addValue( pixelValue );
99 cellCenterX += cellSizeX;
101 cellCenterY -= cellSizeY;
106 void QgsRasterAnalysisUtils::statisticsFromPreciseIntersection(
QgsRasterInterface *rasterInterface,
int rasterBand,
const QgsGeometry &poly,
int nCellsX,
int nCellsY,
double cellSizeX,
double cellSizeY,
const QgsRectangle &rasterBBox,
const std::function<
void(
double,
double )> &addValue,
bool skipNodata )
110 double hCellSizeX = cellSizeX / 2.0;
111 double hCellSizeY = cellSizeY / 2.0;
112 double pixelArea = cellSizeX * cellSizeY;
120 polyEngine->prepareGeometry();
123 iter.startRasterRead( rasterBand, nCellsX, nCellsY, rasterBBox );
125 std::unique_ptr< QgsRasterBlock > block;
131 bool isNoData =
false;
132 while ( iter.readNextRasterPart( rasterBand, iterCols, iterRows, block, iterLeft, iterTop, &blockExtent ) )
134 double currentY = blockExtent.
yMaximum() - 0.5 * cellSizeY;
135 for (
int row = 0; row < iterRows; ++row )
137 double currentX = blockExtent.
xMinimum() + 0.5 * cellSizeX;
138 for (
int col = 0; col < iterCols; ++col )
140 const double pixelValue = block->valueAndNoData( row, col, isNoData );
141 if ( validPixel( pixelValue ) && ( !skipNodata || !isNoData ) )
146 if ( !pixelRectGeometry.
isNull() && polyEngine->intersects( pixelRectGeometry.
constGet() ) )
150 if ( !intersectGeometry.
isEmpty() )
152 double intersectionArea = intersectGeometry.
area();
153 if ( intersectionArea > 0.0 )
155 weight = intersectionArea / pixelArea;
156 addValue( pixelValue, weight );
161 currentX += cellSizeX;
163 currentY -= cellSizeY;
168 bool QgsRasterAnalysisUtils::validPixel(
double value )
170 return !std::isnan( value );
173 void QgsRasterAnalysisUtils::mapToPixel(
const double x,
const double y,
const QgsRectangle bounds,
const double unitsPerPixelX,
const double unitsPerPixelY,
int &px,
int &py )
175 px = trunc( ( x - bounds.
xMinimum() ) / unitsPerPixelX );
176 py = trunc( ( y - bounds.
yMaximum() ) / -unitsPerPixelY );
179 void QgsRasterAnalysisUtils::pixelToMap(
const int px,
const int py,
const QgsRectangle bounds,
const double unitsPerPixelX,
const double unitsPerPixelY,
double &x,
double &y )
181 x = bounds.
xMinimum() + ( px + 0.5 ) * unitsPerPixelX;
182 y = bounds.
yMaximum() - ( py + 0.5 ) * unitsPerPixelY;
185 static QVector< QPair< QString, Qgis::DataType > > sDataTypes;
187 void populateDataTypes()
189 if ( sDataTypes.empty() )
191 sDataTypes.append( qMakePair( QStringLiteral(
"Byte" ),
Qgis::Byte ) );
192 sDataTypes.append( qMakePair( QStringLiteral(
"Int16" ),
Qgis::Int16 ) );
193 sDataTypes.append( qMakePair( QStringLiteral(
"UInt16" ),
Qgis::UInt16 ) );
194 sDataTypes.append( qMakePair( QStringLiteral(
"Int32" ),
Qgis::Int32 ) );
195 sDataTypes.append( qMakePair( QStringLiteral(
"UInt32" ),
Qgis::UInt32 ) );
196 sDataTypes.append( qMakePair( QStringLiteral(
"Float32" ),
Qgis::Float32 ) );
197 sDataTypes.append( qMakePair( QStringLiteral(
"Float64" ),
Qgis::Float64 ) );
198 sDataTypes.append( qMakePair( QStringLiteral(
"CInt16" ),
Qgis::CInt16 ) );
199 sDataTypes.append( qMakePair( QStringLiteral(
"CInt32" ),
Qgis::CInt32 ) );
200 sDataTypes.append( qMakePair( QStringLiteral(
"CFloat32" ),
Qgis::CFloat32 ) );
201 sDataTypes.append( qMakePair( QStringLiteral(
"CFloat64" ),
Qgis::CFloat64 ) );
205 std::unique_ptr<QgsProcessingParameterDefinition> QgsRasterAnalysisUtils::createRasterTypeParameter(
const QString &name,
const QString &description,
Qgis::DataType defaultType )
210 int defaultChoice = 0;
212 for (
auto it = sDataTypes.constBegin(); it != sDataTypes.constEnd(); ++it )
214 names.append( it->first );
215 if ( it->second == defaultType )
220 return qgis::make_unique< QgsProcessingParameterEnum >( name, description, names,
false, defaultChoice );
223 Qgis::DataType QgsRasterAnalysisUtils::rasterTypeChoiceToDataType(
int choice )
225 if ( choice < 0 || choice >= sDataTypes.count() )
228 return sDataTypes.value( choice ).second;
231 void QgsRasterAnalysisUtils::applyRasterLogicOperator(
const std::vector< QgsRasterAnalysisUtils::RasterLogicInput > &inputs,
QgsRasterDataProvider *destinationRaster,
double outputNoDataValue,
const bool treatNoDataAsFalse,
233 std::function<
void(
const std::vector< std::unique_ptr< QgsRasterBlock > > &,
bool &,
bool &,
int,
int,
bool )> &applyLogicFunc,
238 int nbBlocksWidth =
static_cast< int>( std::ceil( 1.0 * width / maxWidth ) );
239 int nbBlocksHeight =
static_cast< int >( std::ceil( 1.0 * height / maxHeight ) );
240 int nbBlocks = nbBlocksWidth * nbBlocksHeight;
244 outputIter.startRasterRead( 1, width, height, extent );
251 std::unique_ptr< QgsRasterBlock > outputBlock;
252 while ( outputIter.readNextRasterPart( 1, iterCols, iterRows, outputBlock, iterLeft, iterTop, &blockExtent ) )
254 std::vector< std::unique_ptr< QgsRasterBlock > > inputBlocks;
255 for (
const QgsRasterAnalysisUtils::RasterLogicInput &i : inputs )
257 for (
int band : i.bands )
259 std::unique_ptr< QgsRasterBlock > b( i.interface->block( band, blockExtent, iterCols, iterRows ) );
260 inputBlocks.emplace_back( std::move( b ) );
264 feedback->
setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
265 for (
int row = 0; row < iterRows; row++ )
270 for (
int column = 0; column < iterCols; column++ )
273 bool resIsNoData =
false;
274 applyLogicFunc( inputBlocks, res, resIsNoData, row, column, treatNoDataAsFalse );
282 outputBlock->setValue( row, column, resIsNoData ? outputNoDataValue : ( res ? 1 : 0 ) );
285 destinationRaster->
writeBlock( outputBlock.get(), 1, iterLeft, iterTop );
290 std::vector<double> QgsRasterAnalysisUtils::getCellValuesFromBlockStack(
const std::vector< std::unique_ptr< QgsRasterBlock > > &inputBlocks,
int &row,
int &col,
bool &noDataInStack )
293 std::vector<double> cellValues;
294 bool hasNoData =
false;
295 cellValues.reserve( inputBlocks.size() );
297 for (
auto &block : inputBlocks )
300 if ( !block || !block->isValid() )
302 noDataInStack =
true;
307 value = block->valueAndNoData( row, col, hasNoData );
310 noDataInStack =
true;
315 cellValues.push_back( value );
322 double QgsRasterAnalysisUtils::meanFromCellValues( std::vector<double> cellValues,
int stackSize )
324 double sum = std::accumulate( cellValues.begin(), cellValues.end(), 0.0 );
325 double mean = sum /
static_cast<double>( stackSize );
329 double QgsRasterAnalysisUtils::medianFromCellValues( std::vector<double> cellValues,
int stackSize )
331 std::sort( cellValues.begin(), cellValues.end() );
332 int medianElementIdx = stackSize / 2;
333 if ( stackSize % 2 == 0 )
335 return ( cellValues[medianElementIdx] + cellValues[medianElementIdx + 1] ) / 2.0;
339 return cellValues[medianElementIdx];
344 double QgsRasterAnalysisUtils::stddevFromCellValues( std::vector<double> cellValues,
int stackSize )
346 double variance = varianceFromCellValues( cellValues, stackSize );
347 double stddev = std::sqrt( variance );
351 double QgsRasterAnalysisUtils::varianceFromCellValues( std::vector<double> cellValues,
int stackSize )
353 double mean = meanFromCellValues( cellValues, stackSize );
355 for (
int i = 0; i < stackSize; i++ )
357 accum += std::pow( ( cellValues.at( i ) - mean ), 2.0 );
359 double variance = accum /
static_cast<double>( stackSize );
363 double QgsRasterAnalysisUtils::maximumFromCellValues( std::vector<double> cellValues )
365 return *std::max_element( cellValues.begin(), cellValues.end() );
368 double QgsRasterAnalysisUtils::minimumFromCellValues( std::vector<double> cellValues )
370 return *std::min_element( cellValues.begin(), cellValues.end() );
373 double QgsRasterAnalysisUtils::majorityFromCellValues( std::vector<double> cellValues,
const double noDataValue,
int stackSize )
375 if ( stackSize == 1 )
378 return cellValues[0];
380 else if ( stackSize == 2 )
383 return (
qgsDoubleNear( cellValues[0], cellValues[1] ) ) ? cellValues[0] : noDataValue;
385 else if ( std::adjacent_find( cellValues.begin(), cellValues.end(), std::not_equal_to<double>() ) == cellValues.end() )
389 return cellValues[0];
394 std::unordered_map<double, int> map;
396 for (
int i = 0; i < stackSize; i++ )
398 map[cellValues[i]]++;
402 bool multipleMajorities =
false;
403 double result = noDataValue;
404 for (
auto pair : map )
406 if ( maxCount < pair.second )
409 maxCount = pair.second;
410 multipleMajorities =
false;
412 else if ( maxCount == pair.second )
414 multipleMajorities =
true;
417 return multipleMajorities ? noDataValue : result;
421 double QgsRasterAnalysisUtils::minorityFromCellValues( std::vector<double> cellValues,
const double noDataValue,
int stackSize )
423 if ( stackSize == 1 )
426 return cellValues[0];
428 else if ( stackSize == 2 )
431 return (
qgsDoubleNear( cellValues[0], cellValues[1] ) ) ? cellValues[0] : noDataValue;
433 else if ( std::adjacent_find( cellValues.begin(), cellValues.end(), std::not_equal_to<double>() ) == cellValues.end() )
437 return cellValues[0];
442 std::unordered_map<double, int> map;
444 for (
int i = 0; i < stackSize; i++ )
446 map[cellValues[i]]++;
449 int minCount = stackSize;
450 bool multipleMinorities =
false;
451 double result = noDataValue;
452 for (
auto pair : map )
454 if ( minCount > pair.second )
457 minCount = pair.second;
458 multipleMinorities =
false;
460 else if ( minCount == pair.second )
462 multipleMinorities =
true;
465 return multipleMinorities ? noDataValue : result;
469 double QgsRasterAnalysisUtils::rangeFromCellValues( std::vector<double> cellValues )
471 double max = *std::max_element( cellValues.begin(), cellValues.end() );
472 double min = *std::min_element( cellValues.begin(), cellValues.end() );
476 double QgsRasterAnalysisUtils::varietyFromCellValues( std::vector<double> cellValues )
478 std::unordered_set<double> uniqueValues( cellValues.begin(), cellValues.end() );
479 return uniqueValues.size();