Allow adding a single asset as a layer

This commit is contained in:
uclaros 2024-11-27 23:51:38 +02:00 committed by Martin Dobias
parent 8abedd0c5f
commit a9e23faa3e
6 changed files with 95 additions and 54 deletions

View File

@ -15,6 +15,8 @@
#include "qgsstacasset.h"
#include <QUrl>
QgsStacAsset::QgsStacAsset( const QString &href,
const QString &title,
const QString &description,
@ -72,3 +74,46 @@ QString QgsStacAsset::formatName() const
return QStringLiteral( "EPT" );
return QString();
}
QgsMimeDataUtils::Uri QgsStacAsset::uri() const
{
QgsMimeDataUtils::Uri uri;
QUrl url( href() );
if ( url.isLocalFile() )
{
uri.uri = href();
}
else if ( formatName() == QLatin1String( "COG" ) )
{
uri.layerType = QStringLiteral( "raster" );
uri.providerKey = QStringLiteral( "gdal" );
if ( href().startsWith( QLatin1String( "http" ), Qt::CaseInsensitive ) ||
href().startsWith( QLatin1String( "ftp" ), Qt::CaseInsensitive ) )
{
uri.uri = QStringLiteral( "/vsicurl/%1" ).arg( href() );
}
else if ( href().startsWith( QLatin1String( "s3://" ), Qt::CaseInsensitive ) )
{
uri.uri = QStringLiteral( "/vsis3/%1" ).arg( href().mid( 5 ) );
}
else
{
uri.uri = href();
}
}
else if ( formatName() == QLatin1String( "COPC" ) )
{
uri.layerType = QStringLiteral( "pointcloud" );
uri.providerKey = QStringLiteral( "copc" );
uri.uri = href();
}
else if ( formatName() == QLatin1String( "EPT" ) )
{
uri.layerType = QStringLiteral( "pointcloud" );
uri.providerKey = QStringLiteral( "ept" );
uri.uri = href();
}
uri.name = title().isEmpty() ? url.fileName() : title();
return uri;
}

View File

@ -19,6 +19,7 @@
#define SIP_NO_FILE
#include "qgis_core.h"
#include "qgsmimedatautils.h"
#include <QString>
#include <QStringList>
@ -73,6 +74,12 @@ class CORE_EXPORT QgsStacAsset
*/
QString formatName() const;
/**
* Returns a uri for the asset if it is a local or cloud optimized file like COG or COPC
* \since QGIS 3.42
*/
QgsMimeDataUtils::Uri uri() const;
private:
QString mHref;
QString mTitle;

View File

