diff --git a/resources/function_help/json/boundary b/resources/function_help/json/boundary new file mode 100644 index 00000000000..2fb8b0f8b01 --- /dev/null +++ b/resources/function_help/json/boundary @@ -0,0 +1,8 @@ +{ + "name": "boundary", + "type": "function", + "description":"Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the geometry). For instance, a polygon geometry will have a boundary consisting of the linestrings for each ring in the polygon. Some geometry types do not have a defined boundary, eg points or geometry collections, and will return null.", + "arguments": [ {"arg":"geometry","description":"a geometry"} ], + "examples": [ { "expression":"geom_to_wkt(boundary(geom_from_wkt('Polygon((1 1, 0 0, -1 1, 1 1))')))", "returns":"'LineString(1 1,0 0,-1 1,1 1)'"}] +} + diff --git a/src/core/qgsexpression.cpp b/src/core/qgsexpression.cpp index 5a684feb63e..0155fa3075a 100644 --- a/src/core/qgsexpression.cpp +++ b/src/core/qgsexpression.cpp @@ -1825,6 +1825,20 @@ static QVariant fcnGeometryN( const QVariantList& values, const QgsExpressionCon return result; } +static QVariant fcnBoundary( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent ) +{ + QgsGeometry geom = getGeometry( values.at( 0 ), parent ); + + if ( geom.isEmpty() ) + return QVariant(); + + QgsAbstractGeometryV2* boundary = geom.geometry()->boundary(); + if ( !boundary ) + return QVariant(); + + return QVariant::fromValue( QgsGeometry( boundary ) ); +} + static QVariant fcnMakePoint( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent ) { if ( values.count() < 2 || values.count() > 4 ) @@ -3173,6 +3187,7 @@ const QStringList& QgsExpression::BuiltinFunctions() << "disjoint" << "intersects" << "touches" << "crosses" << "contains" << "relate" << "overlaps" << "within" << "buffer" << "centroid" << "bounds" << "reverse" << "exterior_ring" + << "boundary" << "bounds_width" << "bounds_height" << "is_closed" << "convex_hull" << "difference" << "distance" << "intersection" << "sym_difference" << "combine" << "extrude" << "azimuth" << "project" << "closest_point" << "shortest_line" @@ -3364,6 +3379,7 @@ const QList& QgsExpression::Functions() << new StaticFunction( "exterior_ring", 1, fcnExteriorRing, "GeometryGroup" ) << new StaticFunction( "interior_ring_n", 2, fcnInteriorRingN, "GeometryGroup" ) << new StaticFunction( "geometry_n", 2, fcnGeometryN, "GeometryGroup" ) + << new StaticFunction( "boundary", ParameterList() << Parameter( "geometry" ), fcnBoundary, "GeometryGroup" ) << new StaticFunction( "bounds", 1, fcnBounds, "GeometryGroup" ) << new StaticFunction( "num_points", 1, fcnGeomNumPoints, "GeometryGroup" ) << new StaticFunction( "num_interior_rings", 1, fcnGeomNumInteriorRings, "GeometryGroup" ) diff --git a/tests/src/core/testqgsexpression.cpp b/tests/src/core/testqgsexpression.cpp index f2c968e5887..456b01760b7 100644 --- a/tests/src/core/testqgsexpression.cpp +++ b/tests/src/core/testqgsexpression.cpp @@ -667,6 +667,11 @@ class TestQgsExpression: public QObject QTest::newRow( "geometry_n collection" ) << "geom_to_wkt(geometry_n(geom_from_wkt('GEOMETRYCOLLECTION(POINT(0 1), POINT(0 0), POINT(1 0), POINT(1 1))'),3))" << false << QVariant( QString( "Point (1 0)" ) ); QTest::newRow( "geometry_n collection bad index 1" ) << "geometry_n(geom_from_wkt('GEOMETRYCOLLECTION(POINT(0 1), POINT(0 0), POINT(1 0), POINT(1 1))'),0)" << false << QVariant(); QTest::newRow( "geometry_n collection bad index 2" ) << "geometry_n(geom_from_wkt('GEOMETRYCOLLECTION(POINT(0 1), POINT(0 0), POINT(1 0), POINT(1 1))'),5)" << false << QVariant(); + QTest::newRow( "boundary not geom" ) << "boundary('g')" << true << QVariant(); + QTest::newRow( "boundary null" ) << "boundary(NULL)" << false << QVariant(); + QTest::newRow( "boundary point" ) << "boundary(geom_from_wkt('POINT(1 2)'))" << false << QVariant(); + QTest::newRow( "boundary polygon" ) << "geom_to_wkt(boundary(geometry:=geom_from_wkt('POLYGON((-1 -1, 4 0, 4 2, 0 2, -1 -1))')))" << false << QVariant( "LineString (-1 -1, 4 0, 4 2, 0 2, -1 -1)" ); + QTest::newRow( "boundary line" ) << "geom_to_wkt(boundary(geom_from_wkt('LINESTRING(0 0, 1 1, 2 2)')))" << false << QVariant( "MultiPoint ((0 0),(2 2))" ); QTest::newRow( "start_point point" ) << "geom_to_wkt(start_point(geom_from_wkt('POINT(2 0)')))" << false << QVariant( "Point (2 0)" ); QTest::newRow( "start_point multipoint" ) << "geom_to_wkt(start_point(geom_from_wkt('MULTIPOINT((3 3), (1 1), (2 2))')))" << false << QVariant( "Point (3 3)" ); QTest::newRow( "start_point line" ) << "geom_to_wkt(start_point(geom_from_wkt('LINESTRING(4 1, 1 1, 2 2)')))" << false << QVariant( "Point (4 1)" );