Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE] Import a vector layer from a datasource to a different one
Merge of another part of my Summer of Code work (remote-tracking branch 'brushtyler/import_layer')
  • Loading branch information
brushtyler committed Sep 15, 2011
2 parents 7093b06 + 37b2c04 commit 1a70ddd
Show file tree
Hide file tree
Showing 15 changed files with 1,547 additions and 44 deletions.
1 change: 1 addition & 0 deletions python/core/core.sip
Expand Up @@ -84,6 +84,7 @@
%Include qgsvectordataprovider.sip
%Include qgsvectorfilewriter.sip
%Include qgsvectorlayer.sip
%Include qgsvectorlayerimport.sip
%Include qgsvectoroverlay.sip

%Include qgsnetworkaccessmanager.sip
Expand Down
2 changes: 0 additions & 2 deletions python/core/qgsproviderregistry.sip
Expand Up @@ -60,8 +60,6 @@ class QgsProviderRegistry
* @note this method was added in QGIS 1.1
*/
virtual QString protocolDrivers() const;



private:

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 @@ -101,6 +101,7 @@ SET(QGIS_CORE_SRCS
qgsvectordataprovider.cpp
qgsvectorfilewriter.cpp
qgsvectorlayer.cpp
qgsvectorlayerimport.cpp
qgsvectorlayerjoinbuffer.cpp
qgsvectorlayerundocommand.cpp
qgsvectoroverlay.cpp
Expand Down Expand Up @@ -333,6 +334,7 @@ SET(QGIS_CORE_HDRS
qgsvectordataprovider.h
qgsvectorfilewriter.h
qgsvectorlayer.h
qgsvectorlayerimport.h
qgsvectoroverlay.h
qgstolerance.h

Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsproviderregistry.cpp
Expand Up @@ -30,6 +30,7 @@
#include "qgslogger.h"
#include "qgsmessageoutput.h"
#include "qgsprovidermetadata.h"
#include "qgsvectorlayer.h"


// typedefs for provider plugin functions of interest
Expand Down Expand Up @@ -468,7 +469,7 @@ void * QgsProviderRegistry::function( QString const & providerKey,
return 0;
}

QLibrary *QgsProviderRegistry::providerLibrary( QString const & providerKey )
QLibrary *QgsProviderRegistry::providerLibrary( QString const & providerKey ) const
{
QString lib = library( providerKey );

Expand Down
5 changes: 3 additions & 2 deletions src/core/qgsproviderregistry.h
Expand Up @@ -28,7 +28,8 @@

class QgsDataProvider;
class QgsProviderMetadata;

class QgsVectorLayer;
class QgsCoordinateReferenceSystem;


/** \ingroup core
Expand Down Expand Up @@ -78,7 +79,7 @@ class CORE_EXPORT QgsProviderRegistry
void *function( const QString & providerKey,
const QString & functionName );

QLibrary *providerLibrary( const QString & providerKey );
QLibrary *providerLibrary( const QString & providerKey ) const;

/** Return list of available providers by their keys */
QStringList providerList() const;
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 1a70ddd

Please sign in to comment.