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 QGS_MARK_ALGORITHM_SOURCE
99 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
104 if ( uri.database().isEmpty() )
107 const QVariantMap parts = md->
decodeUri( mDatabaseUri );
108 mDatabaseUri = parts.value( u
"path"_s ).toString();
112 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn;
123 const QString primaryKeyField = parameterAsString( parameters, u
"PRIMARY_KEY"_s, context );
124 const QString encoding = parameterAsString( parameters, u
"ENCODING"_s, context );
125 const bool overwrite = parameterAsBoolean( parameters, u
"OVERWRITE"_s, context );
127 QString tableName = parameterAsDatabaseTableName( parameters, u
"TABLENAME"_s, context ).trimmed();
128 if ( tableName.isEmpty() )
130 tableName = source->sourceName();
131 tableName = tableName.replace(
'.',
'_' );
133 tableName = tableName.replace(
' ', QString() ).right( 63 );
135 QString geometryColumn = parameterAsString( parameters, u
"GEOMETRY_COLUMN"_s, context );
136 if ( geometryColumn.isEmpty() )
138 geometryColumn = u
"geom"_s;
142 geometryColumn.clear();
145 const bool createSpatialIndex = parameterAsBoolean( parameters, u
"CREATEINDEX"_s, context );
147 QMap<QString, QVariant> options;
150 options[u
"overwrite"_s] =
true;
152 if ( parameterAsBoolean( parameters, u
"LOWERCASE_NAMES"_s, context ) )
154 options[u
"lowercaseFieldNames"_s] =
true;
155 geometryColumn = geometryColumn.toLower();
157 if ( parameterAsBoolean( parameters, u
"DROP_STRING_LENGTH"_s, context ) )
159 options[u
"dropStringConstraints"_s] =
true;
161 if ( parameterAsBoolean( parameters, u
"FORCE_SINGLEPART"_s, context ) )
163 options[u
"forceSinglePartGeometryType"_s] =
true;
165 if ( !encoding.isEmpty() )
167 options[u
"fileEncoding"_s] = encoding;
171 uri.setTable( tableName );
172 uri.setKeyColumn( primaryKeyField );
173 uri.setGeometryColumn( geometryColumn );
175 auto exporter = std::make_unique<QgsVectorLayerExporter>( uri.uri(), u
"spatialite"_s, source->fields(), source->wkbType(), source->sourceCrs(), overwrite, options );
178 throw QgsProcessingException( QObject::tr(
"Error exporting to SpatiaLite\n%1" ).arg( exporter->errorMessage() ) );
182 const double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 0.0;
201 exporter->flushBuffer();
204 throw QgsProcessingException( QObject::tr(
"Error exporting to SpatiaLite\n%1" ).arg( exporter->errorMessage() ) );
208 if ( !geometryColumn.isEmpty() && createSpatialIndex )
214 conn->createSpatialIndex(
"", tableName, opt );
224 conn->vacuum(
"", tableName );
228 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.