27 QString QgsPackageAlgorithm::name()
const
29 return QStringLiteral(
"package" );
32 QString QgsPackageAlgorithm::displayName()
const
34 return QObject::tr(
"Package layers" );
37 QStringList QgsPackageAlgorithm::tags()
const
39 return QObject::tr(
"geopackage,collect,merge,combine,styles" ).split(
',' );
42 QString QgsPackageAlgorithm::group()
const
44 return QObject::tr(
"Database" );
47 QString QgsPackageAlgorithm::groupId()
const
49 return QStringLiteral(
"database" );
52 void 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 ) );
63 QString QgsPackageAlgorithm::shortHelpString()
const
65 return QObject::tr(
"This algorithm collects a number of existing layers and packages them together into a single GeoPackage database." );
68 QgsPackageAlgorithm *QgsPackageAlgorithm::createInstance()
const
70 return new QgsPackageAlgorithm();
75 const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral(
"LAYERS" ), context );
78 mLayers.emplace_back( layer->clone() );
81 if ( mLayers.empty() )
82 feedback->
reportError( QObject::tr(
"No layers selected, geopackage will be empty" ),
false );
89 const bool overwrite = parameterAsBoolean( parameters, QStringLiteral(
"OVERWRITE" ), context );
90 const bool saveStyles = parameterAsBoolean( parameters, QStringLiteral(
"SAVE_STYLES" ), context );
91 QString packagePath = parameterAsString( parameters, QStringLiteral(
"OUTPUT" ), context );
92 if ( packagePath.isEmpty() )
96 if ( overwrite && QFile::exists( packagePath ) )
98 feedback->
pushInfo( QObject::tr(
"Removing existing file '%1'" ).arg( packagePath ) );
99 if ( !QFile( packagePath ).remove() )
105 OGRSFDriverH hGpkgDriver = OGRGetDriverByName(
"GPKG" );
113 if ( !QFile::exists( packagePath ) )
117 throw QgsProcessingException( QObject::tr(
"Creation of database %1 failed (OGR error: %2)" ).arg( packagePath, QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
123 throw QgsProcessingException( QObject::tr(
"Opening database %1 failed (OGR error: %2)" ).arg( packagePath, QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
127 bool errored =
false;
131 QStringList outputLayers;
133 for (
const auto &layer : mLayers )
138 multiStepFeedback.setCurrentStep( i );
144 feedback->
pushDebugInfo( QObject::tr(
"Error retrieving map layer." ) );
149 feedback->
pushInfo( QObject::tr(
"Packaging layer %1/%2: %3" ).arg( i ).arg( mLayers.size() ).arg( layer ? layer->name() : QString() ) );
151 switch ( layer->type() )
155 if ( !packageVectorLayer( qobject_cast< QgsVectorLayer * >( layer.get() ), packagePath,
156 context, &multiStepFeedback, saveStyles ) )
159 outputLayers.append( QStringLiteral(
"%1|layername=%2" ).arg( packagePath, layer->name() ) );
166 feedback->
pushDebugInfo( QObject::tr(
"Packaging raster layers is not supported." ) );
173 feedback->
pushDebugInfo( QObject::tr(
"Packaging plugin layers is not supported." ) );
179 feedback->
pushDebugInfo( QObject::tr(
"Packaging mesh layers is not supported." ) );
185 feedback->
pushDebugInfo( QObject::tr(
"Packaging point cloud layers is not supported." ) );
191 feedback->
pushDebugInfo( QObject::tr(
"Packaging vector tile layers is not supported." ) );
197 feedback->
pushDebugInfo( QObject::tr(
"Packaging annotation layers is not supported." ) );
207 outputs.insert( QStringLiteral(
"OUTPUT" ), packagePath );
208 outputs.insert( QStringLiteral(
"OUTPUT_LAYERS" ), outputLayers );
216 options.
driverName = QStringLiteral(
"GPKG" );
225 const int fidIndex = fields.
lookupField( QStringLiteral(
"fid" ) );
241 feedback->
reportError( QObject::tr(
"Packaging layer failed: %1" ).arg( error ) );
248 std::unique_ptr< QgsVectorLayer > res = qgis::make_unique< QgsVectorLayer >( QStringLiteral(
"%1|layername=%2" ).arg( newFilename, newLayer ) );
252 QDomDocument doc( QStringLiteral(
"qgis" ) );
255 if ( !errorMsg.isEmpty() )
257 feedback->
reportError( QObject::tr(
"Could not retrieve existing layer style: %1 " ).arg( errorMsg ) );
261 if ( !res->importNamedStyle( doc, errorMsg ) )
263 feedback->
reportError( QObject::tr(
"Could not set existing layer style: %1 " ).arg( errorMsg ) );
269 QVariant prevOverwriteStyle = settings.
value( QStringLiteral(
"qgis/overwriteStyle" ) );
270 settings.
setValue( QStringLiteral(
"qgis/overwriteStyle" ),
true );
271 res->saveStyleToDatabase( newLayer, QString(),
true, QString(), errorMsg );
272 settings.
setValue( QStringLiteral(
"qgis/overwriteStyle" ), prevOverwriteStyle );
273 if ( !errorMsg.isEmpty() )
275 feedback->
reportError( QObject::tr(
"Could not save layer style: %1 " ).arg( errorMsg ) );
282 feedback->
reportError( QObject::tr(
"Could not save layer style -- error loading: %1 %2" ).arg( newFilename, newLayer ) );
bool isCanceled() const
Tells whether the operation has been canceled already.
Container of fields for a vector layer.
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
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.
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.
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 pushDebugInfo(const QString &info)
Pushes an informational message containing debugging helpers 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...
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.
QString layerName
Layer name. If let empty, it will be derived from the filename.
QgsVectorFileWriter::ActionOnExistingFile actionOnExistingFile
Action on existing file.
QgsAttributeList attributes
Attributes to export (empty means all unless skipAttributeCreation is set)
bool skipAttributeCreation
Only write geometries.
QgsFeedback * feedback
Optional feedback object allowing cancellation of layer save.
static QgsVectorFileWriter::WriterError writeAsVectorFormatV2(QgsVectorLayer *layer, const QString &fileName, const QgsCoordinateTransformContext &transformContext, const QgsVectorFileWriter::SaveVectorOptions &options, QString *newFilename=nullptr, QString *newLayer=nullptr, QString *errorMessage=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.
@ PointCloudLayer
Added in 3.18.
@ VectorTileLayer
Added in 3.14.
@ AnnotationLayer
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
std::unique_ptr< std::remove_pointer< OGRDataSourceH >::type, OGRDataSourceDeleter > ogr_datasource_unique_ptr
Scoped OGR data source.