From 9297938598649cff9d99d07a4b22863d3589f361 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Wed, 3 Feb 2021 08:22:12 +1000 Subject: [PATCH] [processing] Ask users for existing destination fields for Sum Line Length and Count Points In Polygons result fields when running in in-place mode Makes these algorithms fully compatible with in place execution Fixes #39807 --- .../qgsalgorithmpointsinpolygon.cpp | 45 ++++++++--- .../processing/qgsalgorithmpointsinpolygon.h | 3 +- .../processing/qgsalgorithmsumlinelength.cpp | 74 +++++++++++++------ .../processing/qgsalgorithmsumlinelength.h | 3 +- 4 files changed, 92 insertions(+), 33 deletions(-) diff --git a/src/analysis/processing/qgsalgorithmpointsinpolygon.cpp b/src/analysis/processing/qgsalgorithmpointsinpolygon.cpp index a5487516b036..01537d948e6c 100644 --- a/src/analysis/processing/qgsalgorithmpointsinpolygon.cpp +++ b/src/analysis/processing/qgsalgorithmpointsinpolygon.cpp @@ -23,16 +23,26 @@ ///@cond PRIVATE -void QgsPointsInPolygonAlgorithm::initParameters( const QVariantMap & ) +void QgsPointsInPolygonAlgorithm::initParameters( const QVariantMap &configuration ) { + mIsInPlace = configuration.value( QStringLiteral( "IN_PLACE" ) ).toBool(); + addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "POINTS" ), QObject::tr( "Points" ), QList< int > () << QgsProcessing::TypeVectorPoint ) ); addParameter( new QgsProcessingParameterField( QStringLiteral( "WEIGHT" ), QObject::tr( "Weight field" ), QVariant(), QStringLiteral( "POINTS" ), QgsProcessingParameterField::Any, false, true ) ); addParameter( new QgsProcessingParameterField( QStringLiteral( "CLASSFIELD" ), QObject::tr( "Class field" ), QVariant(), QStringLiteral( "POINTS" ), QgsProcessingParameterField::Any, false, true ) ); - addParameter( new QgsProcessingParameterString( QStringLiteral( "FIELD" ), - QObject::tr( "Count field name" ), QStringLiteral( "NUMPOINTS" ) ) ); + if ( mIsInPlace ) + { + addParameter( new QgsProcessingParameterField( QStringLiteral( "FIELD" ), + QObject::tr( "Count field" ), QStringLiteral( "NUMPOINTS" ), inputParameterName() ) ); + } + else + { + addParameter( new QgsProcessingParameterString( QStringLiteral( "FIELD" ), + QObject::tr( "Count field name" ), QStringLiteral( "NUMPOINTS" ) ) ); + } } QString QgsPointsInPolygonAlgorithm::name() const @@ -235,13 +245,30 @@ QgsFeatureList QgsPointsInPolygonAlgorithm::processFeature( const QgsFeature &fe QgsFields QgsPointsInPolygonAlgorithm::outputFields( const QgsFields &inputFields ) const { - QgsFields outFields = inputFields; - mDestFieldIndex = inputFields.lookupField( mFieldName ); - if ( mDestFieldIndex < 0 ) - outFields.append( QgsField( mFieldName, QVariant::Double ) ); + if ( mIsInPlace ) + { + mDestFieldIndex = inputFields.lookupField( mFieldName ); + return inputFields; + } + else + { + QgsFields outFields = inputFields; + mDestFieldIndex = inputFields.lookupField( mFieldName ); + if ( mDestFieldIndex < 0 ) + outFields.append( QgsField( mFieldName, QVariant::Double ) ); + + mFields = outFields; + return outFields; + } +} - mFields = outFields; - return outFields; +bool QgsPointsInPolygonAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const +{ + if ( const QgsVectorLayer *vl = qobject_cast< const QgsVectorLayer * >( layer ) ) + { + return vl->geometryType() == QgsWkbTypes::PolygonGeometry; + } + return false; } diff --git a/src/analysis/processing/qgsalgorithmpointsinpolygon.h b/src/analysis/processing/qgsalgorithmpointsinpolygon.h index fcce2565b986..f8ad2e40da9b 100644 --- a/src/analysis/processing/qgsalgorithmpointsinpolygon.h +++ b/src/analysis/processing/qgsalgorithmpointsinpolygon.h @@ -57,9 +57,10 @@ class QgsPointsInPolygonAlgorithm : public QgsProcessingFeatureBasedAlgorithm bool prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; QgsFields outputFields( const QgsFields &inputFields ) const override; + bool supportInPlaceEdit( const QgsMapLayer *layer ) const override; private: - + bool mIsInPlace = false; QString mFieldName; QString mWeightFieldName; QString mClassFieldName; diff --git a/src/analysis/processing/qgsalgorithmsumlinelength.cpp b/src/analysis/processing/qgsalgorithmsumlinelength.cpp index 936f7c8db8df..d129a3f9616d 100644 --- a/src/analysis/processing/qgsalgorithmsumlinelength.cpp +++ b/src/analysis/processing/qgsalgorithmsumlinelength.cpp @@ -106,14 +106,26 @@ QString QgsSumLineLengthAlgorithm::outputName() const return QObject::tr( "Line length" ); } -void QgsSumLineLengthAlgorithm::initParameters( const QVariantMap & ) +void QgsSumLineLengthAlgorithm::initParameters( const QVariantMap &configuration ) { + mIsInPlace = configuration.value( QStringLiteral( "IN_PLACE" ) ).toBool(); + addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "LINES" ), QObject::tr( "Lines" ), QList< int > () << QgsProcessing::TypeVectorLine ) ); - addParameter( new QgsProcessingParameterString( QStringLiteral( "LEN_FIELD" ), - QObject::tr( "Lines length field name" ), QStringLiteral( "LENGTH" ) ) ); - addParameter( new QgsProcessingParameterString( QStringLiteral( "COUNT_FIELD" ), - QObject::tr( "Lines count field name" ), QStringLiteral( "COUNT" ) ) ); + if ( mIsInPlace ) + { + addParameter( new QgsProcessingParameterField( QStringLiteral( "LEN_FIELD" ), + QObject::tr( "Lines length field name" ), QStringLiteral( "LENGTH" ), inputParameterName(), QgsProcessingParameterField::Any, false, true ) ); + addParameter( new QgsProcessingParameterField( QStringLiteral( "COUNT_FIELD" ), + QObject::tr( "Lines count field name" ), QStringLiteral( "COUNT" ), inputParameterName(), QgsProcessingParameterField::Any, false, true ) ); + } + else + { + addParameter( new QgsProcessingParameterString( QStringLiteral( "LEN_FIELD" ), + QObject::tr( "Lines length field name" ), QStringLiteral( "LENGTH" ) ) ); + addParameter( new QgsProcessingParameterString( QStringLiteral( "COUNT_FIELD" ), + QObject::tr( "Lines count field name" ), QStringLiteral( "COUNT" ) ) ); + } } bool QgsSumLineLengthAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) @@ -136,17 +148,35 @@ bool QgsSumLineLengthAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsFields QgsSumLineLengthAlgorithm::outputFields( const QgsFields &inputFields ) const { - QgsFields outFields = inputFields; - mLengthFieldIndex = inputFields.lookupField( mLengthFieldName ); - if ( mLengthFieldIndex < 0 ) - outFields.append( QgsField( mLengthFieldName, QVariant::Double ) ); + if ( mIsInPlace ) + { + mLengthFieldIndex = mLengthFieldName.isEmpty() ? -1 : inputFields.lookupField( mLengthFieldName ); + mCountFieldIndex = mCountFieldName.isEmpty() ? -1 : inputFields.lookupField( mCountFieldName ); + return inputFields; + } + else + { + QgsFields outFields = inputFields; + mLengthFieldIndex = inputFields.lookupField( mLengthFieldName ); + if ( mLengthFieldIndex < 0 ) + outFields.append( QgsField( mLengthFieldName, QVariant::Double ) ); - mCountFieldIndex = inputFields.lookupField( mCountFieldName ); - if ( mCountFieldIndex < 0 ) - outFields.append( QgsField( mCountFieldName, QVariant::Double ) ); + mCountFieldIndex = inputFields.lookupField( mCountFieldName ); + if ( mCountFieldIndex < 0 ) + outFields.append( QgsField( mCountFieldName, QVariant::Double ) ); - mFields = outFields; - return outFields; + mFields = outFields; + return outFields; + } +} + +bool QgsSumLineLengthAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const +{ + if ( const QgsVectorLayer *vl = qobject_cast< const QgsVectorLayer * >( layer ) ) + { + return vl->geometryType() == QgsWkbTypes::PolygonGeometry; + } + return false; } QgsFeatureList QgsSumLineLengthAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) @@ -155,14 +185,14 @@ QgsFeatureList QgsSumLineLengthAlgorithm::processFeature( const QgsFeature &feat if ( !feature.hasGeometry() ) { QgsAttributes attrs = feature.attributes(); - if ( mLengthFieldIndex < 0 ) + if ( !mIsInPlace && mLengthFieldIndex < 0 ) attrs.append( 0 ); - else + else if ( mLengthFieldIndex >= 0 ) attrs[mLengthFieldIndex] = 0; - if ( mCountFieldIndex < 0 ) + if ( !mIsInPlace && mCountFieldIndex < 0 ) attrs.append( 0 ); - else + else if ( mCountFieldIndex >= 0 ) attrs[mCountFieldIndex] = 0; outputFeature.setAttributes( attrs ); @@ -196,14 +226,14 @@ QgsFeatureList QgsSumLineLengthAlgorithm::processFeature( const QgsFeature &feat } QgsAttributes attrs = feature.attributes(); - if ( mLengthFieldIndex < 0 ) + if ( !mIsInPlace && mLengthFieldIndex < 0 ) attrs.append( length ); - else + else if ( mLengthFieldIndex >= 0 ) attrs[mLengthFieldIndex] = length; - if ( mCountFieldIndex < 0 ) + if ( !mIsInPlace && mCountFieldIndex < 0 ) attrs.append( count ); - else + else if ( mCountFieldIndex >= 0 ) attrs[mCountFieldIndex] = count; outputFeature.setAttributes( attrs ); diff --git a/src/analysis/processing/qgsalgorithmsumlinelength.h b/src/analysis/processing/qgsalgorithmsumlinelength.h index 2a827175981e..92d472224984 100644 --- a/src/analysis/processing/qgsalgorithmsumlinelength.h +++ b/src/analysis/processing/qgsalgorithmsumlinelength.h @@ -56,9 +56,10 @@ class QgsSumLineLengthAlgorithm : public QgsProcessingFeatureBasedAlgorithm bool prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; QgsFields outputFields( const QgsFields &inputFields ) const override; + bool supportInPlaceEdit( const QgsMapLayer *layer ) const override; private: - + bool mIsInPlace = false; QString mLengthFieldName; QString mCountFieldName; mutable int mLengthFieldIndex = -1;