QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsalgorithmrotate.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmrotate.cpp
3 ---------------------
4 begin : March 2018
5 copyright : (C) 2018 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsalgorithmrotate.h"
19
21
22QString QgsRotateFeaturesAlgorithm::name() const
23{
24 return QStringLiteral( "rotatefeatures" );
25}
26
27QString QgsRotateFeaturesAlgorithm::displayName() const
28{
29 return QObject::tr( "Rotate" );
30}
31
32QStringList QgsRotateFeaturesAlgorithm::tags() const
33{
34 return QObject::tr( "rotate,around,center,point" ).split( ',' );
35}
36
37QString QgsRotateFeaturesAlgorithm::group() const
38{
39 return QObject::tr( "Vector geometry" );
40}
41
42QString QgsRotateFeaturesAlgorithm::groupId() const
43{
44 return QStringLiteral( "vectorgeometry" );
45}
46
47QString QgsRotateFeaturesAlgorithm::outputName() const
48{
49 return QObject::tr( "Rotated" );
50}
51
52QString QgsRotateFeaturesAlgorithm::shortHelpString() const
53{
54 return QObject::tr( "This algorithm rotates feature geometries by the specified angle clockwise." )
55 + QStringLiteral( "\n\n" )
56 + QObject::tr( "Optionally, the rotation can occur around a preset point. If not set the rotation occurs around each feature's centroid." );
57}
58
59QString QgsRotateFeaturesAlgorithm::shortDescription() const
60{
61 return QObject::tr( "Rotates feature geometries by a specified angle clockwise." );
62}
63
64QgsRotateFeaturesAlgorithm *QgsRotateFeaturesAlgorithm::createInstance() const
65{
66 return new QgsRotateFeaturesAlgorithm();
67}
68
69void QgsRotateFeaturesAlgorithm::initParameters( const QVariantMap & )
70{
71 auto rotation = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "ANGLE" ), QObject::tr( "Rotation (degrees clockwise)" ), Qgis::ProcessingNumberParameterType::Double, 0.0 );
72 rotation->setIsDynamic( true );
73 rotation->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "ANGLE" ), QObject::tr( "Rotation (degrees clockwise)" ), QgsPropertyDefinition::Rotation ) );
74 rotation->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
75 addParameter( rotation.release() );
76
77 auto anchor = std::make_unique<QgsProcessingParameterPoint>( QStringLiteral( "ANCHOR" ), QObject::tr( "Rotation anchor point" ), QVariant(), true );
78 addParameter( anchor.release() );
79}
80
81bool QgsRotateFeaturesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
82{
83 mAngle = parameterAsDouble( parameters, QStringLiteral( "ANGLE" ), context );
84 mDynamicAngle = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "ANGLE" ) );
85 if ( mDynamicAngle )
86 mAngleProperty = parameters.value( QStringLiteral( "ANGLE" ) ).value<QgsProperty>();
87
88 mUseAnchor = parameters.value( QStringLiteral( "ANCHOR" ) ).isValid();
89 if ( mUseAnchor )
90 {
91 mAnchor = parameterAsPoint( parameters, QStringLiteral( "ANCHOR" ), context );
92 mAnchorCrs = parameterAsPointCrs( parameters, QStringLiteral( "ANCHOR" ), context );
93 }
94
95 return true;
96}
97
98QgsFeatureList QgsRotateFeaturesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
99{
100 if ( mUseAnchor && !mTransformedAnchor )
101 {
102 mTransformedAnchor = true;
103 if ( mAnchorCrs != sourceCrs() )
104 {
105 const QgsCoordinateTransform ct( mAnchorCrs, sourceCrs(), context.transformContext() );
106 try
107 {
108 mAnchor = ct.transform( mAnchor );
109 }
110 catch ( QgsCsException & )
111 {
112 throw QgsProcessingException( QObject::tr( "Could not transform anchor point to destination CRS" ) );
113 }
114 }
115 }
116
117 QgsFeature f = feature;
118 if ( f.hasGeometry() )
119 {
120 QgsGeometry geometry = f.geometry();
121
122 double angle = mAngle;
123 if ( mDynamicAngle )
124 angle = mAngleProperty.valueAsDouble( context.expressionContext(), angle );
125
126 if ( mUseAnchor )
127 {
128 geometry.rotate( angle, mAnchor );
129 f.setGeometry( geometry );
130 }
131 else
132 {
133 const QgsGeometry centroid = geometry.centroid();
134 if ( !centroid.isNull() )
135 {
136 geometry.rotate( angle, centroid.asPoint() );
137 f.setGeometry( geometry );
138 }
139 else
140 {
141 feedback->reportError( QObject::tr( "Could not calculate centroid for feature %1: %2" ).arg( feature.id() ).arg( centroid.lastError() ) );
142 }
143 }
144 }
145 return QgsFeatureList() << f;
146}
147
148
Handles coordinate transforms between two coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsFeatureId id
Definition qgsfeature.h:66
QgsGeometry geometry
Definition qgsfeature.h:69
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
A geometry is the spatial representation of a feature.
QString lastError() const
Returns an error string referring to the last error encountered either when this geometry was created...
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
QgsGeometry centroid() const
Returns the center of mass of a geometry.
Qgis::GeometryOperationResult rotate(double rotation, const QgsPointXY &center)
Rotate this geometry around the Z axis.
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
static bool isDynamic(const QVariantMap &parameters, const QString &name)
Returns true if the parameter with matching name is a dynamic parameter, and must be evaluated once f...
Definition for a property.
Definition qgsproperty.h:45
@ Rotation
Rotation (value between 0-360 degrees)
Definition qgsproperty.h:58
A store for object properties.
QVariant value(const QgsExpressionContext &context, const QVariant &defaultValue=QVariant(), bool *ok=nullptr) const
Calculates the current value of the property, including any transforms which are set for the property...
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
QList< QgsFeature > QgsFeatureList