QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
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
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
25
26QString QgsRotateFeaturesAlgorithm::name() const
27{
28 return u"rotatefeatures"_s;
29}
30
31QString QgsRotateFeaturesAlgorithm::displayName() const
32{
33 return QObject::tr( "Rotate" );
34}
35
36QStringList QgsRotateFeaturesAlgorithm::tags() const
37{
38 return QObject::tr( "rotate,around,center,point" ).split( ',' );
39}
40
41QString QgsRotateFeaturesAlgorithm::group() const
42{
43 return QObject::tr( "Vector geometry" );
44}
45
46QString QgsRotateFeaturesAlgorithm::groupId() const
47{
48 return u"vectorgeometry"_s;
49}
50
51QString QgsRotateFeaturesAlgorithm::outputName() const
52{
53 return QObject::tr( "Rotated" );
54}
55
56QString QgsRotateFeaturesAlgorithm::shortHelpString() const
57{
58 return QObject::tr( "This algorithm rotates feature geometries by the specified angle clockwise." )
59 + u"\n\n"_s
60 + QObject::tr( "Optionally, the rotation can occur around a preset point. If not set the rotation occurs around each feature's centroid." );
61}
62
63QString QgsRotateFeaturesAlgorithm::shortDescription() const
64{
65 return QObject::tr( "Rotates feature geometries by a specified angle clockwise." );
66}
67
68QgsRotateFeaturesAlgorithm *QgsRotateFeaturesAlgorithm::createInstance() const
69{
70 return new QgsRotateFeaturesAlgorithm();
71}
72
73void QgsRotateFeaturesAlgorithm::initParameters( const QVariantMap & )
74{
75 auto rotation = std::make_unique<QgsProcessingParameterNumber>( u"ANGLE"_s, QObject::tr( "Rotation (degrees clockwise)" ), Qgis::ProcessingNumberParameterType::Double, 0.0 );
76 rotation->setIsDynamic( true );
77 rotation->setDynamicPropertyDefinition( QgsPropertyDefinition( u"ANGLE"_s, QObject::tr( "Rotation (degrees clockwise)" ), QgsPropertyDefinition::Rotation ) );
78 rotation->setDynamicLayerParameterName( u"INPUT"_s );
79 addParameter( rotation.release() );
80
81 auto anchor = std::make_unique<QgsProcessingParameterPoint>( u"ANCHOR"_s, QObject::tr( "Rotation anchor point" ), QVariant(), true );
82 addParameter( anchor.release() );
83}
84
85bool QgsRotateFeaturesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
86{
87 mAngle = parameterAsDouble( parameters, u"ANGLE"_s, context );
88 mDynamicAngle = QgsProcessingParameters::isDynamic( parameters, u"ANGLE"_s );
89 if ( mDynamicAngle )
90 mAngleProperty = parameters.value( u"ANGLE"_s ).value<QgsProperty>();
91
92 mUseAnchor = parameters.value( u"ANCHOR"_s ).isValid();
93 if ( mUseAnchor )
94 {
95 mAnchor = parameterAsPoint( parameters, u"ANCHOR"_s, context );
96 mAnchorCrs = parameterAsPointCrs( parameters, u"ANCHOR"_s, context );
97 }
98
99 return true;
100}
101
102QgsFeatureList QgsRotateFeaturesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
103{
104 if ( mUseAnchor && !mTransformedAnchor )
105 {
106 mTransformedAnchor = true;
107 if ( mAnchorCrs != sourceCrs() )
108 {
109 const QgsCoordinateTransform ct( mAnchorCrs, sourceCrs(), context.transformContext() );
110 try
111 {
112 mAnchor = ct.transform( mAnchor );
113 }
114 catch ( QgsCsException & )
115 {
116 throw QgsProcessingException( QObject::tr( "Could not transform anchor point to destination CRS" ) );
117 }
118 }
119 }
120
121 QgsFeature f = feature;
122 if ( f.hasGeometry() )
123 {
124 QgsGeometry geometry = f.geometry();
125
126 double angle = mAngle;
127 if ( mDynamicAngle )
128 angle = mAngleProperty.valueAsDouble( context.expressionContext(), angle );
129
130 if ( mUseAnchor )
131 {
132 geometry.rotate( angle, mAnchor );
133 f.setGeometry( geometry );
134 }
135 else
136 {
137 const QgsGeometry centroid = geometry.centroid();
138 if ( !centroid.isNull() )
139 {
140 geometry.rotate( angle, centroid.asPoint() );
141 f.setGeometry( geometry );
142 }
143 else
144 {
145 feedback->reportError( QObject::tr( "Could not calculate centroid for feature %1: %2" ).arg( feature.id() ).arg( centroid.lastError() ) );
146 }
147 }
148 }
149 return QgsFeatureList() << f;
150}
151
152
@ Double
Double/float values.
Definition qgis.h:3875
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:60
QgsFeatureId id
Definition qgsfeature.h:68
QgsGeometry geometry
Definition qgsfeature.h:71
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:47
@ Rotation
Rotation (value between 0-360 degrees).
Definition qgsproperty.h:60
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