Skip to content

Commit

Permalink
Add option to save only selected features in package layers algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
stefancon authored and nyalldawson committed Feb 19, 2021
1 parent c8d6149 commit fc3eccd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/analysis/processing/qgsalgorithmpackage.cpp
Expand Up @@ -57,6 +57,7 @@ void QgsPackageAlgorithm::initAlgorithm( const QVariantMap & )
addParameter( outputParameter );
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "OVERWRITE" ), QObject::tr( "Overwrite existing GeoPackage" ), false ) );
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "SAVE_STYLES" ), QObject::tr( "Save layer styles into GeoPackage" ), true ) );
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "SELECTED_FEATURES_ONLY" ), QObject::tr( "Save only selected features" ), false ) );
addOutput( new QgsProcessingOutputMultipleLayers( QStringLiteral( "OUTPUT_LAYERS" ), QObject::tr( "Layers within new package" ) ) );
}

Expand Down Expand Up @@ -88,6 +89,7 @@ QVariantMap QgsPackageAlgorithm::processAlgorithm( const QVariantMap &parameters
{
const bool overwrite = parameterAsBoolean( parameters, QStringLiteral( "OVERWRITE" ), context );
const bool saveStyles = parameterAsBoolean( parameters, QStringLiteral( "SAVE_STYLES" ), context );
const bool selectedFeaturesOnly = parameterAsBoolean( parameters, QStringLiteral( "SELECTED_FEATURES_ONLY" ), context );
QString packagePath = parameterAsString( parameters, QStringLiteral( "OUTPUT" ), context );
if ( packagePath.isEmpty() )
throw QgsProcessingException( QObject::tr( "No output file specified." ) );
Expand Down Expand Up @@ -153,7 +155,7 @@ QVariantMap QgsPackageAlgorithm::processAlgorithm( const QVariantMap &parameters
case QgsMapLayerType::VectorLayer:
{
if ( !packageVectorLayer( qobject_cast< QgsVectorLayer * >( layer.get() ), packagePath,
context, &multiStepFeedback, saveStyles ) )
context, &multiStepFeedback, saveStyles, selectedFeaturesOnly ) )
errored = true;
else
outputLayers.append( QStringLiteral( "%1|layername=%2" ).arg( packagePath, layer->name() ) );
Expand Down Expand Up @@ -210,13 +212,14 @@ QVariantMap QgsPackageAlgorithm::processAlgorithm( const QVariantMap &parameters
}

bool QgsPackageAlgorithm::packageVectorLayer( QgsVectorLayer *layer, const QString &path, QgsProcessingContext &context,
QgsProcessingFeedback *feedback, bool saveStyles )
QgsProcessingFeedback *feedback, bool saveStyles, bool selectedFeaturesOnly )
{
QgsVectorFileWriter::SaveVectorOptions options;
options.driverName = QStringLiteral( "GPKG" );
options.layerName = layer->name();
options.actionOnExistingFile = QgsVectorFileWriter::CreateOrOverwriteLayer;
options.fileEncoding = context.defaultEncoding();
options.onlySelectedFeatures = selectedFeaturesOnly;
options.feedback = feedback;

// remove any existing FID field, let this be completely recreated
Expand Down
3 changes: 2 additions & 1 deletion src/analysis/processing/qgsalgorithmpackage.h
Expand Up @@ -54,7 +54,8 @@ class QgsPackageAlgorithm : public QgsProcessingAlgorithm

private:

bool packageVectorLayer( QgsVectorLayer *layer, const QString &path, QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool saveStyles );
bool packageVectorLayer( QgsVectorLayer *layer, const QString &path, QgsProcessingContext &context, QgsProcessingFeedback *feedback,
bool saveStyles, bool selectedFeaturesOnly );

std::vector< std::unique_ptr< QgsMapLayer> > mLayers;

Expand Down

0 comments on commit fc3eccd

Please sign in to comment.