Skip to content

Commit 3ea70a2

Browse files
committed
Allow adding a single asset as a layer
1 parent b464c3c commit 3ea70a2

File tree

6 files changed

+95
-54
lines changed

6 files changed

+95
-54
lines changed

src/core/stac/qgsstacasset.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
#include "qgsstacasset.h"
1717

18+
#include <QUrl>
19+
1820
QgsStacAsset::QgsStacAsset( const QString &href,
1921
const QString &title,
2022
const QString &description,
@@ -72,3 +74,46 @@ QString QgsStacAsset::formatName() const
7274
return QStringLiteral( "EPT" );
7375
return QString();
7476
}
77+
78+
QgsMimeDataUtils::Uri QgsStacAsset::uri() const
79+
{
80+
QgsMimeDataUtils::Uri uri;
81+
QUrl url( href() );
82+
if ( url.isLocalFile() )
83+
{
84+
uri.uri = href();
85+
}
86+
else if ( formatName() == QLatin1String( "COG" ) )
87+
{
88+
uri.layerType = QStringLiteral( "raster" );
89+
uri.providerKey = QStringLiteral( "gdal" );
90+
if ( href().startsWith( QLatin1String( "http" ), Qt::CaseInsensitive ) ||
91+
href().startsWith( QLatin1String( "ftp" ), Qt::CaseInsensitive ) )
92+
{
93+
uri.uri = QStringLiteral( "/vsicurl/%1" ).arg( href() );
94+
}
95+
else if ( href().startsWith( QLatin1String( "s3://" ), Qt::CaseInsensitive ) )
96+
{
97+
uri.uri = QStringLiteral( "/vsis3/%1" ).arg( href().mid( 5 ) );
98+
}
99+
else
100+
{
101+
uri.uri = href();
102+
}
103+
}
104+
else if ( formatName() == QLatin1String( "COPC" ) )
105+
{
106+
uri.layerType = QStringLiteral( "pointcloud" );
107+
uri.providerKey = QStringLiteral( "copc" );
108+
uri.uri = href();
109+
}
110+
else if ( formatName() == QLatin1String( "EPT" ) )
111+
{
112+
uri.layerType = QStringLiteral( "pointcloud" );
113+
uri.providerKey = QStringLiteral( "ept" );
114+
uri.uri = href();
115+
}
116+
uri.name = title().isEmpty() ? url.fileName() : title();
117+
118+
return uri;
119+
}

src/core/stac/qgsstacasset.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define SIP_NO_FILE
2020

2121
#include "qgis_core.h"
22+
#include "qgsmimedatautils.h"
2223

2324
#include <QString>
2425
#include <QStringList>
@@ -73,6 +74,12 @@ class CORE_EXPORT QgsStacAsset
7374
*/
7475
QString formatName() const;
7576

77+
/**
78+
* Returns a uri for the asset if it is a local or cloud optimized file like COG or COPC
79+
* \since QGIS 3.42
80+
*/
81+
QgsMimeDataUtils::Uri uri() const;
82+
7683
private:
7784
QString mHref;
7885
QString mTitle;

src/core/stac/qgsstacitem.cpp

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -191,50 +191,14 @@ QString QgsStacItem::description() const
191191
QgsMimeDataUtils::UriList QgsStacItem::uris() const
192192
{
193193
QgsMimeDataUtils::UriList uris;
194-
for ( auto it = mAssets.constBegin(); it != mAssets.constEnd(); ++it )
194+
for ( const QgsStacAsset &asset : std::as_const( mAssets ) )
195195
{
196-
QgsMimeDataUtils::Uri uri;
197-
QUrl url( it->href() );
198-
if ( url.isLocalFile() )
199-
{
200-
uri.uri = it->href();
201-
}
202-
else if ( it->formatName() == QLatin1String( "COG" ) )
203-
{
204-
uri.layerType = QStringLiteral( "raster" );
205-
uri.providerKey = QStringLiteral( "gdal" );
206-
if ( it->href().startsWith( QLatin1String( "http" ), Qt::CaseInsensitive ) ||
207-
it->href().startsWith( QLatin1String( "ftp" ), Qt::CaseInsensitive ) )
208-
{
209-
uri.uri = QStringLiteral( "/vsicurl/%1" ).arg( it->href() );
210-
}
211-
else if ( it->href().startsWith( QLatin1String( "s3://" ), Qt::CaseInsensitive ) )
212-
{
213-
uri.uri = QStringLiteral( "/vsis3/%1" ).arg( it->href().mid( 5 ) );
214-
}
215-
else
216-
{
217-
uri.uri = it->href();
218-
}
219-
}
220-
else if ( it->formatName() == QLatin1String( "COPC" ) )
221-
{
222-
uri.layerType = QStringLiteral( "pointcloud" );
223-
uri.providerKey = QStringLiteral( "copc" );
224-
uri.uri = it->href();
225-
}
226-
else if ( it->formatName() == QLatin1String( "EPT" ) )
227-
{
228-
uri.layerType = QStringLiteral( "pointcloud" );
229-
uri.providerKey = QStringLiteral( "ept" );
230-
uri.uri = it->href();
231-
}
196+
QgsMimeDataUtils::Uri uri = asset.uri();
232197

233198
// skip assets with incompatible formats
234199
if ( uri.uri.isEmpty() )
235200
continue;
236201

237-
uri.name = it->title().isEmpty() ? url.fileName() : it->title();
238202
uris.append( uri );
239203
}
240204
return uris;

src/core/stac/qgsstacitem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class CORE_EXPORT QgsStacItem : public QgsStacObject
128128
QString description() const;
129129

130130
/**
131-
* Returns a list of uris of all assets that have a cloud optimized format like COG or COPC
131+
* Returns a list of uris of all assets that are local or have a cloud optimized format like COG or COPC
132132
* \since QGIS 3.42
133133
*/
134134
QgsMimeDataUtils::UriList uris() const;

src/gui/stac/qgsstacsourceselect.cpp

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,7 @@ void QgsStacSourceSelect::addButtonClicked()
123123

124124
for ( auto &uri : std::as_const( allUris ) )
125125
{
126-
if ( uri.layerType == QLatin1String( "raster" ) )
127-
{
128-
Q_NOWARN_DEPRECATED_PUSH
129-
emit addRasterLayer( uri.uri, uri.name, uri.providerKey );
130-
Q_NOWARN_DEPRECATED_POP
131-
emit addLayer( Qgis::LayerType::Raster, uri.uri, uri.name, uri.providerKey );
132-
}
133-
else if ( uri.layerType == QLatin1String( "pointcloud" ) )
134-
{
135-
Q_NOWARN_DEPRECATED_PUSH
136-
emit addPointCloudLayer( uri.uri, uri.name, uri.providerKey );
137-
Q_NOWARN_DEPRECATED_POP
138-
emit addLayer( Qgis::LayerType::PointCloud, uri.uri, uri.name, uri.providerKey );
139-
}
126+
loadUri( uri );
140127
}
141128
}
142129

