Skip to content

Commit

Permalink
Improve QgsExpressionUtils::getGeometry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Djedouas authored and nyalldawson committed Oct 25, 2024
1 parent 322122c commit ef3b180
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
3 changes: 1 addition & 2 deletions src/core/expression/qgsexpressionfunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1492,7 +1492,7 @@ static QVariant fcnLength( const QVariantList &values, const QgsExpressionContex
// two variants, one for geometry, one for string

//geometry variant
QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent, true );
if ( !geom.isNull() )
{
if ( geom.type() == Qgis::GeometryType::Line )
Expand All @@ -1502,7 +1502,6 @@ static QVariant fcnLength( const QVariantList &values, const QgsExpressionContex
}

//otherwise fall back to string variant
parent->setEvalErrorString( QString() ); // clear error string saying geometry not found
QString str = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
return QVariant( str.length() );
}
Expand Down
30 changes: 15 additions & 15 deletions src/core/expression/qgsexpressionutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class CORE_EXPORT QgsExpressionUtils
const double x = value.toDouble( &ok );
if ( !ok )
{
if ( parent != nullptr )
if ( parent )
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to boolean" ).arg( value.toString() ) );
return Unknown;
}
Expand Down Expand Up @@ -218,7 +218,7 @@ class CORE_EXPORT QgsExpressionUtils
{
if ( value.userType() != QMetaType::Type::QByteArray )
{
if ( parent != nullptr )
if ( parent )
parent->setEvalErrorString( QObject::tr( "Value is not a binary value" ) );
return QByteArray();
}
Expand All @@ -231,7 +231,7 @@ class CORE_EXPORT QgsExpressionUtils
const double x = value.toDouble( &ok );
if ( !ok || std::isnan( x ) || !std::isfinite( x ) )
{
if ( parent != nullptr )
if ( parent )
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to double" ).arg( value.toString() ) );
return 0;
}
Expand All @@ -248,7 +248,7 @@ class CORE_EXPORT QgsExpressionUtils
}
else
{
if ( parent != nullptr )
if ( parent )
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to int" ).arg( value.toString() ) );
return 0;
}
Expand All @@ -264,7 +264,7 @@ class CORE_EXPORT QgsExpressionUtils
}
else
{
if ( parent != nullptr )
if ( parent )
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to native int" ).arg( value.toString() ) );
return 0;
}
Expand All @@ -285,7 +285,7 @@ class CORE_EXPORT QgsExpressionUtils
return QDateTime( QDate( 1, 1, 1 ), t );
}

if ( parent != nullptr )
if ( parent )
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to DateTime" ).arg( value.toString() ) );
return QDateTime();
}
Expand All @@ -300,7 +300,7 @@ class CORE_EXPORT QgsExpressionUtils
}
else
{
if ( parent != nullptr )
if ( parent )
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to Date" ).arg( value.toString() ) );
return QDate();
}
Expand All @@ -315,7 +315,7 @@ class CORE_EXPORT QgsExpressionUtils
}
else
{
if ( parent != nullptr )
if ( parent )
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to Time" ).arg( value.toString() ) );
return QTime();
}
Expand All @@ -334,23 +334,23 @@ class CORE_EXPORT QgsExpressionUtils
return inter;
}
// If we get here then we can't convert so we just error and return invalid.
if ( report_error && parent != nullptr )
if ( report_error && parent )
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to interval" ).arg( value.toString() ) );

return QgsInterval();
}

static QgsGradientColorRamp getRamp( const QVariant &value, QgsExpression *parent, bool report_error = false );

static QgsGeometry getGeometry( const QVariant &value, QgsExpression *parent )
static QgsGeometry getGeometry( const QVariant &value, QgsExpression *parent, bool tolerant = false )
{
if ( value.userType() == qMetaTypeId< QgsReferencedGeometry>() )
return value.value<QgsReferencedGeometry>();

if ( value.userType() == qMetaTypeId< QgsGeometry>() )
return value.value<QgsGeometry>();

if ( parent != nullptr )
if ( !tolerant && parent )
parent->setEvalErrorString( QStringLiteral( "Cannot convert to geometry" ) );
return QgsGeometry();
}
Expand All @@ -360,7 +360,7 @@ class CORE_EXPORT QgsExpressionUtils
if ( value.userType() == qMetaTypeId<QgsFeature>() )
return value.value<QgsFeature>();

if ( parent != nullptr )
if ( parent )
parent->setEvalErrorString( QStringLiteral( "Cannot convert to feature" ) );
return 0;
}
Expand All @@ -370,7 +370,7 @@ class CORE_EXPORT QgsExpressionUtils
if ( value.canConvert<QgsExpressionNode *>() )
return value.value<QgsExpressionNode *>();

if ( parent != nullptr )
if ( parent )
parent->setEvalErrorString( QStringLiteral( "Cannot convert to node" ) );
return nullptr;
}
Expand Down Expand Up @@ -419,7 +419,7 @@ class CORE_EXPORT QgsExpressionUtils
}
else
{
if ( parent != nullptr )
if ( parent )
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to array" ).arg( value.toString() ) );
return QVariantList();
}
Expand All @@ -433,7 +433,7 @@ class CORE_EXPORT QgsExpressionUtils
}
else
{
if ( parent != nullptr )
if ( parent )
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to map" ).arg( value.toString() ) );
return QVariantMap();
}
Expand Down

0 comments on commit ef3b180

Please sign in to comment.