Skip to content
Permalink
Browse files

[processing] native save selected features algorithm

  • Loading branch information
nirvn committed Sep 15, 2017
1 parent 625c205 commit 04ee85f0e8e8f938cf4504023cc2d67b64638d43
@@ -499,11 +499,6 @@ qgis:reprojectlayer: >

Attributes are not modified by this algorithm.

qgis:saveselectedfeatures: >
This algorithm creates a new layer with all the selected features in a given vector layer.

If the selected layer has no selected features, all features will be added to the resulting feature.

qgis:selectbyattribute: >
This algorithm creates a selection in a vector layer. The criteria for selected features is defined based on the values of an attribute from the input layer.

@@ -129,7 +129,6 @@
from .Relief import Relief
from .ReverseLineDirection import ReverseLineDirection
from .Ruggedness import Ruggedness
from .SaveSelectedFeatures import SaveSelectedFeatures
from .SelectByAttribute import SelectByAttribute
from .SelectByExpression import SelectByExpression
from .ServiceAreaFromLayer import ServiceAreaFromLayer
@@ -266,7 +265,6 @@ def getAlgs(self):
Relief(),
ReverseLineDirection(),
Ruggedness(),
SaveSelectedFeatures(),
SelectByAttribute(),
SelectByExpression(),
ServiceAreaFromLayer(),

This file was deleted.

@@ -80,10 +80,71 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsExtractByLocationAlgorithm() );
addAlgorithm( new QgsFixGeometriesAlgorithm() );
addAlgorithm( new QgsMergeLinesAlgorithm() );
addAlgorithm( new QgsSaveSelectedFeatures() );
addAlgorithm( new QgsSmoothAlgorithm() );
addAlgorithm( new QgsSimplifyAlgorithm() );
}

void QgsSaveSelectedFeatures::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Selected features" ), QgsProcessing::TypeVectorPoint ) );
}

QString QgsSaveSelectedFeatures::shortHelpString() const
{
return QObject::tr( "This algorithm creates a new layer with all the selected features in a given vector layer.\n\n"
"If the selected layer has no selected features, all features will be added to the resulting feature." );
}

QgsSaveSelectedFeatures *QgsSaveSelectedFeatures::createInstance() const
{
return new QgsSaveSelectedFeatures();
}

QVariantMap QgsSaveSelectedFeatures::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );

QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, selectLayer->fields(), selectLayer->wkbType(), selectLayer->sourceCrs() ) );
if ( !sink )
return QVariantMap();

QgsFeatureIterator it;
QgsFeature feat;

int count = selectLayer->selectedFeatureCount();
if ( count > 0 )
{
it = selectLayer->getSelectedFeatures();
}
else
{
count = selectLayer->featureCount();
it = selectLayer->getFeatures();
}

int current = 0;
double step = count > 0 ? 100.0 / count : 1;

while ( it.nextFeature( feat ) )
{
if ( feedback->isCanceled() )
{
break;
}

sink->addFeature( feat, QgsFeatureSink::FastInsert );

feedback->setProgress( current++ * step );
}

QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), dest );
return outputs;
}

void QgsCentroidAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
@@ -48,6 +48,29 @@ class QgsNativeAlgorithms: public QgsProcessingProvider

};

/**
* Native save selected features algorithm.
*/
class QgsSaveSelectedFeatures : public QgsProcessingAlgorithm
{

public:

QgsSaveSelectedFeatures() = default;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override { return QStringLiteral( "saveselectedfeatures" ); }
QString displayName() const override { return QObject::tr( "Save Selected Features" ); }
QStringList tags() const override { return QObject::tr( "selection,save" ).split( ',' ); }
QString group() const override { return QObject::tr( "Vector general" ); }
QString shortHelpString() const override;
QgsSaveSelectedFeatures *createInstance() const override SIP_FACTORY;

protected:

virtual QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
};

/**
* Native centroid algorithm.
*/

0 comments on commit 04ee85f

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