From 4b3083d9a7163bdd19785289e6597b51b396a71f Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Mon, 7 Dec 2015 17:48:13 +0100 Subject: [PATCH] [FEATURE] Add translate expression function Funded by * Regional Council of Picardy * ADUGA * Ville de Nyon * Wetu GIT cc --- resources/function_help/json/translate | 10 ++++++++++ src/core/qgsexpression.cpp | 9 +++++++++ tests/src/core/testqgsexpression.cpp | 11 ++++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 resources/function_help/json/translate diff --git a/resources/function_help/json/translate b/resources/function_help/json/translate new file mode 100644 index 00000000000..ec2bbd18f9a --- /dev/null +++ b/resources/function_help/json/translate @@ -0,0 +1,10 @@ +{ + "name": "translate", + "type": "function", + "description": "Returns a translated version of a geometry. Calculations are in the Spatial Reference System of this geometry.", + "arguments": [ {"arg":"geom","description":"a geometry"}, + {"arg":"dx","description":"delta x"}, + {"arg":"dy","description":"delta y"} + ], + "examples": [ { "expression":"translate($geometry, 5, 10)", "returns":"a geometry of the same type like the original one"}] +} diff --git a/src/core/qgsexpression.cpp b/src/core/qgsexpression.cpp index 7ad71b9992b..baaa97a06fb 100644 --- a/src/core/qgsexpression.cpp +++ b/src/core/qgsexpression.cpp @@ -1710,6 +1710,14 @@ static QVariant fcnBuffer( const QVariantList& values, const QgsExpressionContex delete geom; return result; } +static QVariant fcnTranslate( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent ) +{ + QgsGeometry fGeom = getGeometry( values.at( 0 ), parent ); + double dx = getDoubleValue( values.at( 1 ), parent ); + double dy = getDoubleValue( values.at( 2 ), parent ); + fGeom.translate( dx, dy ); + return QVariant::fromValue( fGeom ); +} static QVariant fcnCentroid( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent ) { QgsGeometry fGeom = getGeometry( values.at( 0 ), parent ); @@ -2495,6 +2503,7 @@ const QList& QgsExpression::Functions() << new StaticFunction( "contains", 2, fcnContains, "GeometryGroup" ) << new StaticFunction( "overlaps", 2, fcnOverlaps, "GeometryGroup" ) << new StaticFunction( "within", 2, fcnWithin, "GeometryGroup" ) + << new StaticFunction( "translate", 3, fcnTranslate, "GeometryGroup" ) << new StaticFunction( "buffer", -1, fcnBuffer, "GeometryGroup" ) << new StaticFunction( "centroid", 1, fcnCentroid, "GeometryGroup" ) << new StaticFunction( "reverse", 1, fcnReverse, "GeometryGroup" ) diff --git a/tests/src/core/testqgsexpression.cpp b/tests/src/core/testqgsexpression.cpp index b38ef68e45a..11857377edb 100644 --- a/tests/src/core/testqgsexpression.cpp +++ b/tests/src/core/testqgsexpression.cpp @@ -1424,7 +1424,9 @@ class TestQgsExpression: public QObject QTest::addColumn( "needGeom" ); QTest::addColumn( "resultptr" ); - QgsPolyline polygon_ring; + QgsPoint point( 0, 0 ); + QgsPolyline line, polygon_ring; + line << QgsPoint( 0, 0 ) << QgsPoint( 10, 10 ); polygon_ring << QgsPoint( 0, 0 ) << QgsPoint( 10, 10 ) << QgsPoint( 10, 0 ) << QgsPoint( 0, 0 ); QgsPolygon polygon; polygon << polygon_ring; @@ -1460,6 +1462,13 @@ class TestQgsExpression: public QObject QTest::newRow( "convexHull multi" ) << "convexHull( geomFromWKT('GEOMETRYCOLLECTION(POINT(0 1), POINT(0 0), POINT(1 0), POINT(1 1))') )" << ( void* ) geom << false << false << ( void* ) QgsGeometry::fromWkt( "POLYGON ((0 0,0 1,1 1,1 0,0 0))" ); geom = QgsGeometry::fromPolygon( polygon ); QTest::newRow( "bounds" ) << "bounds( $geometry )" << ( void* ) geom << false << true << ( void* ) QgsGeometry::fromRect( geom->boundingBox() ); + + geom = QgsGeometry::fromPolygon( polygon ); + QTest::newRow( "translate" ) << "translate( $geometry, 1, 2)" << ( void* ) geom << false << true << ( void* ) QgsGeometry::fromWkt( "POLYGON ((1 2,11 12,11 2,1 2))" ); + geom = QgsGeometry::fromPolyline( line ); + QTest::newRow( "translate" ) << "translate( $geometry, -1, 2)" << ( void* ) geom << false << true << ( void* ) QgsGeometry::fromWkt( "LINESTRING (-1 2, 9 12)" ); + geom = QgsGeometry::fromPoint( point ); + QTest::newRow( "translate" ) << "translate( $geometry, 1, -2)" << ( void* ) geom << false << true << ( void* ) QgsGeometry::fromWkt( "POINT(1 -2)" ); } void eval_geometry_method()