Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #4507 from nyalldawson/import_sink
QgsVectorLayerImport is a QgsFeatureSink
  • Loading branch information
nyalldawson committed May 6, 2017
2 parents 51d3fac + 1033c7b commit 2af9698
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 71 deletions.
1 change: 1 addition & 0 deletions doc/api_break.dox
Expand Up @@ -2166,6 +2166,7 @@ QgsVectorLayerImport {#qgis_api_break_3_0_QgsVectorLayerImport}
QgsCoordinateReferenceSystem is now implicitly shared, using references to QgsCoordinateReferenceSystem rather than
pointers makes for more robust, safer code. Use an invalid (default constructed) QgsCoordinateReferenceSystem
in code which previously passed a null pointer to QgsVectorLayerImport.
- The unused QProgressBar argument in the QgsVectorLayerImport was removed
- ErrUserCancelled (ImportError enum value) has been renamed to ErrUserCanceled <!--#spellok-->


Expand Down
1 change: 0 additions & 1 deletion python/auto_sip.blacklist
Expand Up @@ -84,7 +84,6 @@ core/qgsvectorfilewritertask.sip
core/qgsvectorlayercache.sip
core/qgsvectorlayereditbuffer.sip
core/qgsvectorlayereditpassthrough.sip
core/qgsvectorlayerimport.sip
core/qgsvectorlayerjoinbuffer.sip
core/qgsvectorlayerjoininfo.sip
core/qgsvectorlayertools.sip
Expand Down
135 changes: 79 additions & 56 deletions python/core/qgsvectorlayerimport.sip
@@ -1,17 +1,27 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsvectorlayerimport.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/

/**
There are two possibilities how to use this class:
1. static call to QgsVectorFileWriter::writeAsShapefile(...) which saves the whole vector layer
2. create an instance of the class and issue calls to addFeature(...)
*/
class QgsVectorLayerImport




class QgsVectorLayerImport : QgsFeatureSink
{
%TypeHeaderCode
#include <qgsvectorlayerimport.h>
#include <qgsfield.h>
class QProgressDialog;
%Docstring
A convenience class for writing vector files to disk.
There are two possibilities how to use this class:
1. static call to QgsVectorFileWriter.writeAsShapefile(...) which saves the whole vector layer
2. create an instance of the class and issue calls to addFeature(...)
%End

%TypeHeaderCode
#include "qgsvectorlayerimport.h"
%End
public:

enum ImportError
Expand All @@ -28,77 +38,90 @@ class QProgressDialog;
ErrInvalidProvider,
ErrProviderUnsupportedFeature,
ErrConnectionFailed,
ErrUserCanceled, /*!< User canceled the import*/
ErrUserCanceled,
};

/**
* Writes the contents of vector layer to a different datasource.
* @param layer source layer
* @param uri URI for destination data source
* @param providerKey string key for destination data provider
* @param destCRS destination CRS, or an invalid (default constructed) CRS if
* not available
* @param onlySelected set to true to export only selected features
* @param errorMessage if non-null, will be set to any error messages
* @param skipAttributeCreation set to true to skip exporting feature attributes
* @param options optional provider dataset options
* @param progress optional progress dialog to show progress of export
* @returns NoError for a successful export, or encountered error
*/
static ImportError importLayer( QgsVectorLayer* layer,
const QString& uri,
const QString& providerKey,
const QgsCoordinateReferenceSystem& destCRS,
static ImportError importLayer( QgsVectorLayer *layer,
const QString &uri,
const QString &providerKey,
const QgsCoordinateReferenceSystem &destCRS,
bool onlySelected = false,
QString *errorMessage /Out/ = 0,
bool skipAttributeCreation = false,
QMap<QString, QVariant> *options = 0,
QProgressDialog *progress = 0
);
%Docstring
Writes the contents of vector layer to a different datasource.
\param layer source layer
\param uri URI for destination data source
\param providerKey string key for destination data provider
\param destCRS destination CRS, or an invalid (default constructed) CRS if
not available
\param onlySelected set to true to export only selected features
\param errorMessage if non-null, will be set to any error messages
\param skipAttributeCreation set to true to skip exporting feature attributes
\param options optional provider dataset options
\param progress optional progress dialog to show progress of export
:return: NoError for a successful export, or encountered error
:rtype: ImportError
%End

/** Constructor for QgsVectorLayerImport.
* @param uri URI for destination data source
* @param provider string key for destination data provider
* @param fields fields to include in created layer
* @param geometryType destination geometry type
* @param crs desired CRS, or an invalid (default constructed) CRS if
* not available
* @param overwrite set to true to overwrite any existing data source
* @param options optional provider dataset options
* @param progress optional progress dialog to show progress of export
*/
QgsVectorLayerImport( const QString &uri,
const QString &provider,
const QgsFields &fields,
QgsWkbTypes::Type geometryType,
const QgsCoordinateReferenceSystem& crs,
const QgsCoordinateReferenceSystem &crs,
bool overwrite = false,
const QMap<QString, QVariant> *options = 0,
QProgressDialog *progress = 0
);
const QMap<QString, QVariant> *options = 0 );
%Docstring
Constructor for QgsVectorLayerImport.
\param uri URI for destination data source
\param provider string key for destination data provider
\param fields fields to include in created layer
\param geometryType destination geometry type
\param crs desired CRS, or an invalid (default constructed) CRS if
not available
\param overwrite set to true to overwrite any existing data source
\param options optional provider dataset options
%End


/** Checks whether there were any errors */
ImportError hasError();
%Docstring
Checks whether there were any errors
:rtype: ImportError
%End

/** Retrieves error message */
QString errorMessage();
%Docstring
Retrieves error message
:rtype: str
%End

int errorCount() const;
%Docstring
:rtype: int
%End

/** Add feature to the new created layer */
bool addFeature( QgsFeature &feature );
virtual bool addFeatures( QgsFeatureList &features );

/** Close the new created layer */
~QgsVectorLayerImport();
virtual bool addFeature( QgsFeature &feature );

protected:
/** Flush the buffer writing the features to the new layer */
bool flushBuffer();

/** Create index */
bool createSpatialIndex();
~QgsVectorLayerImport();
%Docstring
Close the new created layer
%End

private:

QgsVectorLayerImport( const QgsVectorLayerImport &rh );
};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsvectorlayerimport.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
18 changes: 13 additions & 5 deletions src/core/qgsvectorlayerimport.cpp
Expand Up @@ -52,11 +52,9 @@ QgsVectorLayerImport::QgsVectorLayerImport( const QString &uri,
QgsWkbTypes::Type geometryType,
const QgsCoordinateReferenceSystem &crs,
bool overwrite,
const QMap<QString, QVariant> *options,
QProgressDialog *progress )
const QMap<QString, QVariant> *options )
: mErrorCount( 0 )
, mAttributeCount( -1 )
, mProgress( progress )

