Skip to content

Commit

Permalink
[FEATURE] Add num_points(geometry) support in expression evaluator
Browse files Browse the repository at this point in the history
Includes tests and help text.
  • Loading branch information
Sandro Santilli committed Sep 17, 2015
1 parent 586d59a commit 55dbc04
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions resources/function_help/json/num_points
@@ -0,0 +1,7 @@
{
"function": "num_points",
"description": "Returns the number of vertices in a geometry.",
"arguments": [ {"arg":"geom","description":"a geometry"}],
"examples": [ { "expression":"num_points($geometry)", "returns":"number of vertices in $geometry"}]
}

8 changes: 7 additions & 1 deletion src/core/qgsexpression.cpp
Expand Up @@ -1265,6 +1265,11 @@ static QVariant fcnGeomPerimeter( const QVariantList&, const QgsExpressionContex
QgsDistanceArea* calc = parent->geomCalculator();
return QVariant( calc->measurePerimeter( f.constGeometry() ) );
}
static QVariant fcnGeomNumPoints( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
{
QgsGeometry geom = getGeometry( values.at( 0 ), parent );
return QVariant( geom.geometry()->nCoordinates() );
}

static QVariant fcnBounds( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
{
Expand Down Expand Up @@ -1912,7 +1917,7 @@ const QStringList& QgsExpression::BuiltinFunctions()
<< "color_hsl" << "color_hsla" << "color_hsv" << "color_hsva"
<< "color_cymk" << "color_cymka"
<< "xat" << "yat" << "$area"
<< "$length" << "$perimeter" << "$x" << "$y"
<< "$length" << "$perimeter" << "$x" << "$y" << "num_points"
<< "x_at" << "xat" << "y_at" << "yat" << "x_min" << "xmin" << "x_max" << "xmax"
<< "y_min" << "ymin" << "y_max" << "ymax" << "geom_from_wkt" << "geomFromWKT"
<< "geom_from_gml" << "geomFromGML" << "intersects_bbox" << "bbox"
Expand Down Expand Up @@ -2038,6 +2043,7 @@ const QList<QgsExpression::Function*>& QgsExpression::Functions()
<< new StaticFunction( "buffer", -1, fcnBuffer, "GeometryGroup" )
<< new StaticFunction( "centroid", 1, fcnCentroid, "GeometryGroup" )
<< new StaticFunction( "bounds", 1, fcnBounds, "GeometryGroup", "", true )
<< new StaticFunction( "num_points", 1, fcnGeomNumPoints, "GeometryGroup", "", true )
<< new StaticFunction( "bounds_width", 1, fcnBoundsWidth, "GeometryGroup", "", true )
<< new StaticFunction( "bounds_height", 1, fcnBoundsHeight, "GeometryGroup", "", true )
<< new StaticFunction( "convex_hull", 1, fcnConvexHull, "GeometryGroup", QString(), false, QStringList(), false, QStringList() << "convexHull" )
Expand Down
8 changes: 8 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -396,6 +396,9 @@ class TestQgsExpression: public QObject
QTest::newRow( "double to text" ) << "tostring(1.23)" << false << QVariant( "1.23" );
QTest::newRow( "null to text" ) << "tostring(null)" << false << QVariant();

// geometry functions
QTest::newRow( "num_points" ) << "num_points(geom_from_wkt('GEOMETRYCOLLECTION(LINESTRING(0 0, 1 0),POINT(6 5))'))" << false << QVariant( 3 );

// string functions
QTest::newRow( "lower" ) << "lower('HeLLo')" << false << QVariant( "hello" );
QTest::newRow( "upper" ) << "upper('HeLLo')" << false << QVariant( "HELLO" );
Expand Down Expand Up @@ -935,6 +938,11 @@ class TestQgsExpression: public QObject
exp9.evaluate( &context );
QCOMPARE( vYMax.toDouble(), 6.0 );

QgsExpression exp10( "num_points($geometry)" );
QVariant vVertices = exp10.evaluate( &fPolygon );
QCOMPARE( vVertices.toInt(), 5 );


Q_NOWARN_DEPRECATED_POP

}
Expand Down

0 comments on commit 55dbc04

Please sign in to comment.