Skip to content

Commit 3b4437b

Browse files
authoredDec 16, 2019
Merge pull request #33333 from lbartoletti/expression-is_empty
Add expressions is_empty(geom) is_empty_or_null(geom)
2 parents c235c4c + c7fca82 commit 3b4437b

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed
 

‎resources/function_help/json/is_empty

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "is_empty",
3+
"type": "function",
4+
"description": "Returns true if a geometry is empty (without coordinates), false if the geometry is not empty and NULL if there is no geometry. See also `is_empty_or_null`.",
5+
"arguments": [ {"arg":"geom","description":"a geometry"}],
6+
"examples": [ { "expression":"is_empty(geom_from_wkt('LINESTRING(0 0, 1 1, 2 2)'))", "returns":"false"},
7+
{ "expression":"is_empty(geom_from_wkt('LINESTRING EMPTY'))", "returns":"true"},
8+
{ "expression":"is_empty(geom_from_wkt('POINT(7 4)'))", "returns":"false"},
9+
{ "expression":"is_empty(geom_from_wkt('POINT EMPTY'))", "returns":"true"} ]
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "is_empty_or_null",
3+
"type": "function",
4+
"description": "Returns true if a geometry is NULL or empty (without coordinates) or false otherwise. This function is like the expression '$geometry IS NULL or is_empty($geometry)'",
5+
"arguments": [ {"arg":"geom","description":"a geometry"}],
6+
"examples": [ { "expression":"is_empty_or_null(NULL)", "returns":"true"},
7+
{ "expression":"is_empty_or_null(geom_from_wkt('LINESTRING(0 0, 1 1, 2 2)'))", "returns":"false"},
8+
{ "expression":"is_empty_or_null(geom_from_wkt('LINESTRING EMPTY'))", "returns":"true"},
9+
{ "expression":"is_empty_or_null(geom_from_wkt('POINT(7 4)'))", "returns":"false"},
10+
{ "expression":"is_empty_or_null(geom_from_wkt('POINT EMPTY'))", "returns":"true"} ]
11+
}

‎src/core/expression/qgsexpressionfunction.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3124,6 +3124,24 @@ static QVariant fcnIsClosed( const QVariantList &values, const QgsExpressionCont
31243124
return QVariant::fromValue( curve->isClosed() );
31253125
}
31263126

