QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsalgorithmpackage.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmpackage.cpp
3  ---------------------
4  begin : November 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsalgorithmpackage.h"
19 #include "qgsgeometryengine.h"
20 #include "qgsogrutils.h"
21 #include "qgsvectorfilewriter.h"
22 #include "qgsvectorlayer.h"
23 #include "qgssettings.h"
24 
26 
27 QString QgsPackageAlgorithm::name() const
28 {
29  return QStringLiteral( "package" );
30 }
31 
32 QString QgsPackageAlgorithm::displayName() const
33 {
34  return QObject::tr( "Package layers" );
35 }
36 
37 QStringList QgsPackageAlgorithm::tags() const
38 {
39  return QObject::tr( "geopackage,collect,merge,combine,styles" ).split( ',' );
40 }
41 
42 QString QgsPackageAlgorithm::group() const
43 {
44  return QObject::tr( "Database" );
45 }
46 
47 QString QgsPackageAlgorithm::groupId() const
48 {
49  return QStringLiteral( "database" );
50 }
51 
52 void QgsPackageAlgorithm::initAlgorithm( const QVariantMap & )
53 {
54  addParameter( new QgsProcessingParameterMultipleLayers( QStringLiteral( "LAYERS" ), QObject::tr( "Input layers" ), QgsProcessing::TypeVector ) );
55  addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Destination GeoPackage" ), QObject::tr( "GeoPackage files (*.gpkg)" ) ) );
56  addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "OVERWRITE" ), QObject::tr( "Overwrite existing GeoPackage" ), false ) );
57  addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "SAVE_STYLES" ), QObject::tr( "Save layer styles into GeoPackage" ), true ) );
58  addOutput( new QgsProcessingOutputMultipleLayers( QStringLiteral( "OUTPUT_LAYERS" ), QObject::tr( "Layers within new package" ) ) );
59 }
60 
61 QString QgsPackageAlgorithm::shortHelpString() const
62 {
63  return QObject::tr( "This algorithm collects a number of existing layers and packages them together into a single GeoPackage database." );
64 }
65 
66 QgsPackageAlgorithm *QgsPackageAlgorithm::createInstance() const
67 {
68  return new QgsPackageAlgorithm();
69 }
70 
71 bool QgsPackageAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
72 {
73  const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral( "LAYERS" ), context );
74  for ( QgsMapLayer *layer : layers )
75  {
76  mLayers.emplace_back( layer->clone() );
77  }
78 
79  if ( mLayers.empty() )
80  feedback->reportError( QObject::tr( "No layers selected, geopackage will be empty" ), false );
81 
82  return true;
83 }
84 
85 QVariantMap QgsPackageAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
86 {
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() )
91  throw QgsProcessingException( QObject::tr( "No output file specified." ) );
92 
93  // delete existing geopackage if it exists
94  if ( overwrite && QFile::exists( packagePath ) )
95  {
96  feedback->pushInfo( QObject::tr( "Removing existing file '%1'" ).arg( packagePath ) );
97  if ( !QFile( packagePath ).remove() )
98  {
99  throw QgsProcessingException( QObject::tr( "Could not remove existing file '%1'" ).arg( packagePath ) );
100  }
101  }
102 
103  OGRSFDriverH hGpkgDriver = OGRGetDriverByName( "GPKG" );
104  if ( !hGpkgDriver )
105  {
106  throw QgsProcessingException( QObject::tr( "GeoPackage driver not found." ) );
107  }
108 
110 
111  if ( !QFile::exists( packagePath ) )
112  {
113  hDS = gdal::ogr_datasource_unique_ptr( OGR_Dr_CreateDataSource( hGpkgDriver, packagePath.toUtf8().constData(), nullptr ) );
114  if ( !hDS )
115  throw QgsProcessingException( QObject::tr( "Creation of database %1 failed (OGR error: %2)" ).arg( packagePath, QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
116  }
117  else
118  {
119  hDS = gdal::ogr_datasource_unique_ptr( OGROpen( packagePath.toUtf8().constData(), true, nullptr ) );
120  if ( !hDS )
121  throw QgsProcessingException( QObject::tr( "Opening database %1 failed (OGR error: %2)" ).arg( packagePath, QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
122  }
123 
124 
125  bool errored = false;
126 
127  QgsProcessingMultiStepFeedback multiStepFeedback( mLayers.size(), feedback );
128 
129  QStringList outputLayers;
130  int i = 0;
131  for ( const auto &layer : mLayers )
132  {
133  if ( feedback->isCanceled() )
134  break;
135 
136  multiStepFeedback.setCurrentStep( i );
137  i++;
138 
139  if ( !layer )
140  {
141  // don't throw immediately - instead do what we can and error out later
142  feedback->pushDebugInfo( QObject::tr( "Error retrieving map layer." ) );
143  errored = true;
144  continue;
145  }
146 
147  feedback->pushInfo( QObject::tr( "Packaging layer %1/%2: %3" ).arg( i ).arg( mLayers.size() ).arg( layer ? layer->name() : QString() ) );
148 
149  switch ( layer->type() )
150  {
152  {
153  if ( !packageVectorLayer( qobject_cast< QgsVectorLayer * >( layer.get() ), packagePath,
154  context, &multiStepFeedback, saveStyles ) )
155  errored = true;
156  else
157  outputLayers.append( QStringLiteral( "%1|layername=%2" ).arg( packagePath, layer->name() ) );
158  break;
159  }
160 
162  {
163  //not supported
164  feedback->pushDebugInfo( QObject::tr( "Packaging raster layers is not supported." ) );
165  errored = true;
166  break;
167  }
168 
170  //not supported
171  feedback->pushDebugInfo( QObject::tr( "Packaging plugin layers is not supported." ) );
172  errored = true;
173  break;
174 
176  //not supported
177  feedback->pushDebugInfo( QObject::tr( "Packaging mesh layers is not supported." ) );
178  errored = true;
179  break;
180 
182  //not supported
183  feedback->pushDebugInfo( QObject::tr( "Packaging vector tile layers is not supported." ) );
184  errored = true;
185  break;
186 
188  //not supported
189  feedback->pushDebugInfo( QObject::tr( "Packaging annotation layers is not supported." ) );
190  errored = true;
191  break;
192  }
193  }
194 
195  if ( errored )
196  throw QgsProcessingException( QObject::tr( "Error obtained while packaging one or more layers." ) );
197 
198  QVariantMap outputs;
199  outputs.insert( QStringLiteral( "OUTPUT" ), packagePath );
200  outputs.insert( QStringLiteral( "OUTPUT_LAYERS" ), outputLayers );
201  return outputs;
202 }
203 
204 bool QgsPackageAlgorithm::packageVectorLayer( QgsVectorLayer *layer, const QString &path, QgsProcessingContext &context,
205  QgsProcessingFeedback *feedback, bool saveStyles )
206 {
208  options.driverName = QStringLiteral( "GPKG" );
209  options.layerName = layer->name();
211  options.fileEncoding = context.defaultEncoding();
212  options.feedback = feedback;
213 
214  // remove any existing FID field, let this be completely recreated
215  // since many layer sources have fid fields which are not compatible with gpkg requirements
216  QgsFields fields = layer->fields();
217  const int fidIndex = fields.lookupField( QStringLiteral( "fid" ) );
218 
219  options.attributes = fields.allAttributesList();
220  if ( fidIndex >= 0 )
221  options.attributes.removeAll( fidIndex );
222  if ( options.attributes.isEmpty() )
223  {
224  // fid was the only field
225  options.skipAttributeCreation = true;
226  }
227 
228  QString error;
229  QString newFilename;
230  QString newLayer;
231  if ( QgsVectorFileWriter::writeAsVectorFormatV2( layer, path, context.transformContext(), options, &newFilename, &newLayer, &error ) != QgsVectorFileWriter::NoError )
232  {
233  feedback->reportError( QObject::tr( "Packaging layer failed: %1" ).arg( error ) );
234  return false;
235  }
236  else
237  {
238  if ( saveStyles )
239  {
240  std::unique_ptr< QgsVectorLayer > res = qgis::make_unique< QgsVectorLayer >( QStringLiteral( "%1|layername=%2" ).arg( newFilename, newLayer ) );
241  if ( res )
242  {
243  QString errorMsg;
244  QDomDocument doc( QStringLiteral( "qgis" ) );
245  QgsReadWriteContext context;
246  layer->exportNamedStyle( doc, errorMsg, context );
247  if ( !errorMsg.isEmpty() )
248  {
249  feedback->reportError( QObject::tr( "Could not retrieve existing layer style: %1 " ).arg( errorMsg ) );
250  }
251  else
252  {
253  if ( !res->importNamedStyle( doc, errorMsg ) )
254  {
255  feedback->reportError( QObject::tr( "Could not set existing layer style: %1 " ).arg( errorMsg ) );
256  }
257  else
258  {
259  QgsSettings settings;
260  // this is not nice -- but needed to avoid an "overwrite" prompt messagebox from the provider! This api needs a rework to avoid this.
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() )
266  {
267  feedback->reportError( QObject::tr( "Could not save layer style: %1 " ).arg( errorMsg ) );
268  }
269  }
270  }
271  }
272  else
273  {
274  feedback->reportError( QObject::tr( "Could not save layer style -- error loading: %1 %2" ).arg( newFilename, newLayer ) );
275  }
276  }
277  return true;
278  }
279 }
280 
qgsalgorithmpackage.h
QgsVectorFileWriter::SaveVectorOptions
Options to pass to writeAsVectorFormat()
Definition: qgsvectorfilewriter.h:446
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
QgsMapLayerType::MeshLayer
@ MeshLayer
Added in 3.2.
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
QgsMapLayerType::VectorLayer
@ VectorLayer
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:38
QgsProcessingFeedback::pushInfo
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
Definition: qgsprocessingfeedback.cpp:48
QgsProcessingFeedback::reportError
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
Definition: qgsprocessingfeedback.cpp:39
gdal::ogr_datasource_unique_ptr
std::unique_ptr< std::remove_pointer< OGRDataSourceH >::type, OGRDataSourceDeleter > ogr_datasource_unique_ptr
Scoped OGR data source.
Definition: qgsogrutils.h:114
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:45
QgsVectorFileWriter::SaveVectorOptions::layerName
QString layerName
Layer name. If let empty, it will be derived from the filename.
Definition: qgsvectorfilewriter.h:457
qgsogrutils.h
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QgsVectorFileWriter::CreateOrOverwriteLayer
@ CreateOrOverwriteLayer
Create or overwrite layer.
Definition: qgsvectorfilewriter.h:269
QgsProcessingParameterMultipleLayers
A parameter for processing algorithms which accepts multiple map layers.
Definition: qgsprocessingparameters.h:1878
QgsVectorFileWriter::SaveVectorOptions::attributes
QgsAttributeList attributes
Attributes to export (empty means all unless skipAttributeCreation is set)
Definition: qgsvectorfilewriter.h:484
QgsVectorFileWriter::SaveVectorOptions::feedback
QgsFeedback * feedback
Optional feedback object allowing cancellation of layer save.
Definition: qgsvectorfilewriter.h:516
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3283
qgsgeometryengine.h
QgsProcessing::TypeVector
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:53
QgsProcessingMultiStepFeedback
Processing feedback object for multi-step operations.
Definition: qgsprocessingfeedback.h:147
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:44
QgsVectorFileWriter::SaveVectorOptions::fileEncoding
QString fileEncoding
Encoding to use.
Definition: qgsvectorfilewriter.h:463
QgsProcessingParameterFileDestination
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
Definition: qgsprocessingparameters.h:3127
QgsProcessingContext::defaultEncoding
QString defaultEncoding() const
Returns the default encoding to use for newly created files.
Definition: qgsprocessingcontext.h:464
QgsMapLayerType::RasterLayer
@ RasterLayer
QgsProcessingContext::transformContext
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Definition: qgsprocessingcontext.h:149
QgsSettings::setValue
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Definition: qgssettings.cpp:289
QgsMapLayer::exportNamedStyle
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.
Definition: qgsmaplayer.cpp:1098
QgsVectorFileWriter::SaveVectorOptions::actionOnExistingFile
QgsVectorFileWriter::ActionOnExistingFile actionOnExistingFile
Action on existing file.
Definition: qgsvectorfilewriter.h:460
qgsvectorlayer.h
QgsProcessingFeedback::pushDebugInfo
virtual void pushDebugInfo(const QString &info)
Pushes an informational message containing debugging helpers from the algorithm.
Definition: qgsprocessingfeedback.cpp:66
QgsFeedback::isCanceled
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:53
QgsFields::allAttributesList
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
Definition: qgsfields.cpp:371
QgsProcessingParameterBoolean
A boolean parameter for processing algorithms.
Definition: qgsprocessingparameters.h:1507
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
QgsProcessingOutputMultipleLayers
A multi-layer output for processing algorithms which create map layers, when the number and nature of...
Definition: qgsprocessingoutputs.h:249
qgssettings.h
QgsMapLayerType::VectorTileLayer
@ VectorTileLayer
Added in 3.14.
QgsMapLayer::name
QString name
Definition: qgsmaplayer.h:86
QgsVectorFileWriter::NoError
@ NoError
Definition: qgsvectorfilewriter.h:169
QgsVectorFileWriter::writeAsVectorFormatV2
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.
Definition: qgsvectorfilewriter.cpp:3189
qgsvectorfilewriter.h
QgsFields::lookupField
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Definition: qgsfields.cpp:344
QgsVectorFileWriter::SaveVectorOptions::skipAttributeCreation
bool skipAttributeCreation
Only write geometries.
Definition: qgsvectorfilewriter.h:481
QgsVectorFileWriter::SaveVectorOptions::driverName
QString driverName
OGR driver to use.
Definition: qgsvectorfilewriter.h:454
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
QgsMapLayerType::PluginLayer
@ PluginLayer