|
42 | 42 | #include <qgssymbol.h> |
43 | 43 | #include <qgssymbollayer.h> |
44 | 44 | #include <qgstextbuffersettings.h> |
| 45 | +#include <qgsvectorfilewriter.h> |
45 | 46 | #include <qgsvectorlayer.h> |
46 | 47 | #include <qgsvectorlayerlabeling.h> |
47 | 48 | #include <qgsvectorlayerutils.h> |
@@ -545,6 +546,16 @@ bool LayerUtils::hasMValue( QgsVectorLayer *layer ) |
545 | 546 | return QgsWkbTypes::hasM( layer->wkbType() ); |
546 | 547 | } |
547 | 548 |
|
| 549 | +QSet<QVariant> LayerUtils::uniqueValuesForVectorLayerFieldIndex( QgsVectorLayer *layer, int fieldIndex ) |
| 550 | +{ |
| 551 | + if ( !layer ) |
| 552 | + { |
| 553 | + return QSet<QVariant>(); |
| 554 | + } |
| 555 | + |
| 556 | + return layer->uniqueValues( fieldIndex ); |
| 557 | +} |
| 558 | + |
548 | 559 | QgsVectorLayer *LayerUtils::loadVectorLayer( const QString &uri, const QString &name, const QString &provider ) |
549 | 560 | { |
550 | 561 | QgsVectorLayer *layer = new QgsVectorLayer( uri, name, provider ); |
@@ -595,3 +606,64 @@ FeatureIterator LayerUtils::createFeatureIteratorFromRectangle( QgsVectorLayer * |
595 | 606 | const QgsFeatureRequest request = QgsFeatureRequest( rectangle ); |
596 | 607 | return FeatureIterator( layer, request ); |
597 | 608 | } |
| 609 | + |
| 610 | +QString LayerUtils::saveVectorLayerAs( QgsVectorLayer *layer, const QString &filePath, const QString &driverName, const QString &filterExpression ) |
| 611 | +{ |
| 612 | + if ( !layer || filePath.isEmpty() ) |
| 613 | + { |
| 614 | + return QString(); |
| 615 | + } |
| 616 | + |
| 617 | + QFileInfo fileInfo( filePath ); |
| 618 | + const QString finalDriverName = driverName.isEmpty() ? QgsVectorFileWriter::driverForExtension( fileInfo.suffix() ) : driverName; |
| 619 | + if ( finalDriverName.isEmpty() ) |
| 620 | + { |
| 621 | + return QString(); |
| 622 | + } |
| 623 | + QDir dir; |
| 624 | + if ( !dir.mkpath( fileInfo.absolutePath() ) ) |
| 625 | + { |
| 626 | + return QString(); |
| 627 | + } |
| 628 | + |
| 629 | + QStringList datasetOptions = QgsVectorFileWriter::defaultDatasetOptions( finalDriverName ); |
| 630 | + if ( finalDriverName == QStringLiteral( "GPX" ) ) |
| 631 | + { |
| 632 | + datasetOptions.removeAll( QStringLiteral( "GPX_USE_EXTENSIONS=NO" ) ); |
| 633 | + datasetOptions << QStringLiteral( "GPX_USE_EXTENSIONS=YES" ); |
| 634 | + } |
| 635 | + |
| 636 | + QString finalFileName; |
| 637 | + QString finalLayerName; |
| 638 | + QgsVectorFileWriter::SaveVectorOptions saveOptions; |
| 639 | + saveOptions.fileEncoding = QStringLiteral( "UTF8" ); |
| 640 | + saveOptions.layerName = fileInfo.completeBaseName(); |
| 641 | + saveOptions.driverName = finalDriverName; |
| 642 | + saveOptions.datasourceOptions = datasetOptions; |
| 643 | + saveOptions.layerOptions = QgsVectorFileWriter::defaultLayerOptions( finalDriverName ); |
| 644 | + saveOptions.symbologyExport = Qgis::FeatureSymbologyExport::NoSymbology; |
| 645 | + saveOptions.actionOnExistingFile = QgsVectorFileWriter::CreateOrOverwriteFile; |
| 646 | + |
| 647 | + std::unique_ptr<QgsVectorFileWriter> writer( QgsVectorFileWriter::create( filePath, layer->fields(), layer->wkbType(), layer->crs(), QgsProject::instance()->transformContext(), saveOptions, QgsFeatureSink::RegeneratePrimaryKey, &finalFileName, &finalLayerName ) ); |
| 648 | + if ( writer->hasError() ) |
| 649 | + { |
| 650 | + qInfo() << QStringLiteral( "Vector layer file writer error: %1" ).arg( writer->errorMessage() ); |
| 651 | + return QString(); |
| 652 | + } |
| 653 | + |
| 654 | + QgsFeatureRequest request; |
| 655 | + if ( !filterExpression.isEmpty() ) |
| 656 | + { |
| 657 | + request.setFilterExpression( filterExpression ); |
| 658 | + request.setExpressionContext( layer->createExpressionContext() ); |
| 659 | + } |
| 660 | + |
| 661 | + QgsFeatureIterator it = layer->getFeatures( request ); |
| 662 | + QgsFeature feature; |
| 663 | + while ( it.nextFeature( feature ) ) |
| 664 | + { |
| 665 | + writer->addFeature( feature, QgsFeatureSink::FastInsert ); |
| 666 | + } |
| 667 | + |
| 668 | + return finalFileName; |
| 669 | +} |
0 commit comments