-
-
Save roya0045/810217a641126660eee57e0a6a7fdbbb to your computer and use it in GitHub Desktop.
WIP iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************************************************************************** | |
* * | |
* This program is free software; you can redistribute it and/or modify * | |
* it under the terms of the GNU General Public License as published by * | |
* the Free Software Foundation; either version 2 of the License, or * | |
* (at your option) any later version. * | |
* * | |
***************************************************************************/ | |
#include "qgsprocessingmodeliterator.h" | |
#include "qgsapplication.h" | |
#include "qgsprocessingregistry.h" | |
#include "qgsprocessingmodelalgorithm.h" | |
QString QgsValueIterator::name() const | |
{ | |
return QStringLiteral( "valueloop" ); | |
} | |
QString QgsValueIterator::displayName() const | |
{ | |
return QObject::tr( "Value iterator" ); | |
} | |
QStringList QgsValueIterator::tags() const | |
{ | |
return QObject::tr( "value,iterator" ).split( ',' ); | |
} | |
QString QgsValueIterator::group() const | |
{ | |
return QObject::tr( "Iterators" ); | |
} | |
void QgsValueIterator::initAlgorithm( const QVariantMap & ) | |
{ | |
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "FROM" ), QObject::tr( "From" ), QgsProcessingParameterNumber::Integer ) ); | |
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "TO" ), QObject::tr( "To" ), QgsProcessingParameterNumber::Integer ) ); | |
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "STEP" ), QObject::tr( "Step" ), QgsProcessingParameterNumber::Integer ) ); | |
addOutput( new QgsProcessingOutputNumber( QStringLiteral( "CURRENT_VALUE" ), QObject::tr( "Current value" ) ) ); | |
} | |
QString QgsValueIterator::shortHelpString() const | |
{ | |
return QObject::tr( "This algorithm iterate over a for loop." ); | |
} | |
QgsForLoopIterator *QgsValueIterator::createInstance() const | |
{ | |
return new QgsForLoopIterator(); | |
} | |
bool QgsValueIterator::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) | |
{ | |
mCurrentValue = parameterAsInt( parameters, "FROM", context ); | |
mStep = parameterAsInt( parameters, "STEP", context ); | |
mTo = parameterAsInt( parameters, "TO", context ); | |
return true; | |
} | |
QVariantMap QgsValueIterator::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) | |
{ | |
QVariantMap outputs; | |
outputs.insert( QStringLiteral( "CURRENT_VALUE" ), mCurrentValue ); | |
return outputs; | |
} | |
bool QgsValueIterator::next() | |
{ | |
mCurrentValue += mStep; | |
return mCurrentValue != mTo; | |
} | |
///@endcond | |
#include <QDirIterator> | |
QString QgsFileIterator::name() const | |
{ | |
return QStringLiteral( "fileiterator" ); | |
} | |
QString QgsFileIterator::displayName() const | |
{ | |
return QObject::tr( "File iterator" ); | |
} | |
QStringList QgsFileIterator::tags() const | |
{ | |
return QObject::tr( "file,folder,iterator" ).split( ',' ); | |
} | |
QString QgsFileIterator::group() const | |
{ | |
return QObject::tr( "Iterators" ); | |
} | |
void QgsFileIterator::initAlgorithm( const QVariantMap & ) | |
{ | |
addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "SOURCE" ), QObject::tr( "Source" ), QgsProcessingParameterVectorLayer ) ); | |
addParameter( new QgsProcessingParameterString( QStringLiteral( "EXPRESSION" ), QObject::tr( "Expression" ), QgsProcessingParameterExpression ) ); | |
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "RECURSIVE" ), QObject::tr( "Recursive" ), QgsProcessingParameterBoolean::Boolean ) ); | |
addParameter( new QgsProcessingParameterString( QStringLiteral( "TYPE" ), QObject::tr( "Type" ), QgsProcessingParameterString::String ) ); | |
#type | |
addOutput( new QgsProcessingOutputNumber( QStringLiteral( "CURRENT_FIlE" ), QObject::tr( "Current file" ) ) ); | |
} | |
QString QgsFileIterator::shortHelpString() const | |
{ | |
return QObject::tr( "This algorithm iterate over a folder." ); | |
} | |
QgsFileIterator *QgsFileIterator::createInstance() const | |
{ | |
return new QgsFileIterator(); | |
} | |
#todo QDir | |
bool QgsFileIterator::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) | |
{ | |
mSource = parameterAsInt( parameters, "SOURCE", context ); | |
mExp = parameterAsString( parameters, "EXPRESSION", context ); | |
mRec = parameterAsBool( parameters, "RECURSIVE", context ); | |
mType = parameterAsInt( parameters, "TYPE", context ); | |
QStringList exp; | |
if ( ! mExp.isEmpty() ) | |
exp << mExp; | |
switch( mType ) | |
{ | |
case mType=="VectorLayer": /// check vector type? | |
exp << "*.shp"; | |
case mType=="RasterLayer": | |
exp << "*.tiff" << "*.png" << "*.geotif"; | |
case mType == "MeshLayer": | |
exp << "*.???"; | |
} | |
if ( mRec ) | |
mIterator=QDirIterator( mSource, exp,QDirIterator::Subdirectories) | |
else | |
mIterator=QDirIterator( mSource, exp,QDirIterator::NoIteratorFlags) | |
return true; | |
} | |
QVariantMap QgsFileIterator::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) | |
{ | |
QVariantMap outputs; | |
outputs.insert( QStringLiteral( "CURRENT_FILE" ), mCurrentValue ); | |
return outputs; | |
} | |
bool QgsFileIterator::next() | |
{ | |
if ( mIterator.hasNext() ) | |
{ | |
mCurrentValue = mIterator.next(); | |
return true; | |
} | |
return false | |
} | |
///@endcond | |
QString QgsModelFeatureIterator::name() const | |
{ | |
return QStringLiteral( "featureiterator" ); | |
} | |
QString QgsModelFeatureIterator::displayName() const | |
{ | |
return QObject::tr( "Feature iterator" ); | |
} | |
QStringList QgsModelFeatureIterator::tags() const | |
{ | |
return QObject::tr( "feature,iterator" ).split( ',' ); | |
} | |
QString QgsModelFeatureIterator::group() const | |
{ | |
return QObject::tr( "Iterators" ); | |
} | |
void QgsModelFeatureIterator::initAlgorithm( const QVariantMap & ) | |
{ | |
addParameter( new QgsProcessingParameterVectorLayer QStringLiteral( "VECTORLAYER" ), QObject::tr( "Vector layer" ), QgsProcessingParameterVectorLayer ) ); | |
addParameter( new QgsProcessingParameterField( QStringLiteral( "FIELD" ), QObject::tr( "Field" ), QgsProcessingParameterField ) ); | |
addParameter( new QgsProcessingParameterExpression( QStringLiteral( "EXPRESSION" ), QObject::tr( "Expression" ), QgsProcessingParameterExpression ) ); | |
addOutput( new QgsProcessingOutputNumber( QStringLiteral( "CURRENT_VALUE" ), QObject::tr( "Current value" ) ) ); | |
} | |
QString QgsModelFeatureIterator::shortHelpString() const | |
{ | |
return QObject::tr( "This algorithm iterate over features of a layer." ); | |
} | |
QgsModelFeatureIterator *QgsModelFeatureIterator::createInstance() const | |
{ | |
return new QgsModelFeatureIterator(); | |
} | |
bool QgsModelFeatureIterator::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) | |
{ | |
mLayer = parameterAsVectorLayer( parameters, "VECTORLAYER", context ); | |
mField = parameterAsFields( parameters, "FIELD", context ); | |
mExp = parameterAsExpression( parameters, "EXPRESSION", context ); | |
mRequest = QgsFatureRequest(); | |
if(! mExp.isEmpty() ) | |
mRequest.setFilterExpression(mExp); | |
if( ! mField.isEmpty() ) | |
mRequest.setSubsetOfAttributes( fields: mField ); | |
mIter = layer.getFeatures(mRequest); | |
return true; | |
} | |
QVariantMap QgsModelFeatureIterator::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) | |
{ | |
QVariantMap outputs; | |
outputs.insert( QStringLiteral( "CURRENT_VALUE" ), mCurrentValue ); | |
return outputs; | |
} | |
bool QgsModelFeatureIterator::next() | |
{ | |
mCurrentValue += mStep; | |
return mCurrentValue != mTo; | |
} | |
///@endcond | |
QString QgsLayersIterator:name() const | |
{ | |
return QStringLiteral( "layeriterator" ); | |
} | |
QString QgsLayersIterator::displayName() const | |
{ | |
return QObject::tr( "Layer iterator" ); | |
} | |
QStringList QgsLayersIterator::tags() const | |
{ | |
return QObject::tr( "layer,iterator" ).split( ',' ); | |
} | |
QString QgsLayersIterator::group() const | |
{ | |
return QObject::tr( "Iterators" ); | |
} | |
void QgsLayersIterator::initAlgorithm( const QVariantMap & ) | |
{ | |
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "LAYERS" ), QObject::tr( "Layers" ), QgsProcessingParameterMultipleLayers ) ); | |
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "TYPE" ), QObject::tr( "Type" ), QgsProcessingParameterString ) ); | |
addOutput( new QgsProcessingOutputNumber( QStringLiteral( "CURRENT_VALUE" ), QObject::tr( "Current value" ) ) ); | |
} | |
QString QgsLayersIterator::shortHelpString() const | |
{ | |
return QObject::tr( "This algorithm iterate over a list of layers." ); | |
} | |
QgsLayersIterator *QgsLayersIterator::createInstance() const | |
{ | |
return new QgsLayersIterator(); | |
} | |
bool QgsLayersIterator::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) | |
{ | |
mCurrentValue = parameterMultipleLayers( parameters, "LAYERS", context ); | |
mStep = parameterAsString( parameters, "TYPE", context ); | |
return true; | |
} | |
QVariantMap QgsLayersIterator::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) | |
{ | |
QVariantMap outputs; | |
outputs.insert( QStringLiteral( "CURRENT_VALUE" ), mCurrentValue ); | |
return outputs; | |
} | |
bool QgsLayersIterator::next() | |
{ | |
mCurrentValue += mStep; | |
return mCurrentValue != mTo; | |
} | |
///@endcond | |
for (const auto item ; ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment