Skip to content

Commit 6a7f786

Browse files
committed
Merge pull request #2570 from nirvn/point_on_surface_v3
[expression] add point_on_surface function
2 parents 62f90d0 + 35fea9c commit 6a7f786

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "point_on_surface",
3+
"type": "function",
4+
"description": "Returns a point guaranteed to lie on the surface of a geometry.",
5+
"arguments": [ {"arg":"geom","description":"a geometry"}],
6+
"examples": [ { "expression":"point_on_surface($geometry)", "returns":"a point geometry"}]
7+
}

src/core/qgsexpression.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,14 @@ static QVariant fcnCentroid( const QVariantList& values, const QgsExpressionCont
17361736
delete geom;
17371737
return result;
17381738
}
1739+
static QVariant fcnPointOnSurface( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
1740+
{
1741+
QgsGeometry fGeom = getGeometry( values.at( 0 ), parent );
1742+
QgsGeometry* geom = fGeom.pointOnSurface();
1743+
QVariant result = geom ? QVariant::fromValue( *geom ) : QVariant();
1744+
delete geom;
1745+
return result;
1746+
}
17391747
static QVariant fcnConvexHull( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
17401748
{
17411749
QgsGeometry fGeom = getGeometry( values.at( 0 ), parent );
@@ -2548,6 +2556,7 @@ const QList<QgsExpression::Function*>& QgsExpression::Functions()
25482556
<< new StaticFunction( "translate", 3, fcnTranslate, "GeometryGroup" )
25492557
<< new StaticFunction( "buffer", -1, fcnBuffer, "GeometryGroup" )
25502558
<< new StaticFunction( "centroid", 1, fcnCentroid, "GeometryGroup" )
2559+
<< new StaticFunction( "point_on_surface", 1, fcnPointOnSurface, "GeometryGroup" )
25512560
<< new StaticFunction( "reverse", 1, fcnReverse, "GeometryGroup" )
25522561
<< new StaticFunction( "bounds", 1, fcnBounds, "GeometryGroup" )
25532562
<< new StaticFunction( "num_points", 1, fcnGeomNumPoints, "GeometryGroup" )

tests/src/core/testqgsexpression.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,18 @@ class TestQgsExpression: public QObject
449449
QTest::newRow( "reverse point" ) << "reverse(geom_from_wkt('POINT(1 2)'))" << false << QVariant();
450450
QTest::newRow( "reverse polygon" ) << "reverse(geom_from_wkt('POLYGON((-1 -1, 4 0, 4 2, 0 2, -1 -1))'))" << false << QVariant();
451451
QTest::newRow( "reverse line" ) << "geom_to_wkt(reverse(geom_from_wkt('LINESTRING(0 0, 1 1, 2 2)')))" << false << QVariant( "LineString (2 2, 1 1, 0 0)" );
452+
QTest::newRow( "centroid polygon" ) << "geom_to_wkt(centroid( geomFromWKT('POLYGON((0 0,0 9,9 0,0 0))')))" << false << QVariant( "Point (3 3)" );
453+
QTest::newRow( "centroid multi polygon" ) << "geom_to_wkt(centroid( geomFromWKT('MULTIPOLYGON(((0 0,0 1,1 1,1 0,0 0)),((2 0,2 1,3 1,3 0,2 0)))') ))" << false << QVariant( "Point (1.5 0.5)" );
454+
QTest::newRow( "centroid point" ) << "geom_to_wkt(centroid( geomFromWKT('POINT (1.5 0.5)') ))" << false << QVariant( "Point (1.5 0.5)" );
455+
QTest::newRow( "centroid line" ) << "geom_to_wkt(centroid( geomFromWKT('LINESTRING (-1 2, 9 12)') ))" << false << QVariant( "Point (4 7)" );
456+
QTest::newRow( "centroid not geom" ) << "centroid('g')" << true << QVariant();
457+
QTest::newRow( "centroid null" ) << "centroid(NULL)" << false << QVariant();
458+
QTest::newRow( "point on surface polygon" ) << "geom_to_wkt(point_on_surface( geomFromWKT('POLYGON((0 0,0 9,9 0,0 0))')))" << false << QVariant( "Point (2.25 4.5)" );
459+
QTest::newRow( "point on surface multi polygon" ) << "geom_to_wkt(point_on_surface( geomFromWKT('MULTIPOLYGON(((0 0,0 1,1 1,1 0,0 0)),((2 0,2 1,3 1,3 0,2 0)))') ))" << false << QVariant( "Point (0.5 0.5)" );
460+
QTest::newRow( "point on surface point" ) << "geom_to_wkt(point_on_surface( geomFromWKT('POINT (1.5 0.5)') ))" << false << QVariant( "Point (1.5 0.5)" );
461+
QTest::newRow( "point on surface line" ) << "geom_to_wkt(point_on_surface( geomFromWKT('LINESTRING (-1 2, 9 12)') ))" << false << QVariant( "Point (-1 2)" );
462+
QTest::newRow( "point on surface not geom" ) << "point_on_surface('g')" << true << QVariant();
463+
QTest::newRow( "point on surface null" ) << "point_on_surface(NULL)" << false << QVariant();
452464
QTest::newRow( "make_point" ) << "geom_to_wkt(make_point(2.2,4.4))" << false << QVariant( "Point (2.2 4.4)" );
453465
QTest::newRow( "make_point z" ) << "geom_to_wkt(make_point(2.2,4.4,5.5))" << false << QVariant( "PointZ (2.2 4.4 5.5)" );
454466
QTest::newRow( "make_point zm" ) << "geom_to_wkt(make_point(2.2,4.4,5.5,6.6))" << false << QVariant( "PointZM (2.2 4.4 5.5 6.6)" );
@@ -1459,10 +1471,6 @@ class TestQgsExpression: public QObject
14591471
geom = QgsGeometry::fromPolygon( polygon );
14601472
QTest::newRow( "symDifference" ) << "symDifference( $geometry, geomFromWKT('POLYGON((0 0, 0 10, 10 0, 0 0))') )" << ( void* ) geom << false << true << ( void* ) QgsGeometry::fromWkt( "MULTIPOLYGON(((5 5,0 0,0 10,5 5)),((5 5,10 10,10 0,5 5)))" );
14611473

1462-
geom = QgsGeometry::fromPolygon( polygon );
1463-
QTest::newRow( "centroid polygon" ) << "centroid( $geometry )" << ( void* ) geom << false << true << ( void* ) geom->centroid();
1464-
geom = QgsGeometry::fromPolygon( polygon );
1465-
QTest::newRow( "centroid multi polygon" ) << "centroid( geomFromWKT('MULTIPOLYGON(((0 0,0 1,1 1,1 0,0 0)),((2 0,2 1,3 1,3 0,2 0)))') )" << ( void* ) geom << false << false << ( void* ) QgsGeometry::fromWkt( "POINT (1.5 0.5)" );
14661474
geom = QgsGeometry::fromPolygon( polygon );
14671475
QTest::newRow( "convexHull simple" ) << "convexHull( $geometry )" << ( void* ) geom << false << true << ( void* ) geom->convexHull();
14681476
geom = QgsGeometry::fromPolygon( polygon );

0 commit comments

Comments
 (0)