From 532e847336f6fa668a8036ce3e10a392e7d22a76 Mon Sep 17 00:00:00 2001 From: Samweli Date: Tue, 21 Dec 2021 14:34:59 +0300 Subject: [PATCH] added support for loading item footprints when they are available --- src/qgis_stac/api/client.py | 2 ++ src/qgis_stac/api/models.py | 2 +- src/qgis_stac/gui/result_item_widget.py | 40 ++++++++++++++++++++++++- src/qgis_stac/ui/qgis_stac_widget.ui | 2 +- src/qgis_stac/ui/result_item_widget.ui | 10 +++++-- 5 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/qgis_stac/api/client.py b/src/qgis_stac/api/client.py index 74203aa..0c06dd5 100644 --- a/src/qgis_stac/api/client.py +++ b/src/qgis_stac/api/client.py @@ -60,6 +60,8 @@ def handle_items( assets=assets ) + if item.geometry: + item_result.geometry = item.geometry items.append(item_result) self.items_received.emit(items, pagination) diff --git a/src/qgis_stac/api/models.py b/src/qgis_stac/api/models.py index 82123ac..b5e4c97 100644 --- a/src/qgis_stac/api/models.py +++ b/src/qgis_stac/api/models.py @@ -190,7 +190,7 @@ class Item: type: ResourceType = None stac_version: str = None stac_extensions: typing.List[str] = None - geometry: ResourceGeometry = None + geometry: typing.Optional[dict] = None bbox: typing.List[float] = None properties: ResourceProperties = None links: typing.List[ResourceLink] = None diff --git a/src/qgis_stac/gui/result_item_widget.py b/src/qgis_stac/gui/result_item_widget.py index b554591..4c94da1 100644 --- a/src/qgis_stac/gui/result_item_widget.py +++ b/src/qgis_stac/gui/result_item_widget.py @@ -5,6 +5,9 @@ import os +import json +import tempfile + from functools import partial from qgis.PyQt import ( @@ -114,6 +117,42 @@ def initialize_ui(self): 'selection-color: white;' ) + self.footprint_box.setEnabled(self.item.geometry is not None) + self.footprint_box.clicked.connect(self.add_footprint) + + def add_footprint(self): + """ Adds the item footprint inside QGIS as a map layer""" + layer_file = tempfile.NamedTemporaryFile( + mode="w+", + suffix='.geojson', + delete=False + ) + layer_name = f"{self.item.id}_footprint" + json.dump(self.item.geometry, layer_file) + + layer_file.flush() + + layer = QgsVectorLayer( + layer_file.name, + layer_name, + AssetLayerType.VECTOR.value + ) + if layer.isValid(): + QgsProject.instance().addMapLayer(layer) + self.main_widget.show_message( + tr("Successfully loaded footprint layer."), + level=Qgis.Info + ) + + else: + self.main_widget.show_message( + tr( + "Couldn't load footprint into QGIS," + " its layer is not valid." + ), + level=Qgis.Critical + ) + def update_inputs(self, enabled): """ Updates the inputs widgets state in the main search item widget. @@ -156,7 +195,6 @@ def download_asset(self, index): except Exception as e: self.main_widget.show_message("Error in downloading file") - def load_asset(self, index): """ Loads asset into QGIS. Checks if the asset type is a loadable layer inside QGIS. diff --git a/src/qgis_stac/ui/qgis_stac_widget.ui b/src/qgis_stac/ui/qgis_stac_widget.ui index e050bc9..0abaae7 100644 --- a/src/qgis_stac/ui/qgis_stac_widget.ui +++ b/src/qgis_stac/ui/qgis_stac_widget.ui @@ -29,7 +29,7 @@ - 2 + 0 diff --git a/src/qgis_stac/ui/result_item_widget.ui b/src/qgis_stac/ui/result_item_widget.ui index b3cea9a..25227ae 100644 --- a/src/qgis_stac/ui/result_item_widget.ui +++ b/src/qgis_stac/ui/result_item_widget.ui @@ -146,7 +146,7 @@ - <html><head/><body><p>Open resource in Browser</p></body></html> + Add item footprint as a layer Add footprint @@ -155,6 +155,9 @@ + + Add item assets as layers + Add assets as layers @@ -164,6 +167,9 @@ + + Download item assets + Download assets @@ -195,7 +201,7 @@ QFrame::StyledPanel - Thumbnail + No Thumbnail Qt::AlignCenter