Skip to content

Commit 2af9698

Browse files
authored
Merge pull request #4507 from nyalldawson/import_sink
QgsVectorLayerImport is a QgsFeatureSink
2 parents 51d3fac + 1033c7b commit 2af9698

File tree

5 files changed

+103
-71
lines changed

5 files changed

+103
-71
lines changed

doc/api_break.dox

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,6 +2166,7 @@ QgsVectorLayerImport {#qgis_api_break_3_0_QgsVectorLayerImport}
21662166
QgsCoordinateReferenceSystem is now implicitly shared, using references to QgsCoordinateReferenceSystem rather than
21672167
pointers makes for more robust, safer code. Use an invalid (default constructed) QgsCoordinateReferenceSystem
21682168
in code which previously passed a null pointer to QgsVectorLayerImport.
2169+
- The unused QProgressBar argument in the QgsVectorLayerImport was removed
21692170
- ErrUserCancelled (ImportError enum value) has been renamed to ErrUserCanceled <!--#spellok-->
21702171

21712172

python/auto_sip.blacklist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ core/qgsvectorfilewritertask.sip
8484
core/qgsvectorlayercache.sip
8585
core/qgsvectorlayereditbuffer.sip
8686
core/qgsvectorlayereditpassthrough.sip
87-
core/qgsvectorlayerimport.sip
8887
core/qgsvectorlayerjoinbuffer.sip
8988
core/qgsvectorlayerjoininfo.sip
9089
core/qgsvectorlayertools.sip

python/core/qgsvectorlayerimport.sip

Lines changed: 79 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/qgsvectorlayerimport.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
18

2-
/**
3-
There are two possibilities how to use this class:
4-
1. static call to QgsVectorFileWriter::writeAsShapefile(...) which saves the whole vector layer
5-
2. create an instance of the class and issue calls to addFeature(...)
6-
*/
7-
class QgsVectorLayerImport
9+
10+
11+
12+
13+
class QgsVectorLayerImport : QgsFeatureSink
814
{
9-
%TypeHeaderCode
10-
#include <qgsvectorlayerimport.h>
11-
#include <qgsfield.h>
12-
class QProgressDialog;
15+
%Docstring
16+
A convenience class for writing vector files to disk.
17+
There are two possibilities how to use this class:
18+
1. static call to QgsVectorFileWriter.writeAsShapefile(...) which saves the whole vector layer
19+
2. create an instance of the class and issue calls to addFeature(...)
1320
%End
1421

22+
%TypeHeaderCode
23+
#include "qgsvectorlayerimport.h"
24+
%End
1525
public:
1626

1727
enum ImportError
@@ -28,77 +38,90 @@ class QProgressDialog;
2838
ErrInvalidProvider,
2939
ErrProviderUnsupportedFeature,
3040
ErrConnectionFailed,
31-
ErrUserCanceled, /*!< User canceled the import*/
41+
ErrUserCanceled,
3242
};
3343

34-
/**
35-
* Writes the contents of vector layer to a different datasource.
36-
* @param layer source layer
37-
* @param uri URI for destination data source
38-
* @param providerKey string key for destination data provider
39-
* @param destCRS destination CRS, or an invalid (default constructed) CRS if
40-
* not available
41-
* @param onlySelected set to true to export only selected features
42-
* @param errorMessage if non-null, will be set to any error messages
43-
* @param skipAttributeCreation set to true to skip exporting feature attributes
44-
* @param options optional provider dataset options
45-
* @param progress optional progress dialog to show progress of export
46-
* @returns NoError for a successful export, or encountered error
47-
*/
48-
static ImportError importLayer( QgsVectorLayer* layer,
49-
const QString& uri,
50-
const QString& providerKey,
51-
const QgsCoordinateReferenceSystem& destCRS,
44+
static ImportError importLayer( QgsVectorLayer *layer,
45+
const QString &uri,
46+
const QString &providerKey,
47+
const QgsCoordinateReferenceSystem &destCRS,
5248
bool onlySelected = false,
5349
QString *errorMessage /Out/ = 0,
5450
bool skipAttributeCreation = false,
5551
QMap<QString, QVariant> *options = 0,
5652
QProgressDialog *progress = 0
5753
);
54+
%Docstring
55+
Writes the contents of vector layer to a different datasource.
56+
\param layer source layer
57+
\param uri URI for destination data source
58+
\param providerKey string key for destination data provider
59+
\param destCRS destination CRS, or an invalid (default constructed) CRS if
60+
not available
61+
\param onlySelected set to true to export only selected features
62+
\param errorMessage if non-null, will be set to any error messages
63+
\param skipAttributeCreation set to true to skip exporting feature attributes
64+
\param options optional provider dataset options
65+
\param progress optional progress dialog to show progress of export
66+
:return: NoError for a successful export, or encountered error
67+
:rtype: ImportError
68+
%End
5869

59-
/** Constructor for QgsVectorLayerImport.
60-
* @param uri URI for destination data source
61-
* @param provider string key for destination data provider
62-
* @param fields fields to include in created layer
63-
* @param geometryType destination geometry type
64-
* @param crs desired CRS, or an invalid (default constructed) CRS if
65-
* not available
66-
* @param overwrite set to true to overwrite any existing data source
67-
* @param options optional provider dataset options
68-
* @param progress optional progress dialog to show progress of export
69-
*/
7070
QgsVectorLayerImport( const QString &uri,
7171
const QString &provider,
7272
const QgsFields &fields,
7373
QgsWkbTypes::Type geometryType,
74-
const QgsCoordinateReferenceSystem& crs,
74+
const QgsCoordinateReferenceSystem &crs,
7575
bool overwrite = false,
76-
const QMap<QString, QVariant> *options = 0,
77-
QProgressDialog *progress = 0
78-
);
76+
const QMap<QString, QVariant> *options = 0 );
77+
%Docstring
78+
Constructor for QgsVectorLayerImport.
79+
\param uri URI for destination data source
80+
\param provider string key for destination data provider
81+
\param fields fields to include in created layer
82+
\param geometryType destination geometry type
83+
\param crs desired CRS, or an invalid (default constructed) CRS if
84+
not available
85+
\param overwrite set to true to overwrite any existing data source
86+
\param options optional provider dataset options
87+
%End
88+
7989

80-
/** Checks whether there were any errors */
8190
ImportError hasError();
91+
%Docstring
92+
Checks whether there were any errors
93+
:rtype: ImportError
94+
%End
8295

83-
/** Retrieves error message */
8496
QString errorMessage();
97+
%Docstring
98+
Retrieves error message
99+
:rtype: str
100+
%End
85101

86102
int errorCount() const;
103+
%Docstring
104+
:rtype: int
105+
%End
87106

88-
/** Add feature to the new created layer */
89-
bool addFeature( QgsFeature &feature );
107+
virtual bool addFeatures( QgsFeatureList &features );
90108

91-
/** Close the new created layer */
92-
~QgsVectorLayerImport();
109+
virtual bool addFeature( QgsFeature &feature );
93110

94-
protected:
95-
/** Flush the buffer writing the features to the new layer */
96-
bool flushBuffer();
97111

98-
/** Create index */
99-
bool createSpatialIndex();
112+
~QgsVectorLayerImport();
113+
%Docstring
114+
Close the new created layer
115+
%End
100116

101117
private:
102-
103118
QgsVectorLayerImport( const QgsVectorLayerImport &rh );
104119
};
120+
121+
/************************************************************************
122+
* This file has been generated automatically from *
123+
* *
124+
* src/core/qgsvectorlayerimport.h *
125+
* *
126+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
127+
************************************************************************/

