28using namespace Qt::StringLiterals;
32QString QgsExportToSpatialiteAlgorithm::name()
const
34 return u
"importintospatialite"_s;
37QString QgsExportToSpatialiteAlgorithm::displayName()
const
39 return QObject::tr(
"Export to SpatiaLite" );
42QStringList QgsExportToSpatialiteAlgorithm::tags()
const
44 return QObject::tr(
"export,import,spatialite,table,layer,into,copy" ).split(
',' );
47QString QgsExportToSpatialiteAlgorithm::group()
const
49 return QObject::tr(
"Database" );
52QString QgsExportToSpatialiteAlgorithm::groupId()
const
57QString QgsExportToSpatialiteAlgorithm::shortHelpString()
const
59 return QObject::tr(
"This algorithm exports a vector layer to a SpatiaLite database, creating a new table." );
62QString QgsExportToSpatialiteAlgorithm::shortDescription()
const
64 return QObject::tr(
"Exports a vector layer to a SpatiaLite database, creating a new table." );
67QgsExportToSpatialiteAlgorithm *QgsExportToSpatialiteAlgorithm::createInstance()
const
69 return new QgsExportToSpatialiteAlgorithm();
72void QgsExportToSpatialiteAlgorithm::initAlgorithm(
const QVariantMap & )
76 addParameter(
new QgsProcessingParameterString( u
"TABLENAME"_s, QObject::tr(
"Table to export to (leave blank to use layer name)" ), QVariant(),
false,
true ) );
83 addParameter(
new QgsProcessingParameterBoolean( u
"DROP_STRING_LENGTH"_s, QObject::tr(
"Drop length constraints on character fields" ),
false ) );
84 addParameter(
new QgsProcessingParameterBoolean( u
"FORCE_SINGLEPART"_s, QObject::tr(
"Create single-part geometries instead of multipart" ),
false ) );
89 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, u
"DATABASE"_s, context );
97 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
102 if ( uri.database().isEmpty() )
105 const QVariantMap parts = md->
decodeUri( mDatabaseUri );
106 mDatabaseUri = parts.value( u
"path"_s ).toString();
110 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn;
121 const QString primaryKeyField = parameterAsString( parameters, u
"PRIMARY_KEY"_s, context );
122 const QString encoding = parameterAsString( parameters, u
"ENCODING"_s, context );
123 const bool overwrite = parameterAsBoolean( parameters, u
"OVERWRITE"_s, context );
125 QString tableName = parameterAsDatabaseTableName( parameters, u
"TABLENAME"_s, context ).trimmed();
126 if ( tableName.isEmpty() )
128 tableName = source->sourceName();
129 tableName = tableName.replace(
'.',
'_' );
131 tableName = tableName.replace(
' ', QString() ).right( 63 );
133 QString geometryColumn = parameterAsString( parameters, u
"GEOMETRY_COLUMN"_s, context );
134 if ( geometryColumn.isEmpty() )
136 geometryColumn = u
"geom"_s;
140 geometryColumn.clear();
143 const bool createSpatialIndex = parameterAsBoolean( parameters, u
"CREATEINDEX"_s, context );
145 QMap<QString, QVariant> options;
148 options[u
"overwrite"_s] =
true;
150 if ( parameterAsBoolean( parameters, u
"LOWERCASE_NAMES"_s, context ) )
152 options[u
"lowercaseFieldNames"_s] =
true;
153 geometryColumn = geometryColumn.toLower();
155 if ( parameterAsBoolean( parameters, u
"DROP_STRING_LENGTH"_s, context ) )
157 options[u
"dropStringConstraints"_s] =
true;
159 if ( parameterAsBoolean( parameters, u
"FORCE_SINGLEPART"_s, context ) )
161 options[u
"forceSinglePartGeometryType"_s] =
true;
163 if ( !encoding.isEmpty() )
165 options[u
"fileEncoding"_s] = encoding;
169 uri.setTable( tableName );
170 uri.setKeyColumn( primaryKeyField );
171 uri.setGeometryColumn( geometryColumn );
173 auto exporter = std::make_unique<QgsVectorLayerExporter>( uri.uri(), u
"spatialite"_s, source->fields(), source->wkbType(), source->sourceCrs(), overwrite, options );
176 throw QgsProcessingException( QObject::tr(
"Error exporting to SpatiaLite\n%1" ).arg( exporter->errorMessage() ) );
180 const double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 0.0;
199 exporter->flushBuffer();
202 throw QgsProcessingException( QObject::tr(
"Error exporting to SpatiaLite\n%1" ).arg( exporter->errorMessage() ) );
206 if ( !geometryColumn.isEmpty() && createSpatialIndex )
212 conn->createSpatialIndex(
"", tableName, opt );
222 conn->vacuum(
"", tableName );
226 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.
Provides common functionality for database based connections.
virtual QString dataSourceUri(bool expandAuthConfig=false) const
Gets the data source specification.
Stores the component parts of a data source URI (e.g.
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.
QString providerType() const
Returns the provider type (provider key) for this layer.
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.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
A string parameter for processing algorithms.
A vector layer (with or without geometry) 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.
Represents a vector layer which manages a vector based dataset.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.
The SpatialIndexOptions contains extra options relating to spatial index creation.
QString geometryColumnName
Specifies the name of the geometry column to create the index for.