QGIS API Documentation 4.3.0-Master (52aa6503bed)
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 == "FGB"_L1 || format == "PMTiles"_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 == "application/vnd.flatgeobuf"_L1 )
75 return u"FGB"_s;
76 else if ( mMediaType == "application/vnd.pmtiles"_L1 )
77 return u"PMTiles"_s;
78 else if ( mMediaType.contains( "tiledb"_L1, Qt::CaseInsensitive ) )
79 return u"TileDB"_s;
80 return QString();
81}
82
83
84QgsMimeDataUtils::Uri QgsStacAsset::uri( const QString &authcfg ) const
85{
87 QUrl url( href() );
88 if ( ( formatName() == "COG"_L1 ) || ( formatName() == "Zarr"_L1 ) || ( formatName() == "TileDB"_L1 ) || ( formatName() == "Parquet"_L1 ) || ( formatName() == "FGB"_L1 ) || ( formatName() == "PMTiles"_L1 ) )
89 {
90 if ( formatName() == "Parquet"_L1 || ( formatName() == "FGB"_L1 ) || ( formatName() == "PMTiles"_L1 ) )
91 {
92 uri.layerType = u"vector"_s;
93 uri.providerKey = u"ogr"_s;
94 }
95 else
96 {
97 uri.layerType = u"raster"_s;
98 uri.providerKey = u"gdal"_s;
99 }
100
101 if ( href().startsWith( "http"_L1, Qt::CaseInsensitive ) || href().startsWith( "ftp"_L1, Qt::CaseInsensitive ) )
102 {
103 uri.uri = u"/vsicurl/%1"_s.arg( href() );
104 if ( !authcfg.isEmpty() )
105 uri.uri.append( u" authcfg='%1'"_s.arg( authcfg ) );
106 }
107 else if ( href().startsWith( "s3://"_L1, Qt::CaseInsensitive ) )
108 {
109 uri.uri = u"/vsis3/%1"_s.arg( href().mid( 5 ) );
110 }
111 else if ( href().startsWith( "azure://"_L1, Qt::CaseInsensitive ) )
112 {
113 uri.uri = u"/vsiaz/%1"_s.arg( href().mid( 8 ) );
114 }
115 else if ( href().startsWith( "az://"_L1, Qt::CaseInsensitive ) )
116 {
117 uri.uri = u"/vsiaz/%1"_s.arg( href().mid( 5 ) );
118 }
119 else if ( href().startsWith( "gcp://"_L1, Qt::CaseInsensitive ) )
120 {
121 uri.uri = u"/vsigs/%1"_s.arg( href().mid( 6 ) );
122 }
123 else
124 {
125 uri.uri = href();
126 }
127
128 if ( formatName() == "Zarr"_L1 )
129 {
130 uri.uri = u"ZARR:\"%1\""_s.arg( uri.uri );
131 }
132
133 if ( formatName() == "PMTiles"_L1 )
134 {
135 uri.uri = u"%1|option:ZOOM_LEVEL_AUTO=YES"_s.arg( uri.uri );
136 }
137 }
138 else if ( formatName() == "COPC"_L1 )
139 {
140 uri.layerType = u"pointcloud"_s;
141 uri.providerKey = u"copc"_s;
142 uri.uri = href();
143 if ( !authcfg.isEmpty() )
144 uri.uri.append( u" authcfg='%1'"_s.arg( authcfg ) );
145 }
146 else if ( formatName() == "EPT"_L1 )
147 {
148 uri.layerType = u"pointcloud"_s;
149 uri.providerKey = u"ept"_s;
150 uri.uri = href();
151 if ( !authcfg.isEmpty() )
152 uri.uri.append( u" authcfg='%1'"_s.arg( authcfg ) );
153 }
154 else
155 {
156 return {};
157 }
158
159 uri.name = title().isEmpty() ? url.fileName() : title();
160
161 return uri;
162}
163
164QString QgsStacAsset::toHtml( const QString &assetId ) const
165{
166 QString html = u"<h1>%1</h1>\n<hr>\n"_s.arg( "Asset"_L1 );
167 html += "<table class=\"list-view\">\n"_L1;
168 html += u"<tr><td class=\"highlight\">%1</td><td>%2</td></tr>\n"_s.arg( u"id"_s, assetId );
169 html += u"<tr><td class=\"highlight\">%1</td><td>%2</td></tr>\n"_s.arg( u"title"_s, title() );
170 html += u"<tr><td class=\"highlight\">%1</td><td>%2</td></tr>\n"_s.arg( u"description"_s, description() );
171 html += u"<tr><td class=\"highlight\">%1</td><td><a href=\"%2\">%2</a></td></tr>\n"_s.arg( u"url"_s, href() );
172 html += u"<tr><td class=\"highlight\">%1</td><td>%2</td></tr>\n"_s.arg( u"type"_s, mediaType() );
173 html += u"<tr><td class=\"highlight\">%1</td><td>%2</td></tr>\n"_s.arg( u"roles"_s, roles().join( ',' ) );
174 html += "</table><br/>\n"_L1;
175 return html;
176}
177
179{
180 /*
181 * Directory-based data types like Zarr should not offer downloads.
182 * Download attempts might
183 * - fail with 4xx,
184 * - succeed but download an HTML directory listing response, or
185 * - something else that does not meet the user's needs.
186 */
187 if ( formatName() == "Zarr"_L1 || formatName() == "TileDB"_L1 )
188 {
189 return false;
190 }
191
192 return true;
193}
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...