Skip to content

Commit 85fbc4a

Browse files
committed
[FEATURE] Vector layer save as: offer file/layer overwriting, new layer creation, feature and field appending
When saving a vector layer into an existing file, depending on the capabilities of the output driver, the user can now decide whether: - to overwrite the whole file - to overwrite only the target layer (layer name is now configurable) - to append features to the existing target layer - to append features, add new fields if there are any. All above is available for drivers like GPKG, SpatiaLite, FileGDB, ... For drivers like Shapefile, MapInfo .tab, feature append is also available. Backported from master 688d1a5
1 parent 0332f5a commit 85fbc4a

File tree

8 files changed

+971
-74
lines changed

8 files changed

+971
-74
lines changed

python/core/qgsvectorfilewriter.sip

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,43 @@ class QgsVectorFileWriter
132132
virtual QVariant convert( int fieldIdxInLayer, const QVariant& value );
133133
};
134134

135+
/** Edition capability flags
136+
* @note Added in QGIS 3.0 */
137+
enum EditionCapability
138+
{
139+
/** Flag to indicate that a new layer can be added to the dataset */
140+
CanAddNewLayer,
141+
142+
/** Flag to indicate that new features can be added to an existing layer */
143+
CanAppendToExistingLayer ,
144+
145+
/** Flag to indicate that new fields can be added to an existing layer. Imply CanAppendToExistingLayer */
146+
CanAddNewFieldsToExistingLayer,
147+
148+
/** Flag to indicate that an existing layer can be deleted */
149+
CanDeleteLayer
150+
};
151+
152+
typedef QFlags<QgsVectorFileWriter::EditionCapability> EditionCapabilities;
153+
154+
/** Enumeration to describe how to handle existing files
155+
@note Added in QGIS 3.0
156+
*/
157+
enum ActionOnExistingFile
158+
{
159+
/** Create or overwrite file */
160+
CreateOrOverwriteFile,
161+
162+
/** Create or overwrite layer */
163+
CreateOrOverwriteLayer,
164+
165+
/** Append features to existing layer, but do not create new fields */
166+
AppendToLayerNoNewFields,
167+
168+
/** Append features to existing layer, and create new fields if needed */
169+
AppendToLayerAddFields
170+
};
171+
135172
/** Write contents of vector layer to an (OGR supported) vector formt
136173
* @param layer layer to write
137174
* @param fileName file name to write to
@@ -219,6 +256,88 @@ class QgsVectorFileWriter
219256
FieldValueConverter* fieldValueConverter = nullptr
220257
);
221258

259+
260+
/**
261+
* Options to pass to writeAsVectorFormat()
262+
* @note Added in QGIS 3.0
263+
*/
264+
class SaveVectorOptions
265+
{
266+
public:
267+
/** Constructor */
268+
SaveVectorOptions();
269+
270+
/** Destructor */
271+
virtual ~SaveVectorOptions();
272+
273+
/** OGR driver to use */
274+
QString driverName;
275+
276+
/** Layer name. If let empty, it will be derived from the filename */
277+
QString layerName;
278+
279+
/** Action on existing file */
280+
QgsVectorFileWriter::ActionOnExistingFile actionOnExistingFile;
281+
282+
/** Encoding to use */
283+
QString fileEncoding;
284+
285+
/** Transform to reproject exported geometries with, or invalid transform
286+
* for no transformation */
287+
const QgsCoordinateTransform* ct;
288+
289+
/** Write only selected features of layer */
290+
bool onlySelectedFeatures;
291+
292+
/** List of OGR data source creation options */
293+
QStringList datasourceOptions;
294+
295+
/** List of OGR layer creation options */
296+
QStringList layerOptions;
297+
298+
/** Only write geometries */
299+
bool skipAttributeCreation;
300+
301+
/** Attributes to export (empty means all unless skipAttributeCreation is set) */
302+
QgsAttributeList attributes;
303+
304+
/** Symbology to export */
305+
QgsVectorFileWriter::SymbologyExport symbologyExport;
306+
307+
/** Scale of symbology */
308+
double symbologyScale;
309+
310+
/** If not empty, only features intersecting the extent will be saved */
311+
QgsRectangle filterExtent;
312+
313+
/** Set to a valid geometry type to override the default geometry type for the layer. This parameter
314+
* allows for conversion of geometryless tables to null geometries, etc */
315+
QgsWKBTypes::Type overrideGeometryType;
316+
317+
/** Set to true to force creation of multi* geometries */
318+
bool forceMulti;
319+
320+
/** Set to true to include z dimension in output. This option is only valid if overrideGeometryType is set */
321+
bool includeZ;
322+
323+
/** Field value converter */
324+
QgsVectorFileWriter::FieldValueConverter* fieldValueConverter;
325+
};
326+
327+
/** Writes a layer out to a vector file.
328+
* @param layer source layer to write
329+
* @param fileName file name to write to
330+
* @param options options.
331+
* @param newFilename QString pointer which will contain the new file name created (in case it is different to fileName).
332+
* @param errorMessage pointer to buffer fo error message
333+
* @note added in 3.0
334+
*/
335+
static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
336+
const QString& fileName,
337+
const SaveVectorOptions& options,
338+
QString *newFilename = nullptr,
339+
QString *errorMessage = nullptr );
340+
222341
/** Create a new vector file writer */
223342
QgsVectorFileWriter( const QString& vectorFileName,
224343
const QString& fileEncoding,
@@ -292,6 +411,28 @@ class QgsVectorFileWriter
292411

293412
static bool driverMetadata( const QString& driverName, MetaData& driverMetadata );
294413

414+
/**
415+
* Return edition capabilites for an existing dataset name.
416+
* @note added in QGIS 3.0
417+
*/
418+
static EditionCapabilities editionCapabilities( const QString& datasetName );
419+
420+
/**
421+
* Returns whether the target layer already exists.
422+
* @note added in QGIS 3.0
423+
*/
424+
static bool targetLayerExists( const QString& datasetName,
425+
const QString& layerName );
426+
427+
/**
428+
* Returns whether there are among the attributes specified some that do not exist yet in the layer
429+
* @note added in QGIS 3.0
430+
*/
431+
static bool areThereNewFieldsToCreate( const QString& datasetName,
432+
const QString& layerName,
433+
QgsVectorLayer* layer,
434+
const QgsAttributeList& attributes );
435+
295436
protected:
296437
//! @note not available in python bindings
297438
// OGRGeometryH createEmptyGeometry( QGis::WkbType wkbType );
@@ -300,3 +441,5 @@ class QgsVectorFileWriter
300441

301442
QgsVectorFileWriter( const QgsVectorFileWriter& rh );
302443
};
444+
445+
QFlags<QgsVectorFileWriter::EditionCapability> operator|(QgsVectorFileWriter::EditionCapability f1, QFlags<QgsVectorFileWriter::EditionCapability> f2);

0 commit comments

Comments
 (0)