@ -191,50 +191,14 @@ QString QgsStacItem::description() const
QgsMimeDataUtils::UriList QgsStacItem::uris() const
{
QgsMimeDataUtils::UriList uris;
for ( auto it = mAssets.constBegin(); it != mAssets.constEnd(); ++it )
for ( const QgsStacAsset &asset : std::as_const( mAssets ) )
{
QgsMimeDataUtils::Uri uri;
QUrl url( it->href() );
if ( url.isLocalFile() )
{
uri.uri = it->href();
}
else if ( it->formatName() == QLatin1String( "COG" ) )
{
uri.layerType = QStringLiteral( "raster" );
uri.providerKey = QStringLiteral( "gdal" );
if ( it->href().startsWith( QLatin1String( "http" ), Qt::CaseInsensitive ) ||
it->href().startsWith( QLatin1String( "ftp" ), Qt::CaseInsensitive ) )
{
uri.uri = QStringLiteral( "/vsicurl/%1" ).arg( it->href() );
}
else if ( it->href().startsWith( QLatin1String( "s3://" ), Qt::CaseInsensitive ) )
{
uri.uri = QStringLiteral( "/vsis3/%1" ).arg( it->href().mid( 5 ) );
}
else
{
uri.uri = it->href();
}
}
else if ( it->formatName() == QLatin1String( "COPC" ) )
{
uri.layerType = QStringLiteral( "pointcloud" );
uri.providerKey = QStringLiteral( "copc" );
uri.uri = it->href();
}
else if ( it->formatName() == QLatin1String( "EPT" ) )
{
uri.layerType = QStringLiteral( "pointcloud" );
uri.providerKey = QStringLiteral( "ept" );
uri.uri = it->href();
}
QgsMimeDataUtils::Uri uri = asset.uri();
// skip assets with incompatible formats
if ( uri.uri.isEmpty() )
continue;
uri.name = it->title().isEmpty() ? url.fileName() : it->title();
uris.append( uri );
}
return uris;

View File

@ -128,7 +128,7 @@ class CORE_EXPORT QgsStacItem : public QgsStacObject
QString description() const;
/**
* Returns a list of uris of all assets that have a cloud optimized format like COG or COPC
* Returns a list of uris of all assets that are local or have a cloud optimized format like COG or COPC
* \since QGIS 3.42
*/
QgsMimeDataUtils::UriList uris() const;

View File

@ -123,20 +123,7 @@ void QgsStacSourceSelect::addButtonClicked()
for ( auto &uri : std::as_const( allUris ) )
{
if ( uri.layerType == QLatin1String( "raster" ) )
{
Q_NOWARN_DEPRECATED_PUSH
emit addRasterLayer( uri.uri, uri.name, uri.providerKey );
Q_NOWARN_DEPRECATED_POP
emit addLayer( Qgis::LayerType::Raster, uri.uri, uri.name, uri.providerKey );
}
else if ( uri.layerType == QLatin1String( "pointcloud" ) )
{
Q_NOWARN_DEPRECATED_PUSH
emit addPointCloudLayer( uri.uri, uri.name, uri.providerKey );
Q_NOWARN_DEPRECATED_POP
emit addLayer( Qgis::LayerType::PointCloud, uri.uri, uri.name, uri.providerKey );
}
loadUri( uri );
}
}
@ -512,6 +499,23 @@ void QgsStacSourceSelect::showItemsContextMenu( QPoint point )
if ( dsm )
bar = dsm->messageBar();
const QgsStacItem *item = dynamic_cast<QgsStacItem *>( index.data( QgsStacItemListModel::Role::StacObject ).value<QgsStacObject *>() );
QMenu *assetsMenu = menu->addMenu( tr( "Add Layer" ) );
const QMap<QString, QgsStacAsset> assets = item->assets();
for ( const QgsStacAsset &asset : assets )
{
if ( asset.isCloudOptimized() )
{
QAction *loadAssetAction = new QAction( asset.title(), assetsMenu );
connect( loadAssetAction, &QAction::triggered, this, [this, &asset]
{
QgsTemporaryCursorOverride cursorOverride( Qt::WaitCursor );
loadUri( asset.uri() );
} );
assetsMenu->addAction( loadAssetAction );
}
}
QAction *zoomToAction = new QAction( tr( "Zoom to Item" ) );
connect( zoomToAction, &QAction::triggered, this, [index, this]
{
@ -561,8 +565,11 @@ void QgsStacSourceSelect::showItemsContextMenu( QPoint point )
details.exec();
} );
menu->addAction( zoomToAction );
menu->addAction( panToAction );
if ( !assetsMenu->isEmpty() )
menu->addMenu( assetsMenu );
menu->addAction( downloadAction );
menu->addAction( detailsAction );
@ -606,4 +613,21 @@ void QgsStacSourceSelect::showFootprints( bool enable )
}
}
void QgsStacSourceSelect::loadUri( const QgsMimeDataUtils::Uri &uri )
{
if ( uri.layerType == QLatin1String( "raster" ) )
{
Q_NOWARN_DEPRECATED_PUSH
emit addRasterLayer( uri.uri, uri.name, uri.providerKey );
Q_NOWARN_DEPRECATED_POP
emit addLayer( Qgis::LayerType::Raster, uri.uri, uri.name, uri.providerKey );
}
else if ( uri.layerType == QLatin1String( "pointcloud" ) )
{
Q_NOWARN_DEPRECATED_PUSH
emit addPointCloudLayer( uri.uri, uri.name, uri.providerKey );
Q_NOWARN_DEPRECATED_POP
emit addLayer( Qgis::LayerType::PointCloud, uri.uri, uri.name, uri.providerKey );
}
}
///@endcond

View File

@ -19,6 +19,7 @@
#include "ui_qgsstacsourceselectbase.h"
#include "qgsabstractdatasourcewidget.h"
#include "qgis_gui.h"
#include "qgsmimedatautils.h"
#include <QStandardItemModel>
#include <QStyledItemDelegate>
@ -32,7 +33,6 @@ class QgsStacItemListModel;
class QgsStacController;
class QgsRubberBand;
class GUI_EXPORT QgsStacSourceSelect : public QgsAbstractDataSourceWidget, private Ui::QgsStacSourceSelectBase
{
Q_OBJECT
@ -99,6 +99,7 @@ class GUI_EXPORT QgsStacSourceSelect : public QgsAbstractDataSourceWidget, priva
void highlightFootprint( const QModelIndex &index );
void showFootprints( bool enable );
void loadUri( const QgsMimeDataUtils::Uri &uri );
QString mCollectionsUrl;
QString mSearchUrl;