27QString QgsPackageAlgorithm::name()
const
29 return QStringLiteral(
"package" );
32QString QgsPackageAlgorithm::displayName()
const
34 return QObject::tr(
"Package layers" );
37QStringList QgsPackageAlgorithm::tags()
const
39 return QObject::tr(
"geopackage,collect,merge,combine,styles" ).split(
',' );
42QString QgsPackageAlgorithm::group()
const
44 return QObject::tr(
"Database" );
47QString QgsPackageAlgorithm::groupId()
const
49 return QStringLiteral(
"database" );
52void QgsPackageAlgorithm::initAlgorithm(
const QVariantMap & )
56 outputParameter->
setMetadata( QVariantMap( {{QStringLiteral(
"widget_wrapper" ), QVariantMap( {{QStringLiteral(
"dontconfirmoverwrite" ),
true }} ) }} ) );
57 addParameter( outputParameter );
59 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"SAVE_STYLES" ), QObject::tr(
"Save layer styles into GeoPackage" ),
true ) );
60 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"SAVE_METADATA" ), QObject::tr(
"Save layer metadata into GeoPackage" ),
true ) );
61 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"SELECTED_FEATURES_ONLY" ), QObject::tr(
"Save only selected features" ),
false ) );
62 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"EXPORT_RELATED_LAYERS" ), QObject::tr(
"Export related layers following relations defined in the project" ),
false ) );
66QString QgsPackageAlgorithm::shortHelpString()
const
68 return QObject::tr(
"This algorithm collects a number of existing layers and packages them together into a single GeoPackage database." );
71QgsPackageAlgorithm *QgsPackageAlgorithm::createInstance()
const
73 return new QgsPackageAlgorithm();
79 const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral(
"LAYERS" ), context );
81 for (
const QgsMapLayer *layer : std::as_const( layers ) )
84 mClonedLayerIds.insert( clonedLayer->id(), layer->id( ) );
85 mLayers.emplace_back( clonedLayer );
88 if ( mLayers.empty() )
90 feedback->
reportError( QObject::tr(
"No layers selected, geopackage will be empty" ),
false );
98 const bool overwrite = parameterAsBoolean( parameters, QStringLiteral(
"OVERWRITE" ), context );
99 const bool saveStyles = parameterAsBoolean( parameters, QStringLiteral(
"SAVE_STYLES" ), context );
100 const bool saveMetadata = parameterAsBoolean( parameters, QStringLiteral(
"SAVE_METADATA" ), context );
101 const bool selectedFeaturesOnly = parameterAsBoolean( parameters, QStringLiteral(
"SELECTED_FEATURES_ONLY" ), context );
102 const bool exportRelatedLayers = parameterAsBoolean( parameters, QStringLiteral(
"EXPORT_RELATED_LAYERS" ), context );
103 const QString packagePath = parameterAsString( parameters, QStringLiteral(
"OUTPUT" ), context );
104 const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral(
"LAYERS" ), context );
106 if ( packagePath.isEmpty() )
110 if ( overwrite && QFile::exists( packagePath ) )
112 feedback->
pushInfo( QObject::tr(
"Removing existing file '%1'" ).arg( packagePath ) );
113 if ( !QFile( packagePath ).remove() )
119 OGRSFDriverH hGpkgDriver = OGRGetDriverByName(
"GPKG" );
127 if ( exportRelatedLayers )
130 if ( project && ! project->relationManager()->relations().isEmpty() )
134 const int maxRecursion { 10 };
135 int recursionGuard { 0 };
138 const auto findReferenced = [ =, &project, &feedback, &recursionGuard, &layers ](
const QgsVectorLayer * vLayer,
bool onlySaveSelected,
auto &&findReferenced ) ->
void
140 const QgsVectorLayer *originalLayer { qobject_cast<QgsVectorLayer *>( project->mapLayer( mClonedLayerIds.value( vLayer->id(), vLayer->id() ) ) ) };
141 Q_ASSERT( originalLayer );
142 const QList<QgsRelation> relations { project->relationManager()->referencingRelations( originalLayer ) };
143 for (
const QgsRelation &relation : std::as_const( relations ) )
150 bool alreadyAdded {
false };
151 for (
const auto &layerToExport : std::as_const( mLayers ) )
153 const QString originalId { mClonedLayerIds.value( layerToExport->id() ) };
154 if ( originalId == referencedLayer->id() )
156 relatedLayer = qobject_cast<QgsVectorLayer *>( layerToExport.get() );
164 feedback->pushInfo( QObject::tr(
"Adding referenced layer '%1'" ).arg( referencedLayer->name() ) );
165 relatedLayer = referencedLayer->clone();
166 mLayers.emplace_back( relatedLayer );
167 mClonedLayerIds.insert( relatedLayer->id(), referencedLayer->id() );
172 if ( onlySaveSelected )
174 if ( ! layers.contains( qobject_cast<QgsMapLayer *>( referencedLayer ) ) || referencedLayer->selectedFeatureCount() > 0 )
176 Q_ASSERT( relatedLayer );
180 while ( it.nextFeature( selectedFeature ) )
182 QgsFeature referencedFeature { relation.getReferencedFeature( selectedFeature ) };
183 if ( referencedFeature.isValid() )
185 selected.insert( referencedFeature.id() );
193 if ( recursionGuard > maxRecursion )
195 feedback->pushWarning( QObject::tr(
"Max recursion (%1) adding referenced layer '%2', layer was not added" ).arg( QLocale().toString( recursionGuard ), referencedLayer->name() ) );
200 findReferenced( relatedLayer, onlySaveSelected, findReferenced );
207 const auto findReferencing = [ =, &project, &feedback, &recursionGuard, &layers ](
const QgsVectorLayer * vLayer,
bool onlySaveSelected,
auto &&findReferencing ) ->
void
209 const QgsVectorLayer *originalLayer { qobject_cast<QgsVectorLayer *>( project->mapLayer( mClonedLayerIds.value( vLayer->id(), vLayer->id() ) ) ) };
210 Q_ASSERT( originalLayer );
211 const QList<QgsRelation> relations { project->relationManager()->referencedRelations( originalLayer ) };
212 for (
const QgsRelation &relation : std::as_const( relations ) )
217 const bool layerWasExplicitlyAdded { layers.contains( qobject_cast<QgsMapLayer *>( referencingLayer ) ) };
220 bool alreadyAdded {
false };
221 for (
const auto &layerToExport : std::as_const( mLayers ) )
223 const QString originalId { mClonedLayerIds.value( layerToExport->id() ) };
224 if ( originalId == referencingLayer->id() )
226 relatedLayer = qobject_cast<QgsVectorLayer *>( layerToExport.get() );
236 if ( onlySaveSelected && ( ! layerWasExplicitlyAdded || referencingLayer->selectedFeatureCount() > 0 ) )
238 if ( ! layers.contains( qobject_cast<QgsMapLayer *>( referencingLayer ) ) || referencingLayer->selectedFeatureCount() > 0 )
242 while ( it.nextFeature( selectedFeature ) )
244 QgsFeatureIterator referencingFeaturesIterator { relation.getRelatedFeatures( selectedFeature ) };
246 while ( referencingFeaturesIterator.nextFeature( referencingFeature ) )
248 if ( referencingFeature.
isValid() )
250 selected.insert( referencingFeature.
id() );
257 if ( ! alreadyAdded && ( ! onlySaveSelected || ! selected.isEmpty() ) )
259 feedback->pushInfo( QObject::tr(
"Adding referencing layer '%1'" ).arg( referencingLayer->name() ) );
260 relatedLayer = referencingLayer->clone();
261 mLayers.emplace_back( relatedLayer );
262 mClonedLayerIds.insert( relatedLayer->id(), referencingLayer->id() );
265 if ( relatedLayer && ! selected.isEmpty() )
271 if ( recursionGuard > maxRecursion )
273 feedback->pushWarning( QObject::tr(
"Max recursion (%1) adding referencing layer '%2', layer was not added" ).arg( QLocale().toString( recursionGuard ), referencingLayer->name() ) );
275 else if ( relatedLayer )
278 findReferencing( relatedLayer, onlySaveSelected, findReferencing ) ;
284 for (
const QgsMapLayer *layer : std::as_const( layers ) )
286 const QgsVectorLayer *vLayer { qobject_cast<const QgsVectorLayer *>( layer ) };
291 findReferenced( vLayer, onlySaveSelected, findReferenced );
293 findReferencing( vLayer, onlySaveSelected, findReferencing );
302 if ( !QFile::exists( packagePath ) )
306 throw QgsProcessingException( QObject::tr(
"Creation of database %1 failed (OGR error: %2)" ).arg( packagePath, QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
312 throw QgsProcessingException( QObject::tr(
"Opening database %1 failed (OGR error: %2)" ).arg( packagePath, QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
316 bool errored =
false;
320 QStringList outputLayers;
323 for (
const auto &layer : mLayers )
325 if ( feedback->isCanceled() )
328 multiStepFeedback.setCurrentStep( i );
334 feedback->pushDebugInfo( QObject::tr(
"Error retrieving map layer." ) );
339 feedback->pushInfo( QObject::tr(
"Packaging layer %1/%2: %3" ).arg( i ).arg( mLayers.size() ).arg( layer ? layer->name() : QString() ) );
341 switch ( layer->type() )
345 QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( layer.get() );
347 if ( !packageVectorLayer( vectorLayer, packagePath, context, &multiStepFeedback, saveStyles, saveMetadata, onlySaveSelected ) )
350 outputLayers.append( QStringLiteral(
"%1|layername=%2" ).arg( packagePath, layer->name() ) );
357 feedback->pushDebugInfo( QObject::tr(
"Packaging raster layers is not supported." ) );
364 feedback->pushDebugInfo( QObject::tr(
"Packaging plugin layers is not supported." ) );
370 feedback->pushDebugInfo( QObject::tr(
"Packaging mesh layers is not supported." ) );
376 feedback->pushDebugInfo( QObject::tr(
"Packaging point cloud layers is not supported." ) );
382 feedback->pushDebugInfo( QObject::tr(
"Packaging vector tile layers is not supported." ) );
388 feedback->pushDebugInfo( QObject::tr(
"Packaging annotation layers is not supported." ) );
394 feedback->pushDebugInfo( QObject::tr(
"Packaging group layers is not supported." ) );
404 outputs.insert( QStringLiteral(
"OUTPUT" ), packagePath );
405 outputs.insert( QStringLiteral(
"OUTPUT_LAYERS" ), outputLayers );
413 options.
driverName = QStringLiteral(
"GPKG" );
429 const int fidIndex = fields.
lookupField( QStringLiteral(
"fid" ) );
434 const QVariant::Type fidType { layer->
fields().
field( fidIndex ).
type() };
435 if ( ! layer->
fieldConstraints( fidIndex ).testFlag( QgsFieldConstraints::Constraint::ConstraintUnique )
436 && ! layer->
fieldConstraints( fidIndex ).testFlag( QgsFieldConstraints::Constraint::ConstraintNotNull )
437 && fidType != QVariant::Int
438 && fidType != QVariant::UInt
439 && fidType != QVariant::LongLong
440 && fidType != QVariant::ULongLong )
457 feedback->
reportError( QObject::tr(
"Packaging layer failed: %1" ).arg( error ) );
464 std::unique_ptr< QgsVectorLayer > res = std::make_unique< QgsVectorLayer >( QStringLiteral(
"%1|layername=%2" ).arg( newFilename, newLayer ) );
468 QDomDocument doc( QStringLiteral(
"qgis" ) );
471 if ( !errorMsg.isEmpty() )
473 feedback->
reportError( QObject::tr(
"Could not retrieve existing layer style: %1 " ).arg( errorMsg ) );
477 if ( !res->importNamedStyle( doc, errorMsg ) )
479 feedback->
reportError( QObject::tr(
"Could not set existing layer style: %1 " ).arg( errorMsg ) );
485 const QVariant prevOverwriteStyle = settings.
value( QStringLiteral(
"qgis/overwriteStyle" ) );
486 settings.
setValue( QStringLiteral(
"qgis/overwriteStyle" ),
true );
487 res->saveStyleToDatabase( newLayer, QString(),
true, QString(), errorMsg );
488 settings.
setValue( QStringLiteral(
"qgis/overwriteStyle" ), prevOverwriteStyle );
489 if ( !errorMsg.isEmpty() )
491 feedback->
reportError( QObject::tr(
"Could not save layer style: %1 " ).arg( errorMsg ) );
498 feedback->
reportError( QObject::tr(
"Could not save layer style -- error loading: %1 %2" ).arg( newFilename, newLayer ) );
@ AddToSelection
Add selection to current selection.
Wrapper for iterator of features from vector data provider or vector layer.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
bool isValid() const
Returns the validity of this feature.
Container of fields for a vector layer.
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
QgsField field(int fieldIdx) const
Returns the field at particular index (must be in range 0..N-1).
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Base class for all map layer types.
virtual void exportNamedStyle(QDomDocument &doc, QString &errorMsg, const QgsReadWriteContext &context=QgsReadWriteContext(), QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories) const
Export the properties of this layer as named style in a QDomDocument.
QgsLayerMetadata metadata
virtual QgsMapLayer * clone() const =0
Returns a new instance equivalent to this one except for the id which is still unique.
Contains information about the context in which a processing algorithm is executed.
QString defaultEncoding() const
Returns the default encoding to use for newly created files.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
Processing feedback object for multi-step operations.
A multi-layer output for processing algorithms which create map layers, when the number and nature of...
A boolean parameter for processing algorithms.
void setMetadata(const QVariantMap &metadata)
Sets the parameter's freeform metadata.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
A parameter for processing algorithms which accepts multiple map layers.
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
The class is used as a container of context for various read/write operations on other objects.
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Options to pass to writeAsVectorFormat()
QString fileEncoding
Encoding to use.
QString driverName
OGR driver to use.
QgsLayerMetadata layerMetadata
Layer metadata to save for the exported vector file.
QString layerName
Layer name. If let empty, it will be derived from the filename.
bool saveMetadata
Set to true to save layer metadata for the exported vector file.
QgsVectorFileWriter::ActionOnExistingFile actionOnExistingFile
Action on existing file.
QgsAttributeList attributes
Attributes to export (empty means all unless skipAttributeCreation is set)
bool onlySelectedFeatures
Write only selected features of layer.
bool skipAttributeCreation
Only write geometries.
QgsFeedback * feedback
Optional feedback object allowing cancellation of layer save.
static QgsVectorFileWriter::WriterError writeAsVectorFormatV3(QgsVectorLayer *layer, const QString &fileName, const QgsCoordinateTransformContext &transformContext, const QgsVectorFileWriter::SaveVectorOptions &options, QString *errorMessage=nullptr, QString *newFilename=nullptr, QString *newLayer=nullptr)
Writes a layer out to a vector file.
@ CreateOrOverwriteLayer
Create or overwrite layer.
Represents a vector layer which manages a vector based data sets.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QgsFieldConstraints::Constraints fieldConstraints(int fieldIndex) const
Returns any constraints which are present for a specified field index.
int selectedFeatureCount() const
Returns the number of features that are selected in this layer.
@ PointCloudLayer
Point cloud layer. Added in QGIS 3.18.
@ MeshLayer
Mesh layer. Added in QGIS 3.2.
@ VectorLayer
Vector layer.
@ RasterLayer
Raster layer.
@ GroupLayer
Composite group layer. Added in QGIS 3.24.
@ VectorTileLayer
Vector tile layer. Added in QGIS 3.14.
@ AnnotationLayer
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
@ PluginLayer
Plugin based layer.
std::unique_ptr< std::remove_pointer< OGRDataSourceH >::type, OGRDataSourceDeleter > ogr_datasource_unique_ptr
Scoped OGR data source.
QSet< QgsFeatureId > QgsFeatureIds