Skip to content

Commit

Permalink
[feature][expression] Add geometry_type function
Browse files Browse the repository at this point in the history
Returns the high-level type of a geometry (Point/Line/Polygon)
  • Loading branch information
nyalldawson committed Jan 6, 2022
1 parent 3c001de commit 47715fe
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions resources/function_help/json/geometry_type
@@ -0,0 +1,12 @@
{
"name": "geometry_type",
"type": "function",
"groups": ["GeometryGroup"],
"description": "Returns a string value describing the type of a geometry (Point, Line or Polygon)",
"arguments": [ {"arg":"geometry","description":"a geometry"}],
"examples": [ { "expression":"geometry_type( geom_from_wkt( 'LINESTRING(2 5, 3 6, 4 8)') )", "returns":"'Line'"},
{ "expression":"geometry_type( geom_from_wkt( 'MULTILINESTRING((2 5, 3 6, 4 8), (1 1, 0 0))') )", "returns":"'Line'"},
{ "expression":"geometry_type( geom_from_wkt( 'POINT(2 5)') )", "returns":"'Point'"},
{ "expression":"geometry_type( geom_from_wkt( 'POLYGON((-1 -1, 4 0, 4 2, 0 2, -1 -1))') )", "returns":"'Polygon'"}
]
}
10 changes: 10 additions & 0 deletions src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -3822,6 +3822,15 @@ static QVariant fcnBoundsHeight( const QVariantList &values, const QgsExpression
return QVariant::fromValue( geom.boundingBox().height() );
}

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

return QgsWkbTypes::geometryDisplayString( geom.type() );
}

static QVariant fcnXMin( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
Expand Down Expand Up @@ -7540,6 +7549,7 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
functions << yAtFunc;

functions
<< new QgsStaticExpressionFunction( QStringLiteral( "geometry_type" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geometry" ) ), fcnGeometryType, QStringLiteral( "GeometryGroup" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "x_min" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geometry" ) ), fcnXMin, QStringLiteral( "GeometryGroup" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "xmin" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "x_max" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geometry" ) ), fcnXMax, QStringLiteral( "GeometryGroup" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "xmax" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "y_min" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geometry" ) ), fcnYMin, QStringLiteral( "GeometryGroup" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "ymin" ) )
Expand Down
6 changes: 6 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -999,6 +999,12 @@ class TestQgsExpression: public QObject
QTest::newRow( "geom_to_wkb not geom" ) << "geom_to_wkt(geom_from_wkb(geom_to_wkb('a')))" << true << QVariant();
QTest::newRow( "geom_from_wkb not geom" ) << "geom_to_wkt(geom_from_wkb(make_point(4,5)))" << true << QVariant();
QTest::newRow( "geom_from_wkb null" ) << "geom_to_wkt(geom_from_wkb(NULL))" << false << QVariant();
QTest::newRow( "geometry_type not geom" ) << "geometry_type('g')" << true << QVariant();
QTest::newRow( "geometry_type null" ) << "geometry_type(NULL)" << false << QVariant();
QTest::newRow( "geometry_type point" ) << "geometry_type(geom_from_wkt('POINT(1 2)'))" << false << QVariant( QStringLiteral( "Point" ) );
QTest::newRow( "geometry_type polygon" ) << "geometry_type(geom_from_wkt('POLYGON((-1 -1, 4 0, 4 2, 0 2, -1 -1))'))" << false << QVariant( QStringLiteral( "Polygon" ) );
QTest::newRow( "geometry_type line" ) << "geometry_type(geom_from_wkt('LINESTRING(0 0, 1 1, 2 2)'))" << false << QVariant( QStringLiteral( "Line" ) );
QTest::newRow( "geometry_type multipoint" ) << "geometry_type(geom_from_wkt('MULTIPOINT((0 1),(0 0))'))" << false << QVariant( QStringLiteral( "Point" ) );
QTest::newRow( "num_points" ) << "num_points(geom_from_wkt('GEOMETRYCOLLECTION(LINESTRING(0 0, 1 0),POINT(6 5))'))" << false << QVariant( 3 );
QTest::newRow( "num_interior_rings not geom" ) << "num_interior_rings('g')" << true << QVariant();
QTest::newRow( "num_interior_rings null" ) << "num_interior_rings(NULL)" << false << QVariant();
Expand Down

0 comments on commit 47715fe

Please sign in to comment.