24#include <unordered_map>
25#include <unordered_set>
29void QgsRasterAnalysisUtils::cellInfoForBBox(
const QgsRectangle &rasterBBox,
const QgsRectangle &featureBBox,
double cellSizeX,
double cellSizeY,
int &nCellsX,
int &nCellsY,
int rasterWidth,
int rasterHeight,
QgsRectangle &rasterBlockExtent )
42 const int offsetX =
static_cast<int>( std::floor( ( intersectBox.
xMinimum() - rasterBBox.
xMinimum() ) / cellSizeX ) );
43 const int offsetY =
static_cast<int>( std::floor( ( rasterBBox.
yMaximum() - intersectBox.
yMaximum() ) / cellSizeY ) );
45 const int maxColumn =
static_cast<int>( std::floor( ( intersectBox.
xMaximum() - rasterBBox.
xMinimum() ) / cellSizeX ) ) + 1;
46 const int maxRow =
static_cast<int>( std::floor( ( rasterBBox.
yMaximum() - intersectBox.
yMinimum() ) / cellSizeY ) ) + 1;
48 nCellsX = maxColumn - offsetX;
49 nCellsY = maxRow - offsetY;
52 nCellsX = std::min( offsetX + nCellsX, rasterWidth ) - offsetX;
53 nCellsY = std::min( offsetY + nCellsY, rasterHeight ) - offsetY;
55 rasterBlockExtent =
QgsRectangle( rasterBBox.
xMinimum() + offsetX * cellSizeX, rasterBBox.
yMaximum() - offsetY * cellSizeY, rasterBBox.
xMinimum() + ( nCellsX + offsetX ) * cellSizeX, rasterBBox.
yMaximum() - ( nCellsY + offsetY ) * cellSizeY );
58void 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,
const QgsPointXY & )> &addValue,
bool skipNodata )
60 std::unique_ptr<QgsGeos> polyEngine = std::make_unique<QgsGeos>( poly.
constGet() );
65 polyEngine->prepareGeometry();
68 iter.startRasterRead( rasterBand, nCellsX, nCellsY, rasterBBox );
70 std::unique_ptr<QgsRasterBlock> block;
76 bool isNoData =
false;
77 while ( iter.readNextRasterPart( rasterBand, iterCols, iterRows, block, iterLeft, iterTop, &blockExtent ) )
79 double cellCenterY = blockExtent.
yMaximum() - 0.5 * cellSizeY;
81 for (
int row = 0; row < iterRows; ++row )
83 double cellCenterX = blockExtent.
xMinimum() + 0.5 * cellSizeX;
84 for (
int col = 0; col < iterCols; ++col )
86 const double pixelValue = block->valueAndNoData( row, col, isNoData );
87 if ( validPixel( pixelValue ) && ( !skipNodata || !isNoData ) )
89 if ( polyEngine->contains( cellCenterX, cellCenterY ) )
91 addValue( pixelValue,
QgsPointXY( cellCenterX, cellCenterY ) );
94 cellCenterX += cellSizeX;
96 cellCenterY -= cellSizeY;
101void 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,
const QgsPointXY & )> &addValue,
bool skipNodata )
105 const double hCellSizeX = cellSizeX / 2.0;
106 const double hCellSizeY = cellSizeY / 2.0;
107 const double pixelArea = cellSizeX * cellSizeY;
115 polyEngine->prepareGeometry();
118 iter.startRasterRead( rasterBand, nCellsX, nCellsY, rasterBBox );
120 std::unique_ptr<QgsRasterBlock> block;
126 bool isNoData =
false;
127 while ( iter.readNextRasterPart( rasterBand, iterCols, iterRows, block, iterLeft, iterTop, &blockExtent ) )
129 double currentY = blockExtent.
yMaximum() - 0.5 * cellSizeY;
130 for (
int row = 0; row < iterRows; ++row )
132 double currentX = blockExtent.
xMinimum() + 0.5 * cellSizeX;
133 for (
int col = 0; col < iterCols; ++col )
135 const double pixelValue = block->valueAndNoData( row, col, isNoData );
136 if ( validPixel( pixelValue ) && ( !skipNodata || !isNoData ) )
141 if ( !pixelRectGeometry.
isNull() && polyEngine->intersects( pixelRectGeometry.
constGet() ) )
145 if ( !intersectGeometry.
isEmpty() )
147 const double intersectionArea = intersectGeometry.
area();
148 if ( intersectionArea > 0.0 )
150 weight = intersectionArea / pixelArea;
151 addValue( pixelValue, weight,
QgsPointXY( currentX, currentY ) );
156 currentX += cellSizeX;
158 currentY -= cellSizeY;
163bool QgsRasterAnalysisUtils::validPixel(
double value )
165 return !std::isnan( value );
168void QgsRasterAnalysisUtils::mapToPixel(
const double x,
const double y,
const QgsRectangle bounds,
const double unitsPerPixelX,
const double unitsPerPixelY,
int &px,
int &py )
170 px = trunc( ( x - bounds.
xMinimum() ) / unitsPerPixelX );
171 py = trunc( ( y - bounds.
yMaximum() ) / -unitsPerPixelY );
174void QgsRasterAnalysisUtils::pixelToMap(
const int px,
const int py,
const QgsRectangle bounds,
const double unitsPerPixelX,
const double unitsPerPixelY,
double &x,
double &y )
176 x = bounds.
xMinimum() + ( px + 0.5 ) * unitsPerPixelX;
177 y = bounds.
yMaximum() - ( py + 0.5 ) * unitsPerPixelY;
180static QVector<QPair<QString, Qgis::DataType>> sDataTypes;
182void populateDataTypes()
184 if ( sDataTypes.empty() )
201std::unique_ptr<QgsProcessingParameterDefinition> QgsRasterAnalysisUtils::createRasterTypeParameter(
const QString &name,
const QString &description,
Qgis::DataType defaultType )
206 int defaultChoice = 0;
208 for (
auto it = sDataTypes.constBegin(); it != sDataTypes.constEnd(); ++it )
210 names.append( it->first );
211 if ( it->second == defaultType )
216 return std::make_unique<QgsProcessingParameterEnum>( name, description, names,
false, defaultChoice );
219Qgis::DataType QgsRasterAnalysisUtils::rasterTypeChoiceToDataType(
int choice )
221 if ( choice < 0 || choice >= sDataTypes.count() )
224 return sDataTypes.value( choice ).second;
227void QgsRasterAnalysisUtils::applyRasterLogicOperator(
const std::vector<QgsRasterAnalysisUtils::RasterLogicInput> &inputs,
QgsRasterDataProvider *destinationRaster,
double outputNoDataValue,
const bool treatNoDataAsFalse,
int width,
int height,
const QgsRectangle &extent,
QgsFeedback *feedback, std::function<
void(
const std::vector<std::unique_ptr<QgsRasterBlock>> &,
bool &,
bool &,
int,
int,
bool )> &applyLogicFunc,
qgssize &noDataCount,
qgssize &trueCount,
qgssize &falseCount )
231 const int nbBlocksWidth =
static_cast<int>( std::ceil( 1.0 * width / maxWidth ) );
232 const int nbBlocksHeight =
static_cast<int>( std::ceil( 1.0 * height / maxHeight ) );
233 const int nbBlocks = nbBlocksWidth * nbBlocksHeight;
237 outputIter.startRasterRead( 1, width, height, extent );
244 std::unique_ptr<QgsRasterBlock> outputBlock;
245 while ( outputIter.readNextRasterPart( 1, iterCols, iterRows, outputBlock, iterLeft, iterTop, &blockExtent ) )
247 std::vector<std::unique_ptr<QgsRasterBlock>> inputBlocks;
248 for (
const QgsRasterAnalysisUtils::RasterLogicInput &i : inputs )
250 for (
const int band : i.bands )
252 std::unique_ptr<QgsRasterBlock> b( i.interface->block( band, blockExtent, iterCols, iterRows ) );
253 inputBlocks.emplace_back( std::move( b ) );
257 feedback->
setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
258 for (
int row = 0; row < iterRows; row++ )
263 for (
int column = 0; column < iterCols; column++ )
266 bool resIsNoData =
false;
267 applyLogicFunc( inputBlocks, res, resIsNoData, row, column, treatNoDataAsFalse );
275 outputBlock->setValue( row, column, resIsNoData ? outputNoDataValue : ( res ? 1 : 0 ) );
278 destinationRaster->
writeBlock( outputBlock.get(), 1, iterLeft, iterTop );
283std::vector<double> QgsRasterAnalysisUtils::getCellValuesFromBlockStack(
const std::vector<std::unique_ptr<QgsRasterBlock>> &inputBlocks,
int &row,
int &col,
bool &noDataInStack )
286 std::vector<double> cellValues;
287 bool hasNoData =
false;
288 cellValues.reserve( inputBlocks.size() );
290 for (
auto &block : inputBlocks )
293 if ( !block || !block->isValid() )
295 noDataInStack =
true;
300 value = block->valueAndNoData( row, col, hasNoData );
303 noDataInStack =
true;
308 cellValues.push_back( value );
315double QgsRasterAnalysisUtils::meanFromCellValues( std::vector<double> &cellValues,
int stackSize )
317 const double sum = std::accumulate( cellValues.begin(), cellValues.end(), 0.0 );
318 const double mean = sum /
static_cast<double>( stackSize );
322double QgsRasterAnalysisUtils::medianFromCellValues( std::vector<double> &cellValues,
int stackSize )
324 std::sort( cellValues.begin(), cellValues.end() );
325 const bool even = ( stackSize % 2 ) < 1;
328 return ( cellValues[stackSize / 2 - 1] + cellValues[stackSize / 2] ) / 2.0;
332 return cellValues[( stackSize + 1 ) / 2 - 1];
337double QgsRasterAnalysisUtils::stddevFromCellValues( std::vector<double> &cellValues,
int stackSize )
339 const double variance = varianceFromCellValues( cellValues, stackSize );
340 const double stddev = std::sqrt( variance );
344double QgsRasterAnalysisUtils::varianceFromCellValues( std::vector<double> &cellValues,
int stackSize )
346 const double mean = meanFromCellValues( cellValues, stackSize );
348 for (
int i = 0; i < stackSize; i++ )
350 accum += std::pow( ( cellValues.at( i ) - mean ), 2.0 );
352 const double variance = accum /
static_cast<double>( stackSize );
356double QgsRasterAnalysisUtils::maximumFromCellValues( std::vector<double> &cellValues )
358 return *std::max_element( cellValues.begin(), cellValues.end() );
361double QgsRasterAnalysisUtils::minimumFromCellValues( std::vector<double> &cellValues )
363 return *std::min_element( cellValues.begin(), cellValues.end() );
366double QgsRasterAnalysisUtils::majorityFromCellValues( std::vector<double> &cellValues,
const double noDataValue,
int stackSize )
368 if ( stackSize == 1 )
371 return cellValues[0];
373 else if ( stackSize == 2 )
376 return (
qgsDoubleNear( cellValues[0], cellValues[1] ) ) ? cellValues[0] : noDataValue;
378 else if ( std::adjacent_find( cellValues.begin(), cellValues.end(), std::not_equal_to<double>() ) == cellValues.end() )
382 return cellValues[0];
387 std::unordered_map<double, int> map;
389 for (
int i = 0; i < stackSize; i++ )
391 map[cellValues[i]]++;
395 bool multipleMajorities =
false;
396 double result = noDataValue;
397 for (
const auto &pair : std::as_const( map ) )
399 if ( maxCount < pair.second )
402 maxCount = pair.second;
403 multipleMajorities =
false;
405 else if ( maxCount == pair.second )
407 multipleMajorities =
true;
410 return multipleMajorities ? noDataValue : result;
414double QgsRasterAnalysisUtils::minorityFromCellValues( std::vector<double> &cellValues,
const double noDataValue,
int stackSize )
416 if ( stackSize == 1 )
419 return cellValues[0];
421 else if ( stackSize == 2 )
424 return (
qgsDoubleNear( cellValues[0], cellValues[1] ) ) ? cellValues[0] : noDataValue;
426 else if ( std::adjacent_find( cellValues.begin(), cellValues.end(), std::not_equal_to<double>() ) == cellValues.end() )
430 return cellValues[0];
435 std::unordered_map<double, int> map;
437 for (
int i = 0; i < stackSize; i++ )
439 map[cellValues[i]]++;
442 int minCount = stackSize;
443 bool multipleMinorities =
false;
444 double result = noDataValue;
445 for (
const auto &pair : std::as_const( map ) )
447 if ( minCount > pair.second )
450 minCount = pair.second;
451 multipleMinorities =
false;
453 else if ( minCount == pair.second )
455 multipleMinorities =
true;
458 return multipleMinorities ? noDataValue : result;
462double QgsRasterAnalysisUtils::rangeFromCellValues( std::vector<double> &cellValues )
464 const double max = *std::max_element( cellValues.begin(), cellValues.end() );
465 const double min = *std::min_element( cellValues.begin(), cellValues.end() );
469double QgsRasterAnalysisUtils::varietyFromCellValues( std::vector<double> &cellValues )
471 const std::unordered_set<double> uniqueValues( cellValues.begin(), cellValues.end() );
472 return uniqueValues.size();
475double QgsRasterAnalysisUtils::nearestRankPercentile( std::vector<double> &cellValues,
int stackSize,
double percentile )
478 std::sort( cellValues.begin(), cellValues.end() );
481 if ( percentile > 0 )
483 i = std::ceil( percentile *
static_cast<double>( stackSize ) ) - 1;
486 return cellValues[i];
489double QgsRasterAnalysisUtils::interpolatedPercentileInc( std::vector<double> &cellValues,
int stackSize,
double percentile )
491 std::sort( cellValues.begin(), cellValues.end() );
495 return cellValues[stackSize - 1];
499 return cellValues[0];
502 const double x = ( percentile * ( stackSize - 1 ) );
504 const int i =
static_cast<int>( std::floor( x ) );
505 const double xFraction = std::fmod( x, 1 );
507 if ( stackSize == 1 )
509 return cellValues[0];
511 else if ( stackSize == 2 )
513 return cellValues[0] + ( cellValues[1] - cellValues[0] ) * percentile;
517 return cellValues[i] + ( cellValues[i + 1] - cellValues[i] ) * xFraction;
521double QgsRasterAnalysisUtils::interpolatedPercentileExc( std::vector<double> &cellValues,
int stackSize,
double percentile,
double noDataValue )
523 std::sort( cellValues.begin(), cellValues.end() );
524 const double x = ( percentile * ( stackSize + 1 ) );
526 const int i =
static_cast<int>( std::floor( x ) ) - 1;
527 const double xFraction = std::fmod( x, 1 );
528 const double lowerExcValue = 1.0 / (
static_cast<double>( stackSize ) + 1.0 );
529 const double upperExcValue =
static_cast<double>( stackSize ) / (
static_cast<double>( stackSize ) + 1.0 );
531 if ( stackSize < 2 || ( ( percentile < lowerExcValue || percentile > upperExcValue ) ) )
537 return cellValues[i] + ( cellValues[i + 1] - cellValues[i] ) * xFraction;
541double QgsRasterAnalysisUtils::interpolatedPercentRankInc( std::vector<double> &cellValues,
int stackSize,
double value,
double noDataValue )
543 std::sort( cellValues.begin(), cellValues.end() );
545 if ( value < cellValues[0] || value > cellValues[stackSize - 1] )
551 for (
int i = 0; i < stackSize - 1; i++ )
553 if ( cellValues[i] <= value && cellValues[i + 1] >= value )
555 double fraction = 0.0;
559 fraction = ( value - cellValues[i] ) / ( cellValues[i + 1] - cellValues[i] );
561 return ( fraction + i ) / ( stackSize - 1 );
568double QgsRasterAnalysisUtils::interpolatedPercentRankExc( std::vector<double> &cellValues,
int stackSize,
double value,
double noDataValue )
570 std::sort( cellValues.begin(), cellValues.end() );
572 if ( value < cellValues[0] || value > cellValues[stackSize - 1] )
578 for (
int i = 0; i < stackSize - 1; i++ )
580 if ( cellValues[i] <= value && cellValues[i + 1] >= value )
582 double fraction = 0.0;
586 fraction = ( value - cellValues[i] ) / ( cellValues[i + 1] - cellValues[i] );
588 return ( ( i + 1 ) + fraction ) / ( stackSize + 1 );
DataType
Raster data types.
@ Float32
Thirty two bit floating point (float)
@ CFloat64
Complex Float64.
@ Int16
Sixteen bit signed integer (qint16)
@ Int8
Eight bit signed integer (qint8) (added in QGIS 3.30)
@ UInt16
Sixteen bit unsigned integer (quint16)
@ Byte
Eight bit unsigned integer (quint8)
@ Int32
Thirty two bit signed integer (qint32)
@ Float64
Sixty four bit floating point (double)
@ CFloat32
Complex Float32.
@ UInt32
Thirty two bit unsigned integer (quint32)
Base class for feedback objects to be used for cancellation of something running in a worker thread.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
A geometry is the spatial representation of a feature.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
double area() const
Returns the planar, 2-dimensional area of the geometry.
QgsGeometry intersection(const QgsGeometry &geometry, const QgsGeometryParameters ¶meters=QgsGeometryParameters()) const
Returns a geometry representing the points shared by this geometry and other.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
static QgsGeometryEngine * createGeometryEngine(const QgsAbstractGeometry *geometry, double precision=0.0, Qgis::GeosCreationFlags flags=Qgis::GeosCreationFlag::SkipEmptyInteriorRings)
Creates and returns a new geometry engine representing the specified geometry using precision on a gr...
A class to represent a 2D point.
Base class for raster data providers.
bool writeBlock(QgsRasterBlock *block, int band, int xOffset=0, int yOffset=0)
Writes pixel data from a raster block into the provider data source.
virtual bool setEditable(bool enabled)
Turns on/off editing mode of the provider.
Base class for processing filters like renderers, reprojector, resampler etc.
Iterator for sequentially processing raster cells.
static const int DEFAULT_MAXIMUM_TILE_WIDTH
Default maximum tile width.
static const int DEFAULT_MAXIMUM_TILE_HEIGHT
Default maximum tile height.
A rectangle specified with double values.
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
unsigned long long qgssize
Qgssize is used instead of size_t, because size_t is stdlib type, unknown by SIP, and it would be har...
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)