40 , mFunctionName( functionName )
41 , mFunctionArgs( functionArgs )
47 , mRasterName( rasterName )
49 if ( mRasterName.startsWith(
'"' ) && mRasterName.endsWith(
'"' ) )
50 mRasterName = mRasterName.mid( 1, mRasterName.size() - 2 );
57 for (
int i = 0; i < mFunctionArgs.size(); ++i )
59 if ( mFunctionArgs.at( i ) )
60 delete mFunctionArgs.at( i );
71 const QMap<QString, QgsRasterBlock *>::iterator it = rasterData.find( mRasterName );
72 if ( it == rasterData.end() )
74 QgsDebugError( QStringLiteral(
"Error: could not find raster data for \"%1\"" ).arg( mRasterName ) );
78 const int nRows = ( row >= 0 ? 1 : ( *it )->height() );
79 const int startRow = ( row >= 0 ? row : 0 );
80 const int endRow = startRow + nRows;
81 const int nCols = ( *it )->width();
82 const int nEntries = nCols * nRows;
83 double *data =
new double[nEntries];
88 bool isNoData =
false;
89 for (
int dataRow = startRow; dataRow < endRow; ++dataRow, ++outRow )
91 for (
int dataCol = 0; dataCol < nCols; ++dataCol )
93 const double value = ( *it )->valueAndNoData( dataRow, dataCol, isNoData );
94 data[dataCol + nCols * outRow] = isNoData ? result.
nodataValue() : value;
105 if ( !mLeft || !mLeft->
calculate( rasterData, leftMatrix, row ) )
109 if ( mRight && !mRight->
calculate( rasterData, rightMatrix, row ) )
117 leftMatrix.
add( rightMatrix );
126 leftMatrix.
divide( rightMatrix );
129 leftMatrix.
power( rightMatrix );
132 leftMatrix.
equal( rightMatrix );
156 leftMatrix.
min( rightMatrix );
159 leftMatrix.
max( rightMatrix );
197 const int newNColumns = leftMatrix.
nColumns();
198 const int newNRows = leftMatrix.
nRows();
204 const size_t nEntries =
static_cast<size_t>( result.
nColumns() * result.
nRows() );
205 double *data =
new double[nEntries];
206 std::fill( data, data + nEntries, mNumber );
214 double *data =
new double[nEntries];
215 for (
int i = 0; i < nEntries; ++i )
224 std::vector<std::unique_ptr<QgsRasterMatrix>> matrixContainer;
225 for (
int i = 0; i < mFunctionArgs.size(); ++i )
227 std::unique_ptr<QgsRasterMatrix> singleMatrix = std::make_unique<QgsRasterMatrix>( result.
nColumns(), result.
nRows(),
nullptr, result.
nodataValue() );
228 if ( !mFunctionArgs.at( i ) || !mFunctionArgs.at( i )->calculate( rasterData, *singleMatrix, row ) )
232 matrixContainer.emplace_back( std::move( singleMatrix ) );
234 evaluateFunction( matrixContainer, result );
256 result = QStringLiteral(
"( %1 + %2 )" ).arg( left ).arg( right );
259 result = QStringLiteral(
"( %1 - %2 )" ).arg( left ).arg( right );
262 result = QStringLiteral(
"-%1" ).arg( left );
265 result = QStringLiteral(
"%1 * %2" ).arg( left ).arg( right );
268 result = QStringLiteral(
"%1 / %2" ).arg( left ).arg( right );
272 result = QStringLiteral(
"pow( %1, %2 )" ).arg( left ).arg( right );
274 result = QStringLiteral(
"%1^%2" ).arg( left ).arg( right );
278 result = QStringLiteral(
"( float ) ( %1 == %2 )" ).arg( left ).arg( right );
280 result = QStringLiteral(
"%1 = %2" ).arg( left ).arg( right );
284 result = QStringLiteral(
"( float ) ( %1 != %2 )" ).arg( left ).arg( right );
286 result = QStringLiteral(
"%1 != %2" ).arg( left ).arg( right );
290 result = QStringLiteral(
"( float ) ( %1 > %2 )" ).arg( left ).arg( right );
292 result = QStringLiteral(
"%1 > %2" ).arg( left ).arg( right );
296 result = QStringLiteral(
"( float ) ( %1 < %2 )" ).arg( left ).arg( right );
298 result = QStringLiteral(
"%1 < %2" ).arg( left ).arg( right );
302 result = QStringLiteral(
"( float ) ( %1 >= %2 )" ).arg( left ).arg( right );
304 result = QStringLiteral(
"%1 >= %2" ).arg( left ).arg( right );
308 result = QStringLiteral(
"( float ) ( %1 <= %2 )" ).arg( left ).arg( right );
310 result = QStringLiteral(
"%1 <= %2" ).arg( left ).arg( right );
314 result = QStringLiteral(
"( float ) ( %1 && %2 )" ).arg( left ).arg( right );
316 result = QStringLiteral(
"%1 AND %2" ).arg( left ).arg( right );
320 result = QStringLiteral(
"( float ) ( %1 || %2 )" ).arg( left ).arg( right );
322 result = QStringLiteral(
"%1 OR %2" ).arg( left ).arg( right );
325 result = QStringLiteral(
"sqrt( %1 )" ).arg( left );
328 result = QStringLiteral(
"sin( %1 )" ).arg( left );
331 result = QStringLiteral(
"cos( %1 )" ).arg( left );
334 result = QStringLiteral(
"tan( %1 )" ).arg( left );
337 result = QStringLiteral(
"asin( %1 )" ).arg( left );
340 result = QStringLiteral(
"acos( %1 )" ).arg( left );
343 result = QStringLiteral(
"atan( %1 )" ).arg( left );
346 result = QStringLiteral(
"log( %1 )" ).arg( left );
349 result = QStringLiteral(
"log10( %1 )" ).arg( left );
353 result = QStringLiteral(
"fabs( %1 )" ).arg( left );
356 result = QStringLiteral(
"abs( %1 )" ).arg( left );
360 result = QStringLiteral(
"min( ( float ) ( %1 ), ( float ) ( %2 ) )" ).arg( left ).arg( right );
362 result = QStringLiteral(
"min( %1, %2 )" ).arg( left ).arg( right );
366 result = QStringLiteral(
"max( ( float ) ( %1 ), ( float ) ( %2 ) )" ).arg( left ).arg( right );
368 result = QStringLiteral(
"max( %1, %2 )" ).arg( left ).arg( right );
376 result = QStringLiteral(
"( float ) \"%1\"" ).arg( mRasterName );
378 result = QStringLiteral(
"\"%1\"" ).arg( mRasterName );
381 result = QString::number( mNumber );
384 result = QStringLiteral(
"( float ) %1" ).arg( result );
390 if ( mFunctionName ==
"if" )
392 const QString argOne = mFunctionArgs.at( 0 )->toString( cStyle );
393 const QString argTwo = mFunctionArgs.at( 1 )->toString( cStyle );
394 const QString argThree = mFunctionArgs.at( 2 )->toString( cStyle );
396 result = QStringLiteral(
" ( %1 ) ? ( %2 ) : ( %3 ) " ).arg( argOne, argTwo, argThree );
398 result = QStringLiteral(
"if( %1 , %2 , %3 )" ).arg( argOne, argTwo, argThree );
407 QList<const QgsRasterCalcNode *> nodeList;
409 nodeList.push_back(
this );
416 nodeList.append( node->findNodes(
type ) );
423 extern QgsRasterCalcNode *localParseRasterCalcString(
const QString &str, QString &parserErrorMsg );
424 return localParseRasterCalcString( str, parserErrorMsg );
429 QStringList referencedRasters;
432 for (
const auto &i : rasterRef )
434 if ( referencedRasters.contains( i.mid( 0, i.lastIndexOf(
"@" ) ) ) )
436 referencedRasters << i.mid( 0, i.lastIndexOf(
"@" ) );
439 return referencedRasters;
444 QStringList rasterReferences;
449 QString layerRef( r->toString() );
450 if ( layerRef.at( 0 ) == QLatin1String(
"\"" ) && layerRef.at( layerRef.size() - 1 ) == QLatin1String(
"\"" ) )
452 layerRef.remove( 0, 1 );
455 layerRef.remove( QChar(
'\\' ), Qt::CaseInsensitive );
456 rasterReferences << layerRef;
459 return rasterReferences;
462QgsRasterMatrix QgsRasterCalcNode::evaluateFunction(
const std::vector<std::unique_ptr<QgsRasterMatrix>> &matrixVector,
QgsRasterMatrix &result )
const
464 if ( mFunctionName ==
"if" )
467 if ( matrixVector.at( 0 )->isNumber() )
469 result = ( matrixVector.at( 0 )->data() ? *matrixVector.at( 1 ) : *matrixVector.at( 2 ) );
472 int nCols = matrixVector.at( 0 )->
nColumns();
473 int nRows = matrixVector.at( 0 )->nRows();
474 int nEntries = nCols * nRows;
475 std::unique_ptr<double[]> dataResult = std::make_unique<double[]>( nEntries );
476 double *dataResultRawPtr = dataResult.get();
478 double *condition = matrixVector.at( 0 )->data();
479 double *firstOption = matrixVector.at( 1 )->data();
480 double *secondOption = matrixVector.at( 2 )->data();
482 bool isFirstOptionNumber = matrixVector.at( 1 )->isNumber();
483 bool isSecondCOptionNumber = matrixVector.at( 2 )->isNumber();
484 double noDataValueCondition = matrixVector.at( 0 )->nodataValue();
486 for (
int i = 0; i < nEntries; ++i )
488 if ( condition[i] == noDataValueCondition )
493 else if ( condition[i] != 0 )
495 dataResultRawPtr[i] = isFirstOptionNumber ? firstOption[0] : firstOption[i];
498 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
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)
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 referencedLayerNames() const
Returns a list of raster layer names that are referenced in the formula without the quotation marks.
QStringList cleanRasterReferences() const
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)