QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmrectanglesovalsdiamonds.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmrectanglesovalsdiamonds.cpp
3 ---------------------
4 begin : January 2020
5 copyright : (C) 2020 by Alexander Bruy
6 email : alexander dot bruy 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
19
20#include "qgsapplication.h"
21#include "qgslinestring.h"
22#include "qgspolygon.h"
23
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
29
30QString QgsRectanglesOvalsDiamondsAlgorithm::name() const
31{
32 return u"rectanglesovalsdiamonds"_s;
33}
34
35QString QgsRectanglesOvalsDiamondsAlgorithm::displayName() const
36{
37 return QObject::tr( "Rectangles, ovals, diamonds" );
38}
39
40QStringList QgsRectanglesOvalsDiamondsAlgorithm::tags() const
41{
42 return QObject::tr( "buffer,grow,fixed,variable,distance,rectangle,oval,diamond,point" ).split( ',' );
43}
44
45QString QgsRectanglesOvalsDiamondsAlgorithm::group() const
46{
47 return QObject::tr( "Vector geometry" );
48}
49
50QString QgsRectanglesOvalsDiamondsAlgorithm::groupId() const
51{
52 return u"vectorgeometry"_s;
53}
54
55QString QgsRectanglesOvalsDiamondsAlgorithm::shortHelpString() const
56{
57 return QObject::tr( "This algorithm creates rectangle, oval or diamond-shaped polygons from the input point layer using "
58 "specified width, height and (optional) rotation values. Multipart inputs should be promoted "
59 "to singleparts first." );
60}
61
62QString QgsRectanglesOvalsDiamondsAlgorithm::shortDescription() const
63{
64 return QObject::tr( "Creates rectangle, oval or diamond-shaped polygons from an input point layer." );
65}
66
67QIcon QgsRectanglesOvalsDiamondsAlgorithm::icon() const
68{
69 return QgsApplication::getThemeIcon( u"/algorithms/mAlgorithmRectanglesOvalsDiamonds.svg"_s );
70}
71
72QString QgsRectanglesOvalsDiamondsAlgorithm::svgIconPath() const
73{
74 return QgsApplication::iconPath( u"/algorithms/mAlgorithmRectanglesOvalsDiamonds.svg"_s );
75}
76
77QString QgsRectanglesOvalsDiamondsAlgorithm::outputName() const
78{
79 return QObject::tr( "Polygon" );
80}
81
82QList<int> QgsRectanglesOvalsDiamondsAlgorithm::inputLayerTypes() const
83{
84 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPoint );
85}
86
87Qgis::ProcessingSourceType QgsRectanglesOvalsDiamondsAlgorithm::outputLayerType() const
88{
90}
91
92Qgis::WkbType QgsRectanglesOvalsDiamondsAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
93{
95 if ( QgsWkbTypes::hasM( inputWkbType ) )
96 {
97 outputWkbType = QgsWkbTypes::addM( outputWkbType );
98 }
99 if ( QgsWkbTypes::hasZ( inputWkbType ) )
100 {
101 outputWkbType = QgsWkbTypes::addZ( outputWkbType );
102 }
103
104 return outputWkbType;
105}
106
107QgsRectanglesOvalsDiamondsAlgorithm *QgsRectanglesOvalsDiamondsAlgorithm::createInstance() const
108{
109 return new QgsRectanglesOvalsDiamondsAlgorithm();
110}
111
112void QgsRectanglesOvalsDiamondsAlgorithm::initParameters( const QVariantMap & )
113{
114 addParameter( new QgsProcessingParameterEnum( u"SHAPE"_s, QObject::tr( "Shape" ), QStringList() << QObject::tr( "Rectangle" ) << QObject::tr( "Diamond" ) << QObject::tr( "Oval" ), false, 0 ) );
115
116 auto widthParam = std::make_unique<QgsProcessingParameterDistance>( u"WIDTH"_s, QObject::tr( "Width" ), 1.0, u"INPUT"_s, false, 0.0 );
117 widthParam->setIsDynamic( true );
118 widthParam->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Width"_s, QObject::tr( "Width" ), QgsPropertyDefinition::DoublePositive ) );
119 widthParam->setDynamicLayerParameterName( u"INPUT"_s );
120 addParameter( widthParam.release() );
121
122 auto heightParam = std::make_unique<QgsProcessingParameterDistance>( u"HEIGHT"_s, QObject::tr( "Height" ), 1.0, u"INPUT"_s, false, 0.0 );
123 heightParam->setIsDynamic( true );
124 heightParam->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Height"_s, QObject::tr( "Height" ), QgsPropertyDefinition::DoublePositive ) );
125 heightParam->setDynamicLayerParameterName( u"INPUT"_s );
126 addParameter( heightParam.release() );
127
128 auto rotationParam = std::make_unique<QgsProcessingParameterNumber>( u"ROTATION"_s, QObject::tr( "Rotation" ), Qgis::ProcessingNumberParameterType::Double, 0.0, true, -360.0, 360.0 );
129 rotationParam->setIsDynamic( true );
130 rotationParam->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Rotation"_s, QObject::tr( "Rotation" ), QgsPropertyDefinition::Double ) );
131 rotationParam->setDynamicLayerParameterName( u"INPUT"_s );
132 addParameter( rotationParam.release() );
133
134 addParameter( new QgsProcessingParameterNumber( u"SEGMENTS"_s, QObject::tr( "Segments" ), Qgis::ProcessingNumberParameterType::Integer, 36, false, 1 ) );
135}
136
137bool QgsRectanglesOvalsDiamondsAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
138{
139 mShape = parameterAsEnum( parameters, u"SHAPE"_s, context );
140
141 mWidth = parameterAsDouble( parameters, u"WIDTH"_s, context );
142 mDynamicWidth = QgsProcessingParameters::isDynamic( parameters, u"WIDTH"_s );
143 if ( mDynamicWidth )
144 mWidthProperty = parameters.value( u"WIDTH"_s ).value<QgsProperty>();
145
146 mHeight = parameterAsDouble( parameters, u"HEIGHT"_s, context );
147 mDynamicHeight = QgsProcessingParameters::isDynamic( parameters, u"HEIGHT"_s );
148 if ( mDynamicHeight )
149 mHeightProperty = parameters.value( u"HEIGHT"_s ).value<QgsProperty>();
150
151 mRotation = parameterAsDouble( parameters, u"ROTATION"_s, context );
152 mDynamicRotation = QgsProcessingParameters::isDynamic( parameters, u"ROTATION"_s );
153 if ( mDynamicRotation )
154 mRotationProperty = parameters.value( u"ROTATION"_s ).value<QgsProperty>();
155
156 mSegments = parameterAsDouble( parameters, u"SEGMENTS"_s, context );
157
158 return true;
159}
160
161QgsFeatureList QgsRectanglesOvalsDiamondsAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
162{
163 QgsFeature outFeature = feature;
164 if ( outFeature.hasGeometry() )
165 {
166 const QgsGeometry geometry = outFeature.geometry();
167 if ( geometry.isMultipart() )
168 {
169 throw QgsProcessingException( QObject::tr( "Multipart geometry. Please promote input layer to singleparts first." ) );
170 }
171
172 double width = mWidth;
173 if ( mDynamicWidth )
174 width = mWidthProperty.valueAsDouble( context.expressionContext(), width );
175
176 double height = mHeight;
177 if ( mDynamicHeight )
178 height = mHeightProperty.valueAsDouble( context.expressionContext(), height );
179
180 double rotation = mRotation;
181 if ( mDynamicRotation )
182 rotation = mRotationProperty.valueAsDouble( context.expressionContext(), rotation );
183
184 if ( width == 0 || height == 0 )
185 {
186 throw QgsProcessingException( QObject::tr( "Width and height should be greater than 0." ) );
187 }
188
189 const double phi = rotation * M_PI / 180;
190 const double xOffset = width / 2.0;
191 const double yOffset = height / 2.0;
192 const QgsPointXY point = geometry.asPoint();
193 const double x = point.x();
194 const double y = point.y();
195
196 QVector<double> ringX( 5 );
197 QVector<double> ringY( 5 );
198
199 switch ( mShape )
200 {
201 case 0:
202 // rectangle
203 ringX = { -xOffset + x, -xOffset + x, xOffset + x, xOffset + x, -xOffset + x };
204 ringY = { -yOffset + y, yOffset + y, yOffset + y, -yOffset + y, -yOffset + y };
205 break;
206 case 1:
207 // diamond
208 ringX = { x, -xOffset + x, x, xOffset + x, x };
209 ringY = { -yOffset + y, y, yOffset + y, y, -yOffset + y };
210 break;
211 case 2:
212 // oval
213 ringX.resize( mSegments + 1 );
214 ringY.resize( mSegments + 1 );
215 for ( int i = 0; i < mSegments; i++ )
216 {
217 const double t = ( 2 * M_PI ) / mSegments * i;
218 ringX[i] = xOffset * cos( t ) + x;
219 ringY[i] = yOffset * sin( t ) + y;
220 }
221 ringX[mSegments] = ringX.at( 0 );
222 ringY[mSegments] = ringY.at( 0 );
223 break;
224 }
225
226 if ( phi != 0 )
227 {
228 for ( int i = 0; i < ringX.size(); ++i )
229 {
230 const double px = ringX.at( i );
231 const double py = ringY.at( i );
232 ringX[i] = ( px - x ) * cos( phi ) + ( py - y ) * sin( phi ) + x;
233 ringY[i] = -( px - x ) * sin( phi ) + ( py - y ) * cos( phi ) + y;
234 }
235 }
236
237 auto poly = std::make_unique<QgsPolygon>();
238 poly->setExteriorRing( new QgsLineString( ringX, ringY ) );
239
240 if ( geometry.constGet()->is3D() )
241 {
242 poly->addZValue( static_cast<const QgsPoint *>( geometry.constGet() )->z() );
243 }
244 if ( geometry.constGet()->isMeasure() )
245 {
246 poly->addMValue( static_cast<const QgsPoint *>( geometry.constGet() )->m() );
247 }
248
249 outFeature.setGeometry( std::move( poly ) );
250 }
251
252 return QgsFeatureList() << outFeature;
253}
254
ProcessingSourceType
Processing data source types.
Definition qgis.h:3602
@ VectorPoint
Vector point layers.
Definition qgis.h:3605
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3607
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
@ Polygon
Polygon.
Definition qgis.h:284
@ Double
Double/float values.
Definition qgis.h:3875
bool isMeasure() const
Returns true if the geometry contains m values.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
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.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
Line string geometry type, with support for z-dimension and m-values.
Represents a 2D point.
Definition qgspointxy.h:62
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
double z
Definition qgspoint.h:58
double m
Definition qgspoint.h:59
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A numeric parameter for processing algorithms.
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
@ Double
Double value (including negative values).
Definition qgsproperty.h:57
@ DoublePositive
Positive double value (including 0).
Definition qgsproperty.h:58
A store for object properties.
double valueAsDouble(const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a double.
static Qgis::WkbType addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
static Qgis::WkbType addZ(Qgis::WkbType type)
Adds the z dimension to a WKB type and returns the new type.
static Q_INVOKABLE bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static Q_INVOKABLE bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
QList< QgsFeature > QgsFeatureList