Skip to content
Permalink
Browse files

Move fieldNamesToIndices() and indicesToFields() to QgsProcessingUtils

  • Loading branch information
wonder-sk committed Apr 26, 2018
1 parent 21a756f commit 091ed912ab91b528cc34126e1bac4e0f498570b9
@@ -210,6 +210,21 @@ non-duplicate name.
be truncated when saving to these formats.
%End

static QList<int> fieldNamesToIndices( const QStringList &fieldNames, const QgsFields &fields );
%Docstring
Returns a list of field indices parsed from the given list of field names. Unknown field names are ignored.
If the list of field names is empty, it is assumed that all fields are required.

.. versionadded:: 3.2
%End

static QgsFields indicesToFields( const QList<int> &indices, const QgsFields &fields );
%Docstring
Returns a subset of fields based on the indices of desired fields.

.. versionadded:: 3.2
%End

};

class QgsProcessingFeatureSource : QgsFeatureSource
@@ -88,12 +88,12 @@ QVariantMap QgsIntersectionAlgorithm::processAlgorithm( const QVariantMap &param
const QStringList fieldsA = parameterAsFields( parameters, QStringLiteral( "INPUT_FIELDS" ), context );
const QStringList fieldsB = parameterAsFields( parameters, QStringLiteral( "OVERLAY_FIELDS" ), context );

QList<int> fieldIndicesA = QgsOverlayUtils::fieldNamesToIndices( fieldsA, sourceA->fields() );
QList<int> fieldIndicesB = QgsOverlayUtils::fieldNamesToIndices( fieldsB, sourceB->fields() );
QList<int> fieldIndicesA = QgsProcessingUtils::fieldNamesToIndices( fieldsA, sourceA->fields() );
QList<int> fieldIndicesB = QgsProcessingUtils::fieldNamesToIndices( fieldsB, sourceB->fields() );

QgsFields outputFields = QgsProcessingUtils::combineFields(
QgsOverlayUtils::indicesToFields( fieldIndicesA, sourceA->fields() ),
QgsOverlayUtils::indicesToFields( fieldIndicesB, sourceB->fields() ) );
QgsProcessingUtils::indicesToFields( fieldIndicesA, sourceA->fields() ),
QgsProcessingUtils::indicesToFields( fieldIndicesB, sourceB->fields() ) );

QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, outputFields, geomType, sourceA->sourceCrs() ) );
@@ -79,8 +79,8 @@ QVariantMap QgsUnionAlgorithm::processAlgorithm( const QVariantMap &parameters,
QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), dest );

QList<int> fieldIndicesA = QgsOverlayUtils::fieldNamesToIndices( QStringList(), sourceA->fields() );
QList<int> fieldIndicesB = QgsOverlayUtils::fieldNamesToIndices( QStringList(), sourceB->fields() );
QList<int> fieldIndicesA = QgsProcessingUtils::fieldNamesToIndices( QStringList(), sourceA->fields() );
QList<int> fieldIndicesB = QgsProcessingUtils::fieldNamesToIndices( QStringList(), sourceB->fields() );

int count = 0;
int total = sourceA->featureCount() * 2 + sourceB->featureCount();
@@ -218,33 +218,4 @@ void QgsOverlayUtils::intersection( const QgsFeatureSource &sourceA, const QgsFe
}
}


QList<int> QgsOverlayUtils::fieldNamesToIndices( const QStringList &fieldNames, const QgsFields &fields )
{
QList<int> indices;
if ( !fieldNames.isEmpty() )
{
for ( const QString &f : fieldNames )
{
int idx = fields.lookupField( f );
if ( idx >= 0 )
indices.append( idx );
}
}
else
{
for ( int i = 0; i < fields.count(); ++i )
indices.append( i );
}
return indices;
}

QgsFields QgsOverlayUtils::indicesToFields( const QList<int> &indices, const QgsFields &fields )
{
QgsFields fieldsSubset;
for ( int i : indices )
fieldsSubset.append( fields.at( i ) );
return fieldsSubset;
}

///@endcond PRIVATE
@@ -43,9 +43,6 @@ namespace QgsOverlayUtils

void intersection( const QgsFeatureSource &sourceA, const QgsFeatureSource &sourceB, QgsFeatureSink &sink, QgsProcessingContext &context, QgsProcessingFeedback *feedback, int &count, int totalCount, const QList<int> &fieldIndicesA, const QList<int> &fieldIndicesB );

QList<int> fieldNamesToIndices( const QStringList &fieldNames, const QgsFields &fields );

QgsFields indicesToFields( const QList<int> &indices, const QgsFields &fields );
}

///@endcond PRIVATE
@@ -649,6 +649,36 @@ QgsFields QgsProcessingUtils::combineFields( const QgsFields &fieldsA, const Qgs
}


QList<int> QgsProcessingUtils::fieldNamesToIndices( const QStringList &fieldNames, const QgsFields &fields )
{
QList<int> indices;
if ( !fieldNames.isEmpty() )
{
for ( const QString &f : fieldNames )
{
int idx = fields.lookupField( f );
if ( idx >= 0 )
indices.append( idx );
}
}
else
{
for ( int i = 0; i < fields.count(); ++i )
indices.append( i );
}
return indices;
}


QgsFields QgsProcessingUtils::indicesToFields( const QList<int> &indices, const QgsFields &fields )
{
QgsFields fieldsSubset;
for ( int i : indices )
fieldsSubset.append( fields.at( i ) );
return fieldsSubset;
}


//
// QgsProcessingFeatureSource
//
@@ -241,6 +241,19 @@ class CORE_EXPORT QgsProcessingUtils
*/
static QgsFields combineFields( const QgsFields &fieldsA, const QgsFields &fieldsB );

/**
* Returns a list of field indices parsed from the given list of field names. Unknown field names are ignored.
* If the list of field names is empty, it is assumed that all fields are required.
* \since QGIS 3.2
*/
static QList<int> fieldNamesToIndices( const QStringList &fieldNames, const QgsFields &fields );

/**
* Returns a subset of fields based on the indices of desired fields.
* \since QGIS 3.2
*/
static QgsFields indicesToFields( const QList<int> &indices, const QgsFields &fields );

private:

static bool canUseLayer( const QgsRasterLayer *layer );

0 comments on commit 091ed91

Please sign in to comment.
You can’t perform that action at this time.