3127+
static QVariant fcnIsEmpty( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
3128+
{
3129+
QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3130+
if ( fGeom.isNull() )
3131+
return QVariant();
3132+
3133+
return QVariant::fromValue( fGeom.isEmpty() );
3134+
}
3135+
3136+
static QVariant fcnIsEmptyOrNull( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
3137+
{
3138+
if ( values.at( 0 ).isNull() )
3139+
return QVariant::fromValue( true );
3140+
3141+
QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
3142+
return QVariant::fromValue( fGeom.isNull() || fGeom.isEmpty() );
3143+
}
3144+
31273145
static QVariant fcnRelate( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
31283146
{
31293147
if ( values.length() < 2 || values.length() > 3 )
@@ -5661,6 +5679,8 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
56615679
<< new QgsStaticExpressionFunction( QStringLiteral( "bounds_width" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geom" ) ), fcnBoundsWidth, QStringLiteral( "GeometryGroup" ) )
56625680
<< new QgsStaticExpressionFunction( QStringLiteral( "bounds_height" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geom" ) ), fcnBoundsHeight, QStringLiteral( "GeometryGroup" ) )
56635681
<< new QgsStaticExpressionFunction( QStringLiteral( "is_closed" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geom" ) ), fcnIsClosed, QStringLiteral( "GeometryGroup" ) )
5682+
<< new QgsStaticExpressionFunction( QStringLiteral( "is_empty" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geom" ) ), fcnIsEmpty, QStringLiteral( "GeometryGroup" ) )
5683+
<< new QgsStaticExpressionFunction( QStringLiteral( "is_empty_or_null" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geom" ) ), fcnIsEmptyOrNull, QStringLiteral( "GeometryGroup" ), QString(), false, QSet<QString>(), false, QStringList(), true )
56645684
<< new QgsStaticExpressionFunction( QStringLiteral( "convex_hull" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geometry" ) ), fcnConvexHull, QStringLiteral( "GeometryGroup" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "convexHull" ) )
56655685
<< new QgsStaticExpressionFunction( QStringLiteral( "oriented_bbox" ), QgsExpressionFunction::ParameterList()
56665686
<< QgsExpressionFunction::Parameter( QStringLiteral( "geometry" ) ),

‎tests/src/core/testqgsexpression.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,18 @@ class TestQgsExpression: public QObject
985985
QTest::newRow( "is_closed multiline" ) << "is_closed(geom_from_wkt('MultiLineString ((6501338.13976828 4850981.51459331, 6501343.09036573 4850984.01453377, 6501338.13976828 4850988.96491092, 6501335.63971657 4850984.01453377, 6501338.13976828 4850981.51459331))'))" << false << QVariant( true );
986986
QTest::newRow( "is_closed multiline" ) << "is_closed(geom_from_wkt('MultiLineString ((6501338.13976828 4850981.51459331, 6501343.09036573 4850984.01453377, 6501338.13976828 4850988.96491092, 6501335.63971657 4850984.01453377, 6501438.13976828 4850981.51459331))'))" << false << QVariant( false );
987987
QTest::newRow( "is_closed multiline" ) << "is_closed(geom_from_wkt('MultiLineString EMPTY'))" << false << QVariant();
988+
QTest::newRow( "is_empty not geom" ) << "is_empty('g')" << true << QVariant();
989+
QTest::newRow( "is_empty null" ) << "is_empty(NULL)" << false << QVariant();
990+
QTest::newRow( "is_empty point" ) << "is_empty(geom_from_wkt('POINT(1 2)'))" << false << QVariant( false );
991+
QTest::newRow( "is_empty empty point" ) << "is_empty(geom_from_wkt('POINT EMPTY'))" << false << QVariant( true );
992+
QTest::newRow( "is_empty polygon" ) << "is_empty(geom_from_wkt('POLYGON((-1 -1, 4 0, 4 2, 0 2, -1 -1))'))" << false << QVariant( false );
993+
QTest::newRow( "is_empty empty polygon" ) << "is_empty(geom_from_wkt('POLYGON EMPTY'))" << false << QVariant( true );
994+
QTest::newRow( "is_empty_or_null not geom" ) << "is_empty_or_null('g')" << true << QVariant();
995+
QTest::newRow( "is_empty_or_null null" ) << "is_empty_or_null(NULL)" << false << QVariant( true );
996+
QTest::newRow( "is_empty_or_null point" ) << "is_empty_or_null(geom_from_wkt('POINT(1 2)'))" << false << QVariant( false );
997+
QTest::newRow( "is_empty_or_null empty point" ) << "is_empty_or_null(geom_from_wkt('POINT EMPTY'))" << false << QVariant( true );
998+
QTest::newRow( "is_empty_or_null polygon" ) << "is_empty_or_null(geom_from_wkt('POLYGON((-1 -1, 4 0, 4 2, 0 2, -1 -1))'))" << false << QVariant( false );
999+
QTest::newRow( "is_empty_or_null empty polygon" ) << "is_empty_or_null(geom_from_wkt('POLYGON EMPTY'))" << false << QVariant( true );
9881000
QTest::newRow( "collect_geometries none" ) << "geom_to_wkt(collect_geometries())" << false << QVariant( "" );
9891001
QTest::newRow( "collect_geometries not" ) << "geom_to_wkt(collect_geometries(45))" << true << QVariant();
9901002
QTest::newRow( "collect_geometries one" ) << "geom_to_wkt(collect_geometries(make_point(4,5)))" << false << QVariant( "MultiPoint ((4 5))" );

0 commit comments

Comments
 (0)
Please sign in to comment.