Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve asset type error #236

Merged
merged 3 commits into from
Apr 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 46 additions & 10 deletions src/qgis_stac/gui/asset_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
)
from qgis.PyQt.uic import loadUiType

from qgis.core import QgsNetworkAccessManager

from ..api.models import AssetLayerType

from ..utils import tr
from ..utils import log, tr

WidgetUi, _ = loadUiType(
os.path.join(os.path.dirname(__file__), "../ui/asset_widget.ui")
Expand Down Expand Up @@ -49,29 +51,63 @@ def __init__(
def initialize_ui(self):
""" Populate UI inputs when loading the widget"""

layer_types = [
AssetLayerType.COG.value,
AssetLayerType.COPC.value,
AssetLayerType.GEOTIFF.value,
AssetLayerType.NETCDF.value,
]

self.title_la.setText(self.asset.title)
self.type_la.setText(self.asset.type)
asset_load = self.asset_loadable()

self.load_box.setEnabled(self.asset.type in ''.join(layer_types))
self.load_box.setEnabled(asset_load)
self.load_box.toggled.connect(self.asset_load_selected)
self.load_box.stateChanged.connect(self.asset_load_selected)
self.download_box.toggled.connect(self.asset_download_selected)
self.download_box.stateChanged.connect(self.asset_download_selected)

if self.asset.type not in layer_types:
if asset_load:
self.load_box.setToolTip(
tr("Asset contains {} media type which "
"cannot be loaded as a map layer in QGIS"
).format(self.asset.type)
)

def asset_loadable(self):
""" Returns if asset can be added into QGIS"""

layer_types = [
AssetLayerType.COG.value,
AssetLayerType.COPC.value,
AssetLayerType.GEOTIFF.value,
AssetLayerType.NETCDF.value,
]

if self.asset.type is not None:
return self.asset.type in ''.join(layer_types)
else:
try:
request = QtNetwork.QNetworkRequest(
QtCore.QUrl(self.asset.href)
)
response = QgsNetworkAccessManager().\
instance().blockingGet(request)
content_type = response.rawHeader(
QtCore.QByteArray(
'content-type'.encode()
)
)
content_type = str(content_type, 'utf-8')

for layer_type in layer_types:
layer_type_values = layer_type.split(' ')
for value in layer_type_values:
if value in content_type:
return True

except Exception as e:
log(f"Problem fetching asset "
f"type from the asset url {self.asset.href},"
f" error {e}"
)

return False

def asset_load_selected(self, state=None):
""" Emits the needed signal when an asset has been selected
for loading.
Expand Down