src/core/qgsvectorlayerimport.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,9 @@ QgsVectorLayerImport::QgsVectorLayerImport( const QString &uri,
5252
QgsWkbTypes::Type geometryType,
5353
const QgsCoordinateReferenceSystem &crs,
5454
bool overwrite,
55-
const QMap<QString, QVariant> *options,
56-
QProgressDialog *progress )
55+
const QMap<QString, QVariant> *options )
5756
: mErrorCount( 0 )
5857
, mAttributeCount( -1 )
59-
, mProgress( progress )
6058

6159
{
6260
mProvider = nullptr;
@@ -145,6 +143,17 @@ QString QgsVectorLayerImport::errorMessage()
145143
return mErrorMessage;
146144
}
147145

146+
bool QgsVectorLayerImport::addFeatures( QgsFeatureList &features )
147+
{
148+
QgsFeatureList::iterator fIt = features.begin();
149+
bool result = true;
150+
for ( ; fIt != features.end(); ++fIt )
151+
{
152+
result = result && addFeature( *fIt );
153+
}
154+
return result;
155+
}
156+
148157
bool QgsVectorLayerImport::addFeature( QgsFeature &feat )
149158
{
150159
QgsAttributes attrs = feat.attributes();
@@ -297,7 +306,7 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer *layer,
297306
}
298307

