Skip to content
Permalink
Browse files

Move import feature main code to QgsVectorLayerImport and keep just f…

…ew methods in providers
  • Loading branch information
brushtyler committed Sep 12, 2011
1 parent b6587d3 commit 6eb406b1229829e6a8a1668bfddb8e12d5547ad4
@@ -83,6 +83,7 @@
%Include qgsvectordataprovider.sip
%Include qgsvectorfilewriter.sip
%Include qgsvectorlayer.sip
%Include qgsvectorlayerimport.sip
%Include qgsvectoroverlay.sip

%Include qgsnetworkaccessmanager.sip
@@ -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 */
@@ -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();
};

@@ -97,6 +97,7 @@ SET(QGIS_CORE_SRCS
qgsvectordataprovider.cpp
qgsvectorfilewriter.cpp
qgsvectorlayer.cpp
qgsvectorlayerimport.cpp
qgsvectorlayerjoinbuffer.cpp
qgsvectorlayerundocommand.cpp
qgsvectoroverlay.cpp
@@ -327,6 +328,7 @@ SET(QGIS_CORE_HDRS
qgsvectordataprovider.h
qgsvectorfilewriter.h
qgsvectorlayer.h
qgsvectorlayerimport.h
qgsvectoroverlay.h
qgstolerance.h

@@ -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;

@@ -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 );
}
@@ -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 */
@@ -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();

0 comments on commit 6eb406b

Please sign in to comment.
You can’t perform that action at this time.