QGIS API Documentation
3.26.3-Buenos Aires (65e4edfdad)
|
Go to the documentation of this file.
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 ) );
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 ) );
65 QString QgsPackageAlgorithm::shortHelpString()
const
67 return QObject::tr(
"This algorithm collects a number of existing layers and packages them together into a single GeoPackage database." );
70 QgsPackageAlgorithm *QgsPackageAlgorithm::createInstance()
const
72 return new QgsPackageAlgorithm();
77 const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral(
"LAYERS" ), context );
80 mLayers.emplace_back( layer->clone() );
83 if ( mLayers.empty() )
84 feedback->
reportError( QObject::tr(
"No layers selected, geopackage will be empty" ),
false );
91 const bool overwrite = parameterAsBoolean( parameters, QStringLiteral(
"OVERWRITE" ), context );
92 const bool saveStyles = parameterAsBoolean( parameters, QStringLiteral(
"SAVE_STYLES" ), context );
93 const bool saveMetadata = parameterAsBoolean( parameters, QStringLiteral(
"SAVE_METADATA" ), context );
94 const bool selectedFeaturesOnly = parameterAsBoolean( parameters, QStringLiteral(
"SELECTED_FEATURES_ONLY" ), context );
95 const QString packagePath = parameterAsString( parameters, QStringLiteral(
"OUTPUT" ), context );
96 if ( packagePath.isEmpty() )
100 if ( overwrite && QFile::exists( packagePath ) )
102 feedback->
pushInfo( QObject::tr(
"Removing existing file '%1'" ).arg( packagePath ) );
103 if ( !QFile( packagePath ).remove() )
109 OGRSFDriverH hGpkgDriver = OGRGetDriverByName(
"GPKG" );
117 if ( !QFile::exists( packagePath ) )
121 throw QgsProcessingException( QObject::tr(
"Creation of database %1 failed (OGR error: %2)" ).arg( packagePath, QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
127 throw QgsProcessingException( QObject::tr(
"Opening database %1 failed (OGR error: %2)" ).arg( packagePath, QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
131 bool errored =
false;
135 QStringList outputLayers;
137 for (
const auto &layer : mLayers )
142 multiStepFeedback.setCurrentStep( i );
148 feedback->
pushDebugInfo( QObject::tr(
"Error retrieving map layer." ) );
153 feedback->
pushInfo( QObject::tr(
"Packaging layer %1/%2: %3" ).arg( i ).arg( mLayers.size() ).arg( layer ? layer->name() : QString() ) );
155 switch ( layer->type() )
159 QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( layer.get() );
161 if ( !packageVectorLayer( vectorLayer, packagePath, context, &multiStepFeedback, saveStyles, saveMetadata, onlySaveSelected ) )
164 outputLayers.append( QStringLiteral(
"%1|layername=%2" ).arg( packagePath, layer->name() ) );
171 feedback->
pushDebugInfo( QObject::tr(
"Packaging raster layers is not supported." ) );
178 feedback->
pushDebugInfo( QObject::tr(
"Packaging plugin layers is not supported." ) );
184 feedback->
pushDebugInfo( QObject::tr(
"Packaging mesh layers is not supported." ) );
190 feedback->
pushDebugInfo( QObject::tr(
"Packaging point cloud layers is not supported." ) );
196 feedback->
pushDebugInfo( QObject::tr(
"Packaging vector tile layers is not supported." ) );
202 feedback->
pushDebugInfo( QObject::tr(
"Packaging annotation layers is not supported." ) );
208 feedback->
pushDebugInfo( QObject::tr(
"Packaging group layers is not supported." ) );
218 outputs.insert( QStringLiteral(
"OUTPUT" ), packagePath );
219 outputs.insert( QStringLiteral(
"OUTPUT_LAYERS" ), outputLayers );
227 options.
driverName = QStringLiteral(
"GPKG" );
242 const int fidIndex = fields.
lookupField( QStringLiteral(
"fid" ) );
258 feedback->
reportError( QObject::tr(
"Packaging layer failed: %1" ).arg( error ) );
265 std::unique_ptr< QgsVectorLayer > res = std::make_unique< QgsVectorLayer >( QStringLiteral(
"%1|layername=%2" ).arg( newFilename, newLayer ) );
269 QDomDocument doc( QStringLiteral(
"qgis" ) );
272 if ( !errorMsg.isEmpty() )
274 feedback->
reportError( QObject::tr(
"Could not retrieve existing layer style: %1 " ).arg( errorMsg ) );
278 if ( !res->importNamedStyle( doc, errorMsg ) )
280 feedback->
reportError( QObject::tr(
"Could not set existing layer style: %1 " ).arg( errorMsg ) );
286 const QVariant prevOverwriteStyle = settings.
value( QStringLiteral(
"qgis/overwriteStyle" ) );
287 settings.
setValue( QStringLiteral(
"qgis/overwriteStyle" ),
true );
288 res->saveStyleToDatabase( newLayer, QString(),
true, QString(), errorMsg );
289 settings.
setValue( QStringLiteral(
"qgis/overwriteStyle" ), prevOverwriteStyle );
290 if ( !errorMsg.isEmpty() )
292 feedback->
reportError( QObject::tr(
"Could not save layer style: %1 " ).arg( errorMsg ) );
299 feedback->
reportError( QObject::tr(
"Could not save layer style -- error loading: %1 %2" ).arg( newFilename, newLayer ) );
Options to pass to writeAsVectorFormat()
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
@ MeshLayer
Mesh layer. Added in QGIS 3.2.
The class is used as a container of context for various read/write operations on other objects.
@ VectorLayer
Vector layer.
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.
std::unique_ptr< std::remove_pointer< OGRDataSourceH >::type, OGRDataSourceDeleter > ogr_datasource_unique_ptr
Scoped OGR data source.
@ AnnotationLayer
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
Container of fields for a vector layer.
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
QString layerName
Layer name. If let empty, it will be derived from the filename.
This class is a composition of two QSettings instances:
bool saveMetadata
Set to true to save layer metadata for the exported vector file.
@ CreateOrOverwriteLayer
Create or overwrite layer.
A parameter for processing algorithms which accepts multiple map layers.
QgsAttributeList attributes
Attributes to export (empty means all unless skipAttributeCreation is set)
QgsFeedback * feedback
Optional feedback object allowing cancellation of layer save.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QgsLayerMetadata metadata
QgsLayerMetadata layerMetadata
Layer metadata to save for the exported vector file.
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Processing feedback object for multi-step operations.
@ GroupLayer
Composite group layer. Added in QGIS 3.24.
Contains information about the context in which a processing algorithm is executed.
QString fileEncoding
Encoding to use.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
QString defaultEncoding() const
Returns the default encoding to use for newly created files.
void setMetadata(const QVariantMap &metadata)
Sets the parameter's freeform metadata.
@ RasterLayer
Raster layer.
bool onlySelectedFeatures
Write only selected features of layer.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
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.
QgsVectorFileWriter::ActionOnExistingFile actionOnExistingFile
Action on existing file.
virtual void pushDebugInfo(const QString &info)
Pushes an informational message containing debugging helpers from the algorithm.
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.
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
A boolean parameter for processing algorithms.
Represents a vector layer which manages a vector based data sets.
Base class for all map layer types. This is the base class for all map layer types (vector,...
A multi-layer output for processing algorithms which create map layers, when the number and nature of...
@ VectorTileLayer
Vector tile layer. Added in QGIS 3.14.
int selectedFeatureCount() const
Returns the number of features that are selected in this layer.
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
bool skipAttributeCreation
Only write geometries.
QString driverName
OGR driver to use.
@ PointCloudLayer
Point cloud layer. Added in QGIS 3.18.
Custom exception class for processing related exceptions.
@ PluginLayer
Plugin based layer.