QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmwedgebuffers.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmwedgebuffers.cpp
3 ---------------------
4 begin : April 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
19
20#include "qgsmultipoint.h"
21#include "qgsmultisurface.h"
22
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
28
29QString QgsWedgeBuffersAlgorithm::name() const
30{
31 return u"wedgebuffers"_s;
32}
33
34QString QgsWedgeBuffersAlgorithm::displayName() const
35{
36 return QObject::tr( "Create wedge buffers" );
37}
38
39QStringList QgsWedgeBuffersAlgorithm::tags() const
40{
41 return QObject::tr( "arc,segment,circular,circle,slice" ).split( ',' );
42}
43
44QString QgsWedgeBuffersAlgorithm::group() const
45{
46 return QObject::tr( "Vector geometry" );
47}
48
49QString QgsWedgeBuffersAlgorithm::groupId() const
50{
51 return u"vectorgeometry"_s;
52}
53
54QString QgsWedgeBuffersAlgorithm::outputName() const
55{
56 return QObject::tr( "Buffers" );
57}
58
59Qgis::WkbType QgsWedgeBuffersAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
60{
62 if ( QgsWkbTypes::hasZ( inputWkbType ) )
63 out = QgsWkbTypes::addZ( out );
64 if ( QgsWkbTypes::hasM( inputWkbType ) )
65 out = QgsWkbTypes::addM( out );
66 if ( QgsWkbTypes::isMultiType( inputWkbType ) )
67 out = QgsWkbTypes::multiType( out );
68 return out;
69}
70
71QString QgsWedgeBuffersAlgorithm::shortHelpString() const
72{
73 return QObject::tr( "This algorithm creates wedge shaped buffers from input points.\n\n"
74 "The azimuth parameter gives the angle (in degrees) for the middle of the wedge to point. "
75 "The buffer width (in degrees) is specified by the width parameter. Note that the "
76 "wedge will extend to half of the angular width either side of the azimuth direction.\n\n"
77 "The outer radius of the buffer is specified via outer radius, and optionally an "
78 "inner radius can also be specified.\n\n"
79 "The native output from this algorithm are CurvePolygon geometries, but these may "
80 "be automatically segmentized to Polygons depending on the output format." );
81}
82
83QString QgsWedgeBuffersAlgorithm::shortDescription() const
84{
85 return QObject::tr( "Creates wedge shaped buffers from input points." );
86}
87
88QList<int> QgsWedgeBuffersAlgorithm::inputLayerTypes() const
89{
90 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPoint );
91}
92
93Qgis::ProcessingSourceType QgsWedgeBuffersAlgorithm::outputLayerType() const
94{
96}
97
98QgsWedgeBuffersAlgorithm *QgsWedgeBuffersAlgorithm::createInstance() const
99{
100 return new QgsWedgeBuffersAlgorithm();
101}
102
103void QgsWedgeBuffersAlgorithm::initParameters( const QVariantMap & )
104{
105 auto azimuth = std::make_unique<QgsProcessingParameterNumber>( u"AZIMUTH"_s, QObject::tr( "Azimuth (degrees from North)" ), Qgis::ProcessingNumberParameterType::Double, 0, false );
106 azimuth->setIsDynamic( true );
107 azimuth->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Azimuth"_s, QObject::tr( "Azimuth (degrees from North)" ), QgsPropertyDefinition::Double ) );
108 azimuth->setDynamicLayerParameterName( u"INPUT"_s );
109 addParameter( azimuth.release() );
110
111 auto width = std::make_unique<QgsProcessingParameterNumber>( u"WIDTH"_s, QObject::tr( "Wedge width (in degrees)" ), Qgis::ProcessingNumberParameterType::Double, 45, false, 0, 360.0 );
112 width->setIsDynamic( true );
113 width->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Width"_s, QObject::tr( "Wedge width (in degrees)" ), QgsPropertyDefinition::DoublePositive ) );
114 width->setDynamicLayerParameterName( u"INPUT"_s );
115 addParameter( width.release() );
116
117 auto outerRadius = std::make_unique<QgsProcessingParameterNumber>( u"OUTER_RADIUS"_s, QObject::tr( "Outer radius" ), Qgis::ProcessingNumberParameterType::Double, 1, false, 0 );
118 outerRadius->setIsDynamic( true );
119 outerRadius->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Outer radius"_s, QObject::tr( "Outer radius" ), QgsPropertyDefinition::DoublePositive ) );
120 outerRadius->setDynamicLayerParameterName( u"INPUT"_s );
121 addParameter( outerRadius.release() );
122
123 auto innerRadius = std::make_unique<QgsProcessingParameterNumber>( u"INNER_RADIUS"_s, QObject::tr( "Inner radius" ), Qgis::ProcessingNumberParameterType::Double, 0, true, 0 );
124 innerRadius->setIsDynamic( true );
125 innerRadius->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Inner radius"_s, QObject::tr( "Inner radius" ), QgsPropertyDefinition::DoublePositive ) );
126 innerRadius->setDynamicLayerParameterName( u"INPUT"_s );
127 addParameter( innerRadius.release() );
128}
129
130Qgis::ProcessingFeatureSourceFlags QgsWedgeBuffersAlgorithm::sourceFlags() const
131{
133}
134
135bool QgsWedgeBuffersAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
136{
137 mAzimuth = parameterAsDouble( parameters, u"AZIMUTH"_s, context );
138 mDynamicAzimuth = QgsProcessingParameters::isDynamic( parameters, u"AZIMUTH"_s );
139 if ( mDynamicAzimuth )
140 mAzimuthProperty = parameters.value( u"AZIMUTH"_s ).value<QgsProperty>();
141
142 mWidth = parameterAsDouble( parameters, u"WIDTH"_s, context );
143 mDynamicWidth = QgsProcessingParameters::isDynamic( parameters, u"WIDTH"_s );
144 if ( mDynamicWidth )
145 mWidthProperty = parameters.value( u"WIDTH"_s ).value<QgsProperty>();
146
147 mOuterRadius = parameterAsDouble( parameters, u"OUTER_RADIUS"_s, context );
148 mDynamicOuterRadius = QgsProcessingParameters::isDynamic( parameters, u"OUTER_RADIUS"_s );
149 if ( mDynamicOuterRadius )
150 mOuterRadiusProperty = parameters.value( u"OUTER_RADIUS"_s ).value<QgsProperty>();
151
152 mInnerRadius = parameterAsDouble( parameters, u"INNER_RADIUS"_s, context );
153 mDynamicInnerRadius = QgsProcessingParameters::isDynamic( parameters, u"INNER_RADIUS"_s );
154 if ( mDynamicInnerRadius )
155 mInnerRadiusProperty = parameters.value( u"INNER_RADIUS"_s ).value<QgsProperty>();
156
157 return true;
158}
159
160QgsFeatureList QgsWedgeBuffersAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
161{
162 QgsFeature f = feature;
164 {
165 double azimuth = mAzimuth;
166 if ( mDynamicAzimuth )
167 azimuth = mAzimuthProperty.valueAsDouble( context.expressionContext(), azimuth );
168
169 double width = mWidth;
170 if ( mDynamicWidth )
171 width = mWidthProperty.valueAsDouble( context.expressionContext(), width );
172
173 double outerRadius = mOuterRadius;
174 if ( mDynamicOuterRadius )
175 outerRadius = mOuterRadiusProperty.valueAsDouble( context.expressionContext(), outerRadius );
176
177 double innerRadius = mInnerRadius;
178 if ( mDynamicInnerRadius )
179 innerRadius = mInnerRadiusProperty.valueAsDouble( context.expressionContext(), innerRadius );
180
181 const QgsGeometry g = f.geometry();
183 {
184 const QgsMultiPoint *mp = static_cast<const QgsMultiPoint *>( g.constGet() );
185 auto result = std::make_unique<QgsMultiSurface>();
186 result->reserve( mp->numGeometries() );
187 for ( int i = 0; i < mp->numGeometries(); ++i )
188 {
189 const QgsPoint *p = mp->pointN( i );
190 result->addGeometry( QgsGeometry::createWedgeBuffer( *p, azimuth, width, outerRadius, innerRadius ).constGet()->clone() );
191 }
192 f.setGeometry( QgsGeometry( std::move( result ) ) );
193 }
194 else
195 {
196 const QgsPoint *p = static_cast<const QgsPoint *>( g.constGet() );
197 f.setGeometry( QgsGeometry::createWedgeBuffer( *p, azimuth, width, outerRadius, innerRadius ) );
198 }
199 }
200
201 return QgsFeatureList() << f;
202}
203
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
@ Point
Points.
Definition qgis.h:366
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3782
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
@ CurvePolygon
CurvePolygon.
Definition qgis.h:292
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3793
@ Double
Double/float values.
Definition qgis.h:3875
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.
int numGeometries() const
Returns the number of geometries within the collection.
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.
static QgsGeometry createWedgeBuffer(const QgsPoint &center, double azimuth, double angularWidth, double outerRadius, double innerRadius=0)
Creates a wedge shaped buffer from a center point.
Qgis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.).
Multi point geometry collection.
QgsPoint * pointN(int index)
Returns the point with the specified index.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
Base class for providing feedback from a processing algorithm.
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::GeometryType geometryType(Qgis::WkbType type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
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.
static Qgis::WkbType multiType(Qgis::WkbType type)
Returns the multi type for a WKB type.
static Q_INVOKABLE bool isMultiType(Qgis::WkbType type)
Returns true if the WKB type is a multi type.
QList< QgsFeature > QgsFeatureList