Skip to content
Permalink
Browse files
[feature][expression] Add geometry_type function
Returns the high-level type of a geometry (Point/Line/Polygon)
  • Loading branch information
nyalldawson authored and github-actions committed Jan 6, 2022
1 parent 5a3b771 commit 3f39f1b5e44bebc206aec83a91d8f2847c82a316
Showing with 28 additions and 0 deletions.
  1. +12 −0 resources/function_help/json/geometry_type
  2. +10 −0 src/core/expression/qgsexpressionfunction.cpp
  3. +6 −0 tests/src/core/testqgsexpression.cpp
@@ -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'"}
]
}
@@ -3500,6 +3500,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 );
@@ -6988,6 +6997,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" ) )
@@ -924,6 +924,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();

0 comments on commit 3f39f1b

Please sign in to comment.