QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsvectorlayerexporter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorlayerexporter.cpp
3  -------------------
4  begin : Thu Aug 25 2011
5  copyright : (C) 2011 by Giuseppe Sucameli
6  email : brush.tyler at gmail.com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 
19 #include "qgsfields.h"
20 #include "qgsfeature.h"
21 #include "qgsfeatureiterator.h"
22 #include "qgsgeometry.h"
23 #include "qgslogger.h"
24 #include "qgsmessagelog.h"
25 #include "qgsgeometrycollection.h"
27 #include "qgsvectorlayerexporter.h"
28 #include "qgsproviderregistry.h"
29 #include "qgsdatasourceuri.h"
30 #include "qgsexception.h"
31 #include "qgsvectordataprovider.h"
32 #include "qgsvectorlayer.h"
33 #include "qgsabstractgeometry.h"
34 
35 #include <QProgressDialog>
36 
37 #define FEATURE_BUFFER_SIZE 200
38 
40  const QString &uri,
41  const QgsFields &fields,
42  QgsWkbTypes::Type geometryType,
43  const QgsCoordinateReferenceSystem &destCRS,
44  bool overwrite,
45  QMap<int, int> *oldToNewAttrIdx,
46  QString *errorMessage,
47  const QMap<QString, QVariant> *options
48 );
49 
50 
52  const QString &providerKey,
53  const QgsFields &fields,
54  QgsWkbTypes::Type geometryType,
56  bool overwrite,
57  const QMap<QString, QVariant> &options,
58  QgsFeatureSink::SinkFlags sinkFlags )
59  : mErrorCount( 0 )
60  , mAttributeCount( -1 )
61 
62 {
63  mProvider = nullptr;
64 
65  // create an empty layer
66  QString errMsg;
68  mError = pReg->createEmptyLayer( providerKey, uri, fields, geometryType, crs, overwrite, mOldToNewAttrIdx,
69  errMsg, !options.isEmpty() ? &options : nullptr );
70  if ( errorCode() )
71  {
72  mErrorMessage = errMsg;
73  return;
74  }
75 
76  const auto constMOldToNewAttrIdx = mOldToNewAttrIdx;
77  for ( int idx : constMOldToNewAttrIdx )
78  {
79  if ( idx > mAttributeCount )
80  mAttributeCount = idx;
81  }
82 
83  mAttributeCount++;
84 
85  QgsDebugMsgLevel( QStringLiteral( "Created empty layer" ), 2 );
86 
87  QString uriUpdated( uri );
88  // HACK sorry...
89  if ( providerKey == QLatin1String( "ogr" ) )
90  {
91  QString layerName;
92  if ( options.contains( QStringLiteral( "layerName" ) ) )
93  layerName = options.value( QStringLiteral( "layerName" ) ).toString();
94  if ( !layerName.isEmpty() )
95  {
96  uriUpdated += QLatin1String( "|layername=" );
97  uriUpdated += layerName;
98  }
99  }
100 
101  QgsDataProvider::ProviderOptions providerOptions;
102  QgsVectorDataProvider *vectorProvider = qobject_cast< QgsVectorDataProvider * >( pReg->createProvider( providerKey, uriUpdated, providerOptions ) );
103  if ( !vectorProvider || !vectorProvider->isValid() || ( vectorProvider->capabilities() & QgsVectorDataProvider::AddFeatures ) == 0 )
104  {
105  mError = ErrInvalidLayer;
106  mErrorMessage = QObject::tr( "Loading of layer failed" );
107 
108  delete vectorProvider;
109  return;
110  }
111 
112  // If the result is a geopackage layer and there is already a field name FID requested which
113  // might contain duplicates, make sure to generate a new field with a unique name instead
114  // that will be filled by ogr with unique values.
115 
116  // HACK sorry
117  const QString path = QgsProviderRegistry::instance()->decodeUri( QStringLiteral( "ogr" ), uri ).value( QStringLiteral( "path" ) ).toString();
118  if ( sinkFlags.testFlag( QgsFeatureSink::SinkFlag::RegeneratePrimaryKey ) && path.endsWith( QLatin1String( ".gpkg" ), Qt::CaseInsensitive ) )
119  {
120  QString fidName = options.value( QStringLiteral( "FID" ), QStringLiteral( "FID" ) ).toString();
121  int fidIdx = fields.lookupField( fidName );
122  if ( fidIdx != -1 )
123  {
124  mOldToNewAttrIdx.remove( fidIdx );
125  }
126  }
127 
128  mProvider = vectorProvider;
129  mError = NoError;
130 }
131 
133 {
134  flushBuffer();
135  delete mProvider;
136 }
137 
139 {
140  return mError;
141 }
142 
144 {
145  return mErrorMessage;
146 }
147 
149 {
150  QgsFeatureList::iterator fIt = features.begin();
151  bool result = true;
152  for ( ; fIt != features.end(); ++fIt )
153  {
154  result = result && addFeature( *fIt, flags );
155  }
156  return result;
157 }
158 
160 {
161  QgsAttributes attrs = feat.attributes();
162 
163  QgsFeature newFeat;
164  if ( feat.hasGeometry() )
165  newFeat.setGeometry( feat.geometry() );
166 
167  newFeat.initAttributes( mAttributeCount );
168 
169  for ( int i = 0; i < attrs.count(); ++i )
170  {
171  // add only mapped attributes (un-mapped ones will not be present in the
172  // destination layer)
173  int dstIdx = mOldToNewAttrIdx.value( i, -1 );
174  if ( dstIdx < 0 )
175  continue;
176 
177  QgsDebugMsgLevel( QStringLiteral( "moving field from pos %1 to %2" ).arg( i ).arg( dstIdx ), 3 );
178  newFeat.setAttribute( dstIdx, attrs.at( i ) );
179  }
180 
181  mFeatureBuffer.append( newFeat );
182 
183  if ( mFeatureBuffer.count() >= FEATURE_BUFFER_SIZE )
184  {
185  return flushBuffer();
186  }
187 
188  return true;
189 }
190 
192 {
193  if ( mFeatureBuffer.count() <= 0 )
194  return true;
195 
196  if ( !mProvider->addFeatures( mFeatureBuffer, QgsFeatureSink::FastInsert ) )
197  {
198  QStringList errors = mProvider->errors();
199  mProvider->clearErrors();
200 
201  mErrorMessage = QObject::tr( "Creation error for features from #%1 to #%2. Provider errors was: \n%3" )
202  .arg( mFeatureBuffer.first().id() )
203  .arg( mFeatureBuffer.last().id() )
204  .arg( errors.join( QStringLiteral( "\n" ) ) );
205 
206  mError = ErrFeatureWriteFailed;
207  mErrorCount += mFeatureBuffer.count();
208 
209  mFeatureBuffer.clear();
210  QgsDebugMsg( mErrorMessage );
211  return false;
212  }
213 
214  mFeatureBuffer.clear();
215  return true;
216 }
217 
218 bool QgsVectorLayerExporter::createSpatialIndex()
219 {
220  if ( mProvider && ( mProvider->capabilities() & QgsVectorDataProvider::CreateSpatialIndex ) != 0 )
221  {
222  return mProvider->createSpatialIndex();
223  }
224  else
225  {
226  return true;
227  }
228 }
229 
232  const QString &uri,
233  const QString &providerKey,
234  const QgsCoordinateReferenceSystem &destCRS,
235  bool onlySelected,
236  QString *errorMessage,
237  const QMap<QString, QVariant> &options,
238  QgsFeedback *feedback )
239 {
242  bool shallTransform = false;
243 
244  if ( !layer )
245  return ErrInvalidLayer;
246 
247  if ( destCRS.isValid() )
248  {
249  // This means we should transform
250  outputCRS = destCRS;
251  shallTransform = true;
252  }
253  else
254  {
255  // This means we shouldn't transform, use source CRS as output (if defined)
256  outputCRS = layer->crs();
257  }
258 
259 
260  bool overwrite = false;
261  bool forceSinglePartGeom = false;
262  QMap<QString, QVariant> providerOptions = options;
263  if ( !options.isEmpty() )
264  {
265  overwrite = providerOptions.take( QStringLiteral( "overwrite" ) ).toBool();
266  forceSinglePartGeom = providerOptions.take( QStringLiteral( "forceSinglePartGeometryType" ) ).toBool();
267  }
268 
269  QgsFields fields = layer->fields();
270 
271  QgsWkbTypes::Type wkbType = layer->wkbType();
272 
273  // Special handling for Shapefiles
274  if ( layer->providerType() == QLatin1String( "ogr" ) && layer->storageType() == QLatin1String( "ESRI Shapefile" ) )
275  {
276  // convert field names to lowercase
277  for ( int fldIdx = 0; fldIdx < fields.count(); ++fldIdx )
278  {
279  fields.rename( fldIdx, fields.at( fldIdx ).name().toLower() );
280  }
281  }
282 
283  bool convertGeometryToSinglePart = false;
284  if ( forceSinglePartGeom && QgsWkbTypes::isMultiType( wkbType ) )
285  {
286  wkbType = QgsWkbTypes::singleType( wkbType );
287  convertGeometryToSinglePart = true;
288  }
289 
290  QgsVectorLayerExporter *writer =
291  new QgsVectorLayerExporter( uri, providerKey, fields, wkbType, outputCRS, overwrite, providerOptions );
292 
293  // check whether file creation was successful
294  ExportError err = writer->errorCode();
295  if ( err != NoError )
296  {
297  if ( errorMessage )
298  *errorMessage = writer->errorMessage();
299  delete writer;
300  return err;
301  }
302 
303  if ( errorMessage )
304  {
305  errorMessage->clear();
306  }
307 
308  QgsFeature fet;
309 
310  QgsFeatureRequest req;
311  if ( wkbType == QgsWkbTypes::NoGeometry )
313  if ( onlySelected )
314  req.setFilterFids( layer->selectedFeatureIds() );
315 
316  QgsFeatureIterator fit = layer->getFeatures( req );
317 
318  // Create our transform
319  if ( destCRS.isValid() )
320  {
321  ct = QgsCoordinateTransform( layer->crs(), destCRS, layer->transformContext() );
322  }
323 
324  // Check for failure
325  if ( !ct.isValid() )
326  shallTransform = false;
327 
328  long n = 0;
329  long approxTotal = onlySelected ? layer->selectedFeatureCount() : layer->featureCount();
330 
331  if ( errorMessage )
332  {
333  *errorMessage = QObject::tr( "Feature write errors:" );
334  }
335 
336  bool canceled = false;
337 
338  // write all features
339  while ( fit.nextFeature( fet ) )
340  {
341  if ( feedback && feedback->isCanceled() )
342  {
343  canceled = true;
344  if ( errorMessage )
345  {
346  *errorMessage += '\n' + QObject::tr( "Import was canceled at %1 of %2" ).arg( n ).arg( approxTotal );
347  }
348  break;
349  }
350 
351  if ( writer->errorCount() > 1000 )
352  {
353  if ( errorMessage )
354  {
355  *errorMessage += '\n' + QObject::tr( "Stopping after %1 errors" ).arg( writer->errorCount() );
356  }
357  break;
358  }
359 
360  if ( shallTransform )
361  {
362  try
363  {
364  if ( fet.hasGeometry() )
365  {
366  QgsGeometry g = fet.geometry();
367  g.transform( ct );
368  fet.setGeometry( g );
369  }
370  }
371  catch ( QgsCsException &e )
372  {
373  delete writer;
374 
375  QString msg = QObject::tr( "Failed to transform a point while drawing a feature with ID '%1'. Writing stopped. (Exception: %2)" )
376  .arg( fet.id() ).arg( e.what() );
377  QgsMessageLog::logMessage( msg, QObject::tr( "Vector import" ) );
378  if ( errorMessage )
379  *errorMessage += '\n' + msg;
380 
381  return ErrProjection;
382  }
383  }
384 
385  // Handles conversion to single-part
386  if ( convertGeometryToSinglePart && fet.geometry().isMultipart() )
387  {
388  QgsGeometry singlePartGeometry { fet.geometry() };
389  // We want a failure if the geometry cannot be converted to single-part without data loss!
390  // check if there are more than one part
391  const QgsGeometryCollection *c = qgsgeometry_cast<const QgsGeometryCollection *>( singlePartGeometry.constGet() );
392  if ( ( c && c->partCount() > 1 ) || ! singlePartGeometry.convertToSingleType() )
393  {
394  delete writer;
395  QString msg = QObject::tr( "Failed to transform a feature with ID '%1' to single part. Writing stopped." )
396  .arg( fet.id() );
397  QgsMessageLog::logMessage( msg, QObject::tr( "Vector import" ) );
398  if ( errorMessage )
399  *errorMessage += '\n' + msg;
400  return ErrFeatureWriteFailed;
401  }
402  fet.setGeometry( singlePartGeometry );
403  }
404 
405  if ( !writer->addFeature( fet ) )
406  {
407  if ( writer->errorCode() && errorMessage )
408  {
409  *errorMessage += '\n' + writer->errorMessage();
410  }
411  }
412  n++;
413 
414  if ( feedback )
415  {
416  feedback->setProgress( 100.0 * static_cast< double >( n ) / approxTotal );
417  }
418 
419  }
420 
421  // flush the buffer to be sure that all features are written
422  if ( !writer->flushBuffer() )
423  {
424  if ( writer->errorCode() && errorMessage )
425  {
426  *errorMessage += '\n' + writer->errorMessage();
427  }
428  }
429  int errors = writer->errorCount();
430 
431  if ( !writer->createSpatialIndex() )
432  {
433  if ( writer->errorCode() && errorMessage )
434  {
435  *errorMessage += '\n' + writer->errorMessage();
436  }
437  }
438 
439  delete writer;
440 
441  if ( errorMessage )
442  {
443  if ( errors > 0 )
444  {
445  *errorMessage += '\n' + QObject::tr( "Only %1 of %2 features written." ).arg( n - errors ).arg( n );
446  }
447  else
448  {
449  errorMessage->clear();
450  }
451  }
452 
453  if ( canceled )
454  return ErrUserCanceled;
455  else if ( errors > 0 )
456  return ErrFeatureWriteFailed;
457 
458  return NoError;
459 }
460 
461 
462 //
463 // QgsVectorLayerExporterTask
464 //
465 
466 QgsVectorLayerExporterTask::QgsVectorLayerExporterTask( QgsVectorLayer *layer, const QString &uri, const QString &providerKey, const QgsCoordinateReferenceSystem &destinationCrs, const QMap<QString, QVariant> &options, bool ownsLayer )
467  : QgsTask( tr( "Exporting %1" ).arg( layer->name() ), QgsTask::CanCancel )
468  , mLayer( layer )
469  , mOwnsLayer( ownsLayer )
470  , mDestUri( uri )
471  , mDestProviderKey( providerKey )
472  , mDestCrs( destinationCrs )
473  , mOptions( options )
474  , mOwnedFeedback( new QgsFeedback() )
475 {
476  if ( mLayer )
477  setDependentLayers( QList< QgsMapLayer * >() << mLayer );
478 }
479 
480 QgsVectorLayerExporterTask *QgsVectorLayerExporterTask::withLayerOwnership( QgsVectorLayer *layer, const QString &uri, const QString &providerKey, const QgsCoordinateReferenceSystem &destinationCrs, const QMap<QString, QVariant> &options )
481 {
482  std::unique_ptr< QgsVectorLayerExporterTask > newTask( new QgsVectorLayerExporterTask( layer, uri, providerKey, destinationCrs, options ) );
483  newTask->mOwnsLayer = true;
484  return newTask.release();
485 }
486 
488 {
489  mOwnedFeedback->cancel();
490  QgsTask::cancel();
491 }
492 
494 {
495  if ( !mLayer )
496  return false;
497 
498  connect( mOwnedFeedback.get(), &QgsFeedback::progressChanged, this, &QgsVectorLayerExporterTask::setProgress );
499 
500 
502  mLayer.data(), mDestUri, mDestProviderKey, mDestCrs, false, &mErrorMessage,
503  mOptions, mOwnedFeedback.get() );
504 
505  return mError == QgsVectorLayerExporter::NoError;
506 }
507 
509 {
510  // QgsMapLayer has QTimer member, which must not be destroyed from another thread
511  if ( mOwnsLayer )
512  delete mLayer;
513 
514  if ( result )
515  emit exportComplete();
516  else
517  emit errorOccurred( mError, mErrorMessage );
518 }
QgsFeatureRequest::NoGeometry
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Definition: qgsfeaturerequest.h:107
QgsVectorLayer::getFeatures
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Definition: qgsvectorlayer.cpp:993
QgsMapLayer::crs
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:88
qgsfields.h
QgsFeedback::setProgress
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:75
QgsProviderRegistry::createProvider
QgsDataProvider * createProvider(const QString &providerKey, const QString &dataSource, const QgsDataProvider::ProviderOptions &options=QgsDataProvider::ProviderOptions())
Creates a new instance of a provider.
Definition: qgsproviderregistry.cpp:404
QgsGeometry::transform
OperationResult transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection direction=QgsCoordinateTransform::ForwardTransform, bool transformZ=false) SIP_THROW(QgsCsException)
Transforms this geometry as described by the coordinate transform ct.
Definition: qgsgeometry.cpp:2836
QgsVectorDataProvider::createSpatialIndex
virtual bool createSpatialIndex()
Creates a spatial index on the datasource (if supported by the provider type).
Definition: qgsvectordataprovider.cpp:180
QgsVectorLayer::wkbType
Q_INVOKABLE QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
Definition: qgsvectorlayer.cpp:664
QgsVectorLayerExporterTask::cancel
void cancel() override
Notifies the task that it should terminate.
Definition: qgsvectorlayerexporter.cpp:487
QgsDataProvider::ProviderOptions
Setting options for creating vector data providers.
Definition: qgsdataprovider.h:104
QgsVectorLayerExporterTask::errorOccurred
void errorOccurred(int error, const QString &errorMessage)
Emitted when an error occurs which prevented the layer being exported (or if the task is canceled).
QgsVectorLayerExporter::addFeature
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a single feature to the sink.
Definition: qgsvectorlayerexporter.cpp:159
QgsFeature::initAttributes
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
Definition: qgsfeature.cpp:202
QgsWkbTypes::singleType
static Type singleType(Type type)
Returns the single type for a WKB type.
Definition: qgswkbtypes.h:156
QgsDebugMsgLevel
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
QgsProviderRegistry
Definition: qgsproviderregistry.h:54
QgsGeometry::isMultipart
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
Definition: qgsgeometry.cpp:377
QgsVectorDataProvider::CreateSpatialIndex
@ CreateSpatialIndex
Allows creation of spatial index.
Definition: qgsvectordataprovider.h:80
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsVectorLayerExporter::~QgsVectorLayerExporter
~QgsVectorLayerExporter() override
Finalizes the export and closes the new created layer.
Definition: qgsvectorlayerexporter.cpp:132
QgsVectorLayerExporterTask::withLayerOwnership
static QgsVectorLayerExporterTask * withLayerOwnership(QgsVectorLayer *layer, const QString &uri, const QString &providerKey, const QgsCoordinateReferenceSystem &destinationCrs, const QMap< QString, QVariant > &options=QMap< QString, QVariant >())
Creates a new QgsVectorLayerExporterTask which has ownership over a source layer.
Definition: qgsvectorlayerexporter.cpp:480
QgsWkbTypes::isMultiType
static bool isMultiType(Type type)
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:831
qgsfeatureiterator.h
QgsFields::count
int count() const
Returns number of items.
Definition: qgsfields.cpp:133
QgsFields
Definition: qgsfields.h:44
QgsVectorLayerExporter::QgsVectorLayerExporter
QgsVectorLayerExporter(const QString &uri, const QString &provider, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, bool overwrite=false, const QMap< QString, QVariant > &options=QMap< QString, QVariant >(), QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags())
Constructor for QgsVectorLayerExporter.
Definition: qgsvectorlayerexporter.cpp:51
QgsVectorLayerExporter::errorCount
int errorCount() const
Returns the number of error messages encountered during the export.
Definition: qgsvectorlayerexporter.h:135
qgsfeature.h
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsVectorLayerExporter::ErrFeatureWriteFailed
@ ErrFeatureWriteFailed
An error occurred while writing a feature to the destination.
Definition: qgsvectorlayerexporter.h:59
QgsVectorLayerExporterTask::exportComplete
void exportComplete()
Emitted when exporting the layer is successfully completed.
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsCoordinateTransform::isValid
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
Definition: qgscoordinatetransform.cpp:876
QgsVectorLayerExporterTask::QgsVectorLayerExporterTask
QgsVectorLayerExporterTask(QgsVectorLayer *layer, const QString &uri, const QString &providerKey, const QgsCoordinateReferenceSystem &destinationCrs, const QMap< QString, QVariant > &options=QMap< QString, QVariant >(), bool ownsLayer=false)
Constructor for QgsVectorLayerExporterTask.
Definition: qgsvectorlayerexporter.cpp:466
QgsVectorLayer::featureCount
long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
Definition: qgsvectorlayer.cpp:751
QgsField::name
QString name
Definition: qgsfield.h:59
QgsVectorLayerExporter::NoError
@ NoError
No errors were encountered.
Definition: qgsvectorlayerexporter.h:53
QgsMapLayer::providerType
QString providerType() const
Returns the provider type (provider key) for this layer.
Definition: qgsmaplayer.cpp:1614
QgsTask::cancel
virtual void cancel()
Notifies the task that it should terminate.
Definition: qgstaskmanager.cpp:91
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3280
QgsProviderRegistry::createEmptyLayer
QgsVectorLayerExporter::ExportError createEmptyLayer(const QString &providerKey, const QString &uri, const QgsFields &fields, QgsWkbTypes::Type wkbType, const QgsCoordinateReferenceSystem &srs, bool overwrite, QMap< int, int > &oldToNewAttrIdxMap, QString &errorMessage, const QMap< QString, QVariant > *options)
Creates new empty vector layer.
Definition: qgsproviderregistry.cpp:449
QgsFeature::id
QgsFeatureId id
Definition: qgsfeature.h:68
QgsVectorDataProvider::errors
QStringList errors() const
Gets recorded errors.
Definition: qgsvectordataprovider.cpp:707
QgsFeatureRequest
Definition: qgsfeaturerequest.h:75
QgsGeometryCollection
Geometry collection.
Definition: qgsgeometrycollection.h:35
QgsVectorDataProvider::capabilities
virtual QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
Definition: qgsvectordataprovider.cpp:191
QgsVectorLayerExporterTask
Definition: qgsvectorlayerexporter.h:183
QgsCsException
Definition: qgsexception.h:65
createEmptyLayer_t
QgsVectorLayerExporter::ExportError createEmptyLayer_t(const QString &uri, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &destCRS, bool overwrite, QMap< int, int > *oldToNewAttrIdx, QString *errorMessage, const QMap< QString, QVariant > *options)
Definition: qgsvectorlayerexporter.cpp:39
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:137
qgsproviderregistry.h
qgsdatasourceuri.h
QgsFeedback
Definition: qgsfeedback.h:43
QgsTask::setDependentLayers
void setDependentLayers(const QList< QgsMapLayer * > &dependentLayers)
Sets a list of layers on which the task depends.
Definition: qgstaskmanager.cpp:196
QgsException::what
QString what() const
Definition: qgsexception.h:48
QgsVectorLayer::selectedFeatureIds
const Q_INVOKABLE QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
Definition: qgsvectorlayer.cpp:3433
QgsTask::setProgress
void setProgress(double progress)
Sets the task's current progress.
Definition: qgstaskmanager.cpp:232
qgsvectordataprovider.h
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:572
QgsVectorLayerExporter::ErrInvalidLayer
@ ErrInvalidLayer
Could not access newly created destination layer.
Definition: qgsvectorlayerexporter.h:60
QgsCoordinateReferenceSystem::isValid
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Definition: qgscoordinatereferencesystem.cpp:902
QgsProviderRegistry::decodeUri
QVariantMap decodeUri(const QString &providerKey, const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
Definition: qgsproviderregistry.cpp:431
QgsVectorDataProvider::AddFeatures
@ AddFeatures
Allows adding features.
Definition: qgsvectordataprovider.h:75
QgsVectorLayerExporter::ErrUserCanceled
@ ErrUserCanceled
User canceled the export.
Definition: qgsvectorlayerexporter.h:64
QgsFeatureRequest::setFilterFids
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets feature IDs that should be fetched.
Definition: qgsfeaturerequest.cpp:110
QgsVectorLayerExporter::flushBuffer
bool flushBuffer() override
Flushes any internal buffer which may exist in the sink, causing any buffered features to be added to...
Definition: qgsvectorlayerexporter.cpp:191
QgsFeedback::progressChanged
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
QgsFeature::attributes
QgsAttributes attributes
Definition: qgsfeature.h:69
QgsVectorLayerExporter::exportLayer
static ExportError exportLayer(QgsVectorLayer *layer, const QString &uri, const QString &providerKey, const QgsCoordinateReferenceSystem &destCRS, bool onlySelected=false, QString *errorMessage=nullptr, const QMap< QString, QVariant > &options=QMap< QString, QVariant >(), QgsFeedback *feedback=nullptr)
Writes the contents of vector layer to a different datasource.
Definition: qgsvectorlayerexporter.cpp:231
FEATURE_BUFFER_SIZE
#define FEATURE_BUFFER_SIZE
Definition: qgsvectorlayerexporter.cpp:37
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsVectorDataProvider::clearErrors
void clearErrors()
Clear recorded errors.
Definition: qgsvectordataprovider.cpp:697
qgsvectorlayer.h
QgsVectorLayerExporter::errorMessage
QString errorMessage() const
Returns any error message encountered during the export.
Definition: qgsvectorlayerexporter.cpp:143
QgsFeedback::isCanceled
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:66
QgsFeature::setAttribute
bool setAttribute(int field, const QVariant &attr)
Set an attribute's value by field index.
Definition: qgsfeature.cpp:211
QgsVectorLayerExporter::ExportError
ExportError
Error codes.
Definition: qgsvectorlayerexporter.h:51
QgsVectorDataProvider::addFeatures
bool addFeatures(QgsFeatureList &flist, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a list of features to the sink.
Definition: qgsvectordataprovider.cpp:85
QgsWkbTypes::NoGeometry
@ NoGeometry
Definition: qgswkbtypes.h:84
QgsMapLayer::transformContext
QgsCoordinateTransformContext transformContext() const
Returns the layer data provider coordinate transform context or a default transform context if the la...
Definition: qgsmaplayer.cpp:783
qgsgeometry.h
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:373
c
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
Definition: porting_processing.dox:1
QgsGeometry
Definition: qgsgeometry.h:122
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
qgsvectorlayerexporter.h
QgsVectorLayerExporter::errorCode
ExportError errorCode() const
Returns any encountered error code, or false if no error was encountered.
Definition: qgsvectorlayerexporter.cpp:138
QgsVectorLayerExporter::addFeatures
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a list of features to the sink.
Definition: qgsvectorlayerexporter.cpp:148
QgsMessageLog::logMessage
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Definition: qgsmessagelog.cpp:27
QgsVectorLayer::storageType
QString storageType() const
Returns the permanent storage type for this layer as a friendly name.
Definition: qgsvectorlayer.cpp:340
QgsVectorLayerExporter
Definition: qgsvectorlayerexporter.h:46
QgsAttributes
Definition: qgsattributes.h:57
qgsgeometrycollection.h
QgsVectorDataProvider
Definition: qgsvectordataprovider.h:58
QgsFields::rename
bool rename(int fieldIdx, const QString &name)
Renames a name of field.
Definition: qgsfields.cpp:72
qgsexception.h
QgsFeature
Definition: qgsfeature.h:55
QgsVectorLayerExporter::ErrProjection
@ ErrProjection
An error occurred while reprojecting features to destination CRS.
Definition: qgsvectorlayerexporter.h:58
QgsVectorLayer::selectedFeatureCount
int selectedFeatureCount() const
Returns the number of features that are selected in this layer.
Definition: qgsvectorlayer.cpp:3428
qgslogger.h
QgsFields::lookupField
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Definition: qgsfields.cpp:324
QgsFields::at
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:163
QgsCoordinateTransform
Definition: qgscoordinatetransform.h:52
QgsFeatureIterator
Definition: qgsfeatureiterator.h:263
QgsProviderRegistry::instance
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Definition: qgsproviderregistry.cpp:48
qgscoordinatereferencesystem.h
QgsFeatureRequest::setFlags
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
Definition: qgsfeaturerequest.cpp:184
qgsabstractgeometry.h
QgsVectorLayerExporterTask::finished
void finished(bool result) override
If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether ...
Definition: qgsvectorlayerexporter.cpp:508
QgsDataProvider::isValid
virtual bool isValid() const =0
Returns true if this is a valid layer.
QgsFeatureSink::FastInsert
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Definition: qgsfeaturesink.h:70
qgsmessagelog.h
QgsVectorLayerExporterTask::run
bool run() override
Performs the task's operation.
Definition: qgsvectorlayerexporter.cpp:493
QgsTask
Abstract base class for long running background tasks. Tasks can be controlled directly,...
Definition: qgstaskmanager.h:52