29 #include <QProgressDialog>
31 #define FEATURE_BUFFER_SIZE 200
39 QMap<int, int> *oldToNewAttrIdx,
40 QString *errorMessage,
41 const QMap<QString, QVariant> *options
46 const QString &providerKey,
51 const QMap<QString, QVariant> *options,
52 QProgressDialog *progress )
54 , mProgress( progress )
106 if ( vectorProvider )
107 delete vectorProvider;
144 for (
int i = 0; i < attrs.count(); ++i )
152 QgsDebugMsgLevel( QString(
"moving field from pos %1 to %2" ).arg( i ).arg( dstIdx ), 3 );
179 .arg( errors.join(
"\n" ) );
208 const QString& providerKey,
211 QString *errorMessage,
212 bool skipAttributeCreation,
213 QMap<QString, QVariant> *options,
214 QProgressDialog *progress )
218 int shallTransform =
false;
225 if ( destCRS && destCRS->
isValid() )
229 shallTransform =
true;
234 outputCRS = &layer->
crs();
238 bool overwrite =
false;
239 bool forceSinglePartGeom =
false;
242 overwrite = options->take(
"overwrite" ).toBool();
243 forceSinglePartGeom = options->take(
"forceSinglePartGeometryType" ).toBool();
253 for (
int fldIdx = 0; fldIdx < fields.
count(); ++fldIdx )
255 fields[fldIdx].setName( fields[fldIdx].name().toLower() );
258 if ( !forceSinglePartGeom )
288 new QgsVectorLayerImport( uri, providerKey, fields, wkbType, outputCRS, overwrite, options, progress );
302 errorMessage->clear();
311 if ( skipAttributeCreation )
327 shallTransform =
false;
334 *errorMessage =
QObject::tr(
"Feature write errors:" );
345 if ( progress && progress->wasCanceled() )
349 *errorMessage +=
"\n" +
QObject::tr(
"Import was canceled at %1 of %2" ).arg( progress->value() ).arg( progress->maximum() );
363 if ( onlySelected && !ids.contains( fet.
id() ) )
366 if ( shallTransform )
380 QString msg =
QObject::tr(
"Failed to transform a point while drawing a feature with ID '%1'. Writing stopped. (Exception: %2)" )
381 .arg( fet.
id() ).arg( e.
what() );
384 *errorMessage +=
"\n" + msg;
389 if ( skipAttributeCreation )
404 progress->setValue( n );
428 if ( shallTransform )
437 *errorMessage +=
"\n" +
QObject::tr(
"Only %1 of %2 features written." ).arg( n - errors ).arg( n );
441 errorMessage->clear();
QgsFeatureId id() const
Get the feature id for this feature.
Wrapper for iterator of features from vector data provider or vector layer.
QMap< int, int > mOldToNewAttrIdx
map attribute indexes to new field indexes
QgsVectorLayerImport(const QString &uri, const QString &provider, const QgsFields &fields, QGis::WkbType geometryType, const QgsCoordinateReferenceSystem *crs, bool overwrite=false, const QMap< QString, QVariant > *options=0, QProgressDialog *progress=0)
create a empty layer and add fields to it
static QgsProviderRegistry * instance(QString pluginPath=QString::null)
means of accessing canonical single instance
QSet< QgsFeatureId > QgsFeatureIds
#define FEATURE_BUFFER_SIZE
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
QgsGeometry * geometry() const
Get the geometry object associated with this feature.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
bool flushBuffer()
flush the buffer writing the features to the new layer
QgsVectorDataProvider * mProvider
Container of fields for a vector layer.
bool setAttribute(int field, const QVariant &attr)
Set an attribute by id.
QStringList errors()
Get recorded errors.
WkbType
Used for symbology operations.
QgsVectorLayerImport::ImportError createEmptyLayer_t(const QString &uri, const QgsFields &fields, QGis::WkbType geometryType, const QgsCoordinateReferenceSystem *destCRS, bool overwrite, QMap< int, int > *oldToNewAttrIdx, QString *errorMessage, const QMap< QString, QVariant > *options)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
virtual bool addFeatures(QgsFeatureList &flist)
Adds a list of features.
static ImportError importLayer(QgsVectorLayer *layer, const QString &uri, const QString &providerKey, const QgsCoordinateReferenceSystem *destCRS, bool onlySelected=false, QString *errorMessage=0, bool skipAttributeCreation=false, QMap< QString, QVariant > *options=0, QProgressDialog *progress=0)
Write contents of vector layer to a different datasource.
static void logMessage(QString message, QString tag=QString::null, MessageLevel level=WARNING)
add a message to the instance (and create it if necessary)
QGis::WkbType wkbType() const
Returns the WKBType or WKBUnknown in case of error.
allows creation of spatial index
virtual bool createSpatialIndex()
Creates a spatial index on the datasource (if supported by the provider type).
void setGeometry(const QgsGeometry &geom)
Set this feature's geometry from another QgsGeometry object (deep copy)
A convenience class for writing vector files to disk.
QgsDataProvider * provider(const QString &providerKey, const QString &dataSource)
Create an instance of the provider.
const QgsFeatureIds & selectedFeaturesIds() const
Return reference to identifiers of selected features.
#define QgsDebugMsgLevel(str, level)
virtual int capabilities() const
Returns a bitmask containing the supported capabilities Note, some capabilities may change depending ...
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
QString errorMessage()
retrieves error message
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QList< int > QgsAttributeList
const QgsAttributes & attributes() const
~QgsVectorLayerImport()
close the new created layer
void clearErrors()
Clear recorded errors.
int count() const
Return number of items.
ImportError mError
contains error value
bool createSpatialIndex()
create index
A registry / canonical manager of data providers.
bool addFeature(QgsFeature &feature)
add feature to the new created layer
QLibrary * providerLibrary(const QString &providerKey) const
QString providerType() const
Return the provider type for this layer.
virtual long featureCount() const
Number of features in the layer.
QgsAttributeList pendingAllAttributesList()
returns list of attributes
ImportError hasError()
checks whether there were any errors
QVector< QVariant > QgsAttributes
virtual bool isValid()=0
Returns true if this is a valid layer.
Class for storing a coordinate reference system (CRS)
int transform(const QgsCoordinateTransform &ct)
Transform this geometry as described by CoordinateTranasform ct.
const QgsCoordinateReferenceSystem & crs() const
Returns layer's spatial reference system.
void(*)() cast_to_fptr(void *p)
Custom exception class for Coordinate Reference System related exceptions.
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
bool nextFeature(QgsFeature &f)
This is the base class for vector data providers.
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Represents a vector layer which manages a vector based data sets.
QgsFeatureRequest & setFlags(Flags flags)
Set flags that affect how features will be fetched.
QgsFeatureList mFeatureBuffer
QString storageType() const
Returns the permanent storage type for this layer as a friendly name.