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 QGS_MARK_ALGORITHM_SOURCE
101 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
105 const QString connectionName = parameterAsConnectionName( parameters, u
"DATABASE"_s, context );
107 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn;
115 QgsProcessingException( QObject::tr(
"Could not retrieve connection details for %1" ).arg( connectionName ) );
118 const QString primaryKeyField = parameterAsString( parameters, u
"PRIMARY_KEY"_s, context );
119 const QString encoding = parameterAsString( parameters, u
"ENCODING"_s, context );
120 const bool overwrite = parameterAsBoolean( parameters, u
"OVERWRITE"_s, context );
122 QString tableName = parameterAsDatabaseTableName( parameters, u
"TABLENAME"_s, context ).trimmed();
123 if ( tableName.isEmpty() )
125 tableName = source->sourceName();
126 tableName = tableName.replace(
'.',
'_' );
128 tableName = tableName.replace(
' ', QString() ).right( 63 );
130 QString geometryColumn = parameterAsString( parameters, u
"GEOMETRY_COLUMN"_s, context );
131 if ( geometryColumn.isEmpty() )
133 geometryColumn = u
"geom"_s;
137 geometryColumn.clear();
140 const bool createSpatialIndex = parameterAsBoolean( parameters, u
"CREATEINDEX"_s, context );
142 QMap<QString, QVariant> options;
145 options[u
"overwrite"_s] =
true;
147 if ( parameterAsBoolean( parameters, u
"LOWERCASE_NAMES"_s, context ) )
149 options[u
"lowercaseFieldNames"_s] =
true;
150 geometryColumn = geometryColumn.toLower();
152 if ( parameterAsBoolean( parameters, u
"DROP_STRING_LENGTH"_s, context ) )
154 options[u
"dropStringConstraints"_s] =
true;
156 if ( parameterAsBoolean( parameters, u
"FORCE_SINGLEPART"_s, context ) )
158 options[u
"forceSinglePartGeometryType"_s] =
true;
160 if ( !encoding.isEmpty() )
162 options[u
"fileEncoding"_s] = encoding;
170 auto exporter = std::make_unique<QgsVectorLayerExporter>( uri.
uri(), u
"spatialite"_s, source->fields(), source->wkbType(), source->sourceCrs(), overwrite, options );
173 throw QgsProcessingException( QObject::tr(
"Error exporting to SpatiaLite\n%1" ).arg( exporter->errorMessage() ) );
177 const double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 0.0;
196 exporter->flushBuffer();
199 throw QgsProcessingException( QObject::tr(
"Error exporting to SpatiaLite\n%1" ).arg( exporter->errorMessage() ) );
203 if ( !geometryColumn.isEmpty() && createSpatialIndex )
209 conn->createSpatialIndex(
"", tableName, opt );
219 conn->vacuum(
"", tableName );
223 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.