Skip to content

Commit 4b3083d

Browse files
committedDec 8, 2015
[FEATURE] Add translate expression function
Funded by * Regional Council of Picardy * ADUGA * Ville de Nyon * Wetu GIT cc
1 parent f961ece commit 4b3083d

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed
 
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "translate",
3+
"type": "function",
4+
"description": "Returns a translated version of a geometry. Calculations are in the Spatial Reference System of this geometry.",
5+
"arguments": [ {"arg":"geom","description":"a geometry"},
6+
{"arg":"dx","description":"delta x"},
7+
{"arg":"dy","description":"delta y"}
8+
],
9+
"examples": [ { "expression":"translate($geometry, 5, 10)", "returns":"a geometry of the same type like the original one"}]
10+
}

‎src/core/qgsexpression.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,14 @@ static QVariant fcnBuffer( const QVariantList& values, const QgsExpressionContex
17101710
delete geom;
17111711
return result;
17121712
}
1713+
static QVariant fcnTranslate( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
1714+
{
1715+
QgsGeometry fGeom = getGeometry( values.at( 0 ), parent );
1716+
double dx = getDoubleValue( values.at( 1 ), parent );
1717+
double dy = getDoubleValue( values.at( 2 ), parent );
1718+
fGeom.translate( dx, dy );
1719+
return QVariant::fromValue( fGeom );
1720+
}
17131721
static QVariant fcnCentroid( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
17141722
{
17151723
QgsGeometry fGeom = getGeometry( values.at( 0 ), parent );
@@ -2495,6 +2503,7 @@ const QList<QgsExpression::Function*>& QgsExpression::Functions()
24952503
<< new StaticFunction( "contains", 2, fcnContains, "GeometryGroup" )
24962504
<< new StaticFunction( "overlaps", 2, fcnOverlaps, "GeometryGroup" )
24972505
<< new StaticFunction( "within", 2, fcnWithin, "GeometryGroup" )
2506+
<< new StaticFunction( "translate", 3, fcnTranslate, "GeometryGroup" )
24982507
<< new StaticFunction( "buffer", -1, fcnBuffer, "GeometryGroup" )
24992508
<< new StaticFunction( "centroid", 1, fcnCentroid, "GeometryGroup" )
25002509
<< new StaticFunction( "reverse", 1, fcnReverse, "GeometryGroup" )

‎tests/src/core/testqgsexpression.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,9 @@ class TestQgsExpression: public QObject
14241424
QTest::addColumn<bool>( "needGeom" );
14251425
QTest::addColumn<void*>( "resultptr" );
14261426

1427-
QgsPolyline polygon_ring;
1427+
QgsPoint point( 0, 0 );
1428+
QgsPolyline line, polygon_ring;
1429+
line << QgsPoint( 0, 0 ) << QgsPoint( 10, 10 );
14281430
polygon_ring << QgsPoint( 0, 0 ) << QgsPoint( 10, 10 ) << QgsPoint( 10, 0 ) << QgsPoint( 0, 0 );
14291431
QgsPolygon polygon;
14301432
polygon << polygon_ring;
@@ -1460,6 +1462,13 @@ class TestQgsExpression: public QObject
14601462
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))" );
14611463
geom = QgsGeometry::fromPolygon( polygon );
14621464
QTest::newRow( "bounds" ) << "bounds( $geometry )" << ( void* ) geom << false << true << ( void* ) QgsGeometry::fromRect( geom->boundingBox() );
1465+
1466+
geom = QgsGeometry::fromPolygon( polygon );
1467+
QTest::newRow( "translate" ) << "translate( $geometry, 1, 2)" << ( void* ) geom << false << true << ( void* ) QgsGeometry::fromWkt( "POLYGON ((1 2,11 12,11 2,1 2))" );
1468+
geom = QgsGeometry::fromPolyline( line );
1469+
QTest::newRow( "translate" ) << "translate( $geometry, -1, 2)" << ( void* ) geom << false << true << ( void* ) QgsGeometry::fromWkt( "LINESTRING (-1 2, 9 12)" );
1470+
geom = QgsGeometry::fromPoint( point );
1471+
QTest::newRow( "translate" ) << "translate( $geometry, 1, -2)" << ( void* ) geom << false << true << ( void* ) QgsGeometry::fromWkt( "POINT(1 -2)" );
14631472
}
14641473

14651474
void eval_geometry_method()

0 commit comments

Comments
 (0)
Please sign in to comment.