299308
QgsVectorLayerImport *writer =
300-
new QgsVectorLayerImport( uri, providerKey, fields, wkbType, outputCRS, overwrite, options, progress );
309+
new QgsVectorLayerImport( uri, providerKey, fields, wkbType, outputCRS, overwrite, options );
301310

302311
// check whether file creation was successful
303312
ImportError err = writer->hasError();
@@ -314,7 +323,6 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer *layer,
314323
errorMessage->clear();
315324
}
316325

317-
QgsAttributeList allAttr = skipAttributeCreation ? QgsAttributeList() : layer->attributeList();
318326
QgsFeature fet;
319327

320328
QgsFeatureRequest req;

src/core/qgsvectorlayerimport.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "qgis_core.h"
2323
#include "qgis.h"
2424
#include "qgsfeature.h"
25+
#include "qgsfeaturesink.h"
2526

2627
class QProgressDialog;
2728
class QgsVectorDataProvider;
@@ -34,7 +35,7 @@ class QgsFields;
3435
1. static call to QgsVectorFileWriter::writeAsShapefile(...) which saves the whole vector layer
3536
2. create an instance of the class and issue calls to addFeature(...)
3637
*/
37-
class CORE_EXPORT QgsVectorLayerImport
38+
class CORE_EXPORT QgsVectorLayerImport : public QgsFeatureSink
3839
{
3940
public:
4041

@@ -89,17 +90,14 @@ class CORE_EXPORT QgsVectorLayerImport
8990
* not available
9091
* \param overwrite set to true to overwrite any existing data source
9192
* \param options optional provider dataset options
92-
* \param progress optional progress dialog to show progress of export
9393
*/
9494
QgsVectorLayerImport( const QString &uri,
9595
const QString &provider,
9696
const QgsFields &fields,
9797
QgsWkbTypes::Type geometryType,
9898
const QgsCoordinateReferenceSystem &crs,
9999
bool overwrite = false,
100-
const QMap<QString, QVariant> *options = nullptr,
101-
QProgressDialog *progress = nullptr
102-
);
100+
const QMap<QString, QVariant> *options = nullptr );
103101

104102
//! QgsVectorLayerImport cannot be copied
105103
QgsVectorLayerImport( const QgsVectorLayerImport &rh ) = delete;
@@ -114,13 +112,13 @@ class CORE_EXPORT QgsVectorLayerImport
114112

115113
int errorCount() const { return mErrorCount; }
116114

117-
//! Add feature to the new created layer
118-
bool addFeature( QgsFeature &feature );
115+
bool addFeatures( QgsFeatureList &features ) override;
116+
bool addFeature( QgsFeature &feature ) override;
119117

120118
//! Close the new created layer
121119
~QgsVectorLayerImport();
122120

123-
protected:
121+
private:
124122
//! Flush the buffer writing the features to the new layer
125123
bool flushBuffer();
126124

@@ -140,7 +138,10 @@ class CORE_EXPORT QgsVectorLayerImport
140138
int mAttributeCount;
141139

142140
QgsFeatureList mFeatureBuffer;
143-
QProgressDialog *mProgress = nullptr;
141+
142+
#ifdef SIP_RUN
143+
QgsVectorLayerImport( const QgsVectorLayerImport &rh );
144+
#endif
144145

145146
};
146147

0 commit comments

Comments
 (0)