[FEATURE] Expression function for calculating geometry boundary

```The new 'boundary(...)' function returns a geometry's topological
boundary, ie for polygons this is a multilinestring representing
the polygons rings

(cherry-picked from 1a4ceb1)```
nyalldawson committed Aug 9, 2016
1 parent 240c136 commit 8c6f5fdf7f1d56d52cfe3af154245df0aa83eb6f
Showing with 29 additions and 1 deletion.
1. +8 −0 resources/function_help/json/boundary
2. +16 −1 src/core/qgsexpression.cpp
3. +5 −0 tests/src/core/testqgsexpression.cpp
 @@ -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)'"}] }
 @@ -1824,6 +1824,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 fcnLineMerge( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent ) { QgsGeometry geom = getGeometry( values.at( 0 ), parent ); @@ -3186,7 +3200,7 @@ const QStringList& QgsExpression::BuiltinFunctions() << "disjoint" << "intersects" << "touches" << "crosses" << "contains" << "relate" << "overlaps" << "within" << "buffer" << "centroid" << "bounds" << "reverse" << "exterior_ring" << "line_merge" << "boundary" << "line_merge" << "bounds_width" << "bounds_height" << "is_closed" << "convex_hull" << "difference" << "distance" << "intersection" << "sym_difference" << "combine" << "extrude" << "azimuth" << "project" << "closest_point" << "shortest_line" @@ -3378,6 +3392,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( "line_merge", ParameterList() << Parameter( "geometry" ), fcnLineMerge, "GeometryGroup" ) << new StaticFunction( "bounds", 1, fcnBounds, "GeometryGroup" ) << new StaticFunction( "num_points", 1, fcnGeomNumPoints, "GeometryGroup" )
 @@ -665,6 +665,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( "line_merge not geom" ) << "line_merge('g')" << true << QVariant(); QTest::newRow( "line_merge null" ) << "line_merge(NULL)" << false << QVariant(); QTest::newRow( "line_merge point" ) << "line_merge(geom_from_wkt('POINT(1 2)'))" << false << QVariant();