20#include <QRegularExpression>
24QString QgsSaveFeaturesAlgorithm::name()
const
26 return QStringLiteral(
"savefeatures" );
29QString QgsSaveFeaturesAlgorithm::displayName()
const
31 return QObject::tr(
"Save vector features to file" );
34QStringList QgsSaveFeaturesAlgorithm::tags()
const
36 return QObject::tr(
"save,write,export" ).split(
',' );
39QString QgsSaveFeaturesAlgorithm::group()
const
41 return QObject::tr(
"Vector general" );
44QString QgsSaveFeaturesAlgorithm::groupId()
const
46 return QStringLiteral(
"vectorgeneral" );
49QString QgsSaveFeaturesAlgorithm::shortHelpString()
const
51 return QObject::tr(
"This algorithm saves vector features to a specified file dataset.\n\n"
52 "For dataset formats supporting layers, an optional layer name parameter can be used to specify a custom string.\n\n"
53 "Optional GDAL-defined dataset and layer options can be specified. For more information on this, "
54 "read the online GDAL documentation." );
57QgsSaveFeaturesAlgorithm *QgsSaveFeaturesAlgorithm::createInstance()
const
59 return new QgsSaveFeaturesAlgorithm();
62void QgsSaveFeaturesAlgorithm::initAlgorithm(
const QVariantMap & )
67 std::unique_ptr< QgsProcessingParameterString > param = std::make_unique< QgsProcessingParameterString >( QStringLiteral(
"LAYER_NAME" ), QObject::tr(
"Layer name" ), QVariant(),
false,
true );
69 addParameter( param.release() );
70 param = std::make_unique< QgsProcessingParameterString >( QStringLiteral(
"DATASOURCE_OPTIONS" ), QObject::tr(
"GDAL dataset options (separate individual options with semicolons)" ), QVariant(),
false,
true );
72 addParameter( param.release() );
73 param = std::make_unique< QgsProcessingParameterString >( QStringLiteral(
"LAYER_OPTIONS" ), QObject::tr(
"GDAL layer options (separate individual options with semicolons)" ), QVariant(),
false,
true );
75 addParameter( param.release() );
83 std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
85 QString layerName = parameterAsString( parameters, QStringLiteral(
"LAYER_NAME" ), context ).trimmed();
86 QVariantMap createOptions;
87 if ( !layerName.isEmpty() )
89 createOptions[QStringLiteral(
"layerName" )] = layerName;
92#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
93 QStringList datasourceOptions = parameterAsString( parameters, QStringLiteral(
"DATASOURCE_OPTIONS" ), context ).trimmed().split(
';', QString::SkipEmptyParts );
94 QStringList layerOptions = parameterAsString( parameters, QStringLiteral(
"LAYER_OPTIONS" ), context ).trimmed().split(
';', QString::SkipEmptyParts );
96 const QStringList datasourceOptions = parameterAsString( parameters, QStringLiteral(
"DATASOURCE_OPTIONS" ), context ).trimmed().split(
';', Qt::SkipEmptyParts );
97 const QStringList layerOptions = parameterAsString( parameters, QStringLiteral(
"LAYER_OPTIONS" ), context ).trimmed().split(
';', Qt::SkipEmptyParts );
101 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, source->fields(),
102 source->wkbType(), source->sourceCrs(), QgsFeatureSink::SinkFlags(), createOptions, datasourceOptions, layerOptions ) );
106 const double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 1;
125 QString filePath = dest;
127 const int separatorIndex = dest.indexOf(
'|' );
128 if ( separatorIndex > -1 )
130 const QRegularExpression layerNameRx( QStringLiteral(
"\\|layername=([^\\|]*)" ) );
131 const QRegularExpressionMatch match = layerNameRx.match( dest );
132 if ( match.hasMatch() )
134 layerName = match.captured( 1 );
136 filePath = dest.mid( 0, separatorIndex );
140 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
141 outputs.insert( QStringLiteral(
"FILE_PATH" ), filePath );
142 outputs.insert( QStringLiteral(
"LAYER_NAME" ), layerName );
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
This class wraps a request for features to a vector layer (or directly its vector data provider).
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Base class for providing feedback from a processing algorithm.
A string output for processing algorithms.
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
An input feature source (such as vector layers) parameter for processing algorithms.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
static QString fileFilterString(VectorFormatOptions options=SortRecommended)
Returns filter string that can be used for dialogs.