41 , mFunctionName( functionName )
42 , mFunctionArgs( functionArgs )
48 , mRasterName( rasterName )
50 if ( mRasterName.startsWith(
'"' ) && mRasterName.endsWith(
'"' ) )
51 mRasterName = mRasterName.mid( 1, mRasterName.size() - 2 );
58 for (
int i = 0; i < mFunctionArgs.size(); ++i )
60 if ( mFunctionArgs.at( i ) )
61 delete mFunctionArgs.at( i );
72 const QMap<QString, QgsRasterBlock *>::iterator it = rasterData.find( mRasterName );
73 if ( it == rasterData.end() )
75 QgsDebugError( QStringLiteral(
"Error: could not find raster data for \"%1\"" ).arg( mRasterName ) );
79 const int nRows = ( row >= 0 ? 1 : ( *it )->height() );
80 const int startRow = ( row >= 0 ? row : 0 );
81 const int endRow = startRow + nRows;
82 const int nCols = ( *it )->width();
83 const int nEntries = nCols * nRows;
84 double *data =
new double[nEntries];
89 bool isNoData =
false;
90 for (
int dataRow = startRow; dataRow < endRow ; ++dataRow, ++outRow )
92 for (
int dataCol = 0; dataCol < nCols; ++dataCol )
94 const double value = ( *it )->valueAndNoData( dataRow, dataCol, isNoData );
95 data[ dataCol + nCols * outRow] = isNoData ? result.
nodataValue() : value;
106 if ( !mLeft || !mLeft->
calculate( rasterData, leftMatrix, row ) )
110 if ( mRight && !mRight->
calculate( rasterData, rightMatrix, row ) )
118 leftMatrix.
add( rightMatrix );
127 leftMatrix.
divide( rightMatrix );
130 leftMatrix.
power( rightMatrix );
133 leftMatrix.
equal( rightMatrix );
157 leftMatrix.
min( rightMatrix );
160 leftMatrix.
max( rightMatrix );
198 const int newNColumns = leftMatrix.
nColumns();
199 const int newNRows = leftMatrix.
nRows();
205 const size_t nEntries =
static_cast<size_t>( result.
nColumns() * result.
nRows() );
206 double *data =
new double[ nEntries ];
207 std::fill( data, data + nEntries, mNumber );
215 double *data =
new double[nEntries];
216 for (
int i = 0; i < nEntries; ++i )
225 std::vector< std::unique_ptr< QgsRasterMatrix > > matrixContainer;
226 for (
int i = 0; i < mFunctionArgs.size(); ++i )
228 std::unique_ptr< QgsRasterMatrix > singleMatrix = std::make_unique< QgsRasterMatrix >( result.
nColumns(), result.
nRows(),
nullptr, result.
nodataValue() );
229 if ( !mFunctionArgs.at( i ) || !mFunctionArgs.at( i )->calculate( rasterData, *singleMatrix, row ) )
233 matrixContainer.emplace_back( std::move( singleMatrix ) );
235 evaluateFunction( matrixContainer, result );
257 result = QStringLiteral(
"( %1 + %2 )" ).arg( left ).arg( right );
260 result = QStringLiteral(
"( %1 - %2 )" ).arg( left ).arg( right );
263 result = QStringLiteral(
"-%1" ).arg( left );
266 result = QStringLiteral(
"%1 * %2" ).arg( left ).arg( right );
269 result = QStringLiteral(
"%1 / %2" ).arg( left ).arg( right );
273 result = QStringLiteral(
"pow( %1, %2 )" ).arg( left ).arg( right );
275 result = QStringLiteral(
"%1^%2" ).arg( left ).arg( right );
279 result = QStringLiteral(
"( float ) ( %1 == %2 )" ).arg( left ).arg( right );
281 result = QStringLiteral(
"%1 = %2" ).arg( left ).arg( right );
285 result = QStringLiteral(
"( float ) ( %1 != %2 )" ).arg( left ).arg( right );
287 result = QStringLiteral(
"%1 != %2" ).arg( left ).arg( right );
291 result = QStringLiteral(
"( float ) ( %1 > %2 )" ).arg( left ).arg( right );
293 result = QStringLiteral(
"%1 > %2" ).arg( left ).arg( right );
297 result = QStringLiteral(
"( float ) ( %1 < %2 )" ).arg( left ).arg( right );
299 result = QStringLiteral(
"%1 < %2" ).arg( left ).arg( right );
303 result = QStringLiteral(
"( float ) ( %1 >= %2 )" ).arg( left ).arg( right );
305 result = QStringLiteral(
"%1 >= %2" ).arg( left ).arg( right );
309 result = QStringLiteral(
"( float ) ( %1 <= %2 )" ).arg( left ).arg( right );
311 result = QStringLiteral(
"%1 <= %2" ).arg( left ).arg( right );
315 result = QStringLiteral(
"( float ) ( %1 && %2 )" ).arg( left ).arg( right );
317 result = QStringLiteral(
"%1 AND %2" ).arg( left ).arg( right );
321 result = QStringLiteral(
"( float ) ( %1 || %2 )" ).arg( left ).arg( right );
323 result = QStringLiteral(
"%1 OR %2" ).arg( left ).arg( right );
326 result = QStringLiteral(
"sqrt( %1 )" ).arg( left );
329 result = QStringLiteral(
"sin( %1 )" ).arg( left );
332 result = QStringLiteral(
"cos( %1 )" ).arg( left );
335 result = QStringLiteral(
"tan( %1 )" ).arg( left );
338 result = QStringLiteral(
"asin( %1 )" ).arg( left );
341 result = QStringLiteral(
"acos( %1 )" ).arg( left );
344 result = QStringLiteral(
"atan( %1 )" ).arg( left );
347 result = QStringLiteral(
"log( %1 )" ).arg( left );
350 result = QStringLiteral(
"log10( %1 )" ).arg( left );
354 result = QStringLiteral(
"fabs( %1 )" ).arg( left );
357 result = QStringLiteral(
"abs( %1 )" ).arg( left );
361 result = QStringLiteral(
"min( ( float ) ( %1 ), ( float ) ( %2 ) )" ).arg( left ).arg( right );
363 result = QStringLiteral(
"min( %1, %2 )" ).arg( left ).arg( right );
367 result = QStringLiteral(
"max( ( float ) ( %1 ), ( float ) ( %2 ) )" ).arg( left ).arg( right );
369 result = QStringLiteral(
"max( %1, %2 )" ).arg( left ).arg( right );
377 result = QStringLiteral(
"( float ) \"%1\"" ).arg( mRasterName );
379 result = QStringLiteral(
"\"%1\"" ).arg( mRasterName );
382 result = QString::number( mNumber );
385 result = QStringLiteral(
"( float ) %1" ).arg( result );
391 if ( mFunctionName ==
"if" )
393 const QString argOne = mFunctionArgs.at( 0 )->toString( cStyle );
394 const QString argTwo = mFunctionArgs.at( 1 )->toString( cStyle );
395 const QString argThree = mFunctionArgs.at( 2 )->toString( cStyle );
397 result = QStringLiteral(
" ( %1 ) ? ( %2 ) : ( %3 ) " ).arg( argOne, argTwo, argThree );
399 result = QStringLiteral(
"if( %1 , %2 , %3 )" ).arg( argOne, argTwo, argThree );
408 QList<const QgsRasterCalcNode *> nodeList;
410 nodeList.push_back(
this );
417 nodeList.append( node->findNodes(
type ) );
424 extern QgsRasterCalcNode *localParseRasterCalcString(
const QString &
str, QString & parserErrorMsg );
425 return localParseRasterCalcString(
str, parserErrorMsg );
430 QStringList referencedRasters;
433 for (
const auto &i : rasterRef )
435 if ( referencedRasters.contains( i.mid( 0, i.lastIndexOf(
"@" ) ) ) )
continue;
436 referencedRasters << i.mid( 0, i.lastIndexOf(
"@" ) );
439 return referencedRasters;
444 QStringList rasterReferences;
450 QString layerRef( r->toString() );
451 if ( layerRef.at( 0 ) == QLatin1String(
"\"" ) && layerRef.at( layerRef.size() - 1 ) == QLatin1String(
"\"" ) )
453 layerRef.remove( 0, 1 );
457 layerRef.remove( QChar(
'\\' ), Qt::CaseInsensitive );
458 rasterReferences << layerRef;
461 return rasterReferences;
464QgsRasterMatrix QgsRasterCalcNode::evaluateFunction(
const std::vector< std::unique_ptr< QgsRasterMatrix > > &matrixVector,
QgsRasterMatrix &result )
const
467 if ( mFunctionName ==
"if" )
470 if ( matrixVector.at( 0 )->isNumber() )
472 result = ( matrixVector.at( 0 )->data() ? * matrixVector.at( 1 ) : * matrixVector.at( 2 ) );
475 int nCols = matrixVector.at( 0 )->
nColumns();
476 int nRows = matrixVector.at( 0 )->nRows();
477 int nEntries = nCols * nRows;
478 std::unique_ptr< double[] > dataResult = std::make_unique< double[] >( nEntries );
479 double *dataResultRawPtr = dataResult.get();
481 double *condition = matrixVector.at( 0 )->data();
482 double *firstOption = matrixVector.at( 1 )->data();
483 double *secondOption = matrixVector.at( 2 )->data();
485 bool isFirstOptionNumber = matrixVector.at( 1 )->isNumber();
486 bool isSecondCOptionNumber = matrixVector.at( 2 )->isNumber();
487 double noDataValueCondition = matrixVector.at( 0 )->nodataValue();
489 for (
int i = 0; i < nEntries; ++i )
491 if ( condition[i] == noDataValueCondition )
496 else if ( condition[i] != 0 )
498 dataResultRawPtr[i] = isFirstOptionNumber ? firstOption[0] : firstOption[i];
501 dataResultRawPtr[i] = isSecondCOptionNumber ? secondOption[0] : secondOption[i];
Represents a node in a raster calculator.
QString toString(bool cStyle=false) const
Returns a string representation of the expression.
Operator
possible operators
QgsRasterCalcNode()=default
Constructor for QgsRasterCalcNode.
QList< const QgsRasterCalcNode * > findNodes(const QgsRasterCalcNode::Type type) const
Returns a list of nodes of a specific type.
static QgsRasterCalcNode * parseRasterCalcString(const QString &str, QString &parserErrorMsg)
QStringList referencedLayerNames()
Returns a list of raster layer names that are referenced in the formula without the quotation marks.
bool calculate(QMap< QString, QgsRasterBlock * > &rasterData, QgsRasterMatrix &result, int row=-1) const
Calculates result of raster calculation (might be real matrix or single number).
QStringList cleanRasterReferences()
Returns a list of raster layer references that are addressed in the formula, without quotation marks.
Type
defines possible types of node
Represents a matrix in a raster calculator operation.
bool equal(const QgsRasterMatrix &other)
bool max(const QgsRasterMatrix &other)
Calculates the maximum value between two matrices.
bool greaterEqual(const QgsRasterMatrix &other)
double * takeData()
Returns data and ownership.
bool notEqual(const QgsRasterMatrix &other)
bool greaterThan(const QgsRasterMatrix &other)
bool logicalOr(const QgsRasterMatrix &other)
bool lesserEqual(const QgsRasterMatrix &other)
bool absoluteValue()
Calculates the absolute value.
bool logicalAnd(const QgsRasterMatrix &other)
bool lesserThan(const QgsRasterMatrix &other)
bool add(const QgsRasterMatrix &other)
Adds another matrix to this one.
bool multiply(const QgsRasterMatrix &other)
double * data()
Returns data array (but not ownership)
bool power(const QgsRasterMatrix &other)
double nodataValue() const
bool min(const QgsRasterMatrix &other)
Calculates the minimum value between two matrices.
bool divide(const QgsRasterMatrix &other)
void setData(int cols, int rows, double *data, double nodataValue)
bool subtract(const QgsRasterMatrix &other)
Subtracts another matrix from this one.
#define QgsDebugError(str)