20 #include <QRegularExpression>
24 QString QgsSaveFeaturesAlgorithm::name()
const
26 return QStringLiteral(
"savefeatures" );
29 QString QgsSaveFeaturesAlgorithm::displayName()
const
31 return QObject::tr(
"Save vector features to file" );
34 QStringList QgsSaveFeaturesAlgorithm::tags()
const
36 return QObject::tr(
"save,write,export" ).split(
',' );
39 QString QgsSaveFeaturesAlgorithm::group()
const
41 return QObject::tr(
"Vector general" );
44 QString QgsSaveFeaturesAlgorithm::groupId()
const
46 return QStringLiteral(
"vectorgeneral" );
49 QString 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." );
57 QgsSaveFeaturesAlgorithm *QgsSaveFeaturesAlgorithm::createInstance()
const
59 return new QgsSaveFeaturesAlgorithm();
62 void 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 QStringList datasourceOptions = parameterAsString( parameters, QStringLiteral(
"DATASOURCE_OPTIONS" ), context ).trimmed().split(
';', Qt::SkipEmptyParts );
97 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 double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 1;
124 QString filePath = dest;
126 int separatorIndex = dest.indexOf(
'|' );
127 if ( separatorIndex > -1 )
129 QRegularExpression layerNameRx( QStringLiteral(
"\\|layername=([^\\|]*)" ) );
130 QRegularExpressionMatch match = layerNameRx.match( dest );
131 if ( match.hasMatch() )
133 layerName = match.captured( 1 );
135 filePath = dest.mid( 0, separatorIndex );
139 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
140 outputs.insert( QStringLiteral(
"FILE_PATH" ), filePath );
141 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 SIP_HOLDGIL
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...
static QString fileFilterString(VectorFormatOptions options=SortRecommended)
Returns filter string that can be used for dialogs.