Skip to content

Commit

Permalink
[FEATURE][NEEDS-DOCS] Add sinuosity expression
Browse files Browse the repository at this point in the history
  • Loading branch information
lbartoletti authored and nyalldawson committed Jul 28, 2021
1 parent 3cd62b3 commit 8111171
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -3572,6 +3572,19 @@ static QVariant fcnMMax( const QVariantList &values, const QgsExpressionContext
return QVariant( max );
}

static QVariant fcnSinuosity( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( geom.constGet() );
if ( ( geom.type() != QgsWkbTypes::LineGeometry ) || !curve )
{
parent->setEvalErrorString( QObject::tr( "Function `sinuosity` requires a line geometry." ) );
return QVariant();
}

return QVariant( curve->sinuosity() );
}

static QVariant fcnFlipCoordinates( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
Expand Down Expand Up @@ -7004,7 +7017,9 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
<< new QgsStaticExpressionFunction( QStringLiteral( "m_max" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geometry" ) ),
fcnMMax, QStringLiteral( "GeometryGroup" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "m_min" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geometry" ) ),
fcnMMin, QStringLiteral( "GeometryGroup" ) );
fcnMMin, QStringLiteral( "GeometryGroup" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "sinuosity" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geometry" ) ),
fcnSinuosity, QStringLiteral( "GeometryGroup" ) );


QgsStaticExpressionFunction *orderPartsFunc = new QgsStaticExpressionFunction( QStringLiteral( "order_parts" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geometry" ) )
Expand Down
6 changes: 6 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -1316,6 +1316,12 @@ class TestQgsExpression: public QObject
QTest::newRow( "main angle line" ) << "round(main_angle( geom_from_wkt('LINESTRING (-1 2, 9 12)') ))" << false << QVariant( 45 );
QTest::newRow( "main angle not geom" ) << "main_angle('g')" << true << QVariant();
QTest::newRow( "main angle null" ) << "main_angle(NULL)" << false << QVariant();
QTest::newRow( "sinuosity not geom" ) << "sinuosity('g')" << true << QVariant();
QTest::newRow( "sinuosity null" ) << "sinuosity(NULL)" << false << QVariant();
QTest::newRow( "sinuosity point" ) << "sinuosity(geom_from_wkt('POINT(1 2)'))" << true << QVariant( );
QTest::newRow( "sinuosity multi linestring" ) << "sinuosity(geom_from_wkt('MULTILINESTRING( (0 0, 1 1), (2 2, 3 3) )'))" << true << QVariant( );
QTest::newRow( "sinuosity linestring" ) << "round(sinuosity(geom_from_wkt(' LINESTRING(2 0, 2 2, 3 2, 3 3)')), 3)" << false << QVariant( 1.265 );
QTest::newRow( "sinuosity linestring" ) << "sinuosity(geom_from_wkt(' LINESTRING( 3 1, 5 1) '))" << false << QVariant( 1.0 );

// string functions
QTest::newRow( "format_number" ) << "format_number(1999.567,2)" << false << QVariant( "1,999.57" );
Expand Down

0 comments on commit 8111171

Please sign in to comment.