Skip to content
Permalink
Browse files

Make rotation centre point optional, and use geometry bounding box if…

… not specified
  • Loading branch information
nyalldawson committed Jan 2, 2020
1 parent 933f1ca commit 5bb63599e6890d91078ac6f4c1baf31154998b4b
Showing with 16 additions and 7 deletions.
  1. +16 −7 src/core/expression/qgsexpressionfunction.cpp
@@ -3404,17 +3404,22 @@ static QVariant fcnTranslate( const QVariantList &values, const QgsExpressionCon
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 );
const double rotation = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
const QgsGeometry center = values.at( 2 ).isValid() ? QgsExpressionUtils::getGeometry( values.at( 2 ), parent )
: QgsGeometry();

if ( center.isNull() || center.type() != QgsWkbTypes::PointGeometry )
QgsPointXY pt;
if ( center.isNull() )
{
// if center wasn't specified, use bounding box centroid
pt = fGeom.boundingBox().center();
}
else if ( 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() )
else if ( center.isMultipart() )
{
QgsMultiPointXY multiPoint = center.asMultiPoint();
if ( multiPoint.count() == 1 )
@@ -3427,6 +3432,10 @@ static QVariant fcnRotate( const QVariantList &values, const QgsExpressionContex
return QVariant();
}
}
else
{
pt = center.asPoint();
}

fGeom.rotate( rotation, pt );
return QVariant::fromValue( fGeom );
@@ -5698,7 +5707,7 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
fcnTranslate, QStringLiteral( "GeometryGroup" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "rotate" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geom" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "rotation" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "center" ) ),
<< QgsExpressionFunction::Parameter( QStringLiteral( "center" ), true ),
fcnRotate, QStringLiteral( "GeometryGroup" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "buffer" ), -1, fcnBuffer, QStringLiteral( "GeometryGroup" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "force_rhr" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geometry" ) ),

0 comments on commit 5bb6359

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