77 QVariantList argValues;
81 const QList< QgsExpressionNode * > argList = args->
list();
88 v = QVariant::fromValue( n );
92 v = n->eval( parent, context );
94 bool defaultParamIsNull = mParameterList.count() > arg && mParameterList.at( arg ).optional() && !mParameterList.at( arg ).defaultValue().isValid();
95 if ( QgsExpressionUtils::isNull( v ) && !defaultParamIsNull && !
handlesNull() )
98 argValues.append( v );
103 return func( argValues, context, parent, node );
114 return QStringList();
141 return mGroups.isEmpty() ? false : mGroups.contains( QStringLiteral(
"deprecated" ) );
146 return ( QString::compare( mName, other.mName, Qt::CaseInsensitive ) == 0 );
158 const QString &group,
159 const QString &helpText,
163 const QStringList &aliases,
167 , mAliases( aliases )
168 , mUsesGeometry( false )
169 , mUsesGeometryFunc( usesGeometry )
170 , mReferencedColumnsFunc( referencedColumns )
182 if ( mUsesGeometryFunc )
183 return mUsesGeometryFunc( node );
185 return mUsesGeometry;
190 if ( mReferencedColumnsFunc )
191 return mReferencedColumnsFunc( node );
193 return mReferencedColumns;
199 return mIsStaticFunc( node, parent, context );
207 return mPrepareFunc( node, parent, context );
219 mIsStaticFunc =
nullptr;
225 mPrepareFunc = prepareFunc;
230 if ( node && node->
args() )
232 const QList< QgsExpressionNode * > argList = node->
args()->
list();
235 if ( !argNode->isStatic( parent, context ) )
245 double start = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
246 double stop = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
247 double step = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
249 if ( step == 0.0 || ( step > 0.0 && start > stop ) || ( step < 0.0 && start < stop ) )
256 double current = start + step;
257 while ( ( ( step > 0.0 && current <= stop ) || ( step < 0.0 && current >= stop ) ) && length <= 1000000 )
272 QString name = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
278 QString templateString = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
287 QString expString = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
289 return expression.evaluate( context );
294 double x = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
295 return QVariant( std::sqrt( x ) );
300 double val = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
301 return QVariant( std::fabs( val ) );
306 double deg = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
307 return ( deg * M_PI ) / 180;
311 double rad = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
312 return ( 180 * rad ) / M_PI;
316 double x = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
317 return QVariant( std::sin( x ) );
321 double x = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
322 return QVariant( std::cos( x ) );
326 double x = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
327 return QVariant( std::tan( x ) );
331 double x = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
332 return QVariant( std::asin( x ) );
336 double x = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
337 return QVariant( std::acos( x ) );
341 double x = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
342 return QVariant( std::atan( x ) );
346 double y = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
347 double x = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
348 return QVariant( std::atan2( y, x ) );
352 double x = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
353 return QVariant( std::exp( x ) );
357 double x = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
360 return QVariant( std::log( x ) );
364 double x = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
367 return QVariant( log10( x ) );
371 double b = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
372 double x = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
373 if ( x <= 0 || b <= 0 )
375 return QVariant( std::log( x ) / std::log( b ) );
379 double min = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
380 double max = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
384 std::random_device rd;
385 std::mt19937_64 generator( rd() );
387 if ( !QgsExpressionUtils::isNull( values.at( 2 ) ) )
390 if ( QgsExpressionUtils::isIntSafe( values.at( 2 ) ) )
393 seed = QgsExpressionUtils::getIntValue( values.at( 2 ), parent );
398 QString seedStr = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
399 std::hash<std::string> hasher;
400 seed = hasher( seedStr.toStdString() );
402 generator.seed( seed );
406 double f =
static_cast< double >( generator() ) / generator.max();
407 return QVariant( min + f * ( max - min ) );
411 qlonglong min = QgsExpressionUtils::getIntValue( values.at( 0 ), parent );
412 qlonglong max = QgsExpressionUtils::getIntValue( values.at( 1 ), parent );
416 std::random_device rd;
417 std::mt19937_64 generator( rd() );
419 if ( !QgsExpressionUtils::isNull( values.at( 2 ) ) )
422 if ( QgsExpressionUtils::isIntSafe( values.at( 2 ) ) )
425 seed = QgsExpressionUtils::getIntValue( values.at( 2 ), parent );
430 QString seedStr = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
431 std::hash<std::string> hasher;
432 seed = hasher( seedStr.toStdString() );
434 generator.seed( seed );
437 qint64 randomInteger = min + ( generator() % ( max - min + 1 ) );
438 if ( randomInteger > std::numeric_limits<int>::max() || randomInteger < -std::numeric_limits<int>::max() )
440 return QVariant( randomInteger );
445 return QVariant(
int( randomInteger ) );
449 return QVariant( min + ( generator() % ( max - min + 1 ) ) );
454 double val = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
455 double domainMin = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
456 double domainMax = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
457 double rangeMin = QgsExpressionUtils::getDoubleValue( values.at( 3 ), parent );
458 double rangeMax = QgsExpressionUtils::getDoubleValue( values.at( 4 ), parent );
460 if ( domainMin >= domainMax )
462 parent->
setEvalErrorString( QObject::tr(
"Domain max must be greater than domain min" ) );
467 if ( val >= domainMax )
471 else if ( val <= domainMin )
477 double m = ( rangeMax - rangeMin ) / ( domainMax - domainMin );
478 double c = rangeMin - ( domainMin * m );
481 return QVariant( m * val +
c );
486 double val = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
487 double domainMin = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
488 double domainMax = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
489 double rangeMin = QgsExpressionUtils::getDoubleValue( values.at( 3 ), parent );
490 double rangeMax = QgsExpressionUtils::getDoubleValue( values.at( 4 ), parent );
491 double exponent = QgsExpressionUtils::getDoubleValue( values.at( 5 ), parent );
493 if ( domainMin >= domainMax )
495 parent->
setEvalErrorString( QObject::tr(
"Domain max must be greater than domain min" ) );
505 if ( val >= domainMax )
509 else if ( val <= domainMin )
515 return QVariant( ( ( rangeMax - rangeMin ) / std::pow( domainMax - domainMin, exponent ) ) * std::pow( val - domainMin, exponent ) + rangeMin );
520 QVariant result( QVariant::Double );
521 double maxVal = std::numeric_limits<double>::quiet_NaN();
522 for (
const QVariant &val : values )
524 double testVal = val.isNull() ? std::numeric_limits<double>::quiet_NaN() : QgsExpressionUtils::getDoubleValue( val, parent );
525 if ( std::isnan( maxVal ) )
529 else if ( !std::isnan( testVal ) )
531 maxVal = std::max( maxVal, testVal );
535 if ( !std::isnan( maxVal ) )
537 result = QVariant( maxVal );
544 QVariant result( QVariant::Double );
545 double minVal = std::numeric_limits<double>::quiet_NaN();
546 for (
const QVariant &val : values )
548 double testVal = val.isNull() ? std::numeric_limits<double>::quiet_NaN() : QgsExpressionUtils::getDoubleValue( val, parent );
549 if ( std::isnan( minVal ) )
553 else if ( !std::isnan( testVal ) )
555 minVal = std::min( minVal, testVal );
559 if ( !std::isnan( minVal ) )
561 result = QVariant( minVal );
573 QVariant value = node->
eval( parent, context );
575 QgsVectorLayer *vl = QgsExpressionUtils::getVectorLayer( value, parent );
578 parent->
setEvalErrorString( QObject::tr(
"Cannot find layer with name or ID '%1'" ).arg( value.toString() ) );
583 node = QgsExpressionUtils::getNode( values.at( 1 ), parent );
585 value = node->
eval( parent, context );
591 parent->
setEvalErrorString( QObject::tr(
"No such aggregate '%1'" ).arg( value.toString() ) );
596 node = QgsExpressionUtils::getNode( values.at( 2 ), parent );
598 QString subExpression = node->
dump();
602 if ( values.count() > 3 )
604 node = QgsExpressionUtils::getNode( values.at( 3 ), parent );
607 if ( !nl || nl->value().isValid() )
612 if ( values.count() > 4 )
614 node = QgsExpressionUtils::getNode( values.at( 4 ), parent );
616 value = node->
eval( parent, context );
623 if ( values.count() > 5 )
625 node = QgsExpressionUtils::getNode( values.at( 5 ), parent );
628 if ( !nl || nl->value().isValid() )
630 orderBy = node->
dump();
641 if ( filterExp.referencedVariables().contains( QStringLiteral(
"parent" ) )
642 || filterExp.referencedVariables().contains( QString() )
643 || subExp.referencedVariables().contains( QStringLiteral(
"parent" ) )
644 || subExp.referencedVariables().contains( QString() ) )
646 cacheKey = QStringLiteral(
"aggfcn:%1:%2:%3:%4:%5%6:%7" ).arg( vl->id(), QString::number( aggregate ), subExpression, parameters.
filter,
651 cacheKey = QStringLiteral(
"aggfcn:%1:%2:%3:%4:%5" ).arg( vl->id(), QString::number( aggregate ), subExpression, parameters.
filter, orderBy );
660 subContext.appendScope( subScope );
661 result = vl->aggregate( aggregate, subExpression, parameters, &subContext, &ok );
667 result = vl->aggregate( aggregate, subExpression, parameters,
nullptr, &ok );
671 parent->
setEvalErrorString( QObject::tr(
"Could not calculate aggregate for: %1" ).arg( subExpression ) );
682 parent->
setEvalErrorString( QObject::tr(
"Cannot use relation aggregate function in this context" ) );
687 QgsVectorLayer *vl = QgsExpressionUtils::getVectorLayer( context->
variable( QStringLiteral(
"layer" ) ), parent );
690 parent->
setEvalErrorString( QObject::tr(
"Cannot use relation aggregate function in this context" ) );
699 QVariant value = node->
eval( parent, context );
701 QString relationId = value.toString();
708 if ( relations.isEmpty() || relations.at( 0 ).referencedLayer() != vl )
710 parent->
setEvalErrorString( QObject::tr(
"Cannot find relation with id '%1'" ).arg( relationId ) );
715 relation = relations.at( 0 );
722 node = QgsExpressionUtils::getNode( values.at( 1 ), parent );
724 value = node->
eval( parent, context );
730 parent->
setEvalErrorString( QObject::tr(
"No such aggregate '%1'" ).arg( value.toString() ) );
735 node = QgsExpressionUtils::getNode( values.at( 2 ), parent );
737 QString subExpression = node->
dump();
741 if ( values.count() > 3 )
743 node = QgsExpressionUtils::getNode( values.at( 3 ), parent );
745 value = node->
eval( parent, context );
752 if ( values.count() > 4 )
754 node = QgsExpressionUtils::getNode( values.at( 4 ), parent );
757 if ( !nl || nl->value().isValid() )
759 orderBy = node->
dump();
770 QString cacheKey = QStringLiteral(
"relagg:%1:%2:%3:%4:%5" ).arg( vl->
id(),
771 QString::number(
static_cast< int >( aggregate ) ),
783 result = childLayer->
aggregate( aggregate, subExpression, parameters, &subContext, &ok );
787 parent->
setEvalErrorString( QObject::tr(
"Could not calculate aggregate for: %1" ).arg( subExpression ) );
801 parent->
setEvalErrorString( QObject::tr(
"Cannot use aggregate function in this context" ) );
806 QgsVectorLayer *vl = QgsExpressionUtils::getVectorLayer( context->
variable( QStringLiteral(
"layer" ) ), parent );
809 parent->
setEvalErrorString( QObject::tr(
"Cannot use aggregate function in this context" ) );
818 QString subExpression = node->
dump();
822 if ( values.count() > 1 )
824 node = QgsExpressionUtils::getNode( values.at( 1 ), parent );
827 if ( !nl || nl->value().isValid() )
828 groupBy = node->
dump();
832 if ( values.count() > 2 )
834 node = QgsExpressionUtils::getNode( values.at( 2 ), parent );
837 if ( !nl || nl->value().isValid() )
843 if ( orderByPos >= 0 && values.count() > orderByPos )
845 node = QgsExpressionUtils::getNode( values.at( orderByPos ), parent );
848 if ( !nl || nl->value().isValid() )
850 orderBy = node->
dump();
858 if ( !groupBy.isEmpty() )
861 QVariant groupByValue = groupByExp.evaluate( context );
862 QString groupByClause = QStringLiteral(
"%1 %2 %3" ).arg( groupBy,
863 groupByValue.isNull() ? QStringLiteral(
"is" ) : QStringLiteral(
"=" ),
865 if ( !parameters.
filter.isEmpty() )
866 parameters.
filter = QStringLiteral(
"(%1) AND (%2)" ).arg( parameters.
filter, groupByClause );
868 parameters.
filter = groupByClause;
871 QString cacheKey = QStringLiteral(
"agg:%1:%2:%3:%4:%5" ).arg( vl->
id(),
872 QString::number(
static_cast< int >( aggregate ) ),
883 result = vl->
aggregate( aggregate, subExpression, parameters, &subContext, &ok );
887 parent->
setEvalErrorString( QObject::tr(
"Could not calculate aggregate for: %1" ).arg( subExpression ) );
992 if ( values.count() > 3 )
996 QVariant value = node->
eval( parent, context );
1009 if ( values.count() > 3 )
1011 QgsExpressionNode *node = QgsExpressionUtils::getNode( values.at( 3 ), parent );
1013 QVariant value = node->
eval( parent, context );
1015 parameters.
delimiter = value.toString();
1031 QVariant scale = context->
variable( QStringLiteral(
"map_scale" ) );
1033 if ( !scale.isValid() || scale.isNull() )
1036 const double v = scale.toDouble( &ok );
1044 double minValue = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
1045 double testValue = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
1046 double maxValue = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
1049 if ( testValue <= minValue )
1051 return QVariant( minValue );
1053 else if ( testValue >= maxValue )
1055 return QVariant( maxValue );
1059 return QVariant( testValue );
1065 double x = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
1066 return QVariant( std::floor( x ) );
1071 double x = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
1072 return QVariant( std::ceil( x ) );
1077 return QVariant( QgsExpressionUtils::getIntValue( values.at( 0 ), parent ) );
1081 return QVariant( QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent ) );
1085 return QVariant( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ) );
1090 QString format = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
1091 QString language = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
1092 if ( format.isEmpty() && !language.isEmpty() )
1094 parent->
setEvalErrorString( QObject::tr(
"A format is required to convert to DateTime when the language is specified" ) );
1095 return QVariant( QDateTime() );
1098 if ( format.isEmpty() && language.isEmpty() )
1099 return QVariant( QgsExpressionUtils::getDateTimeValue( values.at( 0 ), parent ) );
1101 QString datetimestring = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1102 QLocale locale = QLocale();
1103 if ( !language.isEmpty() )
1105 locale = QLocale( language );
1108 QDateTime datetime = locale.toDateTime( datetimestring, format );
1109 if ( !datetime.isValid() )
1111 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to DateTime" ).arg( datetimestring ) );
1112 datetime = QDateTime();
1114 return QVariant( datetime );
1119 const int year = QgsExpressionUtils::getIntValue( values.at( 0 ), parent );
1120 const int month = QgsExpressionUtils::getIntValue( values.at( 1 ), parent );
1121 const int day = QgsExpressionUtils::getIntValue( values.at( 2 ), parent );
1123 const QDate date( year, month, day );
1124 if ( !date.isValid() )
1126 parent->
setEvalErrorString( QObject::tr(
"'%1-%2-%3' is not a valid date" ).arg( year ).arg( month ).arg( day ) );
1129 return QVariant( date );
1134 const int hours = QgsExpressionUtils::getIntValue( values.at( 0 ), parent );
1135 const int minutes = QgsExpressionUtils::getIntValue( values.at( 1 ), parent );
1136 const double seconds = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
1138 const QTime time( hours, minutes, std::floor( seconds ), ( seconds - std::floor( seconds ) ) * 1000 );
1139 if ( !time.isValid() )
1141 parent->
setEvalErrorString( QObject::tr(
"'%1-%2-%3' is not a valid time" ).arg( hours ).arg( minutes ).arg( seconds ) );
1144 return QVariant( time );
1149 const int year = QgsExpressionUtils::getIntValue( values.at( 0 ), parent );
1150 const int month = QgsExpressionUtils::getIntValue( values.at( 1 ), parent );
1151 const int day = QgsExpressionUtils::getIntValue( values.at( 2 ), parent );
1152 const int hours = QgsExpressionUtils::getIntValue( values.at( 3 ), parent );
1153 const int minutes = QgsExpressionUtils::getIntValue( values.at( 4 ), parent );
1154 const double seconds = QgsExpressionUtils::getDoubleValue( values.at( 5 ), parent );
1156 const QDate date( year, month, day );
1157 if ( !date.isValid() )
1159 parent->
setEvalErrorString( QObject::tr(
"'%1-%2-%3' is not a valid date" ).arg( year ).arg( month ).arg( day ) );
1162 const QTime time( hours, minutes, std::floor( seconds ), ( seconds - std::floor( seconds ) ) * 1000 );
1163 if ( !time.isValid() )
1165 parent->
setEvalErrorString( QObject::tr(
"'%1-%2-%3' is not a valid time" ).arg( hours ).arg( minutes ).arg( seconds ) );
1168 return QVariant( QDateTime( date, time ) );
1173 const double years = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
1174 const double months = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
1175 const double weeks = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
1176 const double days = QgsExpressionUtils::getDoubleValue( values.at( 3 ), parent );
1177 const double hours = QgsExpressionUtils::getDoubleValue( values.at( 4 ), parent );
1178 const double minutes = QgsExpressionUtils::getDoubleValue( values.at( 5 ), parent );
1179 const double seconds = QgsExpressionUtils::getDoubleValue( values.at( 6 ), parent );
1181 return QVariant::fromValue(
QgsInterval( years, months, weeks, days, hours, minutes, seconds ) );
1186 for (
const QVariant &value : values )
1188 if ( value.isNull() )
1197 const QVariant val1 = values.at( 0 );
1198 const QVariant val2 = values.at( 1 );
1208 QString str = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1209 return QVariant( str.toLower() );
1213 QString str = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1214 return QVariant( str.toUpper() );
1218 QString str = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1219 QStringList elems = str.split(
' ' );
1220 for (
int i = 0; i < elems.size(); i++ )
1222 if ( elems[i].size() > 1 )
1223 elems[i] = elems[i].at( 0 ).toUpper() + elems[i].mid( 1 ).toLower();
1225 return QVariant( elems.join( QStringLiteral(
" " ) ) );
1230 QString str = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1231 return QVariant( str.trimmed() );
1236 QString string1 = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1237 QString string2 = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
1243 QString string1 = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1244 QString string2 = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
1250 QString string1 = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1251 QString string2 = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
1258 QString
string = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1264 QChar character = QChar( QgsExpressionUtils::getNativeIntValue( values.at( 0 ), parent ) );
1265 return QVariant( QString( character ) );
1270 QString value = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1272 if ( value.isEmpty() )
1277 int res = value.at( 0 ).unicode();
1278 return QVariant( res );
1283 if ( values.length() == 2 || values.length() == 3 )
1285 QString str = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1286 qlonglong wrap = QgsExpressionUtils::getIntValue( values.at( 1 ), parent );
1288 QString customdelimiter = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
1302 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
1306 return QVariant( geom.
length() );
1310 QString str = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1311 return QVariant( str.length() );
1316 if ( values.count() == 2 && values.at( 1 ).type() == QVariant::Map )
1318 QString str = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1319 QVariantMap map = QgsExpressionUtils::getMapValue( values.at( 1 ), parent );
1321 for ( QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it )
1323 str = str.replace( it.key(), it.value().toString() );
1326 return QVariant( str );
1328 else if ( values.count() == 3 )
1330 QString str = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1331 QVariantList before;
1333 bool isSingleReplacement =
false;
1335 if ( values.at( 1 ).type() != QVariant::List && values.at( 2 ).type() != QVariant::StringList )
1337 before = QVariantList() << QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
1341 before = QgsExpressionUtils::getListValue( values.at( 1 ), parent );
1344 if ( values.at( 2 ).type() != QVariant::List && values.at( 2 ).type() != QVariant::StringList )
1346 after = QVariantList() << QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
1347 isSingleReplacement =
true;
1351 after = QgsExpressionUtils::getListValue( values.at( 2 ), parent );
1354 if ( !isSingleReplacement && before.length() != after.length() )
1356 parent->
setEvalErrorString( QObject::tr(
"Invalid pair of array, length not identical" ) );
1360 for (
int i = 0; i < before.length(); i++ )
1362 str = str.replace( before.at( i ).toString(), after.at( isSingleReplacement ? 0 : i ).toString() );
1365 return QVariant( str );
1369 parent->
setEvalErrorString( QObject::tr(
"Function replace requires 2 or 3 arguments" ) );
1375 QString str = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1376 QString regexp = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
1377 QString after = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
1379 QRegularExpression re( regexp );
1380 if ( !re.isValid() )
1382 parent->
setEvalErrorString( QObject::tr(
"Invalid regular expression '%1': %2" ).arg( regexp, re.errorString() ) );
1385 return QVariant( str.replace( re, after ) );
1390 QString str = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1391 QString regexp = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
1393 QRegularExpression re( regexp );
1394 if ( !re.isValid() )
1396 parent->
setEvalErrorString( QObject::tr(
"Invalid regular expression '%1': %2" ).arg( regexp, re.errorString() ) );
1399 return QVariant( ( str.indexOf( re ) + 1 ) );
1404 QString str = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1405 QString regexp = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
1406 QString empty = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
1408 QRegularExpression re( regexp );
1409 if ( !re.isValid() )
1411 parent->
setEvalErrorString( QObject::tr(
"Invalid regular expression '%1': %2" ).arg( regexp, re.errorString() ) );
1415 QRegularExpressionMatch matches = re.match( str );
1416 if ( matches.hasMatch() )
1419 QStringList list = matches.capturedTexts();
1422 for ( QStringList::const_iterator it = ++list.constBegin(); it != list.constEnd(); ++it )
1424 array += ( !( *it ).isEmpty() ) ? *it : empty;
1427 return QVariant( array );
1437 QString str = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1438 QString regexp = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
1440 QRegularExpression re( regexp );
1441 if ( !re.isValid() )
1443 parent->
setEvalErrorString( QObject::tr(
"Invalid regular expression '%1': %2" ).arg( regexp, re.errorString() ) );
1448 QRegularExpressionMatch match = re.match( str );
1449 if ( match.hasMatch() )
1452 if ( match.lastCapturedIndex() > 0 )
1455 return QVariant( match.captured( 1 ) );
1460 return QVariant( match.captured( 0 ) );
1465 return QVariant(
"" );
1471 return QUuid::createUuid().toString();
1476 if ( !values.at( 0 ).isValid() || !values.at( 1 ).isValid() )
1479 QString str = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1480 int from = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
1483 if ( values.at( 2 ).isValid() )
1484 len = QgsExpressionUtils::getNativeIntValue( values.at( 2 ), parent );
1490 from = str.size() + from;
1496 else if ( from > 0 )
1504 len = str.size() + len - from;
1511 return QVariant( str.mid( from, len ) );
1517 return QVariant(
static_cast< int >( f.
id() ) );
1522 QgsRasterLayer *layer = QgsExpressionUtils::getRasterLayer( values.at( 0 ), parent );
1525 parent->
setEvalErrorString( QObject::tr(
"Function `raster_value` requires a valid raster layer." ) );
1529 int bandNb = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
1530 if ( bandNb < 1 || bandNb > layer->
bandCount() )
1532 parent->
setEvalErrorString( QObject::tr(
"Function `raster_value` requires a valid raster band number." ) );
1536 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 2 ), parent );
1539 parent->
setEvalErrorString( QObject::tr(
"Function `raster_value` requires a valid point geometry." ) );
1547 if ( multiPoint.count() == 1 )
1549 point = multiPoint[0];
1559 return std::isnan( value ) ? QVariant() : value;
1574 if ( values.size() == 1 )
1576 attr = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1579 else if ( values.size() == 2 )
1581 feature = QgsExpressionUtils::getFeature( values.at( 0 ), parent );
1582 attr = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
1586 parent->
setEvalErrorString( QObject::tr(
"Function `attribute` requires one or two parameters. %1 given." ).arg( values.length() ) );
1596 if ( values.size() == 0 || values.at( 0 ).isNull() )
1602 feature = QgsExpressionUtils::getFeature( values.at( 0 ), parent );
1607 for (
int i = 0; i < fields.
count(); ++i )
1618 bool evaluate =
true;
1620 if ( values.isEmpty() )
1623 layer = QgsExpressionUtils::getVectorLayer( context->
variable( QStringLiteral(
"layer" ) ), parent );
1625 else if ( values.size() == 1 )
1627 layer = QgsExpressionUtils::getVectorLayer( context->
variable( QStringLiteral(
"layer" ) ), parent );
1628 feature = QgsExpressionUtils::getFeature( values.at( 0 ), parent );
1630 else if ( values.size() == 2 )
1632 layer = QgsExpressionUtils::getVectorLayer( values.at( 0 ), parent );
1633 feature = QgsExpressionUtils::getFeature( values.at( 1 ), parent );
1635 else if ( values.size() == 3 )
1637 layer = QgsExpressionUtils::getVectorLayer( values.at( 0 ), parent );
1638 feature = QgsExpressionUtils::getFeature( values.at( 1 ), parent );
1639 evaluate = values.value( 2 ).toBool();
1645 parent->
setEvalErrorString( QObject::tr(
"Function `maptip` requires no more than three parameters. %1 given." ).arg( values.length() ) );
1649 parent->
setEvalErrorString( QObject::tr(
"Function `display` requires no more than three parameters. %1 given." ).arg( values.length() ) );
1680 subContext.setFeature( feature );
1689 exp.prepare( &subContext );
1690 return exp.evaluate( &subContext ).toString();
1696 return fcnCoreFeatureMaptipDisplay( values, context, parent,
false );
1701 return fcnCoreFeatureMaptipDisplay( values, context, parent,
true );
1709 if ( values.isEmpty() )
1712 layer = QgsExpressionUtils::getVectorLayer( context->
variable( QStringLiteral(
"layer" ) ), parent );
1714 else if ( values.size() == 1 )
1716 layer = QgsExpressionUtils::getVectorLayer( context->
variable( QStringLiteral(
"layer" ) ), parent );
1717 feature = QgsExpressionUtils::getFeature( values.at( 0 ), parent );
1719 else if ( values.size() == 2 )
1721 layer = QgsExpressionUtils::getVectorLayer( values.at( 0 ), parent );
1722 feature = QgsExpressionUtils::getFeature( values.at( 1 ), parent );
1726 parent->
setEvalErrorString( QObject::tr(
"Function `is_selected` requires no more than two parameters. %1 given." ).arg( values.length() ) );
1730 if ( !layer || !feature.
isValid() )
1732 return QVariant( QVariant::Bool );
1742 if ( values.isEmpty() )
1743 layer = QgsExpressionUtils::getVectorLayer( context->
variable( QStringLiteral(
"layer" ) ), parent );
1744 else if ( values.count() == 1 )
1745 layer = QgsExpressionUtils::getVectorLayer( values.at( 0 ), parent );
1748 parent->
setEvalErrorString( QObject::tr(
"Function `num_selected` requires no more than one parameter. %1 given." ).arg( values.length() ) );
1754 return QVariant( QVariant::LongLong );
1762 static QMap<QString, qlonglong> counterCache;
1763 QVariant functionResult;
1765 std::function<void()> fetchAndIncrementFunc = [ =, &functionResult ]()
1768 const QgsVectorLayer *layer = QgsExpressionUtils::getVectorLayer( values.at( 0 ), parent );
1773 database = decodedUri.value( QStringLiteral(
"path" ) ).toString();
1774 if ( database.isEmpty() )
1776 parent->
setEvalErrorString( QObject::tr(
"Could not extract file path from layer `%1`." ).arg( layer->
name() ) );
1781 database = values.at( 0 ).toString();
1784 const QString table = values.at( 1 ).toString();
1785 const QString idColumn = values.at( 2 ).toString();
1786 const QString filterAttribute = values.at( 3 ).toString();
1787 const QVariant filterValue = values.at( 4 ).toString();
1788 const QVariantMap defaultValues = values.at( 5 ).toMap();
1794 if ( sqliteDb.
open_v2( database, SQLITE_OPEN_READWRITE,
nullptr ) != SQLITE_OK )
1797 functionResult = QVariant();
1801 QString errorMessage;
1802 QString currentValSql;
1804 qlonglong nextId = 0;
1805 bool cachedMode =
false;
1806 bool valueRetrieved =
false;
1808 QString cacheString = QStringLiteral(
"%1:%2:%3:%4:%5" ).arg( database, table, idColumn, filterAttribute, filterValue.toString() );
1815 auto cachedCounter = counterCache.find( cacheString );
1817 if ( cachedCounter != counterCache.end() )
1819 qlonglong &cachedValue = cachedCounter.value();
1820 nextId = cachedValue;
1822 cachedValue = nextId;
1823 valueRetrieved =
true;
1828 if ( !cachedMode || !valueRetrieved )
1830 int result = SQLITE_ERROR;
1833 if ( !filterAttribute.isNull() )
1838 sqliteStatement = sqliteDb.
prepare( currentValSql, result );
1840 if ( result == SQLITE_OK )
1843 if ( sqliteStatement.
step() == SQLITE_ROW )
1849 if ( cachedMode && result == SQLITE_OK )
1851 counterCache.insert( cacheString, nextId );
1855 counterCache.remove( cacheString );
1858 valueRetrieved =
true;
1862 if ( valueRetrieved )
1871 if ( !filterAttribute.isNull() )
1877 for ( QVariantMap::const_iterator iter = defaultValues.constBegin(); iter != defaultValues.constEnd(); ++iter )
1880 vals << iter.value().toString();
1883 upsertSql += QLatin1String(
" (" ) + cols.join(
',' ) +
')';
1884 upsertSql += QLatin1String(
" VALUES " );
1885 upsertSql +=
'(' + vals.join(
',' ) +
')';
1887 int result = SQLITE_ERROR;
1891 if ( transaction->
executeSql( upsertSql, errorMessage ) )
1898 result = sqliteDb.
exec( upsertSql, errorMessage );
1900 if ( result == SQLITE_OK )
1902 functionResult = QVariant( nextId );
1907 parent->
setEvalErrorString( QStringLiteral(
"Could not increment value: SQLite error: \"%1\" (%2)." ).arg( errorMessage, QString::number( result ) ) );
1908 functionResult = QVariant();
1913 functionResult = QVariant();
1918 return functionResult;
1924 for (
const QVariant &value : values )
1926 if ( !value.isNull() )
1927 concat += QgsExpressionUtils::getStringValue( value, parent );
1934 QString
string = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1935 return string.indexOf( QgsExpressionUtils::getStringValue( values.at( 1 ), parent ) ) + 1;
1940 QString
string = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1941 int pos = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
1942 return string.right( pos );
1947 QString
string = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1948 int pos = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
1949 return string.left( pos );
1954 QString
string = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1955 int length = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
1956 QString fill = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
1957 return string.leftJustified( length, fill.at( 0 ),
true );
1962 QString
string = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1963 int length = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
1964 QString fill = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
1965 return string.rightJustified( length, fill.at( 0 ),
true );
1970 if ( values.size() < 1 )
1972 parent->
setEvalErrorString( QObject::tr(
"Function format requires at least 1 argument" ) );
1976 QString
string = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
1977 for (
int n = 1; n < values.length(); n++ )
1979 string =
string.arg( QgsExpressionUtils::getStringValue( values.at( n ), parent ) );
1987 return QVariant( QDateTime::currentDateTime() );
1992 QString format = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
1993 QString language = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
1994 if ( format.isEmpty() && !language.isEmpty() )
1996 parent->
setEvalErrorString( QObject::tr(
"A format is required to convert to Date when the language is specified" ) );
1997 return QVariant( QDate() );
2000 if ( format.isEmpty() && language.isEmpty() )
2001 return QVariant( QgsExpressionUtils::getDateValue( values.at( 0 ), parent ) );
2003 QString datestring = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
2004 QLocale locale = QLocale();
2005 if ( !language.isEmpty() )
2007 locale = QLocale( language );
2010 QDate date = locale.toDate( datestring, format );
2011 if ( !date.isValid() )
2013 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to Date" ).arg( datestring ) );
2016 return QVariant( date );
2021 QString format = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
2022 QString language = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
2023 if ( format.isEmpty() && !language.isEmpty() )
2025 parent->
setEvalErrorString( QObject::tr(
"A format is required to convert to Time when the language is specified" ) );
2026 return QVariant( QTime() );
2029 if ( format.isEmpty() && language.isEmpty() )
2030 return QVariant( QgsExpressionUtils::getTimeValue( values.at( 0 ), parent ) );
2032 QString timestring = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
2033 QLocale locale = QLocale();
2034 if ( !language.isEmpty() )
2036 locale = QLocale( language );
2039 QTime time = locale.toTime( timestring, format );
2040 if ( !time.isValid() )
2042 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to Time" ).arg( timestring ) );
2045 return QVariant( time );
2050 return QVariant::fromValue( QgsExpressionUtils::getInterval( values.at( 0 ), parent ) );
2059 double value = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
2060 QString axis = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
2061 int precision = QgsExpressionUtils::getNativeIntValue( values.at( 2 ), parent );
2063 QString formatString;
2064 if ( values.count() > 3 )
2065 formatString = QgsExpressionUtils::getStringValue( values.at( 3 ), parent );
2067 QgsCoordinateFormatter::FormatFlags flags = QgsCoordinateFormatter::FormatFlags();
2068 if ( formatString.compare( QLatin1String(
"suffix" ), Qt::CaseInsensitive ) == 0 )
2072 else if ( formatString.compare( QLatin1String(
"aligned" ), Qt::CaseInsensitive ) == 0 )
2076 else if ( ! formatString.isEmpty() )
2078 parent->
setEvalErrorString( QObject::tr(
"Invalid formatting parameter: '%1'. It must be empty, or 'suffix' or 'aligned'." ).arg( formatString ) );
2082 if ( axis.compare( QLatin1String(
"x" ), Qt::CaseInsensitive ) == 0 )
2086 else if ( axis.compare( QLatin1String(
"y" ), Qt::CaseInsensitive ) == 0 )
2092 parent->
setEvalErrorString( QObject::tr(
"Invalid axis name: '%1'. It must be either 'x' or 'y'." ).arg( axis ) );
2100 return floatToDegreeFormat( format, values, context, parent, node );
2106 return floatToDegreeFormat( format, values, context, parent, node );
2111 QDateTime d1 = QgsExpressionUtils::getDateTimeValue( values.at( 0 ), parent );
2112 QDateTime d2 = QgsExpressionUtils::getDateTimeValue( values.at( 1 ), parent );
2113 qint64 seconds = d2.secsTo( d1 );
2114 return QVariant::fromValue(
QgsInterval( seconds ) );
2119 if ( !values.at( 0 ).canConvert<QDate>() )
2122 QDate date = QgsExpressionUtils::getDateValue( values.at( 0 ), parent );
2123 if ( !date.isValid() )
2128 return date.dayOfWeek() % 7;
2133 QVariant value = values.at( 0 );
2134 QgsInterval inter = QgsExpressionUtils::getInterval( value, parent,
false );
2137 return QVariant( inter.
days() );
2141 QDateTime d1 = QgsExpressionUtils::getDateTimeValue( value, parent );
2142 return QVariant( d1.date().day() );
2148 QVariant value = values.at( 0 );
2149 QgsInterval inter = QgsExpressionUtils::getInterval( value, parent,
false );
2152 return QVariant( inter.
years() );
2156 QDateTime d1 = QgsExpressionUtils::getDateTimeValue( value, parent );
2157 return QVariant( d1.date().year() );
2163 QVariant value = values.at( 0 );
2164 QgsInterval inter = QgsExpressionUtils::getInterval( value, parent,
false );
2167 return QVariant( inter.
months() );
2171 QDateTime d1 = QgsExpressionUtils::getDateTimeValue( value, parent );
2172 return QVariant( d1.date().month() );
2178 QVariant value = values.at( 0 );
2179 QgsInterval inter = QgsExpressionUtils::getInterval( value, parent,
false );
2182 return QVariant( inter.
weeks() );
2186 QDateTime d1 = QgsExpressionUtils::getDateTimeValue( value, parent );
2187 return QVariant( d1.date().weekNumber() );
2193 QVariant value = values.at( 0 );
2194 QgsInterval inter = QgsExpressionUtils::getInterval( value, parent,
false );
2197 return QVariant( inter.
hours() );
2201 QTime t1 = QgsExpressionUtils::getTimeValue( value, parent );
2202 return QVariant( t1.hour() );
2208 QVariant value = values.at( 0 );
2209 QgsInterval inter = QgsExpressionUtils::getInterval( value, parent,
false );
2212 return QVariant( inter.
minutes() );
2216 QTime t1 = QgsExpressionUtils::getTimeValue( value, parent );
2217 return QVariant( t1.minute() );
2223 QVariant value = values.at( 0 );
2224 QgsInterval inter = QgsExpressionUtils::getInterval( value, parent,
false );
2227 return QVariant( inter.
seconds() );
2231 QTime t1 = QgsExpressionUtils::getTimeValue( value, parent );
2232 return QVariant( t1.second() );
2238 QDateTime dt = QgsExpressionUtils::getDateTimeValue( values.at( 0 ), parent );
2241 return QVariant( dt.toMSecsSinceEpoch() );
2251 long long millisecs_since_epoch = QgsExpressionUtils::getIntValue( values.at( 0 ), parent );
2253 return QVariant( QDateTime::fromMSecsSinceEpoch( millisecs_since_epoch ) );
2256 #define ENSURE_GEOM_TYPE(f, g, geomtype) \
2257 if ( !(f).hasGeometry() ) \
2258 return QVariant(); \
2259 QgsGeometry g = (f).geometry(); \
2260 if ( (g).type() != (geomtype) ) \
2267 if ( g.isMultipart() )
2269 return g.asMultiPoint().at( 0 ).x();
2273 return g.asPoint().x();
2281 if ( g.isMultipart() )
2283 return g.asMultiPoint().at( 0 ).y();
2287 return g.asPoint().y();
2293 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2299 return QVariant( isValid );
2304 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2316 QVariant result( centroid.
asPoint().
x() );
2322 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2334 QVariant result( centroid.
asPoint().
y() );
2340 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2347 const QgsPoint *point = qgsgeometry_cast< const QgsPoint * >( geom.
constGet() );
2355 if ( collection->numGeometries() == 1 )
2357 if (
const QgsPoint *point = qgsgeometry_cast< const QgsPoint * >( collection->geometryN( 0 ) ) )
2368 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2375 const QgsPoint *point = qgsgeometry_cast< const QgsPoint * >( geom.
constGet() );
2383 if ( collection->numGeometries() == 1 )
2385 if (
const QgsPoint *point = qgsgeometry_cast< const QgsPoint * >( collection->geometryN( 0 ) ) )
2396 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2401 int idx = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
2428 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2445 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2462 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2467 bool ignoreClosing =
false;
2468 if ( values.length() > 1 )
2470 ignoreClosing = QgsExpressionUtils::getIntValue( values.at( 1 ), parent );
2480 bool skipLast =
false;
2481 if ( ignoreClosing && ring.count() > 2 && ring.first() == ring.last() )
2486 for (
int i = 0; i < ( skipLast ? ring.count() - 1 : ring.count() ); ++ i )
2498 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2509 for (
int i = 0; i < line->numPoints() - 1; ++i )
2513 << line->pointN( i )
2514 << line->pointN( i + 1 ) );
2525 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2535 if ( collection->numGeometries() == 1 )
2537 curvePolygon = qgsgeometry_cast< const QgsCurvePolygon * >( collection->geometryN( 0 ) );
2542 if ( !curvePolygon )
2546 qlonglong idx = QgsExpressionUtils::getIntValue( values.at( 1 ), parent ) - 1;
2552 QVariant result = curve ? QVariant::fromValue(
QgsGeometry( curve ) ) : QVariant();
2558 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2568 qlonglong idx = QgsExpressionUtils::getIntValue( values.at( 1 ), parent ) - 1;
2574 QVariant result = part ? QVariant::fromValue(
QgsGeometry( part ) ) : QVariant();
2580 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2589 return QVariant::fromValue(
QgsGeometry( boundary ) );
2594 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2603 return QVariant::fromValue( merged );
2608 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2613 double tolerance = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
2616 if ( simplified.
isNull() )
2624 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2629 double tolerance = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
2634 if ( simplified.
isNull() )
2642 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2647 int iterations = std::min( QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent ), 10 );
2648 double offset = qBound( 0.0, QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent ), 0.5 );
2649 double minLength = QgsExpressionUtils::getDoubleValue( values.at( 3 ), parent );
2650 double maxAngle = qBound( 0.0, QgsExpressionUtils::getDoubleValue( values.at( 4 ), parent ), 180.0 );
2652 QgsGeometry smoothed = geom.
smooth(
static_cast<unsigned int>( iterations ), offset, minLength, maxAngle );
2662 if ( values.size() == 1 && ( values.at( 0 ).type() == QVariant::List || values.at( 0 ).type() == QVariant::StringList ) )
2664 list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
2671 QVector< QgsGeometry > parts;
2672 parts.reserve( list.size() );
2673 for (
const QVariant &value : qgis::as_const( list ) )
2691 if ( values.count() < 2 || values.count() > 4 )
2693 parent->
setEvalErrorString( QObject::tr(
"Function make_point requires 2-4 arguments" ) );
2697 double x = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
2698 double y = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
2699 double z = values.count() >= 3 ? QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent ) : 0.0;
2700 double m = values.count() >= 4 ? QgsExpressionUtils::getDoubleValue( values.at( 3 ), parent ) : 0.0;
2701 switch ( values.count() )
2715 double x = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
2716 double y = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
2717 double m = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
2723 if ( values.empty() )
2728 QVector<QgsPoint> points;
2729 points.reserve( values.count() );
2731 auto addPoint = [&points](
const QgsGeometry & geom )
2739 const QgsPoint *point = qgsgeometry_cast< const QgsPoint * >( geom.
constGet() );
2746 for (
const QVariant &value : values )
2748 if ( value.type() == QVariant::List )
2750 const QVariantList list = value.toList();
2751 for (
const QVariant &v : list )
2753 addPoint( QgsExpressionUtils::getGeometry( v, parent ) );
2758 addPoint( QgsExpressionUtils::getGeometry( value, parent ) );
2762 if ( points.count() < 2 )
2770 if ( values.count() < 1 )
2772 parent->
setEvalErrorString( QObject::tr(
"Function make_polygon requires an argument" ) );
2776 QgsGeometry outerRing = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2780 std::unique_ptr< QgsPolygon > polygon = qgis::make_unique< QgsPolygon >();
2782 const QgsCurve *exteriorRing = qgsgeometry_cast< QgsCurve * >( outerRing.
constGet() );
2789 exteriorRing = qgsgeometry_cast< QgsCurve * >( collection->
geometryN( 0 ) );
2794 if ( !exteriorRing )
2797 polygon->setExteriorRing( exteriorRing->
segmentize() );
2800 for (
int i = 1; i < values.count(); ++i )
2802 QgsGeometry ringGeom = QgsExpressionUtils::getGeometry( values.at( i ), parent );
2809 const QgsCurve *ring = qgsgeometry_cast< QgsCurve * >( ringGeom.
constGet() );
2816 ring = qgsgeometry_cast< QgsCurve * >( collection->
geometryN( 0 ) );
2824 polygon->addInteriorRing( ring->
segmentize() );
2827 return QVariant::fromValue(
QgsGeometry( std::move( polygon ) ) );
2832 std::unique_ptr<QgsTriangle> tr(
new QgsTriangle() );
2833 std::unique_ptr<QgsLineString> lineString(
new QgsLineString() );
2834 lineString->clear();
2836 for (
const QVariant &value : values )
2838 QgsGeometry geom = QgsExpressionUtils::getGeometry( value, parent );
2845 const QgsPoint *point = qgsgeometry_cast< const QgsPoint * >( geom.
constGet() );
2852 point = qgsgeometry_cast< const QgsPoint * >( collection->
geometryN( 0 ) );
2860 lineString->addVertex( *point );
2863 tr->setExteriorRing( lineString.release() );
2865 return QVariant::fromValue(
QgsGeometry( tr.release() ) );
2870 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2877 double radius = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
2878 int segment = QgsExpressionUtils::getNativeIntValue( values.at( 2 ), parent );
2885 const QgsPoint *point = qgsgeometry_cast< const QgsPoint * >( geom.
constGet() );
2892 point = qgsgeometry_cast< const QgsPoint * >( collection->
geometryN( 0 ) );
2900 return QVariant::fromValue(
QgsGeometry( circ.toPolygon(
static_cast<unsigned int>( segment ) ) ) );
2905 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2912 double majorAxis = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
2913 double minorAxis = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
2914 double azimuth = QgsExpressionUtils::getDoubleValue( values.at( 3 ), parent );
2915 int segment = QgsExpressionUtils::getNativeIntValue( values.at( 4 ), parent );
2921 const QgsPoint *point = qgsgeometry_cast< const QgsPoint * >( geom.
constGet() );
2928 point = qgsgeometry_cast< const QgsPoint * >( collection->
geometryN( 0 ) );
2935 QgsEllipse elp( *point, majorAxis, minorAxis, azimuth );
2936 return QVariant::fromValue(
QgsGeometry( elp.toPolygon(
static_cast<unsigned int>( segment ) ) ) );
2942 QgsGeometry pt1 = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2949 QgsGeometry pt2 = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
2956 unsigned int nbEdges =
static_cast<unsigned int>( QgsExpressionUtils::getIntValue( values.at( 2 ), parent ) );
2959 parent->
setEvalErrorString( QObject::tr(
"Number of edges/sides must be greater than 2" ) );
2966 parent->
setEvalErrorString( QObject::tr(
"Option can be 0 (inscribed) or 1 (circumscribed)" ) );
2970 const QgsPoint *center = qgsgeometry_cast< const QgsPoint * >( pt1.
constGet() );
2977 center = qgsgeometry_cast< const QgsPoint * >( collection->
geometryN( 0 ) );
2984 const QgsPoint *corner = qgsgeometry_cast< const QgsPoint * >( pt2.
constGet() );
2991 corner = qgsgeometry_cast< const QgsPoint * >( collection->
geometryN( 0 ) );
3006 QgsGeometry pt1 = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3012 QgsGeometry pt2 = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
3018 const QgsPoint *point1 = qgsgeometry_cast< const QgsPoint *>( pt1.
constGet() );
3019 const QgsPoint *point2 = qgsgeometry_cast< const QgsPoint *>( pt2.
constGet() );
3027 QgsGeometry pt1 = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3033 QgsGeometry pt2 = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
3039 QgsGeometry pt3 = QgsExpressionUtils::getGeometry( values.at( 2 ), parent );
3048 parent->
setEvalErrorString( QObject::tr(
"Option can be 0 (distance) or 1 (projected)" ) );
3051 const QgsPoint *point1 = qgsgeometry_cast< const QgsPoint *>( pt1.
constGet() );
3052 const QgsPoint *point2 = qgsgeometry_cast< const QgsPoint *>( pt2.
constGet() );
3053 const QgsPoint *point3 = qgsgeometry_cast< const QgsPoint *>( pt3.
constGet() );
3061 int idx = QgsExpressionUtils::getNativeIntValue( values.at( 0 ), parent );
3077 return QVariant( QPointF( p.
x(), p.
y() ) );
3082 QVariant v = pointAt( values, f, parent );
3083 if ( v.type() == QVariant::PointF )
3084 return QVariant( v.toPointF().x() );
3090 QVariant v = pointAt( values, f, parent );
3091 if ( v.type() == QVariant::PointF )
3092 return QVariant( v.toPointF().y() );
3101 return QVariant::fromValue( geom );
3103 return QVariant( QVariant::UserType );
3108 QString wkt = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
3110 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3116 const QByteArray wkb = QgsExpressionUtils::getBinaryValue( values.at( 0 ), parent );
3122 return !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3127 QString gml = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
3134 ogcContext.
layer = mapLayerPtr.data();
3135 ogcContext.
transformContext = context->
variable( QStringLiteral(
"_project_transform_context" ) ).value<QgsCoordinateTransformContext>();
3139 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3152 return QVariant( area );
3156 return QVariant( f.geometry().area() );
3162 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3167 return QVariant( geom.
area() );
3179 return QVariant( len );
3183 return QVariant( f.geometry().length() );
3196 return QVariant( len );
3200 return f.geometry().isNull() ? QVariant( 0 ) : QVariant( f.geometry().constGet()->perimeter() );
3206 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3212 return QVariant( geom.
length() );
3217 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3223 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3232 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3241 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3256 curvePolygon = qgsgeometry_cast< const QgsCurvePolygon *>( collection->
geometryN( i ) );
3257 if ( !curvePolygon )
3269 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3276 return QVariant( curvePolygon->
ringCount() );
3278 bool foundPoly =
false;
3286 curvePolygon = qgsgeometry_cast< QgsCurvePolygon *>( collection->
geometryN( i ) );
3287 if ( !curvePolygon )
3298 return QVariant( ringCount );
3303 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3305 QVariant result = !geomBounds.
isNull() ? QVariant::fromValue( geomBounds ) : QVariant();
3311 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3317 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3323 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3329 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3335 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3341 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3347 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3355 double max = std::numeric_limits< double >::lowest();
3359 double z = ( *it ).z();
3365 if ( max == std::numeric_limits< double >::lowest() )
3366 return QVariant( QVariant::Double );
3368 return QVariant( max );
3373 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3381 double min = std::numeric_limits< double >::max();
3385 double z = ( *it ).z();
3391 if ( min == std::numeric_limits< double >::max() )
3392 return QVariant( QVariant::Double );
3394 return QVariant( min );
3399 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3407 double min = std::numeric_limits< double >::max();
3411 double m = ( *it ).m();
3417 if ( min == std::numeric_limits< double >::max() )
3418 return QVariant( QVariant::Double );
3420 return QVariant( min );
3425 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3433 double max = std::numeric_limits< double >::lowest();
3437 double m = ( *it ).m();
3443 if ( max == std::numeric_limits< double >::lowest() )
3444 return QVariant( QVariant::Double );
3446 return QVariant( max );
3451 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3455 std::unique_ptr< QgsAbstractGeometry > flipped( geom.
constGet()->
clone() );
3457 return QVariant::fromValue(
QgsGeometry( std::move( flipped ) ) );
3462 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3466 const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( fGeom.
constGet() );
3473 curve = qgsgeometry_cast< const QgsCurve * >( collection->
geometryN( 0 ) );
3481 return QVariant::fromValue( curve->
isClosed() );
3486 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3499 std::unique_ptr< QgsLineString > closedLine( line->
clone() );
3500 closedLine->close();
3502 result = QVariant::fromValue(
QgsGeometry( std::move( closedLine ) ) );
3512 if (
const QgsLineString *line = qgsgeometry_cast<const QgsLineString * >( collection->
geometryN( i ) ) )
3514 std::unique_ptr< QgsLineString > closedLine( line->
clone() );
3515 closedLine->close();
3517 closed->addGeometry( closedLine.release() );
3520 result = QVariant::fromValue(
QgsGeometry( std::move( closed ) ) );
3528 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3532 return QVariant::fromValue( fGeom.
isEmpty() );
3537 if ( values.at( 0 ).isNull() )
3538 return QVariant::fromValue(
true );
3540 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3541 return QVariant::fromValue( fGeom.
isNull() || fGeom.
isEmpty() );
3546 if ( values.length() < 2 || values.length() > 3 )
3549 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3550 QgsGeometry sGeom = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
3557 if ( values.length() == 2 )
3560 QString result = engine->relate( sGeom.
constGet() );
3561 return QVariant::fromValue( result );
3566 QString pattern = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
3567 bool result = engine->relatePattern( sGeom.
constGet(), pattern );
3568 return QVariant::fromValue( result );
3574 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3575 QgsGeometry sGeom = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
3580 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3581 QgsGeometry sGeom = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
3582 return fGeom.
disjoint( sGeom ) ? TVL_True : TVL_False;
3586 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3587 QgsGeometry sGeom = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
3588 return fGeom.
intersects( sGeom ) ? TVL_True : TVL_False;
3592 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3593 QgsGeometry sGeom = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
3594 return fGeom.
touches( sGeom ) ? TVL_True : TVL_False;
3598 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3599 QgsGeometry sGeom = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
3600 return fGeom.
crosses( sGeom ) ? TVL_True : TVL_False;
3604 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3605 QgsGeometry sGeom = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
3606 return fGeom.
contains( sGeom ) ? TVL_True : TVL_False;
3610 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3611 QgsGeometry sGeom = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
3612 return fGeom.
overlaps( sGeom ) ? TVL_True : TVL_False;
3616 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3617 QgsGeometry sGeom = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
3618 return fGeom.
within( sGeom ) ? TVL_True : TVL_False;
3622 if ( values.length() < 2 || values.length() > 3 )
3625 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3626 double dist = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
3628 if ( values.length() == 3 )
3629 seg = QgsExpressionUtils::getNativeIntValue( values.at( 2 ), parent );
3632 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3638 const QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3640 return !reoriented.
isNull() ? QVariant::fromValue( reoriented ) : QVariant();
3645 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3646 const QgsPoint *pt = qgsgeometry_cast<const QgsPoint *>( fGeom.
constGet() );
3653 pt = qgsgeometry_cast< const QgsPoint * >( collection->
geometryN( 0 ) );
3660 parent->
setEvalErrorString( QObject::tr(
"Function `wedge_buffer` requires a point value for the center." ) );
3664 double azimuth = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
3665 double width = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
3666 double outerRadius = QgsExpressionUtils::getDoubleValue( values.at( 3 ), parent );
3667 double innerRadius = QgsExpressionUtils::getDoubleValue( values.at( 4 ), parent );
3670 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3676 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3679 parent->
setEvalErrorString( QObject::tr(
"Function `tapered_buffer` requires a line geometry." ) );
3683 double startWidth = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
3684 double endWidth = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
3685 int segments =
static_cast< int >( QgsExpressionUtils::getIntValue( values.at( 3 ), parent ) );
3688 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3694 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3697 parent->
setEvalErrorString( QObject::tr(
"Function `buffer_by_m` requires a line geometry." ) );
3701 int segments =
static_cast< int >( QgsExpressionUtils::getIntValue( values.at( 1 ), parent ) );
3704 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3710 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3711 double dist = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
3712 int segments = QgsExpressionUtils::getNativeIntValue( values.at( 2 ), parent );
3716 double miterLimit = QgsExpressionUtils::getDoubleValue( values.at( 3 ), parent );
3719 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3725 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3726 double dist = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
3727 int segments = QgsExpressionUtils::getNativeIntValue( values.at( 2 ), parent );
3731 double miterLimit = QgsExpressionUtils::getDoubleValue( values.at( 3 ), parent );
3734 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3740 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3741 double distStart = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
3742 double distEnd = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
3745 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3751 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3752 double dx = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
3753 double dy = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
3755 return QVariant::fromValue( fGeom );
3760 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3761 const double rotation = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
3762 const QgsGeometry center = values.at( 2 ).
isValid() ? QgsExpressionUtils::getGeometry( values.at( 2 ), parent )
3773 parent->
setEvalErrorString( QObject::tr(
"Function 'rotate' requires a point value for the center" ) );
3779 if ( multiPoint.count() == 1 )
3785 parent->
setEvalErrorString( QObject::tr(
"Function 'rotate' requires a point value for the center" ) );
3794 fGeom.
rotate( rotation, pt );
3795 return QVariant::fromValue( fGeom );
3800 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3802 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3807 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3809 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3815 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3816 double tolerance = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
3818 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3824 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3826 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3833 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3835 if ( values.length() == 2 )
3836 segments = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
3844 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3850 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3852 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3858 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3859 QgsGeometry sGeom = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
3861 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3867 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3874 const QgsCurve *curve = qgsgeometry_cast<const QgsCurve * >( fGeom.
constGet() );
3879 result = reversed ? QVariant::fromValue(
QgsGeometry( reversed ) ) : QVariant();
3887 if (
const QgsCurve *curve = qgsgeometry_cast<const QgsCurve * >( collection->
geometryN( i ) ) )
3889 reversed->addGeometry( curve->
reversed() );
3896 result = reversed ? QVariant::fromValue(
QgsGeometry( std::move( reversed ) ) ) : QVariant();
3903 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3914 curvePolygon = qgsgeometry_cast< const QgsCurvePolygon * >( collection->
geometryN( 0 ) );
3923 QVariant result = exterior ? QVariant::fromValue(
QgsGeometry( exterior ) ) : QVariant();
3929 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3930 QgsGeometry sGeom = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
3931 return QVariant( fGeom.
distance( sGeom ) );
3936 QgsGeometry g1 = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3937 QgsGeometry g2 = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
3940 if ( values.length() == 3 && values.at( 2 ).isValid() )
3942 double densify = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
3943 densify = qBound( 0.0, densify, 1.0 );
3951 return res > -1 ? QVariant( res ) : QVariant();
3956 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3957 QgsGeometry sGeom = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
3959 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3964 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3965 QgsGeometry sGeom = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
3967 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3972 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3973 QgsGeometry sGeom = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
3975 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
3981 if ( values.length() < 1 || values.length() > 2 )
3984 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3986 if ( values.length() == 2 )
3987 prec = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
3988 QString wkt = fGeom.
asWkt( prec );
3989 return QVariant( wkt );
3994 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3995 return fGeom.
isNull() ? QVariant() : QVariant( fGeom.asWkb() );
4000 if ( values.length() != 2 )
4002 parent->
setEvalErrorString( QObject::tr(
"Function `azimuth` requires exactly two parameters. %1 given." ).arg( values.length() ) );
4006 QgsGeometry fGeom1 = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
4007 QgsGeometry fGeom2 = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
4009 const QgsPoint *pt1 = qgsgeometry_cast<const QgsPoint *>( fGeom1.
constGet() );
4016 pt1 = qgsgeometry_cast< const QgsPoint * >( collection->
geometryN( 0 ) );
4021 const QgsPoint *pt2 = qgsgeometry_cast<const QgsPoint *>( fGeom2.
constGet() );
4028 pt2 = qgsgeometry_cast< const QgsPoint * >( collection->
geometryN( 0 ) );
4035 parent->
setEvalErrorString( QObject::tr(
"Function `azimuth` requires two points as arguments." ) );
4042 if ( pt1->
y() < pt2->
y() )
4044 else if ( pt1->
y() > pt2->
y() )
4052 if ( pt1->
x() < pt2->
x() )
4054 else if ( pt1->
x() > pt2->
x() )
4055 return M_PI + ( M_PI_2 );
4060 if ( pt1->
x() < pt2->
x() )
4062 if ( pt1->
y() < pt2->
y() )
4064 return std::atan( std::fabs( pt1->
x() - pt2->
x() ) / std::fabs( pt1->
y() - pt2->
y() ) );
4068 return std::atan( std::fabs( pt1->
y() - pt2->
y() ) / std::fabs( pt1->
x() - pt2->
x() ) )
4075 if ( pt1->
y() > pt2->
y() )
4077 return std::atan( std::fabs( pt1->
x() - pt2->
x() ) / std::fabs( pt1->
y() - pt2->
y() ) )
4082 return std::atan( std::fabs( pt1->
y() - pt2->
y() ) / std::fabs( pt1->
x() - pt2->
x() ) )
4083 + ( M_PI + ( M_PI_2 ) );
4090 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
4094 parent->
setEvalErrorString( QStringLiteral(
"'project' requires a point geometry" ) );
4098 double distance = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
4099 double azimuth = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
4100 double inclination = QgsExpressionUtils::getDoubleValue( values.at( 3 ), parent );
4103 QgsPoint newPoint = p->
project( distance, 180.0 * azimuth / M_PI, 180.0 * inclination / M_PI );
4110 QgsGeometry fGeom1 = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
4111 QgsGeometry fGeom2 = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
4113 const QgsPoint *pt1 = qgsgeometry_cast<const QgsPoint *>( fGeom1.
constGet() );
4120 pt1 = qgsgeometry_cast< const QgsPoint * >( collection->
geometryN( 0 ) );
4124 const QgsPoint *pt2 = qgsgeometry_cast<const QgsPoint *>( fGeom2.
constGet() );
4131 pt2 = qgsgeometry_cast< const QgsPoint * >( collection->
geometryN( 0 ) );
4139 parent->
setEvalErrorString( QStringLiteral(
"Function 'inclination' requires two points as arguments." ) );
4149 if ( values.length() != 3 )
4152 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
4153 double x = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
4154 double y = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
4158 QVariant result = geom.
constGet() ? QVariant::fromValue( geom ) : QVariant();
4164 if ( values.length() < 2 )
4167 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
4170 return values.at( 0 );
4172 QString expString = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
4173 QVariant cachedExpression;
4178 if ( cachedExpression.isValid() )
4185 bool asc = values.value( 2 ).toBool();
4203 Q_ASSERT( collection );
4207 QgsExpressionSorter sorter( orderBy );
4209 QList<QgsFeature> partFeatures;
4210 partFeatures.reserve( collection->
partCount() );
4211 for (
int i = 0; i < collection->
partCount(); ++i )
4217 sorter.sortFeatures( partFeatures, unconstedContext );
4221 Q_ASSERT( orderedGeom );
4226 for (
const QgsFeature &feature : qgis::as_const( partFeatures ) )
4231 QVariant result = QVariant::fromValue(
QgsGeometry( orderedGeom ) );
4234 delete unconstedContext;
4241 QgsGeometry fromGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
4242 QgsGeometry toGeom = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
4246 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
4252 QgsGeometry fromGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
4253 QgsGeometry toGeom = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
4257 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
4263 QgsGeometry lineGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
4264 double distance = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
4268 QVariant result = !geom.
isNull() ? QVariant::fromValue( geom ) : QVariant();
4274 QgsGeometry lineGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
4277 parent->
setEvalErrorString( QObject::tr(
"line_substring requires a curve geometry input" ) );
4283 curve = qgsgeometry_cast< const QgsCurve * >( lineGeom.
constGet() );
4290 curve = qgsgeometry_cast< const QgsCurve * >( collection->
geometryN( 0 ) );
4297 double startDistance = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
4298 double endDistance = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
4300 std::unique_ptr< QgsCurve > substring( curve->
curveSubstring( startDistance, endDistance ) );
4302 return !result.isNull() ? QVariant::fromValue( result ) : QVariant();
4307 QgsGeometry lineGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
4308 double distance = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
4315 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
4316 int vertex = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
4321 vertex = count + vertex;
4329 QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
4330 int vertex = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
4335 vertex = count + vertex;
4343 QgsGeometry lineGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
4344 QgsGeometry pointGeom = QgsExpressionUtils::getGeometry( values.at( 1 ), parent );
4348 return distance >= 0 ? distance : QVariant();
4353 if ( values.length() == 2 && values.at( 1 ).toInt() != 0 )
4355 double number = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
4356 return qgsRound( number, QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent ) );
4359 if ( values.length() >= 1 )
4361 double number = QgsExpressionUtils::getIntValue( values.at( 0 ), parent );
4362 return QVariant( qlonglong( std::round( number ) ) );
4377 const double value = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
4378 const int places = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
4379 const QString language = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
4386 QLocale locale = !language.isEmpty() ? QLocale( language ) : QLocale();
4387 locale.setNumberOptions( locale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator );
4388 return locale.toString( value,
'f', places );
4393 const QDateTime datetime = QgsExpressionUtils::getDateTimeValue( values.at( 0 ), parent );
4394 const QString format = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
4395 const QString language = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
4397 QLocale locale = !language.isEmpty() ? QLocale( language ) : QLocale();
4398 return locale.toString( datetime, format );
4404 int avg = ( color.red() + color.green() + color.blue() ) / 3;
4405 int alpha = color.alpha();
4407 color.setRgb( avg, avg, avg, alpha );
4416 double ratio = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
4421 else if ( ratio < 0 )
4426 int red =
static_cast<int>( color1.red() * ( 1 - ratio ) + color2.red() * ratio );
4427 int green =
static_cast<int>( color1.green() * ( 1 - ratio ) + color2.green() * ratio );
4428 int blue =
static_cast<int>( color1.blue() * ( 1 - ratio ) + color2.blue() * ratio );
4429 int alpha =
static_cast<int>( color1.alpha() * ( 1 - ratio ) + color2.alpha() * ratio );
4431 QColor newColor( red, green, blue, alpha );
4438 int red = QgsExpressionUtils::getNativeIntValue( values.at( 0 ), parent );
4439 int green = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
4440 int blue = QgsExpressionUtils::getNativeIntValue( values.at( 2 ), parent );
4441 QColor color = QColor( red, green, blue );
4442 if ( ! color.isValid() )
4444 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1:%2:%3' to color" ).arg( red ).arg( green ).arg( blue ) );
4445 color = QColor( 0, 0, 0 );
4448 return QStringLiteral(
"%1,%2,%3" ).arg( color.red() ).arg( color.green() ).arg( color.blue() );
4453 QgsExpressionNode *node = QgsExpressionUtils::getNode( values.at( 0 ), parent );
4454 QVariant value = node->
eval( parent, context );
4458 node = QgsExpressionUtils::getNode( values.at( 1 ), parent );
4460 value = node->
eval( parent, context );
4468 QgsExpressionNode *node = QgsExpressionUtils::getNode( values.at( 0 ), parent );
4470 QVariant value = node->
eval( parent, context );
4472 if ( value.toBool() )
4474 node = QgsExpressionUtils::getNode( values.at( 1 ), parent );
4476 value = node->
eval( parent, context );
4481 node = QgsExpressionUtils::getNode( values.at( 2 ), parent );
4483 value = node->
eval( parent, context );
4491 int red = QgsExpressionUtils::getNativeIntValue( values.at( 0 ), parent );
4492 int green = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
4493 int blue = QgsExpressionUtils::getNativeIntValue( values.at( 2 ), parent );
4494 int alpha = QgsExpressionUtils::getNativeIntValue( values.at( 3 ), parent );
4495 QColor color = QColor( red, green, blue, alpha );
4496 if ( ! color.isValid() )
4498 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1:%2:%3:%4' to color" ).arg( red ).arg( green ).arg( blue ).arg( alpha ) );
4499 color = QColor( 0, 0, 0 );
4510 expRamp = QgsExpressionUtils::getRamp( values.at( 0 ), parent );
4515 QString rampName = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
4519 parent->
setEvalErrorString( QObject::tr(
"\"%1\" is not a valid color ramp" ).arg( rampName ) );
4524 double value = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
4525 QColor color = ramp->
color( value );
4532 double hue = QgsExpressionUtils::getIntValue( values.at( 0 ), parent ) / 360.0;
4534 double saturation = QgsExpressionUtils::getIntValue( values.at( 1 ), parent ) / 100.0;
4536 double lightness = QgsExpressionUtils::getIntValue( values.at( 2 ), parent ) / 100.0;
4538 QColor color = QColor::fromHslF( hue, saturation, lightness );
4540 if ( ! color.isValid() )
4542 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1:%2:%3' to color" ).arg( hue ).arg( saturation ).arg( lightness ) );
4543 color = QColor( 0, 0, 0 );
4546 return QStringLiteral(
"%1,%2,%3" ).arg( color.red() ).arg( color.green() ).arg( color.blue() );
4552 double hue = QgsExpressionUtils::getIntValue( values.at( 0 ), parent ) / 360.0;
4554 double saturation = QgsExpressionUtils::getIntValue( values.at( 1 ), parent ) / 100.0;
4556 double lightness = QgsExpressionUtils::getIntValue( values.at( 2 ), parent ) / 100.0;
4558 double alpha = QgsExpressionUtils::getIntValue( values.at( 3 ), parent ) / 255.0;
4560 QColor color = QColor::fromHslF( hue, saturation, lightness, alpha );
4561 if ( ! color.isValid() )
4563 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1:%2:%3:%4' to color" ).arg( hue ).arg( saturation ).arg( lightness ).arg( alpha ) );
4564 color = QColor( 0, 0, 0 );
4572 double hue = QgsExpressionUtils::getIntValue( values.at( 0 ), parent ) / 360.0;
4574 double saturation = QgsExpressionUtils::getIntValue( values.at( 1 ), parent ) / 100.0;
4576 double value = QgsExpressionUtils::getIntValue( values.at( 2 ), parent ) / 100.0;
4578 QColor color = QColor::fromHsvF( hue, saturation, value );
4580 if ( ! color.isValid() )
4582 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1:%2:%3' to color" ).arg( hue ).arg( saturation ).arg( value ) );
4583 color = QColor( 0, 0, 0 );
4586 return QStringLiteral(
"%1,%2,%3" ).arg( color.red() ).arg( color.green() ).arg( color.blue() );
4592 double hue = QgsExpressionUtils::getIntValue( values.at( 0 ), parent ) / 360.0;
4594 double saturation = QgsExpressionUtils::getIntValue( values.at( 1 ), parent ) / 100.0;
4596 double value = QgsExpressionUtils::getIntValue( values.at( 2 ), parent ) / 100.0;
4598 double alpha = QgsExpressionUtils::getIntValue( values.at( 3 ), parent ) / 255.0;
4600 QColor color = QColor::fromHsvF( hue, saturation, value, alpha );
4601 if ( ! color.isValid() )
4603 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1:%2:%3:%4' to color" ).arg( hue ).arg( saturation ).arg( value ).arg( alpha ) );
4604 color = QColor( 0, 0, 0 );
4612 double cyan = QgsExpressionUtils::getIntValue( values.at( 0 ), parent ) / 100.0;
4614 double magenta = QgsExpressionUtils::getIntValue( values.at( 1 ), parent ) / 100.0;
4616 double yellow = QgsExpressionUtils::getIntValue( values.at( 2 ), parent ) / 100.0;
4618 double black = QgsExpressionUtils::getIntValue( values.at( 3 ), parent ) / 100.0;
4620 QColor color = QColor::fromCmykF( cyan, magenta, yellow, black );
4622 if ( ! color.isValid() )
4624 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1:%2:%3:%4' to color" ).arg( cyan ).arg( magenta ).arg( yellow ).arg( black ) );
4625 color = QColor( 0, 0, 0 );
4628 return QStringLiteral(
"%1,%2,%3" ).arg( color.red() ).arg( color.green() ).arg( color.blue() );
4634 double cyan = QgsExpressionUtils::getIntValue( values.at( 0 ), parent ) / 100.0;
4636 double magenta = QgsExpressionUtils::getIntValue( values.at( 1 ), parent ) / 100.0;
4638 double yellow = QgsExpressionUtils::getIntValue( values.at( 2 ), parent ) / 100.0;
4640 double black = QgsExpressionUtils::getIntValue( values.at( 3 ), parent ) / 100.0;
4642 double alpha = QgsExpressionUtils::getIntValue( values.at( 4 ), parent ) / 255.0;
4644 QColor color = QColor::fromCmykF( cyan, magenta, yellow, black, alpha );
4645 if ( ! color.isValid() )
4647 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1:%2:%3:%4:%5' to color" ).arg( cyan ).arg( magenta ).arg( yellow ).arg( black ).arg( alpha ) );
4648 color = QColor( 0, 0, 0 );
4656 if ( ! color.isValid() )
4658 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to color" ).arg( values.at( 0 ).toString() ) );
4662 QString part = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
4663 if ( part.compare( QLatin1String(
"red" ), Qt::CaseInsensitive ) == 0 )
4665 else if ( part.compare( QLatin1String(
"green" ), Qt::CaseInsensitive ) == 0 )
4666 return color.green();
4667 else if ( part.compare( QLatin1String(
"blue" ), Qt::CaseInsensitive ) == 0 )
4668 return color.blue();
4669 else if ( part.compare( QLatin1String(
"alpha" ), Qt::CaseInsensitive ) == 0 )
4670 return color.alpha();
4671 else if ( part.compare( QLatin1String(
"hue" ), Qt::CaseInsensitive ) == 0 )
4672 return color.hsvHueF() * 360;
4673 else if ( part.compare( QLatin1String(
"saturation" ), Qt::CaseInsensitive ) == 0 )
4674 return color.hsvSaturationF() * 100;
4675 else if ( part.compare( QLatin1String(
"value" ), Qt::CaseInsensitive ) == 0 )
4676 return color.valueF() * 100;
4677 else if ( part.compare( QLatin1String(
"hsl_hue" ), Qt::CaseInsensitive ) == 0 )
4678 return color.hslHueF() * 360;
4679 else if ( part.compare( QLatin1String(
"hsl_saturation" ), Qt::CaseInsensitive ) == 0 )
4680 return color.hslSaturationF() * 100;
4681 else if ( part.compare( QLatin1String(
"lightness" ), Qt::CaseInsensitive ) == 0 )
4682 return color.lightnessF() * 100;
4683 else if ( part.compare( QLatin1String(
"cyan" ), Qt::CaseInsensitive ) == 0 )
4684 return color.cyanF() * 100;
4685 else if ( part.compare( QLatin1String(
"magenta" ), Qt::CaseInsensitive ) == 0 )
4686 return color.magentaF() * 100;
4687 else if ( part.compare( QLatin1String(
"yellow" ), Qt::CaseInsensitive ) == 0 )
4688 return color.yellowF() * 100;
4689 else if ( part.compare( QLatin1String(
"black" ), Qt::CaseInsensitive ) == 0 )
4690 return color.blackF() * 100;
4692 parent->
setEvalErrorString( QObject::tr(
"Unknown color component '%1'" ).arg( part ) );
4698 const QVariantMap map = QgsExpressionUtils::getMapValue( values.at( 0 ), parent );
4699 if ( map.count() < 1 )
4701 parent->
setEvalErrorString( QObject::tr(
"A minimum of two colors is required to create a ramp" ) );
4705 QList< QColor > colors;
4707 for ( QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it )
4710 if ( !colors.last().isValid() )
4712 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to color" ).arg( it.value().toString() ) );
4716 double step = it.key().toDouble();
4717 if ( it == map.constBegin() )
4722 else if ( it == map.constEnd() )
4732 bool discrete = values.at( 1 ).toBool();
4734 return QVariant::fromValue(
QgsGradientColorRamp( colors.first(), colors.last(), discrete, stops ) );
4740 if ( ! color.isValid() )
4742 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to color" ).arg( values.at( 0 ).toString() ) );
4746 QString part = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
4747 int value = QgsExpressionUtils::getNativeIntValue( values.at( 2 ), parent );
4748 if ( part.compare( QLatin1String(
"red" ), Qt::CaseInsensitive ) == 0 )
4749 color.setRed( value );
4750 else if ( part.compare( QLatin1String(
"green" ), Qt::CaseInsensitive ) == 0 )
4751 color.setGreen( value );
4752 else if ( part.compare( QLatin1String(
"blue" ), Qt::CaseInsensitive ) == 0 )
4753 color.setBlue( value );
4754 else if ( part.compare( QLatin1String(
"alpha" ), Qt::CaseInsensitive ) == 0 )
4755 color.setAlpha( value );
4756 else if ( part.compare( QLatin1String(
"hue" ), Qt::CaseInsensitive ) == 0 )
4757 color.setHsv( value, color.hsvSaturation(), color.value(), color.alpha() );
4758 else if ( part.compare( QLatin1String(
"saturation" ), Qt::CaseInsensitive ) == 0 )
4759 color.setHsvF( color.hsvHueF(), value / 100.0, color.valueF(), color.alphaF() );
4760 else if ( part.compare( QLatin1String(
"value" ), Qt::CaseInsensitive ) == 0 )
4761 color.setHsvF( color.hsvHueF(), color.hsvSaturationF(), value / 100.0, color.alphaF() );
4762 else if ( part.compare( QLatin1String(
"hsl_hue" ), Qt::CaseInsensitive ) == 0 )
4763 color.setHsl( value, color.hslSaturation(), color.lightness(), color.alpha() );
4764 else if ( part.compare( QLatin1String(
"hsl_saturation" ), Qt::CaseInsensitive ) == 0 )
4765 color.setHslF( color.hslHueF(), value / 100.0, color.lightnessF(), color.alphaF() );
4766 else if ( part.compare( QLatin1String(
"lightness" ), Qt::CaseInsensitive ) == 0 )
4767 color.setHslF( color.hslHueF(), color.hslSaturationF(), value / 100.0, color.alphaF() );
4768 else if ( part.compare( QLatin1String(
"cyan" ), Qt::CaseInsensitive ) == 0 )
4769 color.setCmykF( value / 100.0, color.magentaF(), color.yellowF(), color.blackF(), color.alphaF() );
4770 else if ( part.compare( QLatin1String(
"magenta" ), Qt::CaseInsensitive ) == 0 )
4771 color.setCmykF( color.cyanF(), value / 100.0, color.yellowF(), color.blackF(), color.alphaF() );
4772 else if ( part.compare( QLatin1String(
"yellow" ), Qt::CaseInsensitive ) == 0 )
4773 color.setCmykF( color.cyanF(), color.magentaF(), value / 100.0, color.blackF(), color.alphaF() );
4774 else if ( part.compare( QLatin1String(
"black" ), Qt::CaseInsensitive ) == 0 )
4775 color.setCmykF( color.cyanF(), color.magentaF(), color.yellowF(), value / 100.0, color.alphaF() );
4778 parent->
setEvalErrorString( QObject::tr(
"Unknown color component '%1'" ).arg( part ) );
4787 if ( ! color.isValid() )
4789 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to color" ).arg( values.at( 0 ).toString() ) );
4793 color = color.darker( QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent ) );
4801 if ( ! color.isValid() )
4803 parent->
setEvalErrorString( QObject::tr(
"Cannot convert '%1' to color" ).arg( values.at( 0 ).toString() ) );
4807 color = color.lighter( QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent ) );
4814 QgsFeature feat = QgsExpressionUtils::getFeature( values.at( 0 ), parent );
4817 return QVariant::fromValue( geom );
4823 QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
4824 QString sAuthId = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
4825 QString dAuthId = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
4829 return QVariant::fromValue( fGeom );
4832 return QVariant::fromValue( fGeom );
4841 return QVariant::fromValue( fGeom );
4855 QgsVectorLayer *vl = QgsExpressionUtils::getVectorLayer( values.at( 0 ), parent );
4858 QgsFeatureId fid = QgsExpressionUtils::getIntValue( values.at( 1 ), parent );
4868 result = QVariant::fromValue( fet );
4878 std::unique_ptr<QgsVectorLayerFeatureSource> featureSource = QgsExpressionUtils::getFeatureSource( values.at( 0 ), parent );
4881 if ( !featureSource )
4886 QString attribute = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
4887 int attributeId = featureSource->fields().lookupField( attribute );
4888 if ( attributeId == -1 )
4893 const QVariant &attVal = values.at( 2 );
4895 const QString cacheValueKey = QStringLiteral(
"getfeature:%1:%2:%3" ).arg( featureSource->id(), QString::number( attributeId ), attVal.toString() );
4917 res = QVariant::fromValue( fet );
4932 if ( !values.isEmpty() )
4935 if ( col && ( values.size() == 1 || !values.at( 1 ).isValid() ) )
4936 fieldName = col->
name();
4937 else if ( values.size() == 2 )
4938 fieldName = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
4941 QVariant value = values.at( 0 );
4946 if ( fieldIndex == -1 )
4948 parent->
setEvalErrorString( QCoreApplication::translate(
"expression",
"%1: Field not found %2" ).arg( QStringLiteral(
"represent_value" ), fieldName ) );
4952 QgsVectorLayer *layer = QgsExpressionUtils::getVectorLayer( context->
variable( QStringLiteral(
"layer" ) ), parent );
4954 const QString cacheValueKey = QStringLiteral(
"repvalfcnval:%1:%2:%3" ).arg( layer ? layer->
id() : QStringLiteral(
"[None]" ), fieldName, value.toString() );
4963 const QString cacheKey = QStringLiteral(
"repvalfcn:%1:%2" ).arg( layer ? layer->
id() : QStringLiteral(
"[None]" ), fieldName );
4974 result =
formatter->representValue( layer, fieldIndex, setup.
config(), cache, value );
4981 parent->
setEvalErrorString( QCoreApplication::translate(
"expression",
"%1: function cannot be evaluated without a context." ).arg( QStringLiteral(
"represent_value" ), fieldName ) );
4989 QgsMapLayer *layer = QgsExpressionUtils::getMapLayer( values.at( 0 ), parent );
4995 QString layerProperty = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
4996 if ( QString::compare( layerProperty, QStringLiteral(
"name" ), Qt::CaseInsensitive ) == 0 )
4997 return layer->
name();
4998 else if ( QString::compare( layerProperty, QStringLiteral(
"id" ), Qt::CaseInsensitive ) == 0 )
5000 else if ( QString::compare( layerProperty, QStringLiteral(
"title" ), Qt::CaseInsensitive ) == 0 )
5002 else if ( QString::compare( layerProperty, QStringLiteral(
"abstract" ), Qt::CaseInsensitive ) == 0 )
5004 else if ( QString::compare( layerProperty, QStringLiteral(
"keywords" ), Qt::CaseInsensitive ) == 0 )
5006 QStringList keywords;
5008 for (
auto it = keywordMap.constBegin(); it != keywordMap.constEnd(); ++it )
5010 keywords.append( it.value() );
5012 if ( !keywords.isEmpty() )
5016 else if ( QString::compare( layerProperty, QStringLiteral(
"data_url" ), Qt::CaseInsensitive ) == 0 )
5018 else if ( QString::compare( layerProperty, QStringLiteral(
"attribution" ), Qt::CaseInsensitive ) == 0 )
5022 else if ( QString::compare( layerProperty, QStringLiteral(
"attribution_url" ), Qt::CaseInsensitive ) == 0 )
5024 else if ( QString::compare( layerProperty, QStringLiteral(
"source" ), Qt::CaseInsensitive ) == 0 )
5026 else if ( QString::compare( layerProperty, QStringLiteral(
"min_scale" ), Qt::CaseInsensitive ) == 0 )
5028 else if ( QString::compare( layerProperty, QStringLiteral(
"max_scale" ), Qt::CaseInsensitive ) == 0 )
5030 else if ( QString::compare( layerProperty, QStringLiteral(
"is_editable" ), Qt::CaseInsensitive ) == 0 )
5032 else if ( QString::compare( layerProperty, QStringLiteral(
"crs" ), Qt::CaseInsensitive ) == 0 )
5034 else if ( QString::compare( layerProperty, QStringLiteral(
"crs_definition" ), Qt::CaseInsensitive ) == 0 )
5036 else if ( QString::compare( layerProperty, QStringLiteral(
"crs_description" ), Qt::CaseInsensitive ) == 0 )
5038 else if ( QString::compare( layerProperty, QStringLiteral(
"extent" ), Qt::CaseInsensitive ) == 0 )
5041 QVariant result = QVariant::fromValue( extentGeom );
5044 else if ( QString::compare( layerProperty, QStringLiteral(
"distance_units" ), Qt::CaseInsensitive ) == 0 )
5046 else if ( QString::compare( layerProperty, QStringLiteral(
"type" ), Qt::CaseInsensitive ) == 0 )
5048 switch ( layer->
type() )
5051 return QCoreApplication::translate(
"expressions",
"Vector" );
5053 return QCoreApplication::translate(
"expressions",
"Raster" );
5055 return QCoreApplication::translate(
"expressions",
"Mesh" );
5057 return QCoreApplication::translate(
"expressions",
"Vector Tile" );
5059 return QCoreApplication::translate(
"expressions",
"Plugin" );
5065 QgsVectorLayer *vLayer = qobject_cast< QgsVectorLayer * >( layer );
5068 if ( QString::compare( layerProperty, QStringLiteral(
"storage_type" ), Qt::CaseInsensitive ) == 0 )
5070 else if ( QString::compare( layerProperty, QStringLiteral(
"geometry_type" ), Qt::CaseInsensitive ) == 0 )
5072 else if ( QString::compare( layerProperty, QStringLiteral(
"feature_count" ), Qt::CaseInsensitive ) == 0 )
5074 else if ( QString::compare( layerProperty, QStringLiteral(
"path" ), Qt::CaseInsensitive ) == 0 )
5079 return decodedUri.value( QStringLiteral(
"path" ) );
5090 QgsMapLayer *layer = QgsExpressionUtils::getMapLayer( values.at( 0 ), parent );
5093 parent->
setEvalErrorString( QObject::tr(
"Cannot find layer %1" ).arg( values.at( 0 ).toString() ) );
5103 const QString uriPart = values.at( 1 ).toString();
5107 if ( !uriPart.isNull() )
5109 return decodedUri.value( values.at( 1 ).toString() );
5119 QString layerIdOrName = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
5126 if ( !layersByName.isEmpty() )
5128 layer = layersByName.at( 0 );
5139 int band = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
5140 if ( band < 1 || band > rl->
bandCount() )
5142 parent->
setEvalErrorString( QObject::tr(
"Invalid band number %1 for layer %2" ).arg( band ).arg( layerIdOrName ) );
5146 QString layerProperty = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
5149 if ( QString::compare( layerProperty, QStringLiteral(
"avg" ), Qt::CaseInsensitive ) == 0 )
5151 else if ( QString::compare( layerProperty, QStringLiteral(
"stdev" ), Qt::CaseInsensitive ) == 0 )
5153 else if ( QString::compare( layerProperty, QStringLiteral(
"min" ), Qt::CaseInsensitive ) == 0 )
5155 else if ( QString::compare( layerProperty, QStringLiteral(
"max" ), Qt::CaseInsensitive ) == 0 )
5157 else if ( QString::compare( layerProperty, QStringLiteral(
"range" ), Qt::CaseInsensitive ) == 0 )
5159 else if ( QString::compare( layerProperty, QStringLiteral(
"sum" ), Qt::CaseInsensitive ) == 0 )
5163 parent->
setEvalErrorString( QObject::tr(
"Invalid raster statistic: '%1'" ).arg( layerProperty ) );
5193 QVariantList list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
5194 bool ascending = values.value( 1 ).toBool();
5195 std::sort( list.begin(), list.end(), [ascending]( QVariant a, QVariant b ) ->
bool { return ( !ascending ? qgsVariantLessThan( b, a ) : qgsVariantLessThan( a, b ) ); } );
5201 return QgsExpressionUtils::getListValue( values.at( 0 ), parent ).length();
5206 return QVariant( QgsExpressionUtils::getListValue( values.at( 0 ), parent ).contains( values.at( 1 ) ) );
5211 QVariantList listA = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
5212 QVariantList listB = QgsExpressionUtils::getListValue( values.at( 1 ), parent );
5214 for (
const auto &item : listB )
5216 if ( listA.contains( item ) )
5220 return QVariant( match == listB.count() );
5225 return QgsExpressionUtils::getListValue( values.at( 0 ), parent ).indexOf( values.at( 1 ) );
5230 const QVariantList list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
5231 const int pos = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
5232 if ( pos < 0 || pos >= list.length() )
return QVariant();
5233 return list.at( pos );
5238 const QVariantList list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
5239 return list.value( 0 );
5244 const QVariantList list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
5245 return list.value( list.size() - 1 );
5248 static QVariant convertToSameType(
const QVariant &value, QVariant::Type type )
5250 QVariant result = value;
5251 result.convert(
static_cast<int>( type ) );
5257 QVariantList list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
5258 list.append( values.at( 1 ) );
5259 return convertToSameType( list, values.at( 0 ).type() );
5264 QVariantList list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
5265 list.prepend( values.at( 1 ) );
5266 return convertToSameType( list, values.at( 0 ).type() );
5271 QVariantList list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
5272 list.insert( QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent ), values.at( 2 ) );
5273 return convertToSameType( list, values.at( 0 ).type() );
5278 QVariantList list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
5279 list.removeAt( QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent ) );
5280 return convertToSameType( list, values.at( 0 ).type() );
5285 QVariantList list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
5286 list.removeAll( values.at( 1 ) );
5287 return convertToSameType( list, values.at( 0 ).type() );
5293 for (
const QVariant &cur : values )
5295 list += QgsExpressionUtils::getListValue( cur, parent );
5297 return convertToSameType( list, values.at( 0 ).type() );
5302 QVariantList list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
5303 int start_pos = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
5304 const int end_pos = QgsExpressionUtils::getNativeIntValue( values.at( 2 ), parent );
5305 int slice_length = 0;
5307 if ( start_pos < 0 )
5309 start_pos = list.length() + start_pos;
5313 slice_length = end_pos - start_pos + 1;
5317 slice_length = list.length() + end_pos - start_pos + 1;
5320 if ( slice_length < 0 )
5324 list = list.mid( start_pos, slice_length );
5330 QVariantList list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
5331 std::reverse( list.begin(), list.end() );
5337 const QVariantList array1 = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
5338 const QVariantList array2 = QgsExpressionUtils::getListValue( values.at( 1 ), parent );
5339 for (
const QVariant &cur : array2 )
5341 if ( array1.contains( cur ) )
5342 return QVariant(
true );
5344 return QVariant(
false );
5349 QVariantList array = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
5351 QVariantList distinct;
5353 for ( QVariantList::const_iterator it = array.constBegin(); it != array.constEnd(); ++it )
5355 if ( !distinct.contains( *it ) )
5357 distinct += ( *it );
5366 QVariantList array = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
5367 QString delimiter = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
5368 QString empty = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
5372 for ( QVariantList::const_iterator it = array.constBegin(); it != array.constEnd(); ++it )
5374 str += ( !( *it ).toString().isEmpty() ) ? ( *it ).toString() : empty;
5375 if ( it != ( array.constEnd() - 1 ) )
5381 return QVariant( str );
5386 QString str = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
5387 QString delimiter = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
5388 QString empty = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
5390 QStringList list = str.split( delimiter );
5393 for ( QStringList::const_iterator it = list.constBegin(); it != list.constEnd(); ++it )
5395 array += ( !( *it ).isEmpty() ) ? *it : empty;
5403 QString str = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
5404 QJsonDocument document = QJsonDocument::fromJson( str.toUtf8() );
5405 if ( document.isNull() )
5408 return document.toVariant();
5414 QJsonDocument document = QJsonDocument::fromVariant( values.at( 0 ) );
5415 return document.toJson( QJsonDocument::Compact );
5420 QString str = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
5421 if ( str.isEmpty() )
5422 return QVariantMap();
5423 str = str.trimmed();
5430 QVariantMap map = QgsExpressionUtils::getMapValue( values.at( 0 ), parent );
5437 for (
int i = 0; i + 1 < values.length(); i += 2 )
5439 result.insert( QgsExpressionUtils::getStringValue( values.at( i ), parent ), values.at( i + 1 ) );
5446 return QgsExpressionUtils::getMapValue( values.at( 0 ), parent ).value( values.at( 1 ).toString() );
5451 return QgsExpressionUtils::getMapValue( values.at( 0 ), parent ).contains( values.at( 1 ).toString() );
5456 QVariantMap map = QgsExpressionUtils::getMapValue( values.at( 0 ), parent );
5457 map.remove( values.at( 1 ).toString() );
5463 QVariantMap map = QgsExpressionUtils::getMapValue( values.at( 0 ), parent );
5464 map.insert( values.at( 1 ).toString(), values.at( 2 ) );
5471 for (
const QVariant &cur : values )
5473 const QVariantMap curMap = QgsExpressionUtils::getMapValue( cur, parent );
5474 for ( QVariantMap::const_iterator it = curMap.constBegin(); it != curMap.constEnd(); ++it )
5475 result.insert( it.key(), it.value() );
5482 return QStringList( QgsExpressionUtils::getMapValue( values.at( 0 ), parent ).keys() );
5487 return QgsExpressionUtils::getMapValue( values.at( 0 ), parent ).values();
5492 QString envVarName = values.at( 0 ).toString();
5493 return QProcessEnvironment::systemEnvironment().value( envVarName );
5498 const QString file = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
5499 return QFileInfo( file ).completeBaseName();
5504 const QString file = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
5505 return QFileInfo( file ).completeSuffix();
5510 const QString file = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
5511 return QFileInfo::exists( file );
5516 const QString file = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
5517 return QFileInfo( file ).fileName();
5522 const QString file = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
5523 return QFileInfo( file ).isFile();
5528 const QString file = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
5529 return QFileInfo( file ).isDir();
5534 const QString file = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
5535 return QDir::toNativeSeparators( QFileInfo( file ).path() );
5540 const QString file = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
5541 return QFileInfo( file ).size();
5544 static QVariant fcnHash(
const QString str,
const QCryptographicHash::Algorithm
algorithm )
5547 return QString( QCryptographicHash::hash( str.toUtf8(),
algorithm ).toHex() );
5553 QString str = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
5554 QString method = QgsExpressionUtils::getStringValue( values.at( 1 ), parent ).toLower();
5556 if ( method == QLatin1String(
"md4" ) )
5558 hash = fcnHash( str, QCryptographicHash::Md4 );
5560 else if ( method == QLatin1String(
"md5" ) )
5562 hash = fcnHash( str, QCryptographicHash::Md5 );
5564 else if ( method == QLatin1String(
"sha1" ) )
5566 hash = fcnHash( str, QCryptographicHash::Sha1 );
5568 else if ( method == QLatin1String(
"sha224" ) )
5570 hash = fcnHash( str, QCryptographicHash::Sha224 );
5572 else if ( method == QLatin1String(
"sha256" ) )
5574 hash = fcnHash( str, QCryptographicHash::Sha256 );
5576 else if ( method == QLatin1String(
"sha384" ) )
5578 hash = fcnHash( str, QCryptographicHash::Sha384 );
5580 else if ( method == QLatin1String(
"sha512" ) )
5582 hash = fcnHash( str, QCryptographicHash::Sha512 );
5584 else if ( method == QLatin1String(
"sha3_224" ) )
5586 hash = fcnHash( str, QCryptographicHash::Sha3_224 );
5588 else if ( method == QLatin1String(
"sha3_256" ) )
5590 hash = fcnHash( str, QCryptographicHash::Sha3_256 );
5592 else if ( method == QLatin1String(
"sha3_384" ) )
5594 hash = fcnHash( str, QCryptographicHash::Sha3_384 );
5596 else if ( method == QLatin1String(
"sha3_512" ) )
5598 hash = fcnHash( str, QCryptographicHash::Sha3_512 );
5600 #if QT_VERSION >= QT_VERSION_CHECK( 5, 9, 2 )
5601 else if ( method == QLatin1String(
"keccak_224" ) )
5603 hash = fcnHash( str, QCryptographicHash::Keccak_224 );
5605 else if ( method == QLatin1String(
"keccak_256" ) )
5607 hash = fcnHash( str, QCryptographicHash::Keccak_256 );
5609 else if ( method == QLatin1String(
"keccak_384" ) )
5611 hash = fcnHash( str, QCryptographicHash::Keccak_384 );
5613 else if ( method == QLatin1String(
"keccak_512" ) )
5615 hash = fcnHash( str, QCryptographicHash::Keccak_512 );
5620 parent->
setEvalErrorString( QObject::tr(
"Hash method %1 is not available on this system." ).arg( str ) );
5627 return fcnHash( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ), QCryptographicHash::Md5 );
5632 return fcnHash( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ), QCryptographicHash::Sha256 );
5637 const QByteArray input = values.at( 0 ).toByteArray();
5638 return QVariant( QString( input.toBase64() ) );
5643 const QString value = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
5644 const QByteArray base64 = value.toLocal8Bit();
5645 const QByteArray decoded = QByteArray::fromBase64( base64 );
5646 return QVariant( decoded );
5655 static QMutex sFunctionsMutex( QMutex::Recursive );
5656 QMutexLocker locker( &sFunctionsMutex );
5658 QList<QgsExpressionFunction *> &functions = *sFunctions();
5660 if ( functions.isEmpty() )
5697 functions << randFunc;
5701 functions << randfFunc;
5704 <<
new QgsStaticExpressionFunction( QStringLiteral(
"max" ), -1, fcnMax, QStringLiteral(
"Math" ), QString(),
false, QSet<QString>(),
false, QStringList(),
true )
5705 <<
new QgsStaticExpressionFunction( QStringLiteral(
"min" ), -1, fcnMin, QStringLiteral(
"Math" ), QString(),
false, QSet<QString>(),
false, QStringList(),
true )
5711 <<
new QgsStaticExpressionFunction( QStringLiteral(
"pi" ), 0, fcnPi, QStringLiteral(
"Math" ), QString(),
false, QSet<QString>(),
false, QStringList() << QStringLiteral(
"$pi" ) )
5715 <<
new QgsStaticExpressionFunction( QStringLiteral(
"to_datetime" ),
QgsExpressionFunction::ParameterList() <<
QgsExpressionFunction::Parameter( QStringLiteral(
"value" ) ) <<
QgsExpressionFunction::Parameter( QStringLiteral(
"format" ),
true, QVariant() ) <<
QgsExpressionFunction::Parameter( QStringLiteral(
"language" ),
true, QVariant() ), fcnToDateTime, QStringList() << QStringLiteral(
"Conversions" ) << QStringLiteral(
"Date and Time" ), QString(),
false, QSet<QString>(),
false, QStringList() << QStringLiteral(
"todatetime" ) )
5716 <<
new QgsStaticExpressionFunction( QStringLiteral(
"to_date" ),
QgsExpressionFunction::ParameterList() <<
QgsExpressionFunction::Parameter( QStringLiteral(
"value" ) ) <<
QgsExpressionFunction::Parameter( QStringLiteral(
"format" ),
true, QVariant() ) <<
QgsExpressionFunction::Parameter( QStringLiteral(
"language" ),
true, QVariant() ), fcnToDate, QStringList() << QStringLiteral(
"Conversions" ) << QStringLiteral(
"Date and Time" ), QString(),
false, QSet<QString>(),
false, QStringList() << QStringLiteral(
"todate" ) )
5717 <<
new QgsStaticExpressionFunction( QStringLiteral(
"to_time" ),
QgsExpressionFunction::ParameterList() <<
QgsExpressionFunction::Parameter( QStringLiteral(
"value" ) ) <<
QgsExpressionFunction::Parameter( QStringLiteral(
"format" ),
true, QVariant() ) <<
QgsExpressionFunction::Parameter( QStringLiteral(
"language" ),
true, QVariant() ), fcnToTime, QStringList() << QStringLiteral(
"Conversions" ) << QStringLiteral(
"Date and Time" ), QString(),
false, QSet<QString>(),
false, QStringList() << QStringLiteral(
"totime" ) )
5721 <<
new QgsStaticExpressionFunction( QStringLiteral(
"coalesce" ), -1, fcnCoalesce, QStringLiteral(
"Conditionals" ), QString(),
false, QSet<QString>(),
false, QStringList(),
true )
5735 QStringLiteral(
"Aggregates" ),
5744 if ( !node->
args() )
5747 QSet<QString> referencedVars;
5750 QgsExpressionNode *subExpressionNode = node->args()->at( 2 );
5751 referencedVars = subExpressionNode->referencedVariables();
5756 QgsExpressionNode *filterNode = node->args()->at( 3 );
5757 referencedVars.unite( filterNode->referencedVariables() );
5759 return referencedVars.contains( QStringLiteral(
"parent" ) ) || referencedVars.contains( QString() );
5768 if ( !node->
args() )
5769 return QSet<QString>();
5771 QSet<QString> referencedCols;
5772 QSet<QString> referencedVars;
5787 if ( referencedVars.contains( QStringLiteral(
"parent" ) ) || referencedVars.contains( QString() ) )
5790 return referencedCols;
5803 <<
new QgsStaticExpressionFunction( QStringLiteral(
"count" ), aggParams, fcnAggregateCount, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5804 <<
new QgsStaticExpressionFunction( QStringLiteral(
"count_distinct" ), aggParams, fcnAggregateCountDistinct, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5805 <<
new QgsStaticExpressionFunction( QStringLiteral(
"count_missing" ), aggParams, fcnAggregateCountMissing, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5806 <<
new QgsStaticExpressionFunction( QStringLiteral(
"minimum" ), aggParams, fcnAggregateMin, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5807 <<
new QgsStaticExpressionFunction( QStringLiteral(
"maximum" ), aggParams, fcnAggregateMax, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5808 <<
new QgsStaticExpressionFunction( QStringLiteral(
"sum" ), aggParams, fcnAggregateSum, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5809 <<
new QgsStaticExpressionFunction( QStringLiteral(
"mean" ), aggParams, fcnAggregateMean, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5810 <<
new QgsStaticExpressionFunction( QStringLiteral(
"median" ), aggParams, fcnAggregateMedian, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5811 <<
new QgsStaticExpressionFunction( QStringLiteral(
"stdev" ), aggParams, fcnAggregateStdev, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5812 <<
new QgsStaticExpressionFunction( QStringLiteral(
"range" ), aggParams, fcnAggregateRange, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5813 <<
new QgsStaticExpressionFunction( QStringLiteral(
"minority" ), aggParams, fcnAggregateMinority, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5814 <<
new QgsStaticExpressionFunction( QStringLiteral(
"majority" ), aggParams, fcnAggregateMajority, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5815 <<
new QgsStaticExpressionFunction( QStringLiteral(
"q1" ), aggParams, fcnAggregateQ1, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5816 <<
new QgsStaticExpressionFunction( QStringLiteral(
"q3" ), aggParams, fcnAggregateQ3, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5817 <<
new QgsStaticExpressionFunction( QStringLiteral(
"iqr" ), aggParams, fcnAggregateIQR, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5818 <<
new QgsStaticExpressionFunction( QStringLiteral(
"min_length" ), aggParams, fcnAggregateMinLength, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5819 <<
new QgsStaticExpressionFunction( QStringLiteral(
"max_length" ), aggParams, fcnAggregateMaxLength, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5820 <<
new QgsStaticExpressionFunction( QStringLiteral(
"collect" ), aggParams, fcnAggregateCollectGeometry, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5821 <<
new QgsStaticExpressionFunction( QStringLiteral(
"concatenate" ), aggParamsConcat, fcnAggregateStringConcat, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5822 <<
new QgsStaticExpressionFunction( QStringLiteral(
"concatenate_unique" ), aggParamsConcat, fcnAggregateStringConcatUnique, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5823 <<
new QgsStaticExpressionFunction( QStringLiteral(
"array_agg" ), aggParamsArray, fcnAggregateArray, QStringLiteral(
"Aggregates" ), QString(),
false, QSet<QString>(), true )
5828 <<
new QgsStaticExpressionFunction( QStringLiteral(
"now" ), 0, fcnNow, QStringLiteral(
"Date and Time" ), QString(),
false, QSet<QString>(),
false, QStringList() << QStringLiteral(
"$now" ) )
5831 fcnAge, QStringLiteral(
"Date and Time" ) )
5845 fcnMakeDate, QStringLiteral(
"Date and Time" ) )
5849 fcnMakeTime, QStringLiteral(
"Date and Time" ) )
5856 fcnMakeDateTime, QStringLiteral(
"Date and Time" ) )
5864 fcnMakeInterval, QStringLiteral(
"Date and Time" ) )
5882 false, QSet< QString >(),
false, QStringList(), true )
5883 <<
new QgsStaticExpressionFunction( QStringLiteral(
"concat" ), -1, fcnConcat, QStringLiteral(
"String" ), QString(),
false, QSet<QString>(),
false, QStringList(), true )
5896 fcnColorMixRgb, QStringLiteral(
"Color" ) )
5900 fcnColorRgb, QStringLiteral(
"Color" ) )
5905 fncColorRgba, QStringLiteral(
"Color" ) )
5911 fcnCreateRamp, QStringLiteral(
"Color" ) )
5915 fcnColorHsl, QStringLiteral(
"Color" ) )
5920 fncColorHsla, QStringLiteral(
"Color" ) )
5924 fcnColorHsv, QStringLiteral(
"Color" ) )
5929 fncColorHsva, QStringLiteral(
"Color" ) )
5934 fcnColorCmyk, QStringLiteral(
"Color" ) )
5940 fncColorCmyka, QStringLiteral(
"Color" ) )
5943 fncColorPart, QStringLiteral(
"Color" ) )
5946 fncDarker, QStringLiteral(
"Color" ) )
5949 fncLighter, QStringLiteral(
"Color" ) )
5954 fcnBaseFileName, QStringLiteral(
"Files and Paths" ) )
5956 fcnFileSuffix, QStringLiteral(
"Files and Paths" ) )
5958 fcnFileExists, QStringLiteral(
"Files and Paths" ) )
5960 fcnFileName, QStringLiteral(
"Files and Paths" ) )
5962 fcnPathIsFile, QStringLiteral(
"Files and Paths" ) )
5964 fcnPathIsDir, QStringLiteral(
"Files and Paths" ) )
5966 fcnFilePath, QStringLiteral(
"Files and Paths" ) )
5968 fcnFileSize, QStringLiteral(
"Files and Paths" ) )
5972 fcnGenericHash, QStringLiteral(
"Conversions" ) )
5974 fcnHashMd5, QStringLiteral(
"Conversions" ) )
5976 fcnHashSha256, QStringLiteral(
"Conversions" ) )
5980 fcnToBase64, QStringLiteral(
"Conversions" ) )
5982 fcnFromBase64, QStringLiteral(
"Conversions" ) )
5988 geomFunc->setIsStatic(
false );
5989 functions << geomFunc;
5993 functions << areaFunc;
5999 functions << lengthFunc;
6003 functions << perimeterFunc;
6024 <<
new QgsStaticExpressionFunction( QStringLiteral(
"nodes_to_points" ), -1, fcnNodesToPoints, QStringLiteral(
"GeometryGroup" ) )
6026 <<
new QgsStaticExpressionFunction( QStringLiteral(
"collect_geometries" ), -1, fcnCollectGeometries, QStringLiteral(
"GeometryGroup" ) )
6031 fcnMakePointM, QStringLiteral(
"GeometryGroup" ) )
6037 fcnMakeTriangle, QStringLiteral(
"GeometryGroup" ) )
6042 fcnMakeCircle, QStringLiteral(
"GeometryGroup" ) )
6049 fcnMakeEllipse, QStringLiteral(
"GeometryGroup" ) )
6055 fcnMakeRegularPolygon, QStringLiteral(
"GeometryGroup" ) )
6059 fcnMakeSquare, QStringLiteral(
"GeometryGroup" ) )
6065 fcnMakeRectangleFrom3Points, QStringLiteral(
"GeometryGroup" ) );
6068 functions << xAtFunc;
6072 functions << yAtFunc;
6087 fcnDisjoint, QStringLiteral(
"GeometryGroup" ) )
6090 fcnIntersects, QStringLiteral(
"GeometryGroup" ) )
6093 fcnTouches, QStringLiteral(
"GeometryGroup" ) )
6096 fcnCrosses, QStringLiteral(
"GeometryGroup" ) )
6099 fcnContains, QStringLiteral(
"GeometryGroup" ) )
6102 fcnOverlaps, QStringLiteral(
"GeometryGroup" ) )
6105 fcnWithin, QStringLiteral(
"GeometryGroup" ) )
6109 fcnTranslate, QStringLiteral(
"GeometryGroup" ) )
6113 fcnRotate, QStringLiteral(
"GeometryGroup" ) )
6116 fcnForceRHR, QStringLiteral(
"GeometryGroup" ) )
6126 , fcnTaperedBuffer, QStringLiteral(
"GeometryGroup" ) )
6129 , fcnBufferByM, QStringLiteral(
"GeometryGroup" ) )
6135 fcnOffsetCurve, QStringLiteral(
"GeometryGroup" ) )
6141 fcnSingleSidedBuffer, QStringLiteral(
"GeometryGroup" ) )
6145 fcnExtend, QStringLiteral(
"GeometryGroup" ) )
6154 fcnInteriorRingN, QStringLiteral(
"GeometryGroup" ) )
6157 fcnGeometryN, QStringLiteral(
"GeometryGroup" ) )
6180 fcnOrientedBBox, QStringLiteral(
"GeometryGroup" ) )
6184 fcnMinimalCircle, QStringLiteral(
"GeometryGroup" ) )
6187 fcnDifference, QStringLiteral(
"GeometryGroup" ) )
6190 fcnDistance, QStringLiteral(
"GeometryGroup" ) )
6193 fcnHausdorffDistance, QStringLiteral(
"GeometryGroup" ) )
6196 fcnIntersection, QStringLiteral(
"GeometryGroup" ) )
6199 fcnSymDifference, QStringLiteral(
"GeometryGroup" ), QString(),
false, QSet<QString>(),
false, QStringList() << QStringLiteral(
"symDifference" ) )
6202 fcnCombine, QStringLiteral(
"GeometryGroup" ) )
6205 fcnCombine, QStringLiteral(
"GeometryGroup" ) )
6206 <<
new QgsStaticExpressionFunction( QStringLiteral(
"geom_to_wkt" ), -1, fcnGeomToWKT, QStringLiteral(
"GeometryGroup" ), QString(),
false, QSet<QString>(),
false, QStringList() << QStringLiteral(
"geomToWKT" ) )
6212 fcnTransformGeometry, QStringLiteral(
"GeometryGroup" ) )
6216 fcnExtrude, QStringLiteral(
"GeometryGroup" ), QString() )
6218 fcnGeomIsMultipart, QStringLiteral(
"GeometryGroup" ) )
6220 fcnZMax, QStringLiteral(
"GeometryGroup" ) )
6222 fcnZMin, QStringLiteral(
"GeometryGroup" ) )
6224 fcnMMax, QStringLiteral(
"GeometryGroup" ) )
6226 fcnMMin, QStringLiteral(
"GeometryGroup" ) );
6232 fcnOrderParts, QStringLiteral(
"GeometryGroup" ), QString() );
6237 const QList< QgsExpressionNode *> argList = node->
args()->list();
6240 if ( !argNode->isStatic( parent, context ) )
6246 QgsExpressionNode *argNode = node->args()->at( 1 );
6248 QString expString = argNode->eval( parent, context ).toString();
6250 QgsExpression e( expString );
6252 if ( e.rootNode() && e.rootNode()->isStatic( parent, context ) )
6263 QgsExpressionNode *argNode = node->args()->at( 1 );
6264 QString expression = argNode->eval( parent, context ).toString();
6265 QgsExpression e( expression );
6266 e.prepare( context );
6267 context->setCachedValue( expression, QVariant::fromValue( e ) );
6272 functions << orderPartsFunc;
6277 fcnClosestPoint, QStringLiteral(
"GeometryGroup" ) )
6280 fcnShortestLine, QStringLiteral(
"GeometryGroup" ) )
6299 functions << idFunc;
6303 functions << currentFeatureFunc;
6307 functions << uuidFunc;
6313 fcnGetFeature, QStringLiteral(
"Record and Attributes" ), QString(),
false, QSet<QString>(),
false, QStringList() << QStringLiteral(
"QgsExpressionUtils::getFeature" ) )
6316 fcnGetFeatureById, QStringLiteral(
"Record and Attributes" ), QString(),
false, QSet<QString>(),
false );
6321 functions << attributesFunc;
6324 QStringLiteral(
"maptip" ),
6327 QStringLiteral(
"Record and Attributes" ),
6333 functions << maptipFunc;
6336 QStringLiteral(
"display_expression" ),
6338 fcnFeatureDisplayExpression,
6339 QStringLiteral(
"Record and Attributes" ),
6345 functions << displayFunc;
6348 QStringLiteral(
"is_selected" ),
6351 QStringLiteral(
"Record and Attributes" ),
6357 functions << isSelectedFunc;
6361 QStringLiteral(
"num_selected" ),
6364 QStringLiteral(
"Record and Attributes" ),
6372 QStringLiteral(
"sqlite_fetch_and_increment" ),
6380 fcnSqliteFetchAndIncrement,
6381 QStringLiteral(
"Record and Attributes" )
6399 parent->
setEvalErrorString( tr(
"If represent_value is called with 1 parameter, it must be an attribute." ) );
6409 parent->
setEvalErrorString( tr(
"represent_value must be called with exactly 1 or 2 parameters." ) );
6415 functions << representValueFunc;
6421 fcnGetLayerProperty, QStringLiteral(
"General" ) )
6426 fcnDecodeUri, QStringLiteral(
"Map Layers" ) )
6443 QgsExpressionNode *argNode = node->args()->at( 0 );
6445 if ( !argNode->isStatic( parent, context ) )
6448 QString varName = argNode->eval( parent, context ).toString();
6450 const QgsExpressionContextScope *scope = context->activeScopeForVariable( varName );
6451 return scope ? scope->isStatic( varName ) : false;
6468 QgsExpressionNode *argNode = node->args()->at( 0 );
6470 if ( argNode->isStatic( parent, context ) )
6472 QString expString = argNode->eval( parent, context ).toString();
6474 QgsExpression e( expString );
6476 if ( e.rootNode() && e.rootNode()->isStatic( parent, context ) )
6484 functions << evalFunc;
6490 const QList< QgsExpressionNode *> argList = node->
args()->list();
6493 if ( !argNode->isStatic( parent, context ) )
6505 functions << attributeFunc;
6515 <<
new QgsStaticExpressionFunction( QStringLiteral(
"array" ), -1, fcnArray, QStringLiteral(
"Arrays" ), QString(),
false, QSet<QString>(),
false, QStringList(),
true )
6560 *sOwnedFunctions() << func;
6561 *sBuiltinFunctions() << func->name();
6562 sBuiltinFunctions()->append( func->aliases() );
6575 sFunctions()->append(
function );
6576 if ( transferOwnership )
6577 sOwnedFunctions()->append(
function );
6591 sFunctions()->removeAt( fnIdx );
6599 qDeleteAll( *sOwnedFunctions() );
6600 sOwnedFunctions()->clear();
6605 if ( sBuiltinFunctions()->isEmpty() )
6609 return *sBuiltinFunctions();
6617 QStringLiteral(
"Arrays" ) )
6628 if ( args->
count() < 2 )
6631 if ( args->
at( 0 )->
isStatic( parent, context ) && args->
at( 1 )->
isStatic( parent, context ) )
6641 QVariantList result;
6643 if ( args->
count() < 2 )
6647 QVariantList array = args->
at( 0 )->
eval( parent, context ).toList();
6650 std::unique_ptr< QgsExpressionContext > tempContext;
6653 tempContext = qgis::make_unique< QgsExpressionContext >();
6654 subContext = tempContext.get();
6660 for ( QVariantList::const_iterator it = array.constBegin(); it != array.constEnd(); ++it )
6663 result << args->
at( 1 )->
eval( parent, subContext );
6688 if ( args->
count() < 2 )
6692 args->
at( 0 )->
prepare( parent, context );
6696 subContext = *context;
6702 args->
at( 1 )->
prepare( parent, &subContext );
6711 QStringLiteral(
"Arrays" ) )
6722 if ( args->
count() < 2 )
6725 if ( args->
at( 0 )->
isStatic( parent, context ) && args->
at( 1 )->
isStatic( parent, context ) )
6735 QVariantList result;
6737 if ( args->
count() < 2 )
6741 const QVariantList array = args->
at( 0 )->
eval( parent, context ).toList();
6744 std::unique_ptr< QgsExpressionContext > tempContext;
6747 tempContext = qgis::make_unique< QgsExpressionContext >();
6748 subContext = tempContext.get();
6754 for (
const QVariant &value : array )
6757 if ( args->
at( 1 )->
eval( parent, subContext ).toBool() )
6783 if ( args->
count() < 2 )
6787 args->
at( 0 )->
prepare( parent, context );
6791 subContext = *context;
6797 args->
at( 1 )->
prepare( parent, &subContext );
6806 QStringLiteral(
"General" ) )
6817 if ( args->
count() < 3 )
6821 if ( args->
at( 0 )->
isStatic( parent, context ) && args->
at( 1 )->
isStatic( parent, context ) )
6823 QVariant
name = args->
at( 0 )->
eval( parent, context );
6824 QVariant value = args->
at( 1 )->
eval( parent, context );
6827 appendTemporaryVariable( context,
name.toString(), value );
6828 if ( args->
at( 2 )->
isStatic( parent, context ) )
6830 popTemporaryVariable( context );
6841 if ( args->
count() < 3 )
6845 QVariant
name = args->
at( 0 )->
eval( parent, context );
6846 QVariant value = args->
at( 1 )->
eval( parent, context );
6849 std::unique_ptr< QgsExpressionContext > tempContext;
6850 if ( !updatedContext )
6852 tempContext = qgis::make_unique< QgsExpressionContext >();
6853 updatedContext = tempContext.get();
6856 appendTemporaryVariable( updatedContext,
name.toString(), value );
6857 result = args->
at( 2 )->
eval( parent, updatedContext );
6860 popTemporaryVariable( updatedContext );
6881 if ( args->
count() < 3 )
6886 QVariant value = args->
at( 1 )->
prepare( parent, context );
6889 std::unique_ptr< QgsExpressionContext > tempContext;
6890 if ( !updatedContext )
6892 tempContext = qgis::make_unique< QgsExpressionContext >();
6893 updatedContext = tempContext.get();
6896 appendTemporaryVariable( updatedContext,
name.toString(), value );
6897 args->
at( 2 )->
prepare( parent, updatedContext );
6900 popTemporaryVariable( updatedContext );
6905 void QgsWithVariableExpressionFunction::popTemporaryVariable(
const QgsExpressionContext *context )
const
6911 void QgsWithVariableExpressionFunction::appendTemporaryVariable(
const QgsExpressionContext *context,
const QString &name,
const QVariant &value )
const