@@ -512,6 +499,23 @@ void QgsStacSourceSelect::showItemsContextMenu( QPoint point )
512499
if ( dsm )
513500
bar = dsm->messageBar();
514501

502+
const QgsStacItem *item = dynamic_cast<QgsStacItem *>( index.data( QgsStacItemListModel::Role::StacObject ).value<QgsStacObject *>() );
503+
QMenu *assetsMenu = menu->addMenu( tr( "Add Layer" ) );
504+
const QMap<QString, QgsStacAsset> assets = item->assets();
505+
for ( const QgsStacAsset &asset : assets )
506+
{
507+
if ( asset.isCloudOptimized() )
508+
{
509+
QAction *loadAssetAction = new QAction( asset.title(), assetsMenu );
510+
connect( loadAssetAction, &QAction::triggered, this, [this, &asset]
511+
{
512+
QgsTemporaryCursorOverride cursorOverride( Qt::WaitCursor );
513+
loadUri( asset.uri() );
514+
} );
515+
assetsMenu->addAction( loadAssetAction );
516+
}
517+
}
518+
515519
QAction *zoomToAction = new QAction( tr( "Zoom to Item" ) );
516520
connect( zoomToAction, &QAction::triggered, this, [index, this]
517521
{
@@ -561,8 +565,11 @@ void QgsStacSourceSelect::showItemsContextMenu( QPoint point )
561565
details.exec();
562566
} );
563567

568+
564569
menu->addAction( zoomToAction );
565570
menu->addAction( panToAction );
571+
if ( !assetsMenu->isEmpty() )
572+
menu->addMenu( assetsMenu );
566573
menu->addAction( downloadAction );
567574
menu->addAction( detailsAction );
568575

@@ -606,4 +613,21 @@ void QgsStacSourceSelect::showFootprints( bool enable )
606613
}
607614
}
608615

616+
void QgsStacSourceSelect::loadUri( const QgsMimeDataUtils::Uri &uri )
617+
{
618+
if ( uri.layerType == QLatin1String( "raster" ) )
619+
{
620+
Q_NOWARN_DEPRECATED_PUSH
621+
emit addRasterLayer( uri.uri, uri.name, uri.providerKey );
622+
Q_NOWARN_DEPRECATED_POP
623+
emit addLayer( Qgis::LayerType::Raster, uri.uri, uri.name, uri.providerKey );
624+
}
625+
else if ( uri.layerType == QLatin1String( "pointcloud" ) )
626+
{
627+
Q_NOWARN_DEPRECATED_PUSH
628+
emit addPointCloudLayer( uri.uri, uri.name, uri.providerKey );
629+
Q_NOWARN_DEPRECATED_POP
630+
emit addLayer( Qgis::LayerType::PointCloud, uri.uri, uri.name, uri.providerKey );
631+
}
632+
}
609633
///@endcond

src/gui/stac/qgsstacsourceselect.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "ui_qgsstacsourceselectbase.h"
2020
#include "qgsabstractdatasourcewidget.h"
2121
#include "qgis_gui.h"
22+
#include "qgsmimedatautils.h"
2223

2324
#include <QStandardItemModel>
2425
#include <QStyledItemDelegate>
@@ -32,7 +33,6 @@ class QgsStacItemListModel;
3233
class QgsStacController;
3334
class QgsRubberBand;
3435

35-
3636
class GUI_EXPORT QgsStacSourceSelect : public QgsAbstractDataSourceWidget, private Ui::QgsStacSourceSelectBase
3737
{
3838
Q_OBJECT
@@ -99,6 +99,7 @@ class GUI_EXPORT QgsStacSourceSelect : public QgsAbstractDataSourceWidget, priva
9999

100100
void highlightFootprint( const QModelIndex &index );
101101
void showFootprints( bool enable );
102+
void loadUri( const QgsMimeDataUtils::Uri &uri );
102103

103104
QString mCollectionsUrl;
104105
QString mSearchUrl;

0 commit comments

Comments
 (0)