Skip to content

Commit 593ea7e

Browse files
committed
Implement import layer feature for OGR provider using QgsVectorFileWriter
1 parent 9a2972a commit 593ea7e

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/providers/ogr/qgsogrprovider.cpp

+48-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ email : sherman at mrcc.com
4242
#include "qgsfield.h"
4343
#include "qgsgeometry.h"
4444
#include "qgscoordinatereferencesystem.h"
45-
#include "qgsvectorfilewriter.h"
4645
#include "qgsvectorlayer.h"
4746

4847
static const QString TEXT_PROVIDER_KEY = "ogr";
@@ -83,6 +82,43 @@ class QgsCPLErrorHandler
8382
}
8483
};
8584

85+
86+
87+
QgsVectorFileWriter::WriterError
88+
QgsOgrProvider::importVector(
89+
QgsVectorLayer *layer,
90+
const QString& fileName,
91+
const QgsCoordinateReferenceSystem *destCRS,
92+
bool onlySelected,
93+
QString *errorMessage,
94+
bool skipAttributeCreation,
95+
const QMap<QString,QVariant> *options )
96+
{
97+
QString encoding;
98+
QString driverName = "ESRI Shapefile";
99+
QStringList dsOptions, layerOptions;
100+
101+
if ( options )
102+
{
103+
if ( options->contains( "fileEncoding" ) )
104+
encoding = options->value( "fileEncoding" ).toString();
105+
106+
if ( options->contains( "driverName" ) )
107+
driverName = options->value( "driverName" ).toString();
108+
109+
if ( options->contains( "datasourceOptions" ) )
110+
dsOptions << options->value( "datasourceOptions" ).toStringList();
111+
112+
if ( options->contains( "layerOptions" ) )
113+
layerOptions << options->value( "layerOptions" ).toStringList();
114+
}
115+
116+
return QgsVectorFileWriter::writeAsVectorFormat(
117+
layer, fileName, encoding, destCRS, driverName, onlySelected,
118+
errorMessage, dsOptions, layerOptions, skipAttributeCreation );
119+
}
120+
121+
86122
QgsOgrProvider::QgsOgrProvider( QString const & uri )
87123
: QgsVectorDataProvider( uri ),
88124
ogrDataSource( 0 ),
@@ -2369,3 +2405,14 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
23692405
return 0;
23702406
}
23712407

2408+
QGISEXTERN int importVector(
2409+
QgsVectorLayer *layer,
2410+
const QString& uri,
2411+
const QgsCoordinateReferenceSystem *destCRS,
2412+
bool onlySelected,
2413+
QString *errorMessage,
2414+
bool skipAttributeCreation,
2415+
const QMap<QString,QVariant> *options )
2416+
{
2417+
return QgsOgrProvider::importVector( layer, uri, destCRS, onlySelected, errorMessage, skipAttributeCreation, options );
2418+
}

src/providers/ogr/qgsogrprovider.h

+12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ email : sherman at mrcc.com
1818
#include "qgsdataitem.h"
1919
#include "qgsrectangle.h"
2020
#include "qgsvectordataprovider.h"
21+
#include "qgsvectorfilewriter.h"
2122

2223
class QgsFeature;
2324
class QgsField;
@@ -34,6 +35,17 @@ class QgsOgrProvider : public QgsVectorDataProvider
3435

3536
public:
3637

38+
/** convert a vector layer to a vector file */
39+
static QgsVectorFileWriter::WriterError importVector(
40+
QgsVectorLayer *layer,
41+
const QString& fileName,
42+
const QgsCoordinateReferenceSystem *destCRS,
43+
bool onlySelected,
44+
QString *errorMessage,
45+
bool skipAttributeCreation,
46+
const QMap<QString,QVariant> *options
47+
);
48+
3749
/**
3850
* Constructor of the vector provider
3951
* @param uri uniform resource locator (URI) for a dataset

0 commit comments

Comments
 (0)