27using namespace Qt::StringLiterals;
31QString QgsExportToRegisteredSpatialiteAlgorithm::name()
const
33 return u
"importintospatialiteregistered"_s;
36QString QgsExportToRegisteredSpatialiteAlgorithm::displayName()
const
38 return QObject::tr(
"Export to SpatiaLite (registered)" );
41QStringList QgsExportToRegisteredSpatialiteAlgorithm::tags()
const
43 return QObject::tr(
"export,import,spatialite,table,layer,into,copy" ).split(
',' );
46QString QgsExportToRegisteredSpatialiteAlgorithm::group()
const
48 return QObject::tr(
"Database" );
51QString QgsExportToRegisteredSpatialiteAlgorithm::groupId()
const
56QString QgsExportToRegisteredSpatialiteAlgorithm::shortHelpString()
const
58 return QObject::tr(
"Exports a vector layer to a SpatiaLite database, creating "
60 "Prior to this a connection between QGIS and the SpatiaLite "
61 "database has to be created (for example through the QGIS "
65QString QgsExportToRegisteredSpatialiteAlgorithm::shortDescription()
const
67 return QObject::tr(
"Exports a vector layer to a registered SpatiaLite database, creating a new table." );
75QgsExportToRegisteredSpatialiteAlgorithm *QgsExportToRegisteredSpatialiteAlgorithm::createInstance()
const
77 return new QgsExportToRegisteredSpatialiteAlgorithm();
80void QgsExportToRegisteredSpatialiteAlgorithm::initAlgorithm(
const QVariantMap & )
84 addParameter(
new QgsProcessingParameterDatabaseTable( u
"TABLENAME"_s, QObject::tr(
"Table to export to (leave blank to use layer name)" ), u
"DATABASE"_s, QString(), QVariant(),
true,
true ) );
91 addParameter(
new QgsProcessingParameterBoolean( u
"DROP_STRING_LENGTH"_s, QObject::tr(
"Drop length constraints on character fields" ),
false ) );
92 addParameter(
new QgsProcessingParameterBoolean( u
"FORCE_SINGLEPART"_s, QObject::tr(
"Create single-part geometries instead of multipart" ),
false ) );
97 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
101 const QString connectionName = parameterAsConnectionName( parameters, u
"DATABASE"_s, context );
103 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn;
111 QgsProcessingException( QObject::tr(
"Could not retrieve connection details for %1" ).arg( connectionName ) );
114 const QString primaryKeyField = parameterAsString( parameters, u
"PRIMARY_KEY"_s, context );
115 const QString encoding = parameterAsString( parameters, u
"ENCODING"_s, context );
116 const bool overwrite = parameterAsBoolean( parameters, u
"OVERWRITE"_s, context );
118 QString tableName = parameterAsDatabaseTableName( parameters, u
"TABLENAME"_s, context ).trimmed();
119 if ( tableName.isEmpty() )
121 tableName = source->sourceName();
122 tableName = tableName.replace(
'.',
'_' );
124 tableName = tableName.replace(
' ', QString() ).right( 63 );
126 QString geometryColumn = parameterAsString( parameters, u
"GEOMETRY_COLUMN"_s, context );
127 if ( geometryColumn.isEmpty() )
129 geometryColumn = u
"geom"_s;
133 geometryColumn.clear();
136 const bool createSpatialIndex = parameterAsBoolean( parameters, u
"CREATEINDEX"_s, context );
138 QMap<QString, QVariant> options;
141 options[u
"overwrite"_s] =
true;
143 if ( parameterAsBoolean( parameters, u
"LOWERCASE_NAMES"_s, context ) )
145 options[u
"lowercaseFieldNames"_s] =
true;
146 geometryColumn = geometryColumn.toLower();
148 if ( parameterAsBoolean( parameters, u
"DROP_STRING_LENGTH"_s, context ) )
150 options[u
"dropStringConstraints"_s] =
true;
152 if ( parameterAsBoolean( parameters, u
"FORCE_SINGLEPART"_s, context ) )
154 options[u
"forceSinglePartGeometryType"_s] =
true;
156 if ( !encoding.isEmpty() )
158 options[u
"fileEncoding"_s] = encoding;
166 auto exporter = std::make_unique<QgsVectorLayerExporter>( uri.
uri(), u
"spatialite"_s, source->fields(), source->wkbType(), source->sourceCrs(), overwrite, options );
169 throw QgsProcessingException( QObject::tr(
"Error exporting to SpatiaLite\n%1" ).arg( exporter->errorMessage() ) );
173 const double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 0.0;
192 exporter->flushBuffer();
195 throw QgsProcessingException( QObject::tr(
"Error exporting to SpatiaLite\n%1" ).arg( exporter->errorMessage() ) );
199 if ( !geometryColumn.isEmpty() && createSpatialIndex )
205 conn->createSpatialIndex(
"", tableName, opt );
215 conn->vacuum(
"", tableName );
219 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.