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 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 );