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