Skip to content

Commit

Permalink
[processing] Ask users for existing destination fields for
Browse files Browse the repository at this point in the history
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

(cherry picked from commit 9297938)
  • Loading branch information
nyalldawson committed Feb 19, 2021
1 parent ac6df58 commit e2f9a06
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 33 deletions.
45 changes: 36 additions & 9 deletions src/analysis/processing/qgsalgorithmpointsinpolygon.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}


Expand Down
3 changes: 2 additions & 1 deletion src/analysis/processing/qgsalgorithmpointsinpolygon.h
Expand Up @@ -57,9 +57,10 @@ class QgsPointsInPolygonAlgorithm : public QgsProcessingFeatureBasedAlgorithm
bool prepareAlgorithm( const QVariantMap &parameters, 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;
Expand Down
74 changes: 52 additions & 22 deletions src/analysis/processing/qgsalgorithmsumlinelength.cpp
Expand Up @@ -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 &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
Expand All @@ -136,17 +148,35 @@ bool QgsSumLineLengthAlgorithm::prepareAlgorithm( const QVariantMap &parameters,

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 )
Expand All @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
3 changes: 2 additions & 1 deletion src/analysis/processing/qgsalgorithmsumlinelength.h
Expand Up @@ -56,9 +56,10 @@ class QgsSumLineLengthAlgorithm : public QgsProcessingFeatureBasedAlgorithm
bool prepareAlgorithm( const QVariantMap &parameters, 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;
Expand Down

0 comments on commit e2f9a06

Please sign in to comment.