Skip to content

Commit

Permalink
Merge pull request #31039 from elpaso/pg_raster_support
Browse files Browse the repository at this point in the history
Expose PostGIS rasters to the browser and the source select dialog
  • Loading branch information
elpaso committed Aug 1, 2019
2 parents 4f7975d + 25da979 commit e998757
Show file tree
Hide file tree
Showing 5 changed files with 276 additions and 110 deletions.
24 changes: 23 additions & 1 deletion src/providers/postgres/qgspgsourceselect.cpp
Expand Up @@ -493,6 +493,9 @@ void QgsPgSourceSelect::addButtonClicked()
{
mSelectedTables.clear();

QStringList dbTables;
QList<QPair<QString, QString>> rasterTables;

const auto constIndexes = mTablesTreeView->selectionModel()->selection().indexes();
for ( const QModelIndex &idx : constIndexes )
{
Expand All @@ -504,6 +507,14 @@ void QgsPgSourceSelect::addButtonClicked()
continue;

mSelectedTables << uri;
if ( uri.startsWith( QStringLiteral( "PG: " ) ) )
{
rasterTables.append( QPair<QString, QString>( idx.data().toString(), uri ) );
}
else
{
dbTables.append( uri );
}
}

if ( mSelectedTables.empty() )
Expand All @@ -512,7 +523,18 @@ void QgsPgSourceSelect::addButtonClicked()
}
else
{
emit addDatabaseLayers( mSelectedTables, QStringLiteral( "postgres" ) );
if ( ! dbTables.isEmpty() )
{
emit addDatabaseLayers( dbTables, QStringLiteral( "postgres" ) );
}
if ( ! rasterTables.isEmpty() )
{
for ( const auto &u : qgis::as_const( rasterTables ) )
{
emit addRasterLayer( u.second, u.first, QLatin1String( "gdal" ) );
}
}

if ( !mHoldDialogOpen->isChecked() && widgetMode() == QgsProviderRegistry::WidgetMode::None )
{
accept();
Expand Down
66 changes: 52 additions & 14 deletions src/providers/postgres/qgspgtablemodel.cpp
Expand Up @@ -62,24 +62,36 @@ void QgsPgTableModel::addTableEntry( const QgsPostgresLayerProperty &layerProper

QString tip;
bool withTipButSelectable = false;
if ( wkbType == QgsWkbTypes::Unknown )
if ( ! layerProperty.isRaster )
{
tip = tr( "Specify a geometry type in the '%1' column" ).arg( tr( "Data Type" ) );
if ( wkbType == QgsWkbTypes::Unknown )
{
tip = tr( "Specify a geometry type in the '%1' column" ).arg( tr( "Data Type" ) );
}
else if ( wkbType != QgsWkbTypes::NoGeometry && srid == std::numeric_limits<int>::min() )
{
tip = tr( "Enter a SRID into the '%1' column" ).arg( tr( "SRID" ) );
}
else if ( !layerProperty.pkCols.isEmpty() )
{
tip = tr( "Select columns in the '%1' column that uniquely identify features of this layer" ).arg( tr( "Feature id" ) );
withTipButSelectable = true;
}
}
else if ( wkbType != QgsWkbTypes::NoGeometry && srid == std::numeric_limits<int>::min() )

QStandardItem *schemaNameItem = new QStandardItem( layerProperty.schemaName );
QStandardItem *typeItem = nullptr;
if ( layerProperty.isRaster )
{
tip = tr( "Enter a SRID into the '%1' column" ).arg( tr( "SRID" ) );
typeItem = new QStandardItem( QgsApplication::getThemeIcon( "/mIconRasterLayer.svg" ), tr( "Raster" ) );
}
else if ( !layerProperty.pkCols.isEmpty() )
else
{
tip = tr( "Select columns in the '%1' column that uniquely identify features of this layer" ).arg( tr( "Feature id" ) );
withTipButSelectable = true;
typeItem = new QStandardItem( iconForWkbType( wkbType ), wkbType == QgsWkbTypes::Unknown ? tr( "Select…" ) : QgsPostgresConn::displayStringForWkbType( wkbType ) );
}

QStandardItem *schemaNameItem = new QStandardItem( layerProperty.schemaName );
QStandardItem *typeItem = new QStandardItem( iconForWkbType( wkbType ), wkbType == QgsWkbTypes::Unknown ? tr( "Select…" ) : QgsPostgresConn::displayStringForWkbType( wkbType ) );
typeItem->setData( wkbType == QgsWkbTypes::Unknown, Qt::UserRole + 1 );
typeItem->setData( wkbType, Qt::UserRole + 2 );
typeItem->setData( layerProperty.isRaster, Qt::UserRole + 3 );
if ( wkbType == QgsWkbTypes::Unknown )
typeItem->setFlags( typeItem->flags() | Qt::ItemIsEditable );

Expand Down Expand Up @@ -146,6 +158,16 @@ void QgsPgTableModel::addTableEntry( const QgsPostgresLayerProperty &layerProper

QStandardItem *sqlItem = new QStandardItem( layerProperty.sql );

// For rasters, disable
if ( layerProperty.isRaster )
{
selItem->setFlags( selItem->flags() & ~ Qt::ItemIsUserCheckable );
selItem->setCheckState( Qt::Unchecked );
checkPkUnicityItem->setFlags( checkPkUnicityItem->flags() & ~ Qt::ItemIsUserCheckable );
checkPkUnicityItem->setCheckState( Qt::Unchecked );
sqlItem->setEnabled( false );
}

QList<QStandardItem *> childItemList;

childItemList << schemaNameItem;
Expand Down Expand Up @@ -359,12 +381,28 @@ QString QgsPgTableModel::layerURI( const QModelIndex &index, const QString &conn
return QString();
}

QgsWkbTypes::Type wkbType = ( QgsWkbTypes::Type ) itemFromIndex( index.sibling( index.row(), DbtmType ) )->data( Qt::UserRole + 2 ).toInt();
bool isRaster = itemFromIndex( index.sibling( index.row(), DbtmType ) )->data( Qt::UserRole + 3 ).toBool();
QgsWkbTypes::Type wkbType = static_cast<QgsWkbTypes::Type>( itemFromIndex( index.sibling( index.row(), DbtmType ) )->data( Qt::UserRole + 2 ).toInt() );
if ( wkbType == QgsWkbTypes::Unknown )
{
QgsDebugMsg( QStringLiteral( "unknown geometry type" ) );
// no geometry type selected
return QString();
if ( isRaster )
{
// GDAL connection string
const QString schemaName = index.sibling( index.row(), DbtmSchema ).data( Qt::DisplayRole ).toString();
const QString tableName = index.sibling( index.row(), DbtmTable ).data( Qt::DisplayRole ).toString();
const QString geomColumnName = index.sibling( index.row(), DbtmGeomCol ).data( Qt::DisplayRole ).toString();
return QStringLiteral( "PG: %1 mode=2 schema='%2' column='%3' table='%4'" )
.arg( connInfo )
.arg( schemaName )
.arg( geomColumnName )
.arg( tableName );
}
else
{
QgsDebugMsg( QStringLiteral( "unknown geometry type" ) );
// no geometry type selected
return QString();
}
}

QStandardItem *pkItem = itemFromIndex( index.sibling( index.row(), DbtmPkCol ) );
Expand Down

0 comments on commit e998757

Please sign in to comment.