Skip to content
Permalink
Browse files

[FEATURE] rotate() expression function

Allows rotation of geometries around a point
  • Loading branch information
raymondnijssen authored and nyalldawson committed Nov 26, 2019
1 parent 5792966 commit 933f1cac724aa70472a3857db6a94f77abda4e53
@@ -0,0 +1,10 @@
{
"name": "rotate",
"type": "function",
"description": "Returns a rotated version of a geometry. Calculations are in the Spatial Reference System of this geometry.",
"arguments": [ {"arg":"geom","description":"a geometry"},
{"arg":"rotation","description":"clockwise rotation in degrees"},
{"arg":"point","description":"rotation center"}
],
"examples": [ { "expression":"rotate($geometry, 45, centroid($geometry))", "returns":"a rotated geometry of the same type as the original one"}]
}
@@ -3400,6 +3400,38 @@ static QVariant fcnTranslate( const QVariantList &values, const QgsExpressionCon
fGeom.translate( dx, dy );
return QVariant::fromValue( fGeom );
}

static QVariant fcnRotate( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
double rotation = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
QgsGeometry center = QgsExpressionUtils::getGeometry( values.at( 2 ), parent );

if ( center.isNull() || center.type() != QgsWkbTypes::PointGeometry )
{
parent->setEvalErrorString( QObject::tr( "Function 'rotate' requires a point value for the center" ) );
return QVariant();
}

QgsPointXY pt = center.asPoint();
if ( center.isMultipart() )
{
QgsMultiPointXY multiPoint = center.asMultiPoint();
if ( multiPoint.count() == 1 )
{
pt = multiPoint[0];
}
else
{
parent->setEvalErrorString( QObject::tr( "Function 'rotate' requires a point value for the center" ) );
return QVariant();
}
}

fGeom.rotate( rotation, pt );
return QVariant::fromValue( fGeom );
}

static QVariant fcnCentroid( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
@@ -5664,6 +5696,10 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
<< QgsExpressionFunction::Parameter( QStringLiteral( "dx" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "dy" ) ),
fcnTranslate, QStringLiteral( "GeometryGroup" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "rotate" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geom" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "rotation" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "center" ) ),
fcnRotate, QStringLiteral( "GeometryGroup" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "buffer" ), -1, fcnBuffer, QStringLiteral( "GeometryGroup" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "force_rhr" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geometry" ) ),
fcnForceRHR, QStringLiteral( "GeometryGroup" ) )
@@ -2947,6 +2947,15 @@ class TestQgsExpression: public QObject
QTest::newRow( "translate" ) << "translate( $geometry, -1, 2)" << geom << false << true << QgsGeometry::fromWkt( QStringLiteral( "LINESTRING (-1 2, 9 12)" ) );
geom = QgsGeometry::fromPointXY( point );
QTest::newRow( "translate" ) << "translate( $geometry, 1, -2)" << geom << false << true << QgsGeometry::fromWkt( QStringLiteral( "POINT(1 -2)" ) );

geom = QgsGeometry::fromPolygonXY( polygon );
QTest::newRow( "rotate" ) << "rotate( $geometry, 90, geomFromWKT( 'POINT(10 10)' ) )" << geom << false << true << QgsGeometry::fromWkt( QStringLiteral( "Polygon ((0 20, 10 10, 0 10, 0 20))" ) );
geom = QgsGeometry::fromPolylineXY( line );
QTest::newRow( "rotate" ) << "rotate( $geometry, -90, geomFromWKT( 'POINT(5 5)' ) )" << geom << false << true << QgsGeometry::fromWkt( QStringLiteral( "LineString (10 0, 0 10)'" ) );
geom = QgsGeometry::fromPointXY( point );
QTest::newRow( "rotate" ) << "rotate( $geometry, 180, geomFromWKT( 'POINT(-5 -3)' ) )" << geom << false << true << QgsGeometry::fromWkt( QStringLiteral( "Point (-10 -6)" ) );
QTest::newRow( "rotate" ) << "rotate( $geometry, 180, geomFromWKT( 'MULTIPOINT((-5 -3))' ) )" << geom << false << true << QgsGeometry::fromWkt( QStringLiteral( "Point (-10 -6)" ) );
QTest::newRow( "rotate" ) << "rotate( $geometry, 180, geomFromWKT( 'MULTIPOINT((-5 -3, 7 7, 4 4))' ) )" << geom << true << true << QgsVariant();
}

void eval_geometry_method()

0 comments on commit 933f1ca

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