Skip to content

Commit

Permalink
[processing] Make buffer distance a dynamic property
Browse files Browse the repository at this point in the history
Data defined buffer distance through the processing toolbox!
  • Loading branch information
nyalldawson committed Dec 2, 2017
1 parent 901dae1 commit b782fab
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
23 changes: 18 additions & 5 deletions src/analysis/processing/qgsalgorithmbuffer.cpp
Expand Up @@ -42,7 +42,12 @@ QString QgsBufferAlgorithm::group() const
void QgsBufferAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "DISTANCE" ), QObject::tr( "Distance" ), QgsProcessingParameterNumber::Double, 10 ) );

auto bufferParam = qgis::make_unique < QgsProcessingParameterNumber >( QStringLiteral( "DISTANCE" ), QObject::tr( "Distance" ), QgsProcessingParameterNumber::Double, 10 );
bufferParam->setIsDynamic( true );
bufferParam->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Distance" ), QObject::tr( "Buffer distance" ), QgsPropertyDefinition::Double ) );
bufferParam->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
addParameter( bufferParam.release() );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "SEGMENTS" ), QObject::tr( "Segments" ), QgsProcessingParameterNumber::Integer, 5, false, 1 ) );

addParameter( new QgsProcessingParameterEnum( QStringLiteral( "END_CAP_STYLE" ), QObject::tr( "End cap style" ), QStringList() << QObject::tr( "Round" ) << QObject::tr( "Flat" ) << QObject::tr( "Square" ), false ) );
Expand Down Expand Up @@ -86,7 +91,14 @@ QVariantMap QgsBufferAlgorithm::processAlgorithm( const QVariantMap &parameters,
double miterLimit = parameterAsDouble( parameters, QStringLiteral( "MITER_LIMIT" ), context );
double bufferDistance = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context );
bool dynamicBuffer = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) );
const QgsProcessingParameterDefinition *distanceParamDef = parameterDefinition( QStringLiteral( "DISTANCE" ) );
QgsExpressionContext expressionContext;
QgsProperty bufferProperty;
if ( dynamicBuffer )
{
bufferProperty = parameters.value( QStringLiteral( "DISTANCE" ) ).value< QgsProperty >();
expressionContext = createExpressionContext( parameters, context, dynamic_cast< QgsProcessingFeatureSource * >( source.get() ) );
bufferProperty.prepare( expressionContext );
}

long count = source->featureCount();

Expand All @@ -111,13 +123,14 @@ QVariantMap QgsBufferAlgorithm::processAlgorithm( const QVariantMap &parameters,
QgsFeature out = f;
if ( out.hasGeometry() )
{
double distance = bufferDistance;
if ( dynamicBuffer )
{
context.expressionContext().setFeature( f );
bufferDistance = QgsProcessingParameters::parameterAsDouble( distanceParamDef, parameters, context );
expressionContext.setFeature( f );
distance = bufferProperty.valueAsDouble( expressionContext, bufferDistance );
}

QgsGeometry outputGeometry = f.geometry().buffer( bufferDistance, segments, endCapStyle, joinStyle, miterLimit );
QgsGeometry outputGeometry = f.geometry().buffer( distance, segments, endCapStyle, joinStyle, miterLimit );
if ( !outputGeometry )
{
QgsMessageLog::logMessage( QObject::tr( "Error calculating buffer for feature %1" ).arg( f.id() ), QObject::tr( "Processing" ), QgsMessageLog::WARNING );
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/processing/qgsalgorithmextractbyattribute.cpp
Expand Up @@ -168,7 +168,7 @@ QVariantMap QgsExtractByAttributeAlgorithm::processAlgorithm( const QVariantMap
throw QgsProcessingException( expression.parserErrorString() );
}

QgsExpressionContext expressionContext = createExpressionContext( parameters, context );
QgsExpressionContext expressionContext = createExpressionContext( parameters, context, dynamic_cast< QgsProcessingFeatureSource * >( source.get() ) );

long count = source->featureCount();

Expand Down
Expand Up @@ -87,7 +87,7 @@ QVariantMap QgsExtractByExpressionAlgorithm::processAlgorithm( const QVariantMap
throw QgsProcessingException( expression.parserErrorString() );
}

QgsExpressionContext expressionContext = createExpressionContext( parameters, context );
QgsExpressionContext expressionContext = createExpressionContext( parameters, context, dynamic_cast< QgsProcessingFeatureSource * >( source.get() ) );

long count = source->featureCount();

Expand Down

0 comments on commit b782fab

Please sign in to comment.