Skip to content

Commit

Permalink
[FEATURE] Add translate expression function
Browse files Browse the repository at this point in the history
Funded by
 * Regional Council of Picardy
 * ADUGA
 * Ville de Nyon
 * Wetu GIT cc
  • Loading branch information
m-kuhn committed Dec 8, 2015
1 parent f961ece commit 4b3083d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
10 changes: 10 additions & 0 deletions 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"}]
}
9 changes: 9 additions & 0 deletions src/core/qgsexpression.cpp
Expand Up @@ -1710,6 +1710,14 @@ static QVariant fcnBuffer( const QVariantList& values, const QgsExpressionContex
delete geom; delete geom;
return result; 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 ) static QVariant fcnCentroid( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
{ {
QgsGeometry fGeom = getGeometry( values.at( 0 ), parent ); QgsGeometry fGeom = getGeometry( values.at( 0 ), parent );
Expand Down Expand Up @@ -2495,6 +2503,7 @@ const QList<QgsExpression::Function*>& QgsExpression::Functions()
<< new StaticFunction( "contains", 2, fcnContains, "GeometryGroup" ) << new StaticFunction( "contains", 2, fcnContains, "GeometryGroup" )
<< new StaticFunction( "overlaps", 2, fcnOverlaps, "GeometryGroup" ) << new StaticFunction( "overlaps", 2, fcnOverlaps, "GeometryGroup" )
<< new StaticFunction( "within", 2, fcnWithin, "GeometryGroup" ) << new StaticFunction( "within", 2, fcnWithin, "GeometryGroup" )
<< new StaticFunction( "translate", 3, fcnTranslate, "GeometryGroup" )
<< new StaticFunction( "buffer", -1, fcnBuffer, "GeometryGroup" ) << new StaticFunction( "buffer", -1, fcnBuffer, "GeometryGroup" )
<< new StaticFunction( "centroid", 1, fcnCentroid, "GeometryGroup" ) << new StaticFunction( "centroid", 1, fcnCentroid, "GeometryGroup" )
<< new StaticFunction( "reverse", 1, fcnReverse, "GeometryGroup" ) << new StaticFunction( "reverse", 1, fcnReverse, "GeometryGroup" )
Expand Down
11 changes: 10 additions & 1 deletion tests/src/core/testqgsexpression.cpp
Expand Up @@ -1424,7 +1424,9 @@ class TestQgsExpression: public QObject
QTest::addColumn<bool>( "needGeom" ); QTest::addColumn<bool>( "needGeom" );
QTest::addColumn<void*>( "resultptr" ); QTest::addColumn<void*>( "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 ); polygon_ring << QgsPoint( 0, 0 ) << QgsPoint( 10, 10 ) << QgsPoint( 10, 0 ) << QgsPoint( 0, 0 );
QgsPolygon polygon; QgsPolygon polygon;
polygon << polygon_ring; polygon << polygon_ring;
Expand Down Expand Up @@ -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))" ); 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 ); geom = QgsGeometry::fromPolygon( polygon );
QTest::newRow( "bounds" ) << "bounds( $geometry )" << ( void* ) geom << false << true << ( void* ) QgsGeometry::fromRect( geom->boundingBox() ); 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() void eval_geometry_method()
Expand Down

0 comments on commit 4b3083d

Please sign in to comment.