{
mProvider = nullptr;
Expand Down Expand Up @@ -145,6 +143,17 @@ QString QgsVectorLayerImport::errorMessage()
return mErrorMessage;
}

bool QgsVectorLayerImport::addFeatures( QgsFeatureList &features )
{
QgsFeatureList::iterator fIt = features.begin();
bool result = true;
for ( ; fIt != features.end(); ++fIt )
{
result = result && addFeature( *fIt );
}
return result;
}

bool QgsVectorLayerImport::addFeature( QgsFeature &feat )
{
QgsAttributes attrs = feat.attributes();
Expand Down Expand Up @@ -297,7 +306,7 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer *layer,
}

QgsVectorLayerImport *writer =
new QgsVectorLayerImport( uri, providerKey, fields, wkbType, outputCRS, overwrite, options, progress );
new QgsVectorLayerImport( uri, providerKey, fields, wkbType, outputCRS, overwrite, options );

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

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

QgsFeatureRequest req;
Expand Down
19 changes: 10 additions & 9 deletions src/core/qgsvectorlayerimport.h
Expand Up @@ -22,6 +22,7 @@
#include "qgis_core.h"
#include "qgis.h"
#include "qgsfeature.h"
#include "qgsfeaturesink.h"

class QProgressDialog;
class QgsVectorDataProvider;
Expand All @@ -34,7 +35,7 @@ class QgsFields;
1. static call to QgsVectorFileWriter::writeAsShapefile(...) which saves the whole vector layer
2. create an instance of the class and issue calls to addFeature(...)
*/
class CORE_EXPORT QgsVectorLayerImport
class CORE_EXPORT QgsVectorLayerImport : public QgsFeatureSink
{
public:

Expand Down Expand Up @@ -89,17 +90,14 @@ class CORE_EXPORT QgsVectorLayerImport
* not available
* \param overwrite set to true to overwrite any existing data source
* \param options optional provider dataset options
* \param progress optional progress dialog to show progress of export
*/
QgsVectorLayerImport( const QString &uri,
const QString &provider,
const QgsFields &fields,
QgsWkbTypes::Type geometryType,
const QgsCoordinateReferenceSystem &crs,
bool overwrite = false,
const QMap<QString, QVariant> *options = nullptr,
QProgressDialog *progress = nullptr
);
const QMap<QString, QVariant> *options = nullptr );

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

int errorCount() const { return mErrorCount; }

//! Add feature to the new created layer
bool addFeature( QgsFeature &feature );
bool addFeatures( QgsFeatureList &features ) override;
bool addFeature( QgsFeature &feature ) override;

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

protected:
private:
//! Flush the buffer writing the features to the new layer
bool flushBuffer();

Expand All @@ -140,7 +138,10 @@ class CORE_EXPORT QgsVectorLayerImport
int mAttributeCount;

QgsFeatureList mFeatureBuffer;
QProgressDialog *mProgress = nullptr;

#ifdef SIP_RUN
QgsVectorLayerImport( const QgsVectorLayerImport &rh );
#endif

};

Expand Down

0 comments on commit 2af9698

Please sign in to comment.