Skip to content

Commit

Permalink
Move import feature main code to QgsVectorLayerImport and keep just f…
Browse files Browse the repository at this point in the history
…ew methods in providers
  • Loading branch information
brushtyler committed Sep 12, 2011
1 parent b6587d3 commit 6eb406b
Show file tree
Hide file tree
Showing 15 changed files with 892 additions and 762 deletions.
1 change: 1 addition & 0 deletions python/core/core.sip
Expand Up @@ -83,6 +83,7 @@
%Include qgsvectordataprovider.sip
%Include qgsvectorfilewriter.sip
%Include qgsvectorlayer.sip
%Include qgsvectorlayerimport.sip
%Include qgsvectoroverlay.sip

%Include qgsnetworkaccessmanager.sip
Expand Down
15 changes: 0 additions & 15 deletions python/core/qgsproviderregistry.sip
Expand Up @@ -61,21 +61,6 @@ class QgsProviderRegistry
*/
virtual QString protocolDrivers() const;

/** allows to import a vector layer using the provider
* @note this method was added in QGIS 1.8
*/
int importVector( QgsVectorLayer* layer,
const QString& providerKey,
const QString& uri,
const QgsCoordinateReferenceSystem *destCRS,
bool onlySelected = FALSE,
QString *errorMessage /Out/ = 0,
bool skipAttributeCreation = FALSE,
const QMap<QString, QVariant> *options = 0
) const;



private:

/** ctor private since instance() creates it */
Expand Down
65 changes: 65 additions & 0 deletions python/core/qgsvectorlayerimport.sip
@@ -0,0 +1,65 @@

/**
There are two possibilities how to use this class:
1. static call to QgsVectorLayerImport::importLayer(...) which saves the whole vector layer
2. create an instance of the class and issue calls to addFeature(...)
*/
class QgsVectorLayerImport
{
%TypeHeaderCode
#include <qgsvectorlayerimport.h>
#include <qgsfield.h>
%End

public:

enum ImportError
{
NoError = 0,
ErrDriverNotFound,
ErrCreateDataSource,
ErrCreateLayer,
ErrAttributeTypeUnsupported,
ErrAttributeCreationFailed,
ErrProjection,
ErrFeatureWriteFailed,
ErrInvalidLayer,
ErrInvalidProvider,
ErrProviderUnsupportedFeature,
ErrConnectionFailed
};

/** Write contents of vector layer to a different datasource */
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
);

/** create a empty layer and add fields to it */
QgsVectorLayerImport( const QString &uri,
const QString &provider,
const QgsFieldMap& fields,
QGis::WkbType geometryType,
const QgsCoordinateReferenceSystem* crs,
bool overwrite = false,
const QMap<QString, QVariant> *options = 0
);

/** checks whether there were any errors */
ImportError hasError();

/** retrieves error message */
QString errorMessage();

/** add feature to the new created layer */
bool addFeature( QgsFeature& feature );

/** close the new created layer */
~QgsVectorLayerImport();
};

2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -97,6 +97,7 @@ SET(QGIS_CORE_SRCS
qgsvectordataprovider.cpp
qgsvectorfilewriter.cpp
qgsvectorlayer.cpp
qgsvectorlayerimport.cpp
qgsvectorlayerjoinbuffer.cpp
qgsvectorlayerundocommand.cpp
qgsvectoroverlay.cpp
Expand Down Expand Up @@ -327,6 +328,7 @@ SET(QGIS_CORE_HDRS
qgsvectordataprovider.h
qgsvectorfilewriter.h
qgsvectorlayer.h
qgsvectorlayerimport.h
qgsvectoroverlay.h
qgstolerance.h

Expand Down
40 changes: 0 additions & 40 deletions src/core/qgsproviderregistry.cpp
Expand Up @@ -43,14 +43,6 @@ typedef QString directoryDrivers_t();
typedef QString protocolDrivers_t();
//typedef int dataCapabilities_t();
//typedef QgsDataItem * dataItem_t(QString);
typedef int importVector_t( QgsVectorLayer* layer,
const QString& uri,
const QgsCoordinateReferenceSystem *destCRS,
bool onlySelected = false,
QString *errorMessage = 0,
bool skipAttributeCreation = false,
const QMap<QString, QVariant> *options = 0
);

QgsProviderRegistry *QgsProviderRegistry::_instance = 0;

Expand Down Expand Up @@ -540,35 +532,3 @@ QgsProviderRegistry::openVector( QString const & dataSource, QString const & pro
return getProvider( providerKey, dataSource );
} // QgsProviderRegistry::openVector
*/


int QgsProviderRegistry::importVector( QgsVectorLayer* layer,
const QString& providerKey,
const QString& uri,
const QgsCoordinateReferenceSystem *destCRS,
bool onlySelected,
QString *errorMessage,
bool skipAttributeCreation,
const QMap<QString, QVariant> *options
) const
{
QLibrary *myLib = providerLibrary( providerKey );
if ( !myLib )
{
if ( errorMessage )
*errorMessage = QObject::tr( "unable to load %1 provider" ).arg( providerKey );
return -1;
}

importVector_t * pImport = ( importVector_t * ) cast_to_fptr( myLib->resolve( "importVector" ) );
if ( !pImport )
{
delete myLib;
if ( errorMessage )
*errorMessage = QObject::tr( "provider %1 has no importVector feature" ).arg( providerKey );
return -2;
}

delete myLib;
return pImport( layer, uri, destCRS, onlySelected, errorMessage, skipAttributeCreation, options );
}
11 changes: 0 additions & 11 deletions src/core/qgsproviderregistry.h
Expand Up @@ -140,17 +140,6 @@ class CORE_EXPORT QgsProviderRegistry
/** type for data provider metadata associative container */
typedef std::map<QString, QgsProviderMetadata*> Providers;

/** allows to import a vector layer using the provider */
int importVector( QgsVectorLayer* layer,
const QString& providerKey,
const QString& uri,
const QgsCoordinateReferenceSystem *destCRS,
bool onlySelected = false,
QString *errorMessage = 0,
bool skipAttributeCreation = false,
const QMap<QString, QVariant> *options = 0
) const;

private:

/** ctor private since instance() creates it */
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgsvectorfilewriter.h
Expand Up @@ -116,6 +116,8 @@ class CORE_EXPORT QgsVectorFileWriter
/** add feature to the currently opened shapefile */
bool addFeature( QgsFeature& feature );

QMap<int, int> attrIdxToOgrIdx() { return mAttrIdxToOgrIdx; }

/** close opened shapefile for writing */
~QgsVectorFileWriter();

Expand Down

0 comments on commit 6eb406b

Please sign in to comment.