Skip to content

Commit ceddb7e

Browse files
committedDec 13, 2015
[FEATURE] is_closed function for expressions
Returns whether a linestring is closed
1 parent 2d9f361 commit ceddb7e

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed
 
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "is_closed",
3+
"type": "function",
4+
"description": "Returns true if a line string is closed (start and end points are coincident), or false if a line string is not closed. If the geometry is not a line string then the result will be null.",
5+
"arguments": [ {"arg":"geom","description":"a line string geometry"}],
6+
"examples": [ { "expression":"is_closed(geom_from_wkt('LINESTRING(0 0, 1 1, 2 2)'))", "returns":"false"},
7+
{ "expression":"is_closed(geom_from_wkt('LINESTRING(0 0, 1 1, 2 2, 0 0)'))", "returns":"true"}]
8+
}

‎src/core/qgsexpression.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,19 @@ static QVariant fcnYMax( const QVariantList& values, const QgsExpressionContext*
16281628
return QVariant::fromValue( geom.boundingBox().yMaximum() );
16291629
}
16301630

1631+
static QVariant fcnIsClosed( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
1632+
{
1633+
QgsGeometry fGeom = getGeometry( values.at( 0 ), parent );
1634+
if ( fGeom.isEmpty() )
1635+
return QVariant();
1636+
1637+
QgsCurveV2* curve = dynamic_cast< QgsCurveV2* >( fGeom.geometry() );
1638+
if ( !curve )
1639+
return QVariant();
1640+
1641+
return QVariant::fromValue( curve->isClosed() );
1642+
}
1643+
16311644
static QVariant fcnRelate( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
16321645
{
16331646
if ( values.length() < 2 || values.length() > 3 )
@@ -2430,7 +2443,7 @@ const QStringList& QgsExpression::BuiltinFunctions()
24302443
<< "disjoint" << "intersects" << "touches" << "crosses" << "contains"
24312444
<< "relate"
24322445
<< "overlaps" << "within" << "buffer" << "centroid" << "bounds" << "reverse" << "exterior_ring"
2433-
<< "bounds_width" << "bounds_height" << "convex_hull" << "difference"
2446+
<< "bounds_width" << "bounds_height" << "is_closed" << "convex_hull" << "difference"
24342447
<< "distance" << "intersection" << "sym_difference" << "combine"
24352448
<< "union" << "geom_to_wkt" << "geomToWKT" << "geometry"
24362449
<< "transform" << "get_feature" << "getFeature"
@@ -2578,6 +2591,7 @@ const QList<QgsExpression::Function*>& QgsExpression::Functions()
25782591
<< new StaticFunction( "num_points", 1, fcnGeomNumPoints, "GeometryGroup" )
25792592
<< new StaticFunction( "bounds_width", 1, fcnBoundsWidth, "GeometryGroup" )
25802593
<< new StaticFunction( "bounds_height", 1, fcnBoundsHeight, "GeometryGroup" )
2594+
<< new StaticFunction( "is_closed", 1, fcnIsClosed, "GeometryGroup" )
25812595
<< new StaticFunction( "convex_hull", 1, fcnConvexHull, "GeometryGroup", QString(), false, QStringList(), false, QStringList() << "convexHull" )
25822596
<< new StaticFunction( "difference", 2, fcnDifference, "GeometryGroup" )
25832597
<< new StaticFunction( "distance", 2, fcnDistance, "GeometryGroup" )

‎tests/src/core/testqgsexpression.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,12 @@ class TestQgsExpression: public QObject
466466
QTest::newRow( "point on surface line" ) << "geom_to_wkt(point_on_surface( geomFromWKT('LINESTRING (-1 2, 9 12)') ))" << false << QVariant( "Point (-1 2)" );
467467
QTest::newRow( "point on surface not geom" ) << "point_on_surface('g')" << true << QVariant();
468468
QTest::newRow( "point on surface null" ) << "point_on_surface(NULL)" << false << QVariant();
469+
QTest::newRow( "is_closed not geom" ) << "is_closed('g')" << true << QVariant();
470+
QTest::newRow( "is_closed null" ) << "is_closed(NULL)" << false << QVariant();
471+
QTest::newRow( "is_closed point" ) << "is_closed(geom_from_wkt('POINT(1 2)'))" << false << QVariant();
472+
QTest::newRow( "is_closed polygon" ) << "is_closed(geom_from_wkt('POLYGON((-1 -1, 4 0, 4 2, 0 2, -1 -1))'))" << false << QVariant();
473+
QTest::newRow( "is_closed not closed" ) << "is_closed(geom_from_wkt('LINESTRING(0 0, 1 1, 2 2)'))" << false << QVariant( false );
474+
QTest::newRow( "is_closed closed" ) << "is_closed(geom_from_wkt('LINESTRING(0 0, 1 1, 2 2, 0 0)'))" << false << QVariant( true );
469475
QTest::newRow( "make_point" ) << "geom_to_wkt(make_point(2.2,4.4))" << false << QVariant( "Point (2.2 4.4)" );
470476
QTest::newRow( "make_point z" ) << "geom_to_wkt(make_point(2.2,4.4,5.5))" << false << QVariant( "PointZ (2.2 4.4 5.5)" );
471477
QTest::newRow( "make_point zm" ) << "geom_to_wkt(make_point(2.2,4.4,5.5,6.6))" << false << QVariant( "PointZM (2.2 4.4 5.5 6.6)" );

0 commit comments

Comments
 (0)
Please sign in to comment.