QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
74 "This algorithm creates wedge shaped buffers from input points.\n\n"
75 "The azimuth parameter gives the angle (in degrees) for the middle of the wedge to point. "
76 "The buffer width (in degrees) is specified by the width parameter. Note that the "
77 "wedge will extend to half of the angular width either side of the azimuth direction.\n\n"
78 "The outer radius of the buffer is specified via outer radius, and optionally an "
79 "inner radius can also be specified.\n\n"
80 "The native output from this algorithm are CurvePolygon geometries, but these may "
81 "be automatically segmentized to Polygons depending on the output format."
82 );
83}
84
85QString QgsWedgeBuffersAlgorithm::shortDescription() const
86{
87 return QObject::tr( "Creates wedge shaped buffers from input points." );
88}
89
90QList<int> QgsWedgeBuffersAlgorithm::inputLayerTypes() const
91{
92 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPoint );
93}
94
95Qgis::ProcessingSourceType QgsWedgeBuffersAlgorithm::outputLayerType() const
96{
98}
99
100QgsWedgeBuffersAlgorithm *QgsWedgeBuffersAlgorithm::createInstance() const
101{
102 return new QgsWedgeBuffersAlgorithm();
103}
104
105void QgsWedgeBuffersAlgorithm::initParameters( const QVariantMap & )
106{
107 auto azimuth = std::make_unique<QgsProcessingParameterNumber>( u"AZIMUTH"_s, QObject::tr( "Azimuth (degrees from North)" ), Qgis::ProcessingNumberParameterType::Double, 0, false );
108 azimuth->setIsDynamic( true );
109 azimuth->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Azimuth"_s, QObject::tr( "Azimuth (degrees from North)" ), QgsPropertyDefinition::Double ) );
110 azimuth->setDynamicLayerParameterName( u"INPUT"_s );
111 addParameter( azimuth.release() );
112
113 auto width = std::make_unique<QgsProcessingParameterNumber>( u"WIDTH"_s, QObject::tr( "Wedge width (in degrees)" ), Qgis::ProcessingNumberParameterType::Double, 45, false, 0, 360.0 );
114 width->setIsDynamic( true );
115 width->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Width"_s, QObject::tr( "Wedge width (in degrees)" ), QgsPropertyDefinition::DoublePositive ) );
116 width->setDynamicLayerParameterName( u"INPUT"_s );
117 addParameter( width.release() );
118
119 auto outerRadius = std::make_unique<QgsProcessingParameterNumber>( u"OUTER_RADIUS"_s, QObject::tr( "Outer radius" ), Qgis::ProcessingNumberParameterType::Double, 1, false, 0 );
120 outerRadius->setIsDynamic( true );
121 outerRadius->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Outer radius"_s, QObject::tr( "Outer radius" ), QgsPropertyDefinition::DoublePositive ) );
122 outerRadius->setDynamicLayerParameterName( u"INPUT"_s );
123 addParameter( outerRadius.release() );
124
125 auto innerRadius = std::make_unique<QgsProcessingParameterNumber>( u"INNER_RADIUS"_s, QObject::tr( "Inner radius" ), Qgis::ProcessingNumberParameterType::Double, 0, true, 0 );
126 innerRadius->setIsDynamic( true );
127 innerRadius->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Inner radius"_s, QObject::tr( "Inner radius" ), QgsPropertyDefinition::DoublePositive ) );
128 innerRadius->setDynamicLayerParameterName( u"INPUT"_s );
129 addParameter( innerRadius.release() );
130}
131
132Qgis::ProcessingFeatureSourceFlags QgsWedgeBuffersAlgorithm::sourceFlags() const
133{
135}
136
137bool QgsWedgeBuffersAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
138{
139 mAzimuth = parameterAsDouble( parameters, u"AZIMUTH"_s, context );
140 mDynamicAzimuth = QgsProcessingParameters::isDynamic( parameters, u"AZIMUTH"_s );
141 if ( mDynamicAzimuth )
142 mAzimuthProperty = parameters.value( u"AZIMUTH"_s ).value<QgsProperty>();
143
144 mWidth = parameterAsDouble( parameters, u"WIDTH"_s, context );
145 mDynamicWidth = QgsProcessingParameters::isDynamic( parameters, u"WIDTH"_s );
146 if ( mDynamicWidth )
147 mWidthProperty = parameters.value( u"WIDTH"_s ).value<QgsProperty>();
148
149 mOuterRadius = parameterAsDouble( parameters, u"OUTER_RADIUS"_s, context );
150 mDynamicOuterRadius = QgsProcessingParameters::isDynamic( parameters, u"OUTER_RADIUS"_s );
151 if ( mDynamicOuterRadius )
152 mOuterRadiusProperty = parameters.value( u"OUTER_RADIUS"_s ).value<QgsProperty>();
153
154 mInnerRadius = parameterAsDouble( parameters, u"INNER_RADIUS"_s, context );
155 mDynamicInnerRadius = QgsProcessingParameters::isDynamic( parameters, u"INNER_RADIUS"_s );
156 if ( mDynamicInnerRadius )
157 mInnerRadiusProperty = parameters.value( u"INNER_RADIUS"_s ).value<QgsProperty>();
158
159 return true;
160}
161
162QgsFeatureList QgsWedgeBuffersAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
163{
164 QgsFeature f = feature;
166 {
167 double azimuth = mAzimuth;
168 if ( mDynamicAzimuth )
169 azimuth = mAzimuthProperty.valueAsDouble( context.expressionContext(), azimuth );
170
171 double width = mWidth;
172 if ( mDynamicWidth )
173 width = mWidthProperty.valueAsDouble( context.expressionContext(), width );
174
175 double outerRadius = mOuterRadius;
176 if ( mDynamicOuterRadius )
177 outerRadius = mOuterRadiusProperty.valueAsDouble( context.expressionContext(), outerRadius );
178
179 double innerRadius = mInnerRadius;
180 if ( mDynamicInnerRadius )
181 innerRadius = mInnerRadiusProperty.valueAsDouble( context.expressionContext(), innerRadius );
182
183 const QgsGeometry g = f.geometry();
185 {
186 const QgsMultiPoint *mp = static_cast<const QgsMultiPoint *>( g.constGet() );
187 auto result = std::make_unique<QgsMultiSurface>();
188 result->reserve( mp->numGeometries() );
189 for ( int i = 0; i < mp->numGeometries(); ++i )
190 {
191 const QgsPoint *p = mp->pointN( i );
192 result->addGeometry( QgsGeometry::createWedgeBuffer( *p, azimuth, width, outerRadius, innerRadius ).constGet()->clone() );
193 }
194 f.setGeometry( QgsGeometry( std::move( result ) ) );
195 }
196 else
197 {
198 const QgsPoint *p = static_cast<const QgsPoint *>( g.constGet() );
199 f.setGeometry( QgsGeometry::createWedgeBuffer( *p, azimuth, width, outerRadius, innerRadius ) );
200 }
201 }
202
203 return QgsFeatureList() << f;
204}
205
ProcessingSourceType
Processing data source types.
Definition qgis.h:3645
@ VectorPoint
Vector point layers.
Definition qgis.h:3648
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3650
@ Point
Points.
Definition qgis.h:380
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3828
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
@ CurvePolygon
CurvePolygon.
Definition qgis.h:306
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3839
@ Double
Double/float values.
Definition qgis.h:3921
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:56
@ DoublePositive
Positive double value (including 0).
Definition qgsproperty.h:57
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