@@ -42,7 +42,12 @@ QString QgsBufferAlgorithm::group() const
42
42
void QgsBufferAlgorithm::initAlgorithm ( const QVariantMap & )
43
43
{
44
44
addParameter ( new QgsProcessingParameterFeatureSource ( QStringLiteral ( " INPUT" ), QObject::tr ( " Input layer" ) ) );
45
- addParameter ( new QgsProcessingParameterNumber ( QStringLiteral ( " DISTANCE" ), QObject::tr ( " Distance" ), QgsProcessingParameterNumber::Double, 10 ) );
45
+
46
+ auto bufferParam = qgis::make_unique < QgsProcessingParameterNumber >( QStringLiteral ( " DISTANCE" ), QObject::tr ( " Distance" ), QgsProcessingParameterNumber::Double, 10 );
47
+ bufferParam->setIsDynamic ( true );
48
+ bufferParam->setDynamicPropertyDefinition ( QgsPropertyDefinition ( QStringLiteral ( " Distance" ), QObject::tr ( " Buffer distance" ), QgsPropertyDefinition::Double ) );
49
+ bufferParam->setDynamicLayerParameterName ( QStringLiteral ( " INPUT" ) );
50
+ addParameter ( bufferParam.release () );
46
51
addParameter ( new QgsProcessingParameterNumber ( QStringLiteral ( " SEGMENTS" ), QObject::tr ( " Segments" ), QgsProcessingParameterNumber::Integer, 5 , false , 1 ) );
47
52
48
53
addParameter ( new QgsProcessingParameterEnum ( QStringLiteral ( " END_CAP_STYLE" ), QObject::tr ( " End cap style" ), QStringList () << QObject::tr ( " Round" ) << QObject::tr ( " Flat" ) << QObject::tr ( " Square" ), false ) );
@@ -86,7 +91,14 @@ QVariantMap QgsBufferAlgorithm::processAlgorithm( const QVariantMap ¶meters,
86
91
double miterLimit = parameterAsDouble ( parameters, QStringLiteral ( " MITER_LIMIT" ), context );
87
92
double bufferDistance = parameterAsDouble ( parameters, QStringLiteral ( " DISTANCE" ), context );
88
93
bool dynamicBuffer = QgsProcessingParameters::isDynamic ( parameters, QStringLiteral ( " DISTANCE" ) );
89
- const QgsProcessingParameterDefinition *distanceParamDef = parameterDefinition ( QStringLiteral ( " DISTANCE" ) );
94
+ QgsExpressionContext expressionContext;
95
+ QgsProperty bufferProperty;
96
+ if ( dynamicBuffer )
97
+ {
98
+ bufferProperty = parameters.value ( QStringLiteral ( " DISTANCE" ) ).value < QgsProperty >();
99
+ expressionContext = createExpressionContext ( parameters, context, dynamic_cast < QgsProcessingFeatureSource * >( source.get () ) );
100
+ bufferProperty.prepare ( expressionContext );
101
+ }
90
102
91
103
long count = source->featureCount ();
92
104
@@ -111,13 +123,14 @@ QVariantMap QgsBufferAlgorithm::processAlgorithm( const QVariantMap ¶meters,
111
123
QgsFeature out = f;
112
124
if ( out.hasGeometry () )
113
125
{
126
+ double distance = bufferDistance;
114
127
if ( dynamicBuffer )
115
128
{
116
- context. expressionContext () .setFeature ( f );
117
- bufferDistance = QgsProcessingParameters::parameterAsDouble ( distanceParamDef, parameters, context );
129
+ expressionContext.setFeature ( f );
130
+ distance = bufferProperty. valueAsDouble ( expressionContext, bufferDistance );
118
131
}
119
132
120
- QgsGeometry outputGeometry = f.geometry ().buffer ( bufferDistance , segments, endCapStyle, joinStyle, miterLimit );
133
+ QgsGeometry outputGeometry = f.geometry ().buffer ( distance , segments, endCapStyle, joinStyle, miterLimit );
121
134
if ( !outputGeometry )
122
135
{
123
136
QgsMessageLog::logMessage ( QObject::tr ( " Error calculating buffer for feature %1" ).arg ( f.id () ), QObject::tr ( " Processing" ), QgsMessageLog::WARNING );
0 commit comments