27QString QgsExportToRegisteredSpatialiteAlgorithm::name()
const
29 return QStringLiteral(
"importintospatialiteregistered" );
32QString QgsExportToRegisteredSpatialiteAlgorithm::displayName()
const
34 return QObject::tr(
"Export to SpatiaLite (registered)" );
37QStringList QgsExportToRegisteredSpatialiteAlgorithm::tags()
const
39 return QObject::tr(
"export,import,spatialite,table,layer,into,copy" ).split(
',' );
42QString QgsExportToRegisteredSpatialiteAlgorithm::group()
const
44 return QObject::tr(
"Database" );
47QString QgsExportToRegisteredSpatialiteAlgorithm::groupId()
const
49 return QStringLiteral(
"database" );
52QString QgsExportToRegisteredSpatialiteAlgorithm::shortHelpString()
const
54 return QObject::tr(
"Exports a vector layer to a SpatiaLite database, creating "
56 "Prior to this a connection between QGIS and the SpatiaLite "
57 "database has to be created (for example through the QGIS "
61QString QgsExportToRegisteredSpatialiteAlgorithm::shortDescription()
const
63 return QObject::tr(
"Exports a vector layer to a registered SpatiaLite database, creating a new table." );
71QgsExportToRegisteredSpatialiteAlgorithm *QgsExportToRegisteredSpatialiteAlgorithm::createInstance()
const
73 return new QgsExportToRegisteredSpatialiteAlgorithm();
76void QgsExportToRegisteredSpatialiteAlgorithm::initAlgorithm(
const QVariantMap & )
80 addParameter(
new QgsProcessingParameterDatabaseTable( QStringLiteral(
"TABLENAME" ), QObject::tr(
"Table to export to (leave blank to use layer name)" ), QStringLiteral(
"DATABASE" ), QString(), QVariant(),
true,
true ) );
82 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"GEOMETRY_COLUMN" ), QObject::tr(
"Geometry column" ), QStringLiteral(
"geom" ) ) );
83 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"ENCODING" ), QObject::tr(
"Encoding" ), QStringLiteral(
"UTF-8" ),
false,
true ) );
86 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"LOWERCASE_NAMES" ), QObject::tr(
"Convert field names to lowercase" ),
true ) );
87 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"DROP_STRING_LENGTH" ), QObject::tr(
"Drop length constraints on character fields" ),
false ) );
88 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"FORCE_SINGLEPART" ), QObject::tr(
"Create single-part geometries instead of multipart" ),
false ) );
93 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
97 const QString connectionName = parameterAsConnectionName( parameters, QStringLiteral(
"DATABASE" ), context );
99 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn;
107 QgsProcessingException( QObject::tr(
"Could not retrieve connection details for %1" ).arg( connectionName ) );
110 const QString primaryKeyField = parameterAsString( parameters, QStringLiteral(
"PRIMARY_KEY" ), context );
111 const QString encoding = parameterAsString( parameters, QStringLiteral(
"ENCODING" ), context );
112 const bool overwrite = parameterAsBoolean( parameters, QStringLiteral(
"OVERWRITE" ), context );
114 QString tableName = parameterAsDatabaseTableName( parameters, QStringLiteral(
"TABLENAME" ), context ).trimmed();
115 if ( tableName.isEmpty() )
117 tableName = source->sourceName();
118 tableName = tableName.replace(
'.',
'_' );
120 tableName = tableName.replace(
' ', QString() ).right( 63 );
122 QString geometryColumn = parameterAsString( parameters, QStringLiteral(
"GEOMETRY_COLUMN" ), context );
123 if ( geometryColumn.isEmpty() )
125 geometryColumn = QStringLiteral(
"geom" );
129 geometryColumn.clear();
132 const bool createSpatialIndex = parameterAsBoolean( parameters, QStringLiteral(
"CREATEINDEX" ), context );
134 QMap<QString, QVariant> options;
137 options[QStringLiteral(
"overwrite" )] =
true;
139 if ( parameterAsBoolean( parameters, QStringLiteral(
"LOWERCASE_NAMES" ), context ) )
141 options[QStringLiteral(
"lowercaseFieldNames" )] =
true;
142 geometryColumn = geometryColumn.toLower();
144 if ( parameterAsBoolean( parameters, QStringLiteral(
"DROP_STRING_LENGTH" ), context ) )
146 options[QStringLiteral(
"dropStringConstraints" )] =
true;
148 if ( parameterAsBoolean( parameters, QStringLiteral(
"FORCE_SINGLEPART" ), context ) )
150 options[QStringLiteral(
"forceSinglePartGeometryType" )] =
true;
152 if ( !encoding.isEmpty() )
154 options[QStringLiteral(
"fileEncoding" )] = encoding;
162 auto exporter = std::make_unique<QgsVectorLayerExporter>( uri.
uri(), QStringLiteral(
"spatialite" ), source->fields(), source->wkbType(), source->sourceCrs(), overwrite, options );
165 throw QgsProcessingException( QObject::tr(
"Error exporting to SpatiaLite\n%1" ).arg( exporter->errorMessage() ) );
169 const double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 0.0;
188 exporter->flushBuffer();
191 throw QgsProcessingException( QObject::tr(
"Error exporting to SpatiaLite\n%1" ).arg( exporter->errorMessage() ) );
195 if ( !geometryColumn.isEmpty() && createSpatialIndex )
201 conn->createSpatialIndex(
"", tableName, opt );
211 conn->vacuum(
"", tableName );
215 feedback->
reportError( QObject::tr(
"Error vacuuming table:\n%1" ).arg( e.
what() ) );
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ Success
No errors were encountered.
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
@ NoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
Provides common functionality for database based connections.
Stores the component parts of a data source URI (e.g.
void setTable(const QString &table)
Sets table to table.
void setGeometryColumn(const QString &geometryColumn)
Sets geometry column name to geometryColumn.
QString uri(bool expandAuthConfig=true) const
Returns the complete URI as a string.
void setKeyColumn(const QString &column)
Sets the name of the (primary) key column.
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.
@ 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.
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A boolean parameter for processing algorithms.
A database table name parameter for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
A data provider connection parameter for processing algorithms, allowing users to select from availab...
A string parameter for processing algorithms.
Custom exception class for provider connection related exceptions.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.
The SpatialIndexOptions contains extra options relating to spatial index creation.
QString geometryColumnName
Specifies the name of the geometry column to create the index for.