Skip to content

Commit da0ee8b

Browse files
authored
Merge pull request #3597 from rouault/saveas_overwrite_append
[FEATURE] Vector layer save as: offer file/layer overwriting, new layer creation, feature and field appending
2 parents 03f08a6 + 34894c6 commit da0ee8b

File tree

8 files changed

+964
-73
lines changed

8 files changed

+964
-73
lines changed

python/core/qgsvectorfilewriter.sip

Lines changed: 142 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
@@ -220,6 +257,88 @@ class QgsVectorFileWriter
220257
FieldValueConverter* fieldValueConverter = nullptr
221258
);
222259

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

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

303443
QgsVectorFileWriter( const QgsVectorFileWriter& rh );
304444
};
445+
446+
QFlags<QgsVectorFileWriter::EditionCapability> operator|(QgsVectorFileWriter::EditionCapability f1, QFlags<QgsVectorFileWriter::EditionCapability> f2);

0 commit comments

Comments
 (0)