Skip to content
Permalink
Browse files

Implement spatial index creation for Postgis connections

  • Loading branch information
nyalldawson committed Mar 16, 2020
1 parent 6a4b67f commit c6ee7b67736c0b5a760fdade2aaa09211aee4508
@@ -62,7 +62,8 @@ void QgsPostgresProviderConnection::setDefaultCapabilities()
Capability::Tables,
Capability::Schemas,
Capability::Spatial,
Capability::TableExists
Capability::TableExists,
Capability::CreateSpatialIndex
};
}

@@ -322,6 +323,21 @@ void QgsPostgresProviderConnection::vacuum( const QString &schema, const QString
.arg( QgsPostgresConn::quotedIdentifier( name ) ) );
}

void QgsPostgresProviderConnection::createSpatialIndex( const QString &schema, const QString &name, const QgsAbstractDatabaseProviderConnection::SpatialIndexOptions &options ) const
{
if ( options.geometryColumnName.isEmpty() )
throw QgsProviderConnectionException( QObject::tr( "Geometry column name not specified while creating spatial index" ) );

checkCapability( Capability::Vacuum );

const QString indexName = QStringLiteral( "sidx_%1_%2" ).arg( name, options.geometryColumnName );
executeSql( QStringLiteral( "CREATE INDEX %1 ON %2.%3 USING GIST (%4);" )
.arg( indexName,
QgsPostgresConn::quotedIdentifier( schema ),
QgsPostgresConn::quotedIdentifier( name ),
QgsPostgresConn::quotedIdentifier( options.geometryColumnName ) ) );
}

QList<QgsPostgresProviderConnection::TableProperty> QgsPostgresProviderConnection::tables( const QString &schema, const TableFlags &flags ) const
{
checkCapability( Capability::Tables );
@@ -47,6 +47,7 @@ class QgsPostgresProviderConnection : public QgsAbstractDatabaseProviderConnecti
void renameSchema( const QString &name, const QString &newName ) const override;
QList<QVariantList> executeSql( const QString &sql ) const override;
void vacuum( const QString &schema, const QString &name ) const override;
void createSpatialIndex( const QString &schema, const QString &name, const QgsAbstractDatabaseProviderConnection::SpatialIndexOptions &options = QgsAbstractDatabaseProviderConnection::SpatialIndexOptions() ) const override;
QList<QgsAbstractDatabaseProviderConnection::TableProperty> tables( const QString &schema,
const TableFlags &flags = nullptr ) const override;
QStringList schemas( ) const override;
@@ -275,7 +275,9 @@ def _test_operations(self, md, conn):

# Create spatial index
if capabilities & QgsAbstractDatabaseProviderConnection.CreateSpatialIndex:
conn.createSpatialIndex('myNewSchema', 'myNewTable')
options = QgsAbstractDatabaseProviderConnection.SpatialIndexOptions()
options.geometryColumnName = 'geom'
conn.createSpatialIndex('myNewSchema', 'myNewTable', options)

if capabilities & QgsAbstractDatabaseProviderConnection.DropSchema:
# Drop schema (should fail)

0 comments on commit c6ee7b6

Please sign in to comment.
You can’t perform that action at this time.