Skip to content
Permalink
Browse files

Add option to save only selected features in package layers algorithm

  • Loading branch information
stefancon authored and nyalldawson committed Feb 19, 2021
1 parent c8d6149 commit fc3eccd62370339e0ea0167f6dd760d2cf9a9c55
@@ -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" ) ) );
}

@@ -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." ) );
@@ -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() ) );
@@ -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
@@ -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;

0 comments on commit fc3eccd

Please sign in to comment.