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
59 "Exports a vector layer to a SpatiaLite database, creating "
61 "Prior to this a connection between QGIS and the SpatiaLite "
62 "database has to be created (for example through the QGIS "
67QString QgsExportToRegisteredSpatialiteAlgorithm::shortDescription()
const
69 return QObject::tr(
"Exports a vector layer to a registered SpatiaLite database, creating a new table." );
77QgsExportToRegisteredSpatialiteAlgorithm *QgsExportToRegisteredSpatialiteAlgorithm::createInstance()
const
79 return new QgsExportToRegisteredSpatialiteAlgorithm();
82void QgsExportToRegisteredSpatialiteAlgorithm::initAlgorithm(
const QVariantMap & )
86 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 ) );
93 addParameter(
new QgsProcessingParameterBoolean( u
"DROP_STRING_LENGTH"_s, QObject::tr(
"Drop length constraints on character fields" ),
false ) );
94 addParameter(
new QgsProcessingParameterBoolean( u
"FORCE_SINGLEPART"_s, QObject::tr(
"Create single-part geometries instead of multipart" ),
false ) );
99 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
103 const QString connectionName = parameterAsConnectionName( parameters, u
"DATABASE"_s, context );
105 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn;
113 QgsProcessingException( QObject::tr(
"Could not retrieve connection details for %1" ).arg( connectionName ) );
116 const QString primaryKeyField = parameterAsString( parameters, u
"PRIMARY_KEY"_s, context );
117 const QString encoding = parameterAsString( parameters, u
"ENCODING"_s, context );
118 const bool overwrite = parameterAsBoolean( parameters, u
"OVERWRITE"_s, context );
120 QString tableName = parameterAsDatabaseTableName( parameters, u
"TABLENAME"_s, context ).trimmed();
121 if ( tableName.isEmpty() )
123 tableName = source->sourceName();
124 tableName = tableName.replace(
'.',
'_' );
126 tableName = tableName.replace(
' ', QString() ).right( 63 );
128 QString geometryColumn = parameterAsString( parameters, u
"GEOMETRY_COLUMN"_s, context );
129 if ( geometryColumn.isEmpty() )
131 geometryColumn = u
"geom"_s;
135 geometryColumn.clear();
138 const bool createSpatialIndex = parameterAsBoolean( parameters, u
"CREATEINDEX"_s, context );
140 QMap<QString, QVariant> options;
143 options[u
"overwrite"_s] =
true;
145 if ( parameterAsBoolean( parameters, u
"LOWERCASE_NAMES"_s, context ) )
147 options[u
"lowercaseFieldNames"_s] =
true;
148 geometryColumn = geometryColumn.toLower();
150 if ( parameterAsBoolean( parameters, u
"DROP_STRING_LENGTH"_s, context ) )
152 options[u
"dropStringConstraints"_s] =
true;
154 if ( parameterAsBoolean( parameters, u
"FORCE_SINGLEPART"_s, context ) )
156 options[u
"forceSinglePartGeometryType"_s] =
true;
158 if ( !encoding.isEmpty() )
160 options[u
"fileEncoding"_s] = encoding;
168 auto exporter = std::make_unique<QgsVectorLayerExporter>( uri.
uri(), u
"spatialite"_s, source->fields(), source->wkbType(), source->sourceCrs(), overwrite, options );
171 throw QgsProcessingException( QObject::tr(
"Error exporting to SpatiaLite\n%1" ).arg( exporter->errorMessage() ) );
175 const double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 0.0;
194 exporter->flushBuffer();
197 throw QgsProcessingException( QObject::tr(
"Error exporting to SpatiaLite\n%1" ).arg( exporter->errorMessage() ) );
201 if ( !geometryColumn.isEmpty() && createSpatialIndex )
207 conn->createSpatialIndex(
"", tableName, opt );
217 conn->vacuum(
"", tableName );
221 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.