Skip to content

Commit

Permalink
Merge pull request #236 from Samweli/fix_asset_type_v2
Browse files Browse the repository at this point in the history
Resolve asset type error
  • Loading branch information
Samweli authored Apr 13, 2023
2 parents 4221444 + a868804 commit 2ef210d
Showing 1 changed file with 46 additions and 10 deletions.
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

0 comments on commit 2ef210d

Please sign in to comment.