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 & )
57 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"SAVE_STYLES" ), QObject::tr(
"Save layer styles into GeoPackage" ),
true ) );
61 QString QgsPackageAlgorithm::shortHelpString()
const
63 return QObject::tr(
"This algorithm collects a number of existing layers and packages them together into a single GeoPackage database." );
66 QgsPackageAlgorithm *QgsPackageAlgorithm::createInstance()
const
68 return new QgsPackageAlgorithm();
73 const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral(
"LAYERS" ), context );
76 mLayers.emplace_back( layer->clone() );
79 if ( mLayers.empty() )
80 feedback->
reportError( QObject::tr(
"No layers selected, geopackage will be empty" ),
false );
87 const bool overwrite = parameterAsBoolean( parameters, QStringLiteral(
"OVERWRITE" ), context );
88 const bool saveStyles = parameterAsBoolean( parameters, QStringLiteral(
"SAVE_STYLES" ), context );
89 QString packagePath = parameterAsString( parameters, QStringLiteral(
"OUTPUT" ), context );
90 if ( packagePath.isEmpty() )
94 if ( overwrite && QFile::exists( packagePath ) )
96 feedback->
pushInfo( QObject::tr(
"Removing existing file '%1'" ).arg( packagePath ) );
97 if ( !QFile( packagePath ).remove() )
103 OGRSFDriverH hGpkgDriver = OGRGetDriverByName(
"GPKG" );
111 if ( !QFile::exists( packagePath ) )
115 throw QgsProcessingException( QObject::tr(
"Creation of database %1 failed (OGR error: %2)" ).arg( packagePath, QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
121 throw QgsProcessingException( QObject::tr(
"Opening database %1 failed (OGR error: %2)" ).arg( packagePath, QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
125 bool errored =
false;
129 QStringList outputLayers;
131 for (
const auto &layer : mLayers )
136 multiStepFeedback.setCurrentStep( i );
142 feedback->
pushDebugInfo( QObject::tr(
"Error retrieving map layer." ) );
147 feedback->
pushInfo( QObject::tr(
"Packaging layer %1/%2: %3" ).arg( i ).arg( mLayers.size() ).arg( layer ? layer->name() : QString() ) );
149 switch ( layer->type() )
153 if ( !packageVectorLayer( qobject_cast< QgsVectorLayer * >( layer.get() ), packagePath,
154 context, &multiStepFeedback, saveStyles ) )
157 outputLayers.append( QStringLiteral(
"%1|layername=%2" ).arg( packagePath, layer->name() ) );
164 feedback->
pushDebugInfo( QObject::tr(
"Packaging raster layers is not supported." ) );
171 feedback->
pushDebugInfo( QObject::tr(
"Packaging plugin layers is not supported." ) );
177 feedback->
pushDebugInfo( QObject::tr(
"Packaging mesh layers is not supported." ) );
183 feedback->
pushDebugInfo( QObject::tr(
"Packaging vector tile layers is not supported." ) );
189 feedback->
pushDebugInfo( QObject::tr(
"Packaging annotation layers is not supported." ) );
199 outputs.insert( QStringLiteral(
"OUTPUT" ), packagePath );
200 outputs.insert( QStringLiteral(
"OUTPUT_LAYERS" ), outputLayers );
208 options.
driverName = QStringLiteral(
"GPKG" );
217 const int fidIndex = fields.
lookupField( QStringLiteral(
"fid" ) );
233 feedback->
reportError( QObject::tr(
"Packaging layer failed: %1" ).arg( error ) );
240 std::unique_ptr< QgsVectorLayer > res = qgis::make_unique< QgsVectorLayer >( QStringLiteral(
"%1|layername=%2" ).arg( newFilename, newLayer ) );
244 QDomDocument doc( QStringLiteral(
"qgis" ) );
247 if ( !errorMsg.isEmpty() )
249 feedback->
reportError( QObject::tr(
"Could not retrieve existing layer style: %1 " ).arg( errorMsg ) );
253 if ( !res->importNamedStyle( doc, errorMsg ) )
255 feedback->
reportError( QObject::tr(
"Could not set existing layer style: %1 " ).arg( errorMsg ) );
261 QVariant prevOverwriteStyle = settings.
value( QStringLiteral(
"qgis/overwriteStyle" ) );
262 settings.
setValue( QStringLiteral(
"qgis/overwriteStyle" ),
true );
263 res->saveStyleToDatabase( newLayer, QString(),
true, QString(), errorMsg );
264 settings.
setValue( QStringLiteral(
"qgis/overwriteStyle" ), prevOverwriteStyle );
265 if ( !errorMsg.isEmpty() )
267 feedback->
reportError( QObject::tr(
"Could not save layer style: %1 " ).arg( errorMsg ) );
274 feedback->
reportError( QObject::tr(
"Could not save layer style -- error loading: %1 %2" ).arg( newFilename, newLayer ) );