Skip to content

Commit

Permalink
Merge pull request #5204 from nirvn/saveselected_native
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Sep 16, 2017
2 parents f9bc925 + 0cdabb5 commit 01468d0
Show file tree
Hide file tree
Showing 5 changed files with 74 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.

51 changes: 51 additions & 0 deletions src/core/processing/qgsnativealgorithms.cpp
Expand Up @@ -80,12 +80,63 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsExtractByLocationAlgorithm() );
addAlgorithm( new QgsFixGeometriesAlgorithm() );
addAlgorithm( new QgsMergeLinesAlgorithm() );
addAlgorithm( new QgsSaveSelectedFeatures() );
addAlgorithm( new QgsSmoothAlgorithm() );
addAlgorithm( new QgsSimplifyAlgorithm() );
addAlgorithm( new QgsExtractByExtentAlgorithm() );
addAlgorithm( new QgsExtentToLayerAlgorithm() );
}

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, the newly created layer will be empty." );
}

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();


int count = selectLayer->selectedFeatureCount();
int current = 0;
double step = count > 0 ? 100.0 / count : 1;

QgsFeatureIterator it = selectLayer->getSelectedFeatures();;
QgsFeature feat;
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 01468d0

Please sign in to comment.