Skip to content

Commit

Permalink
[processing] native save selected features algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Sep 15, 2017
1 parent 625c205 commit 04ee85f
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 79 deletions.
5 changes: 0 additions & 5 deletions python/plugins/processing/algs/help/qgis.yaml
Expand Up @@ -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.

Expand Down
2 changes: 0 additions & 2 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -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
Expand Down Expand Up @@ -266,7 +265,6 @@ def getAlgs(self):
Relief(),
ReverseLineDirection(),
Ruggedness(),
SaveSelectedFeatures(),
SelectByAttribute(),
SelectByExpression(),
ServiceAreaFromLayer(),
Expand Down
72 changes: 0 additions & 72 deletions python/plugins/processing/algs/qgis/SaveSelectedFeatures.py

This file was deleted.

61 changes: 61 additions & 0 deletions src/core/processing/qgsnativealgorithms.cpp
Expand Up @@ -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" ) ) );
Expand Down
23 changes: 23 additions & 0 deletions src/core/processing/qgsnativealgorithms.h
Expand Up @@ -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.
*/
Expand Down

0 comments on commit 04ee85f

Please sign in to comment.