QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
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
20
21#include "qgsabstractgeometry.h"
24#include "qgsexception.h"
25#include "qgsfeature.h"
26#include "qgsfeatureiterator.h"
27#include "qgsfields.h"
28#include "qgsgeometry.h"
30#include "qgslogger.h"
31#include "qgsmessagelog.h"
32#include "qgsproviderregistry.h"
34#include "qgsvectorlayer.h"
35
36#include <QProgressDialog>
37#include <QString>
38#include <QThread>
39
40#include "moc_qgsvectorlayerexporter.cpp"
41
42using namespace Qt::StringLiterals;
43
45 const QString &uri,
46 const QgsFields &fields,
47 Qgis::WkbType geometryType,
48 const QgsCoordinateReferenceSystem &destCRS,
49 bool overwrite,
50 QMap<int, int> *oldToNewAttrIdx,
51 QString *errorMessage,
52 const QMap<QString, QVariant> *options
53);
54
55
56//
57// QgsVectorLayerExporter::ExportOptions
58//
59
61{
62 mTransformContext = context;
63}
64
69
74
79
84
89
91{
92 mFilterExpression = expression;
93}
94
96{
97 return mFilterExpression;
98}
99
101{
102 mExpressionContext = context;
103}
104
106{
107 return mExpressionContext;
108}
109
110QList<QgsVectorLayerExporter::OutputField> QgsVectorLayerExporter::ExportOptions::outputFields() const
111{
112 return mOutputFields;
113}
114
115void QgsVectorLayerExporter::ExportOptions::setOutputFields( const QList<QgsVectorLayerExporter::OutputField> &fields )
116{
117 mOutputFields = fields;
118}
119
120
121//
122// QgsVectorLayerExporter
123//
124
126 const QString &providerKey,
127 const QgsFields &fields,
128 Qgis::WkbType geometryType,
130 bool overwrite,
131 const QMap<QString, QVariant> &options,
132 QgsFeatureSink::SinkFlags sinkFlags )
133{
134 mProvider = nullptr;
135
136 QMap<QString, QVariant> modifiedOptions( options );
137
138 if ( providerKey == "ogr"_L1 &&
139 options.contains( u"driverName"_s ) &&
140 ( options[ u"driverName"_s ].toString().compare( "GPKG"_L1, Qt::CaseInsensitive ) == 0 ||
141 options[ u"driverName"_s ].toString().compare( "SQLite"_L1, Qt::CaseInsensitive ) == 0 ) )
142 {
143 if ( geometryType != Qgis::WkbType::NoGeometry )
144 {
145 // For GPKG/Spatialite, we explicitly ask not to create a spatial index at
146 // layer creation since this would slow down inserts. Defer its creation
147 // to end of exportLayer() or destruction of this object.
148 QStringList modifiedLayerOptions;
149 if ( options.contains( u"layerOptions"_s ) )
150 {
151 const QStringList layerOptions = options.value( u"layerOptions"_s ).toStringList();
152 for ( const QString &layerOption : layerOptions )
153 {
154 if ( layerOption.compare( "SPATIAL_INDEX=YES"_L1, Qt::CaseInsensitive ) == 0 ||
155 layerOption.compare( "SPATIAL_INDEX=ON"_L1, Qt::CaseInsensitive ) == 0 ||
156 layerOption.compare( "SPATIAL_INDEX=TRUE"_L1, Qt::CaseInsensitive ) == 0 ||
157 layerOption.compare( "SPATIAL_INDEX=1"_L1, Qt::CaseInsensitive ) == 0 )
158 {
159 // do nothing
160 }
161 else if ( layerOption.compare( "SPATIAL_INDEX=NO"_L1, Qt::CaseInsensitive ) == 0 ||
162 layerOption.compare( "SPATIAL_INDEX=OFF"_L1, Qt::CaseInsensitive ) == 0 ||
163 layerOption.compare( "SPATIAL_INDEX=FALSE"_L1, Qt::CaseInsensitive ) == 0 ||
164 layerOption.compare( "SPATIAL_INDEX=0"_L1, Qt::CaseInsensitive ) == 0 )
165 {
166 mCreateSpatialIndex = false;
167 }
168 else
169 {
170 modifiedLayerOptions << layerOption;
171 }
172 }
173 }
174 modifiedLayerOptions << u"SPATIAL_INDEX=FALSE"_s;
175 modifiedOptions[ u"layerOptions"_s ] = modifiedLayerOptions;
176 }
177 }
178
179 // create an empty layer
180 QString errMsg;
182 QString uriUpdated;
183 mError = pReg->createEmptyLayer( providerKey, uri, fields, geometryType, crs, overwrite, mOldToNewAttrIdx,
184 errMsg, !modifiedOptions.isEmpty() ? &modifiedOptions : nullptr, uriUpdated );
185
187 {
188 mErrorMessage = errMsg;
189 return;
190 }
191
192 const auto constMOldToNewAttrIdx = mOldToNewAttrIdx;
193 for ( const int idx : constMOldToNewAttrIdx )
194 {
195 if ( idx > mAttributeCount )
196 mAttributeCount = idx;
197 }
198
199 mAttributeCount++;
200
201 QgsDebugMsgLevel( u"Created empty layer"_s, 2 );
202
203 // Oracle specific HACK: we cannot guess the geometry type when there is no rows, so we need
204 // to force it in the uri
205 if ( providerKey == "oracle"_L1 )
206 {
207 uriUpdated += u" type=%1"_s.arg( QgsWkbTypes::displayString( geometryType ) );
208 }
209
210 const QgsDataProvider::ProviderOptions providerOptions;
211 QgsVectorDataProvider *vectorProvider = qobject_cast< QgsVectorDataProvider * >( pReg->createProvider( providerKey, uriUpdated, providerOptions ) );
212 if ( !vectorProvider || !vectorProvider->isValid() || ( vectorProvider->capabilities() & Qgis::VectorProviderCapability::AddFeatures ) == 0 )
213 {
215 mErrorMessage = QObject::tr( "Loading of layer failed" );
216
217 delete vectorProvider;
218 return;
219 }
220
221 // If the result is a geopackage layer and there is already a field name FID requested which
222 // might contain duplicates, make sure to generate a new field with a unique name instead
223 // that will be filled by ogr with unique values.
224
225 // HACK sorry
226 const QString path = QgsProviderRegistry::instance()->decodeUri( u"ogr"_s, uri ).value( u"path"_s ).toString();
227 if ( sinkFlags.testFlag( QgsFeatureSink::SinkFlag::RegeneratePrimaryKey ) && path.endsWith( ".gpkg"_L1, Qt::CaseInsensitive ) )
228 {
229 const QString fidName = options.value( u"FID"_s, u"FID"_s ).toString();
230 const int fidIdx = fields.lookupField( fidName );
231 if ( fidIdx != -1 )
232 {
233 mOldToNewAttrIdx.remove( fidIdx );
234 }
235 }
236
237 mProvider = vectorProvider;
239}
240
242{
243 flushBuffer();
244
245 if ( mCreateSpatialIndex )
246 {
247 createSpatialIndex();
248 }
249
250 delete mProvider;
251}
252
257
259{
260 return mErrorMessage;
261}
262
267
269{
270 QgsFeatureList::iterator fIt = features.begin();
271 bool result = true;
272 for ( ; fIt != features.end(); ++fIt )
273 {
274 result = result && addFeature( *fIt, flags );
275 }
276 return result;
277}
278
280{
281 const QgsAttributes attrs = feat.attributes();
282
283 QgsFeature newFeat;
284 if ( feat.hasGeometry() )
285 newFeat.setGeometry( feat.geometry() );
286
287 newFeat.initAttributes( mAttributeCount );
288
289 for ( int i = 0; i < attrs.count(); ++i )
290 {
291 // add only mapped attributes (un-mapped ones will not be present in the
292 // destination layer)
293 const int dstIdx = mOldToNewAttrIdx.value( i, -1 );
294 if ( dstIdx < 0 )
295 continue;
296
297 QgsDebugMsgLevel( u"moving field from pos %1 to %2"_s.arg( i ).arg( dstIdx ), 3 );
298 newFeat.setAttribute( dstIdx, attrs.at( i ) );
299 }
300
301 mFeatureBuffer.append( newFeat );
302 mFeatureBufferMemoryUsage += newFeat.approximateMemoryUsage();
303
304 if ( mFeatureBufferMemoryUsage >= 100 * 1000 * 1000 )
305 {
306 return flushBuffer();
307 }
308
309 return true;
310}
311
313{
314 return mErrorMessage;
315}
316
318{
319 mFeatureBufferMemoryUsage = 0;
320 if ( mFeatureBuffer.count() <= 0 )
321 return true;
322
323 if ( !mProvider->addFeatures( mFeatureBuffer, QgsFeatureSink::FastInsert ) )
324 {
325 const QStringList errors = mProvider->errors();
326 mProvider->clearErrors();
327
328 mErrorMessage = QObject::tr( "Creation error for features from #%1 to #%2. Provider errors were: \n%3" )
329 .arg( mFeatureBuffer.first().id() )
330 .arg( mFeatureBuffer.last().id() )
331 .arg( errors.join( QLatin1Char( '\n' ) ) );
332
334 mErrorCount += mFeatureBuffer.count();
335
336 mFeatureBuffer.clear();
337 QgsDebugError( mErrorMessage );
338 return false;
339 }
340
341 mFeatureBuffer.clear();
342 return true;
343}
344
345bool QgsVectorLayerExporter::createSpatialIndex()
346{
347 mCreateSpatialIndex = false;
348 if ( mProvider && ( mProvider->capabilities() & Qgis::VectorProviderCapability::CreateSpatialIndex ) != 0 )
349 {
350 return mProvider->createSpatialIndex();
351 }
352 else
353 {
354 return true;
355 }
356}
357
359 const QString &uri,
360 const QString &providerKey,
361 const QgsCoordinateReferenceSystem &destCRS,
362 bool onlySelected,
363 QString *errorMessage,
364 const QMap<QString, QVariant> &options,
365 QgsFeedback *feedback )
366{
367 ExportOptions exportOptions;
368 exportOptions.setSelectedOnly( onlySelected );
369 exportOptions.setDestinationCrs( destCRS );
370 exportOptions.setTransformContext( layer->transformContext() );
371 return exportLayer( layer, uri, providerKey, exportOptions, errorMessage, options, feedback );
372}
373
374Qgis::VectorExportResult QgsVectorLayerExporter::exportLayer( QgsVectorLayer *layer, const QString &uri, const QString &providerKey, const ExportOptions &exportOptions, QString *errorMessage, const QMap<QString, QVariant> &_providerOptions, QgsFeedback *feedback )
375{
376 QgsExpressionContext expressionContext = exportOptions.expressionContext();
377
380 bool shallTransform = false;
381
382 if ( !layer )
384
385 if ( exportOptions.destinationCrs().isValid() )
386 {
387 // This means we should transform
388 outputCRS = exportOptions.destinationCrs();
389 shallTransform = true;
390 }
391 else
392 {
393 // This means we shouldn't transform, use source CRS as output (if defined)
394 outputCRS = layer->crs();
395 }
396
397 QMap<QString, QVariant> providerOptions = _providerOptions;
398 const bool overwrite = providerOptions.take( u"overwrite"_s ).toBool();
399 const bool forceSinglePartGeom = providerOptions.take( u"forceSinglePartGeometryType"_s ).toBool();
400
401 QgsFields outputFields;
402 bool useFieldMapping = false;
403 QList<QgsExpression> expressions;
404 if ( exportOptions.outputFields().isEmpty() )
405 {
406 outputFields = layer->fields();
407 }
408 else
409 {
410 useFieldMapping = true;
411 const QList<QgsVectorLayerExporter::OutputField> exportFieldDefinitions = exportOptions.outputFields();
412 for ( const QgsVectorLayerExporter::OutputField &field : exportFieldDefinitions )
413 {
414 outputFields.append( field.field );
415 expressions.append( QgsExpression( field.expression ) );
416 expressions.last().prepare( &expressionContext );
417 if ( expressions.last().hasParserError() )
418 {
419 if ( errorMessage )
420 *errorMessage = QObject::tr( "Parser error for field \"%1\" with expression \"%2\": %3" )
421 .arg(
422 field.field.name(),
423 field.expression,
424 expressions.last().parserErrorString()
425 );
427 }
428 }
429 }
430
431 Qgis::WkbType wkbType = layer->wkbType();
432
433 bool convertGeometryToSinglePart = false;
434 if ( forceSinglePartGeom && QgsWkbTypes::isMultiType( wkbType ) )
435 {
436 wkbType = QgsWkbTypes::singleType( wkbType );
437 convertGeometryToSinglePart = true;
438 }
439
440 auto writer = std::make_unique< QgsVectorLayerExporter >(
441 uri, providerKey, outputFields, wkbType, outputCRS, overwrite, providerOptions );
442
443 // check whether file creation was successful
444 const Qgis::VectorExportResult err = writer->errorCode();
446 {
447 if ( errorMessage )
448 *errorMessage = writer->errorMessage();
449 return err;
450 }
451
452 if ( errorMessage )
453 {
454 errorMessage->clear();
455 }
456
457 // Create our transform
458 if ( exportOptions.destinationCrs().isValid() )
459 {
460 ct = QgsCoordinateTransform( layer->crs(), exportOptions.destinationCrs(), exportOptions.transformContext() );
461 }
462
463 // Check for failure
464 if ( !ct.isValid() )
465 shallTransform = false;
466
468 if ( wkbType == Qgis::WkbType::NoGeometry )
470
471 if ( !exportOptions.extent().isNull() )
472 {
473 QgsCoordinateTransform extentFilterTransform( exportOptions.extent().crs(), layer->crs(), exportOptions.transformContext() );
474 extentFilterTransform.setBallparkTransformsAreAppropriate( true );
475
476 try
477 {
478 const QgsRectangle layerExtent = extentFilterTransform.transformBoundingBox( exportOptions.extent() );
479 req.setFilterRect( layerExtent );
480 }
481 catch ( QgsCsException &e )
482 {
483 QgsDebugError( u"Could not transform filter extent: %1"_s.arg( e.what() ) );
484 }
485 }
486
487 if ( !exportOptions.filterExpression().isEmpty() )
488 {
489 req.setFilterExpression( exportOptions.filterExpression() );
490 req.setExpressionContext( expressionContext );
491 }
492 else if ( exportOptions.selectedOnly() )
493 {
494 req.setFilterFids( layer->selectedFeatureIds() );
495 }
496
497 QgsFeatureIterator fit = layer->getFeatures( req );
498
499 long long n = 0;
500 const long long approxTotal = exportOptions.selectedOnly() ? layer->selectedFeatureCount() : layer->featureCount();
501
502 if ( errorMessage )
503 {
504 *errorMessage = QObject::tr( "Feature write errors:" );
505 }
506
507 bool canceled = false;
508
509 // write all features
510 QgsFeature sourceFeature;
511 while ( fit.nextFeature( sourceFeature ) )
512 {
513 if ( feedback && feedback->isCanceled() )
514 {
515 canceled = true;
516 if ( errorMessage )
517 {
518 *errorMessage += '\n' + QObject::tr( "Import was canceled at %1 of %2" ).arg( n ).arg( approxTotal );
519 }
520 break;
521 }
522
523 if ( writer->errorCount() > 1000 )
524 {
525 if ( errorMessage )
526 {
527 *errorMessage += '\n' + QObject::tr( "Stopping after %n error(s)", nullptr, writer->errorCount() );
528 }
529 break;
530 }
531
532 QgsFeature outputFeature( outputFields );
533 outputFeature.setId( sourceFeature.id() );
534 outputFeature.setGeometry( sourceFeature.geometry() );
535
536 if ( shallTransform )
537 {
538 try
539 {
540 if ( outputFeature.hasGeometry() )
541 {
542 QgsGeometry g = outputFeature.geometry();
543 g.transform( ct );
544 outputFeature.setGeometry( g );
545 }
546 }
547 catch ( QgsCsException &e )
548 {
549 const QString msg = QObject::tr( "Failed to transform feature with ID '%1'. Writing stopped. (Exception: %2)" )
550 .arg( sourceFeature.id() ).arg( e.what() );
551 QgsMessageLog::logMessage( msg, QObject::tr( "Vector import" ) );
552 if ( errorMessage )
553 *errorMessage += '\n' + msg;
554
556 }
557 }
558
559 // Handles conversion to single-part
560 if ( convertGeometryToSinglePart && outputFeature.geometry().isMultipart() )
561 {
562 QgsGeometry singlePartGeometry { outputFeature.geometry() };
563 // We want a failure if the geometry cannot be converted to single-part without data loss!
564 // check if there are more than one part
566 if ( ( c && c->partCount() > 1 ) || ! singlePartGeometry.convertToSingleType() )
567 {
568 const QString msg = QObject::tr( "Failed to transform a feature with ID '%1' to single part. Writing stopped." )
569 .arg( sourceFeature.id() );
570 QgsMessageLog::logMessage( msg, QObject::tr( "Vector import" ) );
571 if ( errorMessage )
572 *errorMessage += '\n' + msg;
574 }
575 outputFeature.setGeometry( singlePartGeometry );
576 }
577
578 // handle attribute mapping
579 if ( useFieldMapping )
580 {
581 QgsAttributes attributes;
582 attributes.reserve( expressions.size() );
583 for ( auto it = expressions.begin(); it != expressions.end(); ++it )
584 {
585 if ( it->isValid() )
586 {
587 expressionContext.setFeature( sourceFeature );
588 const QVariant value = it->evaluate( &expressionContext );
589 if ( it->hasEvalError() )
590 {
591 const QString msg = QObject::tr( "Evaluation error in expression \"%1\": %2" ).arg( it->expression(), it->evalErrorString() );
592 QgsMessageLog::logMessage( msg, QObject::tr( "Vector import" ) );
593 if ( errorMessage )
594 *errorMessage += '\n' + msg;
596 }
597 attributes.append( value );
598 }
599 else
600 {
601 attributes.append( QVariant() );
602 }
603 }
604 outputFeature.setAttributes( attributes );
605 }
606 else
607 {
608 outputFeature.setAttributes( sourceFeature.attributes() );
609 }
610
611 if ( !writer->addFeature( outputFeature ) )
612 {
613 if ( writer->errorCode() != Qgis::VectorExportResult::Success && errorMessage )
614 {
615 *errorMessage += '\n' + writer->errorMessage();
616 }
617 }
618 n++;
619
620 if ( feedback )
621 {
622 feedback->setProgress( 100.0 * static_cast< double >( n ) / static_cast< double >( approxTotal ) );
623 }
624
625 }
626
627 // flush the buffer to be sure that all features are written
628 if ( !writer->flushBuffer() )
629 {
630 if ( writer->errorCode() != Qgis::VectorExportResult::Success && errorMessage )
631 {
632 *errorMessage += '\n' + writer->errorMessage();
633 }
634 }
635 const int errors = writer->errorCount();
636
637 if ( writer->mCreateSpatialIndex && !writer->createSpatialIndex() )
638 {
639 if ( writer->errorCode() != Qgis::VectorExportResult::Success && errorMessage )
640 {
641 *errorMessage += '\n' + writer->errorMessage();
642 }
643 }
644
645 writer.reset();
646
647 if ( errorMessage )
648 {
649 if ( errors > 0 )
650 {
651 *errorMessage += '\n' + QObject::tr( "Only %1 of %2 features written." ).arg( n - errors ).arg( n );
652 }
653 else
654 {
655 errorMessage->clear();
656 }
657 }
658
659 if ( canceled )
661 else if ( errors > 0 )
663
665}
666
667//
668// QgsVectorLayerExporterTask
669//
670
671QgsVectorLayerExporterTask::QgsVectorLayerExporterTask( QgsVectorLayer *layer, const QString &uri, const QString &providerKey, const QgsCoordinateReferenceSystem &destinationCrs, const QMap<QString, QVariant> &options, bool ownsLayer )
672 : QgsTask( tr( "Exporting %1" ).arg( layer->name() ), QgsTask::CanCancel )
673 , mLayer( layer )
674 , mOwnsLayer( ownsLayer )
675 , mDestUri( uri )
676 , mDestProviderKey( providerKey )
677 , mOptions( options )
678 , mOwnedFeedback( new QgsFeedback() )
679{
680 mExportOptions.setDestinationCrs( destinationCrs );
681 mExportOptions.setTransformContext( layer->transformContext() );
682
683 if ( mLayer )
684 setDependentLayers( QList< QgsMapLayer * >() << mLayer );
685}
686
687QgsVectorLayerExporterTask::QgsVectorLayerExporterTask( QgsVectorLayer *layer, const QString &uri, const QString &providerKey, const QgsVectorLayerExporter::ExportOptions &exportOptions, const QMap<QString, QVariant> &providerOptions, bool ownsLayer )
688 : QgsTask( tr( "Exporting %1" ).arg( layer->name() ), QgsTask::CanCancel )
689 , mLayer( layer )
690 , mOwnsLayer( ownsLayer )
691 , mDestUri( uri )
692 , mDestProviderKey( providerKey )
693 , mExportOptions( exportOptions )
694 , mOptions( providerOptions )
695 , mOwnedFeedback( new QgsFeedback() )
696{
697 if ( mLayer )
698 setDependentLayers( QList< QgsMapLayer * >() << mLayer );
699 if ( mLayer && mOwnsLayer )
700 mLayer->moveToThread( nullptr );
701}
702
703QgsVectorLayerExporterTask *QgsVectorLayerExporterTask::withLayerOwnership( QgsVectorLayer *layer, const QString &uri, const QString &providerKey, const QgsCoordinateReferenceSystem &destinationCrs, const QMap<QString, QVariant> &options )
704{
705 auto newTask = std::make_unique<QgsVectorLayerExporterTask>( layer, uri, providerKey, destinationCrs, options, true );
706 return newTask.release();
707}
708
710{
711 mOwnedFeedback->cancel();
713}
714
716{
717 if ( !mLayer )
718 return false;
719
720 if ( mOwnsLayer )
721 mLayer->moveToThread( QThread::currentThread() );
722
723 connect( mOwnedFeedback.get(), &QgsFeedback::progressChanged, this, &QgsVectorLayerExporterTask::setProgress );
724
725
727 mLayer.data(), mDestUri, mDestProviderKey, mExportOptions, &mErrorMessage,
728 mOptions, mOwnedFeedback.get() );
729
730 if ( mOwnsLayer )
731 mLayer->moveToThread( nullptr );
732
733 return mError == Qgis::VectorExportResult::Success;
734}
735
737{
738 if ( mOwnsLayer && mLayer )
739 mLayer->moveToThread( QThread::currentThread() );
740
741 // QgsMapLayer has QTimer member, which must not be destroyed from another thread
742 if ( mOwnsLayer )
743 delete mLayer;
744
745 if ( result )
746 emit exportComplete();
747 else
748 emit errorOccurred( mError, mErrorMessage );
749}
@ AddFeatures
Allows adding features.
Definition qgis.h:520
@ CreateSpatialIndex
Allows creation of spatial index.
Definition qgis.h:525
VectorExportResult
Vector layer export result codes.
Definition qgis.h:1071
@ Success
No errors were encountered.
Definition qgis.h:1072
@ ErrorInvalidLayer
Could not access newly created destination layer.
Definition qgis.h:1079
@ ErrorFeatureWriteFailed
An error occurred while writing a feature to the destination.
Definition qgis.h:1078
@ ErrorAttributeCreationFailed
Destination provider was unable to create an attribute.
Definition qgis.h:1076
@ UserCanceled
User canceled the export.
Definition qgis.h:1083
@ ErrorProjectingFeatures
An error occurred while reprojecting features to destination CRS.
Definition qgis.h:1077
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Definition qgis.h:2254
QFlags< VectorDataProviderAttributeEditCapability > VectorDataProviderAttributeEditCapabilities
Attribute editing capabilities which may be supported by vector data providers.
Definition qgis.h:621
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
@ NoGeometry
No geometry.
Definition qgis.h:298
A vector of attributes.
Represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Contains information about the context in which a coordinate transform is executed.
Handles coordinate transforms between two coordinate systems.
void setBallparkTransformsAreAppropriate(bool appropriate)
Sets whether approximate "ballpark" results are appropriate for this coordinate transform.
QgsRectangle transformBoundingBox(const QgsRectangle &rectangle, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool handle180Crossover=false) const
Transforms a rectangle from the source CRS to the destination CRS.
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
Custom exception class for Coordinate Reference System related exceptions.
virtual bool isValid() const =0
Returns true if this is a valid layer.
QString what() const
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Handles parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets the feature IDs that should be fetched.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
QFlags< SinkFlag > SinkFlags
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
QFlags< Flag > Flags
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
Q_INVOKABLE bool setAttribute(int field, const QVariant &attr)
Sets an attribute's value by field index.
QgsAttributes attributes
Definition qgsfeature.h:69
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
QgsFeatureId id
Definition qgsfeature.h:68
int approximateMemoryUsage() const
Returns the approximate RAM usage of the feature, in bytes.
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
void setId(QgsFeatureId id)
Sets the feature id for this feature.
QgsGeometry geometry
Definition qgsfeature.h:71
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:55
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:63
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:76
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
A geometry is the spatial representation of a feature.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool convertToSingleType()
Converts multi type geometry into single type geometry e.g.
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:90
QgsCoordinateTransformContext transformContext() const
Returns the layer data provider coordinate transform context or a default transform context if the la...
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Adds a message to the log instance (and creates it if necessary).
A registry / canonical manager of data providers.
QVariantMap decodeUri(const QString &providerKey, const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
Qgis::VectorExportResult createEmptyLayer(const QString &providerKey, const QString &uri, const QgsFields &fields, Qgis::WkbType wkbType, const QgsCoordinateReferenceSystem &srs, bool overwrite, QMap< int, int > &oldToNewAttrIdxMap, QString &errorMessage, const QMap< QString, QVariant > *options, QString &createdLayerName)
Creates new empty vector layer.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
QgsDataProvider * createProvider(const QString &providerKey, const QString &dataSource, const QgsDataProvider::ProviderOptions &options=QgsDataProvider::ProviderOptions(), Qgis::DataProviderReadFlags flags=Qgis::DataProviderReadFlags())
Creates a new instance of a provider.
A rectangle specified with double values.
QgsCoordinateReferenceSystem crs() const
Returns the associated coordinate reference system, or an invalid CRS if no reference system is set.
A QgsRectangle with associated coordinate reference system.
virtual void cancel()
Notifies the task that it should terminate.
QgsTask(const QString &description=QString(), QgsTask::Flags flags=AllFlags)
Constructor for QgsTask.
@ CanCancel
Task can be canceled.
void setDependentLayers(const QList< QgsMapLayer * > &dependentLayers)
Sets a list of layers on which the task depends.
void setProgress(double progress)
Sets the task's current progress.
Base class for vector data providers.
virtual bool createSpatialIndex()
Creates a spatial index on the datasource (if supported by the provider type).
virtual Q_INVOKABLE Qgis::VectorProviderCapabilities capabilities() const
Returns flags containing the supported capabilities.
void finished(bool result) override
If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether ...
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.
void exportComplete()
Emitted when exporting the layer is successfully completed.
void errorOccurred(Qgis::VectorExportResult error, const QString &errorMessage)
Emitted when an error occurs which prevented the layer being exported (or if the task is canceled).
void cancel() override
Notifies the task that it should terminate.
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.
bool run() override
Performs the task's operation.
Encapsulates options for use with QgsVectorLayerExporter.
QgsReferencedRectangle extent() const
Returns the extent filter for the features to export.
void setExtent(const QgsReferencedRectangle &extent)
Sets an extent filter for the features to export.
void setOutputFields(const QList< QgsVectorLayerExporter::OutputField > &fields)
Sets the output field definitions for the destination table.
void setSelectedOnly(bool selected)
Sets whether the export should only include selected features.
QString filterExpression() const
Returns the filter expression for features to export.
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Sets the destination coordinate reference system to use for exported features.
const QgsExpressionContext & expressionContext() const
Returns the expression context used when a filterExpression() is set.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context used when transforming exported features.
void setFilterExpression(const QString &expression)
Set the filter expression for the features to export.
bool selectedOnly() const
Returns whether the export will only include selected features.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context to use when a filterExpression() is set.
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context to use when transforming exported features.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system used for exported features.
QList< QgsVectorLayerExporter::OutputField > outputFields() const
Returns the output field definitions for the destination table.
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a list of features to the sink.
static Qgis::VectorExportResult 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.
QString errorMessage() const
Returns any error message encountered during the export.
Qgis::VectorDataProviderAttributeEditCapabilities attributeEditCapabilities() const
Returns the attribute capabilities of the exporter.
~QgsVectorLayerExporter() override
Finalizes the export and closes the new created layer.
QString lastError() const override
Returns the most recent error encountered by the sink, e.g.
bool flushBuffer() override
Flushes any internal buffer which may exist in the sink, causing any buffered features to be added to...
Qgis::VectorExportResult errorCode() const
Returns any encountered error code, or false if no error was encountered.
QgsVectorLayerExporter(const QString &uri, const QString &provider, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &crs, bool overwrite=false, const QMap< QString, QVariant > &options=QMap< QString, QVariant >(), QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags())
Constructor for QgsVectorLayerExporter.
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a single feature to the sink.
Represents a vector layer which manages a vector based dataset.
long long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
int selectedFeatureCount() const
Returns the number of features that are selected in this layer.
Q_INVOKABLE Qgis::WkbType wkbType() const final
Returns the WKBType or WKBUnknown in case of error.
Q_INVOKABLE const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const final
Queries the layer for features specified in request.
static Q_INVOKABLE QString displayString(Qgis::WkbType type)
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
static Qgis::WkbType singleType(Qgis::WkbType type)
Returns the single type for a WKB type.
Definition qgswkbtypes.h:52
static Q_INVOKABLE bool isMultiType(Qgis::WkbType type)
Returns true if the WKB type is a multi type.
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
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QList< QgsFeature > QgsFeatureList
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
#define QgsDebugError(str)
Definition qgslogger.h:59
Qgis::VectorExportResult 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)
Setting options for creating vector data providers.
Encapsulates output field definition.