QGIS API Documentation 4.3.0-Master (389d690bb77)
Loading...
Searching...
No Matches
qgsstacasset.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsstacasset.cpp
3 ---------------------
4 begin : October 2024
5 copyright : (C) 2024 by Stefanos Natsis
6 email : uclaros at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsstacasset.h"
17
18#include <QString>
19#include <QUrl>
20
21using namespace Qt::StringLiterals;
22
23QgsStacAsset::QgsStacAsset( const QString &href, const QString &title, const QString &description, const QString &mediaType, const QStringList &roles )
24 : mHref( href )
25 , mTitle( title )
26 , mDescription( description )
27 , mMediaType( mediaType )
28 , mRoles( roles )
29{}
30
31QString QgsStacAsset::href() const
32{
33 return mHref;
34}
35
36QString QgsStacAsset::title() const
37{
38 return mTitle;
39}
40
42{
43 return mDescription;
44}
45
47{
48 return mMediaType;
49}
50
51QStringList QgsStacAsset::roles() const
52{
53 return mRoles;
54}
55
57{
58 const QString format = formatName();
59 return format == "COG"_L1 || format == "COPC"_L1 || format == "EPT"_L1 || format == "Zarr"_L1 || format == "Parquet"_L1 || format == "TileDB"_L1;
60}
61
63{
64 if ( mMediaType == "image/tiff; application=geotiff; profile=cloud-optimized"_L1 || mMediaType == "image/vnd.stac.geotiff; cloud-optimized=true"_L1 )
65 return u"COG"_s;
66 else if ( mMediaType == "application/vnd.laszip+copc"_L1 )
67 return u"COPC"_s;
68 else if ( mHref.endsWith( "/ept.json"_L1 ) )
69 return u"EPT"_s;
70 else if ( mMediaType == "application/vnd+zarr"_L1 )
71 return u"Zarr"_s;
72 else if ( mMediaType == "application/vnd.apache.parquet"_L1 )
73 return u"Parquet"_s;
74 else if ( mMediaType.contains( "tiledb"_L1, Qt::CaseInsensitive ) )
75 return u"TileDB"_s;
76 return QString();
77}
78
79
80QgsMimeDataUtils::Uri QgsStacAsset::uri( const QString &authcfg ) const
81{
83 QUrl url( href() );
84 if ( ( formatName() == "COG"_L1 ) || ( formatName() == "Zarr"_L1 ) || ( formatName() == "TileDB"_L1 ) || ( formatName() == "Parquet"_L1 ) )
85 {
86 if ( formatName() == "Parquet"_L1 )
87 {
88 uri.layerType = u"vector"_s;
89 uri.providerKey = u"ogr"_s;
90 }
91 else
92 {
93 uri.layerType = u"raster"_s;
94 uri.providerKey = u"gdal"_s;
95 }
96
97 if ( href().startsWith( "http"_L1, Qt::CaseInsensitive ) || href().startsWith( "ftp"_L1, Qt::CaseInsensitive ) )
98 {
99 uri.uri = u"/vsicurl/%1"_s.arg( href() );
100 if ( !authcfg.isEmpty() )
101 uri.uri.append( u" authcfg='%1'"_s.arg( authcfg ) );
102 }
103 else if ( href().startsWith( "s3://"_L1, Qt::CaseInsensitive ) )
104 {
105 uri.uri = u"/vsis3/%1"_s.arg( href().mid( 5 ) );
106 }
107 else if ( href().startsWith( "azure://"_L1, Qt::CaseInsensitive ) )
108 {
109 uri.uri = u"/vsiaz/%1"_s.arg( href().mid( 8 ) );
110 }
111 else if ( href().startsWith( "az://"_L1, Qt::CaseInsensitive ) )
112 {
113 uri.uri = u"/vsiaz/%1"_s.arg( href().mid( 5 ) );
114 }
115 else if ( href().startsWith( "gcp://"_L1, Qt::CaseInsensitive ) )
116 {
117 uri.uri = u"/vsigs/%1"_s.arg( href().mid( 6 ) );
118 }
119 else
120 {
121 uri.uri = href();
122 }
123
124 if ( formatName() == "Zarr"_L1 )
125 {
126 uri.uri = u"ZARR:\"%1\""_s.arg( uri.uri );
127 }
128 }
129 else if ( formatName() == "COPC"_L1 )
130 {
131 uri.layerType = u"pointcloud"_s;
132 uri.providerKey = u"copc"_s;
133 uri.uri = href();
134 if ( !authcfg.isEmpty() )
135 uri.uri.append( u" authcfg='%1'"_s.arg( authcfg ) );
136 }
137 else if ( formatName() == "EPT"_L1 )
138 {
139 uri.layerType = u"pointcloud"_s;
140 uri.providerKey = u"ept"_s;
141 uri.uri = href();
142 if ( !authcfg.isEmpty() )
143 uri.uri.append( u" authcfg='%1'"_s.arg( authcfg ) );
144 }
145 else
146 {
147 return {};
148 }
149
150 uri.name = title().isEmpty() ? url.fileName() : title();
151
152 return uri;
153}
154
155QString QgsStacAsset::toHtml( const QString &assetId ) const
156{
157 QString html = u"<h1>%1</h1>\n<hr>\n"_s.arg( "Asset"_L1 );
158 html += "<table class=\"list-view\">\n"_L1;
159 html += u"<tr><td class=\"highlight\">%1</td><td>%2</td></tr>\n"_s.arg( u"id"_s, assetId );
160 html += u"<tr><td class=\"highlight\">%1</td><td>%2</td></tr>\n"_s.arg( u"title"_s, title() );
161 html += u"<tr><td class=\"highlight\">%1</td><td>%2</td></tr>\n"_s.arg( u"description"_s, description() );
162 html += u"<tr><td class=\"highlight\">%1</td><td><a href=\"%2\">%2</a></td></tr>\n"_s.arg( u"url"_s, href() );
163 html += u"<tr><td class=\"highlight\">%1</td><td>%2</td></tr>\n"_s.arg( u"type"_s, mediaType() );
164 html += u"<tr><td class=\"highlight\">%1</td><td>%2</td></tr>\n"_s.arg( u"roles"_s, roles().join( ',' ) );
165 html += "</table><br/>\n"_L1;
166 return html;
167}
168
170{
171 /*
172 * Directory-based data types like Zarr should not offer downloads.
173 * Download attempts might
174 * - fail with 4xx,
175 * - succeed but download an HTML directory listing response, or
176 * - something else that does not meet the user's needs.
177 */
178 if ( formatName() == "Zarr"_L1 || formatName() == "TileDB"_L1 )
179 {
180 return false;
181 }
182
183 return true;
184}
bool isCloudOptimized() const
Returns whether the asset is in a cloud optimized format like COG or COPC.
QString formatName() const
Returns the format name for cloud optimized formats.
QStringList roles() const
Returns the roles assigned to the asset.
QString toHtml(const QString &assetId) const
Returns an HTML representation of the STAC Asset including its ID within its container.
bool isDownloadable() const
Returns whether the asset can be downloaded.
QString mediaType() const
Returns the media type of the asset.
QString href() const
Returns the URI to the asset object.
QgsMimeDataUtils::Uri uri(const QString &authcfg=QString()) const
Returns a uri for the asset if it is a cloud optimized file like COG or COPC.
QgsStacAsset(const QString &href, const QString &title, const QString &description, const QString &mediaType, const QStringList &roles)
Constructor.
QString title() const
Returns the displayed title for clients and users.
QString description() const
Returns a description of the Asset providing additional details, such as how it was processed or crea...