Skip to content

Commit

Permalink
add is_multipart expression
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCaha authored and nyalldawson committed Apr 21, 2020
1 parent 45bead2 commit a2cbd9c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
8 changes: 8 additions & 0 deletions resources/function_help/json/is_multipart
@@ -0,0 +1,8 @@
{
"name": "is_multipart",
"type": "function",
"description": "Returns true if the geometry is of Multi type.",
"arguments": [ {"arg":"geometry","description":"a geometry"} ],
"examples": [ { "expression":"is_multipart(geom_from_wkt('MULTIPOINT ((0 0),(1 1),(2 2))'))", "returns":"true"},
{ "expression":"is_multipart(geom_from_wkt('POINT (0 0)'))", "returns":"false"}]
}
14 changes: 13 additions & 1 deletion src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -3138,6 +3138,15 @@ static QVariant fcnGeomNumGeometries( const QVariantList &values, const QgsExpre
return QVariant( geom.constGet()->partCount() );
}

static QVariant fcnGeomIsMultipart( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
if ( geom.isNull() )
return QVariant();

return QVariant( geom.isMultipart() );
}

static QVariant fcnGeomNumInteriorRings( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
Expand Down Expand Up @@ -5927,7 +5936,10 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
<< new QgsStaticExpressionFunction( QStringLiteral( "extrude" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geom" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "x" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "y" ) ),
fcnExtrude, QStringLiteral( "GeometryGroup" ), QString() );
fcnExtrude, QStringLiteral( "GeometryGroup" ), QString() )
<< new QgsStaticExpressionFunction( QStringLiteral( "is_multipart" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geometry" ) ),
fcnGeomIsMultipart, QStringLiteral( "GeometryGroup" ) );


QgsStaticExpressionFunction *orderPartsFunc = new QgsStaticExpressionFunction( QStringLiteral( "order_parts" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geom" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "orderby" ) )
Expand Down
4 changes: 4 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -1180,6 +1180,10 @@ class TestQgsExpression: public QObject
QTest::newRow( "rotate line fixed multi point" ) << "geom_to_wkt(rotate(geom_from_wkt('LineString(0 0, 10 0, 10 10)'),90, geom_from_wkt('MULTIPOINT((-5 -3))')))" << false << QVariant( "LineString (-2 -8, -2 -18, 8 -18)" );
QTest::newRow( "rotate line fixed multi point multiple" ) << "geom_to_wkt(rotate(geom_from_wkt('LineString(0 0, 10 0, 10 10)'),90, geom_from_wkt('MULTIPOINT(-5 -3,1 2)')))" << true << QVariant();
QTest::newRow( "rotate polygon centroid" ) << "geom_to_wkt(rotate(geom_from_wkt('Polygon((0 0, 10 0, 10 10, 0 0))'),-90))" << false << QVariant( "Polygon ((10 0, 10 10, 0 10, 10 0))" );
QTest::newRow( "is_multipart true" ) << "is_multipart(geom_from_wkt('MULTIPOINT ((0 0),(1 1),(2 2))'))" << false << QVariant( true );
QTest::newRow( "is_multipart false" ) << "is_multipart(geom_from_wkt('POINT (0 0)'))" << false << QVariant( false );
QTest::newRow( "is_multipart false empty geometry" ) << "is_multipart(geom_from_wkt('POINT EMPTY'))" << false << QVariant( false );
QTest::newRow( "is_multipart null" ) << "is_multipart(NULL)" << false << QVariant();

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

0 comments on commit a2cbd9c

Please sign in to comment.