28QString QgsExportToSpatialiteAlgorithm::name()
const
30 return QStringLiteral(
"importintospatialite" );
33QString QgsExportToSpatialiteAlgorithm::displayName()
const
35 return QObject::tr(
"Export to SpatiaLite" );
38QStringList QgsExportToSpatialiteAlgorithm::tags()
const
40 return QObject::tr(
"export,import,spatialite,table,layer,into,copy" ).split(
',' );
43QString QgsExportToSpatialiteAlgorithm::group()
const
45 return QObject::tr(
"Database" );
48QString QgsExportToSpatialiteAlgorithm::groupId()
const
50 return QStringLiteral(
"database" );
53QString QgsExportToSpatialiteAlgorithm::shortHelpString()
const
55 return QObject::tr(
"This algorithm exports a vector layer to a SpatiaLite database, creating a new table." );
58QString QgsExportToSpatialiteAlgorithm::shortDescription()
const
60 return QObject::tr(
"Exports a vector layer to a SpatiaLite database, creating a new table." );
63QgsExportToSpatialiteAlgorithm *QgsExportToSpatialiteAlgorithm::createInstance()
const
65 return new QgsExportToSpatialiteAlgorithm();
68void QgsExportToSpatialiteAlgorithm::initAlgorithm(
const QVariantMap & )
72 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"TABLENAME" ), QObject::tr(
"Table to export to (leave blank to use layer name)" ), QVariant(),
false,
true ) );
74 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"GEOMETRY_COLUMN" ), QObject::tr(
"Geometry column" ), QStringLiteral(
"geom" ) ) );
75 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"ENCODING" ), QObject::tr(
"Encoding" ), QStringLiteral(
"UTF-8" ),
false,
true ) );
78 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"LOWERCASE_NAMES" ), QObject::tr(
"Convert field names to lowercase" ),
true ) );
79 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"DROP_STRING_LENGTH" ), QObject::tr(
"Drop length constraints on character fields" ),
false ) );
80 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"FORCE_SINGLEPART" ), QObject::tr(
"Create single-part geometries instead of multipart" ),
false ) );
85 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral(
"DATABASE" ), context );
93 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
98 if ( uri.database().isEmpty() )
101 const QVariantMap parts = md->
decodeUri( mDatabaseUri );
102 mDatabaseUri = parts.value( QStringLiteral(
"path" ) ).toString();
103 uri =
QgsDataSourceUri( QStringLiteral(
"dbname='%1'" ).arg( mDatabaseUri ) );
106 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn;
117 const QString primaryKeyField = parameterAsString( parameters, QStringLiteral(
"PRIMARY_KEY" ), context );
118 const QString encoding = parameterAsString( parameters, QStringLiteral(
"ENCODING" ), context );
119 const bool overwrite = parameterAsBoolean( parameters, QStringLiteral(
"OVERWRITE" ), context );
121 QString tableName = parameterAsDatabaseTableName( parameters, QStringLiteral(
"TABLENAME" ), context ).trimmed();
122 if ( tableName.isEmpty() )
124 tableName = source->sourceName();
125 tableName = tableName.replace(
'.',
'_' );
127 tableName = tableName.replace(
' ', QString() ).right( 63 );
129 QString geometryColumn = parameterAsString( parameters, QStringLiteral(
"GEOMETRY_COLUMN" ), context );
130 if ( geometryColumn.isEmpty() )
132 geometryColumn = QStringLiteral(
"geom" );
136 geometryColumn.clear();
139 const bool createSpatialIndex = parameterAsBoolean( parameters, QStringLiteral(
"CREATEINDEX" ), context );
141 QMap<QString, QVariant> options;
144 options[QStringLiteral(
"overwrite" )] =
true;
146 if ( parameterAsBoolean( parameters, QStringLiteral(
"LOWERCASE_NAMES" ), context ) )
148 options[QStringLiteral(
"lowercaseFieldNames" )] =
true;
149 geometryColumn = geometryColumn.toLower();
151 if ( parameterAsBoolean( parameters, QStringLiteral(
"DROP_STRING_LENGTH" ), context ) )
153 options[QStringLiteral(
"dropStringConstraints" )] =
true;
155 if ( parameterAsBoolean( parameters, QStringLiteral(
"FORCE_SINGLEPART" ), context ) )
157 options[QStringLiteral(
"forceSinglePartGeometryType" )] =
true;
159 if ( !encoding.isEmpty() )
161 options[QStringLiteral(
"fileEncoding" )] = encoding;
165 uri.setTable( tableName );
166 uri.setKeyColumn( primaryKeyField );
167 uri.setGeometryColumn( geometryColumn );
169 auto exporter = std::make_unique<QgsVectorLayerExporter>( uri.uri(), QStringLiteral(
"spatialite" ), source->fields(), source->wkbType(), source->sourceCrs(), overwrite, options );
172 throw QgsProcessingException( QObject::tr(
"Error exporting to SpatiaLite\n%1" ).arg( exporter->errorMessage() ) );
176 const double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 0.0;
195 exporter->flushBuffer();
198 throw QgsProcessingException( QObject::tr(
"Error exporting to SpatiaLite\n%1" ).arg( exporter->errorMessage() ) );
202 if ( !geometryColumn.isEmpty() && createSpatialIndex )
208 conn->createSpatialIndex(
"", tableName, opt );
218 conn->vacuum(
"", tableName );
222 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.