QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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#include "qgsmultipoint.h"
20#include "qgsmultisurface.h"
21
23
24QString QgsWedgeBuffersAlgorithm::name() const
25{
26 return QStringLiteral( "wedgebuffers" );
27}
28
29QString QgsWedgeBuffersAlgorithm::displayName() const
30{
31 return QObject::tr( "Create wedge buffers" );
32}
33
34QStringList QgsWedgeBuffersAlgorithm::tags() const
35{
36 return QObject::tr( "arc,segment,circular,circle,slice" ).split( ',' );
37}
38
39QString QgsWedgeBuffersAlgorithm::group() const
40{
41 return QObject::tr( "Vector geometry" );
42}
43
44QString QgsWedgeBuffersAlgorithm::groupId() const
45{
46 return QStringLiteral( "vectorgeometry" );
47}
48
49QString QgsWedgeBuffersAlgorithm::outputName() const
50{
51 return QObject::tr( "Buffers" );
52}
53
54Qgis::WkbType QgsWedgeBuffersAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
55{
57 if ( QgsWkbTypes::hasZ( inputWkbType ) )
58 out = QgsWkbTypes::addZ( out );
59 if ( QgsWkbTypes::hasM( inputWkbType ) )
60 out = QgsWkbTypes::addM( out );
61 if ( QgsWkbTypes::isMultiType( inputWkbType ) )
62 out = QgsWkbTypes::multiType( out );
63 return out;
64}
65
66QString QgsWedgeBuffersAlgorithm::shortHelpString() const
67{
68 return QObject::tr( "This algorithm creates wedge shaped buffers from input points.\n\n"
69 "The azimuth parameter gives the angle (in degrees) for the middle of the wedge to point. "
70 "The buffer width (in degrees) is specified by the width parameter. Note that the "
71 "wedge will extend to half of the angular width either side of the azimuth direction.\n\n"
72 "The outer radius of the buffer is specified via outer radius, and optionally an "
73 "inner radius can also be specified.\n\n"
74 "The native output from this algorithm are CurvePolygon geometries, but these may "
75 "be automatically segmentized to Polygons depending on the output format." );
76}
77
78QList<int> QgsWedgeBuffersAlgorithm::inputLayerTypes() const
79{
80 return QList<int>() << static_cast< int >( Qgis::ProcessingSourceType::VectorPoint );
81}
82
83Qgis::ProcessingSourceType QgsWedgeBuffersAlgorithm::outputLayerType() const
84{
86}
87
88QgsWedgeBuffersAlgorithm *QgsWedgeBuffersAlgorithm::createInstance() const
89{
90 return new QgsWedgeBuffersAlgorithm();
91}
92
93void QgsWedgeBuffersAlgorithm::initParameters( const QVariantMap & )
94{
95 std::unique_ptr< QgsProcessingParameterNumber > azimuth = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "AZIMUTH" ), QObject::tr( "Azimuth (degrees from North)" ), Qgis::ProcessingNumberParameterType::Double, 0, false );
96 azimuth->setIsDynamic( true );
97 azimuth->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Azimuth" ), QObject::tr( "Azimuth (degrees from North)" ), QgsPropertyDefinition::Double ) );
98 azimuth->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
99 addParameter( azimuth.release() );
100
101 std::unique_ptr< QgsProcessingParameterNumber > width = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "WIDTH" ), QObject::tr( "Wedge width (in degrees)" ), Qgis::ProcessingNumberParameterType::Double, 45, false, 0, 360.0 );
102 width->setIsDynamic( true );
103 width->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Width" ), QObject::tr( "Wedge width (in degrees)" ), QgsPropertyDefinition::DoublePositive ) );
104 width->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
105 addParameter( width.release() );
106
107 std::unique_ptr< QgsProcessingParameterNumber > outerRadius = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "OUTER_RADIUS" ), QObject::tr( "Outer radius" ), Qgis::ProcessingNumberParameterType::Double, 1, false, 0 );
108 outerRadius->setIsDynamic( true );
109 outerRadius->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Outer radius" ), QObject::tr( "Outer radius" ), QgsPropertyDefinition::DoublePositive ) );
110 outerRadius->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
111 addParameter( outerRadius.release() );
112
113 std::unique_ptr< QgsProcessingParameterNumber > innerRadius = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "INNER_RADIUS" ), QObject::tr( "Inner radius" ), Qgis::ProcessingNumberParameterType::Double, 0, true, 0 );
114 innerRadius->setIsDynamic( true );
115 innerRadius->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Inner radius" ), QObject::tr( "Inner radius" ), QgsPropertyDefinition::DoublePositive ) );
116 innerRadius->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
117 addParameter( innerRadius.release() );
118}
119
120Qgis::ProcessingFeatureSourceFlags QgsWedgeBuffersAlgorithm::sourceFlags() const
121{
123}
124
125bool QgsWedgeBuffersAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
126{
127 mAzimuth = parameterAsDouble( parameters, QStringLiteral( "AZIMUTH" ), context );
128 mDynamicAzimuth = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "AZIMUTH" ) );
129 if ( mDynamicAzimuth )
130 mAzimuthProperty = parameters.value( QStringLiteral( "AZIMUTH" ) ).value< QgsProperty >();
131
132 mWidth = parameterAsDouble( parameters, QStringLiteral( "WIDTH" ), context );
133 mDynamicWidth = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "WIDTH" ) );
134 if ( mDynamicWidth )
135 mWidthProperty = parameters.value( QStringLiteral( "WIDTH" ) ).value< QgsProperty >();
136
137 mOuterRadius = parameterAsDouble( parameters, QStringLiteral( "OUTER_RADIUS" ), context );
138 mDynamicOuterRadius = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "OUTER_RADIUS" ) );
139 if ( mDynamicOuterRadius )
140 mOuterRadiusProperty = parameters.value( QStringLiteral( "OUTER_RADIUS" ) ).value< QgsProperty >();
141
142 mInnerRadius = parameterAsDouble( parameters, QStringLiteral( "INNER_RADIUS" ), context );
143 mDynamicInnerRadius = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "INNER_RADIUS" ) );
144 if ( mDynamicInnerRadius )
145 mInnerRadiusProperty = parameters.value( QStringLiteral( "INNER_RADIUS" ) ).value< QgsProperty >();
146
147 return true;
148}
149
150QgsFeatureList QgsWedgeBuffersAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
151{
152 QgsFeature f = feature;
154 {
155 double azimuth = mAzimuth;
156 if ( mDynamicAzimuth )
157 azimuth = mAzimuthProperty.valueAsDouble( context.expressionContext(), azimuth );
158
159 double width = mWidth;
160 if ( mDynamicWidth )
161 width = mWidthProperty.valueAsDouble( context.expressionContext(), width );
162
163 double outerRadius = mOuterRadius;
164 if ( mDynamicOuterRadius )
165 outerRadius = mOuterRadiusProperty.valueAsDouble( context.expressionContext(), outerRadius );
166
167 double innerRadius = mInnerRadius;
168 if ( mDynamicInnerRadius )
169 innerRadius = mInnerRadiusProperty.valueAsDouble( context.expressionContext(), innerRadius );
170
171 const QgsGeometry g = f.geometry();
173 {
174 const QgsMultiPoint *mp = static_cast< const QgsMultiPoint * >( g.constGet() );
175 std::unique_ptr< QgsMultiSurface > result = std::make_unique< QgsMultiSurface >();
176 result->reserve( mp->numGeometries() );
177 for ( int i = 0; i < mp->numGeometries(); ++i )
178 {
179 const QgsPoint *p = mp->pointN( i );
180 result->addGeometry( QgsGeometry::createWedgeBuffer( *p, azimuth, width, outerRadius, innerRadius ).constGet()->clone() );
181 }
182 f.setGeometry( QgsGeometry( std::move( result ) ) );
183 }
184 else
185 {
186 const QgsPoint *p = static_cast< const QgsPoint * >( g.constGet() );
187 f.setGeometry( QgsGeometry::createWedgeBuffer( *p, azimuth, width, outerRadius, innerRadius ) );
188 }
189 }
190
191 return QgsFeatureList() << f;
192}
193
195
ProcessingSourceType
Processing data source types.
Definition: qgis.h:2858
@ VectorPoint
Vector point layers.
@ VectorPolygon
Vector polygon layers.
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition: qgis.h:182
@ CurvePolygon
CurvePolygon.
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition: qgis.h:3011
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsGeometry geometry
Definition: qgsfeature.h:67
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:167
int numGeometries() const
Returns the number of geometries within the collection.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:162
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.
Definition: qgsmultipoint.h:29
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:49
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:45
@ Double
Double value (including negative values)
Definition: qgsproperty.h:55
@ DoublePositive
Positive double value (including 0)
Definition: qgsproperty.h:56
A store for object properties.
Definition: qgsproperty.h:228
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...
Definition: qgswkbtypes.h:862
static bool isMultiType(Qgis::WkbType type)
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:758
static Qgis::WkbType addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1092
static Qgis::WkbType addZ(Qgis::WkbType type)
Adds the z dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1068
static bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:973
static bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:1023
static Qgis::WkbType multiType(Qgis::WkbType type)
Returns the multi type for a WKB type.
Definition: qgswkbtypes.h:200
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:917