Skip to content

Commit 1a70ddd

Browse files
committedSep 15, 2011
[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')
2 parents 7093b06 + 37b2c04 commit 1a70ddd

15 files changed

+1547
-44
lines changed
 

‎python/core/core.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
%Include qgsvectordataprovider.sip
8585
%Include qgsvectorfilewriter.sip
8686
%Include qgsvectorlayer.sip
87+
%Include qgsvectorlayerimport.sip
8788
%Include qgsvectoroverlay.sip
8889

8990
%Include qgsnetworkaccessmanager.sip

‎python/core/qgsproviderregistry.sip

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ class QgsProviderRegistry
6060
* @note this method was added in QGIS 1.1
6161
*/
6262
virtual QString protocolDrivers() const;
63-
64-
6563

6664
private:
6765

‎python/core/qgsvectorlayerimport.sip

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
/**
3+
There are two possibilities how to use this class:
4+
1. static call to QgsVectorLayerImport::importLayer(...) which saves the whole vector layer
5+
2. create an instance of the class and issue calls to addFeature(...)
6+
*/
7+
class QgsVectorLayerImport
8+
{
9+
%TypeHeaderCode
10+
#include <qgsvectorlayerimport.h>
11+
#include <qgsfield.h>
12+
%End
13+
14+
public:
15+
16+
enum ImportError
17+
{
18+
NoError = 0,
19+
ErrDriverNotFound,
20+
ErrCreateDataSource,
21+
ErrCreateLayer,
22+
ErrAttributeTypeUnsupported,
23+
ErrAttributeCreationFailed,
24+
ErrProjection,
25+
ErrFeatureWriteFailed,
26+
ErrInvalidLayer,
27+
ErrInvalidProvider,
28+
ErrProviderUnsupportedFeature,
29+
ErrConnectionFailed
30+
};
31+
32+
/** Write contents of vector layer to a different datasource */
33+
static ImportError importLayer( QgsVectorLayer* layer,
34+
const QString& uri,
35+
const QString& providerKey,
36+
const QgsCoordinateReferenceSystem *destCRS,
37+
bool onlySelected = FALSE,
38+
QString *errorMessage /Out/ = 0,
39+
bool skipAttributeCreation = FALSE,
40+
QMap<QString, QVariant> *options = 0
41+
);
42+
43+
/** create a empty layer and add fields to it */
44+
QgsVectorLayerImport( const QString &uri,
45+
const QString &provider,
46+
const QgsFieldMap& fields,
47+
QGis::WkbType geometryType,
48+
const QgsCoordinateReferenceSystem* crs,
49+
bool overwrite = false,
50+
const QMap<QString, QVariant> *options = 0
51+
);
52+
53+
/** checks whether there were any errors */
54+
ImportError hasError();
55+
56+
/** retrieves error message */
57+
QString errorMessage();
58+
59+
/** add feature to the new created layer */
60+
bool addFeature( QgsFeature& feature );
61+
62+
/** close the new created layer */
63+
~QgsVectorLayerImport();
64+
};
65+

‎src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ SET(QGIS_CORE_SRCS
101101
qgsvectordataprovider.cpp
102102
qgsvectorfilewriter.cpp
103103
qgsvectorlayer.cpp
104+
qgsvectorlayerimport.cpp
104105
qgsvectorlayerjoinbuffer.cpp
105106
qgsvectorlayerundocommand.cpp
106107
qgsvectoroverlay.cpp
@@ -333,6 +334,7 @@ SET(QGIS_CORE_HDRS
333334
qgsvectordataprovider.h
334335
qgsvectorfilewriter.h
335336
qgsvectorlayer.h
337+
qgsvectorlayerimport.h
336338
qgsvectoroverlay.h
337339
qgstolerance.h
338340

‎src/core/qgsproviderregistry.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "qgslogger.h"
3131
#include "qgsmessageoutput.h"
3232
#include "qgsprovidermetadata.h"
33+
#include "qgsvectorlayer.h"
3334

3435

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

471-
QLibrary *QgsProviderRegistry::providerLibrary( QString const & providerKey )
472+
QLibrary *QgsProviderRegistry::providerLibrary( QString const & providerKey ) const
472473
{
473474
QString lib = library( providerKey );
474475

‎src/core/qgsproviderregistry.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828

2929
class QgsDataProvider;
3030
class QgsProviderMetadata;
31-
31+
class QgsVectorLayer;
32+
class QgsCoordinateReferenceSystem;
3233

3334

3435
/** \ingroup core
@@ -78,7 +79,7 @@ class CORE_EXPORT QgsProviderRegistry
7879
void *function( const QString & providerKey,
7980
const QString & functionName );
8081

81-
QLibrary *providerLibrary( const QString & providerKey );
82+
QLibrary *providerLibrary( const QString & providerKey ) const;
8283

8384
/** Return list of available providers by their keys */
8485
QStringList providerList() const;

‎src/core/qgsvectorfilewriter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ class CORE_EXPORT QgsVectorFileWriter
116116
/** add feature to the currently opened shapefile */
117117
bool addFeature( QgsFeature& feature );
118118

119+
QMap<int, int> attrIdxToOgrIdx() { return mAttrIdxToOgrIdx; }
120+
119121
/** close opened shapefile for writing */
120122
~QgsVectorFileWriter();
121123

0 commit comments

Comments
 (0)
Please sign in to comment.