Skip to content
Permalink
Browse files

[FEATURE] Add translate expression function

Funded by
 * Regional Council of Picardy
 * ADUGA
 * Ville de Nyon
 * Wetu GIT cc
  • Loading branch information
m-kuhn committed Dec 7, 2015
1 parent f961ece commit 4b3083d9a7163bdd19785289e6597b51b396a71f
Showing with 29 additions and 1 deletion.
  1. +10 −0 resources/function_help/json/translate
  2. +9 −0 src/core/qgsexpression.cpp
  3. +10 −1 tests/src/core/testqgsexpression.cpp
@@ -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"}]
}
@@ -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::Function*>& 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" )
@@ -1424,7 +1424,9 @@ class TestQgsExpression: public QObject
QTest::addColumn<bool>( "needGeom" );
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 );
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()

0 comments on commit 4b3083d

Please sign in to comment.
You can’t perform that action at this time.