From d3bd340d3d7975c03ec23f49924fed310a16c388 Mon Sep 17 00:00:00 2001 From: Samweli Date: Thu, 9 Feb 2023 14:13:04 +0300 Subject: [PATCH 1/2] catch exceptions when doing search --- .../pe_dailyimages_search_results_widget.py | 49 ++++++++++++++----- planet_explorer/pe_utils.py | 48 ++++++++---------- planet_explorer/tests/test_daily_imagery.py | 8 ++- 3 files changed, 63 insertions(+), 42 deletions(-) diff --git a/planet_explorer/gui/pe_dailyimages_search_results_widget.py b/planet_explorer/gui/pe_dailyimages_search_results_widget.py index d6ba784c..8910dc28 100644 --- a/planet_explorer/gui/pe_dailyimages_search_results_widget.py +++ b/planet_explorer/gui/pe_dailyimages_search_results_widget.py @@ -26,10 +26,12 @@ import iso8601 from qgis.core import ( + Qgis, QgsApplication, QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsGeometry, + QgsMessageLog, QgsProject, QgsRectangle, QgsWkbTypes, @@ -48,6 +50,8 @@ QTreeWidgetItemIterator, ) +from planet.api.exceptions import APIException, BadQuery + from ..gui.pe_results_configuration_dialog import ( PlanetNodeMetadata, ResultsConfigurationDialog, @@ -61,6 +65,7 @@ from ..pe_utils import ( PLANET_COLOR, + QGIS_LOG_SECTION_NAME, SEARCH_AOI_COLOR, area_coverage_for_image, create_preview_group, @@ -229,19 +234,39 @@ def update_request(self, request, local_filters): self.tree.clear() stats_request = {"interval": "year"} stats_request.update(self._request) - resp = self._p_client.stats(stats_request).get() - self._total_count = sum([b["count"] for b in resp["buckets"]]) - if self._total_count: - response = self._p_client.quick_search( - self._request, - page_size=TOP_ITEMS_BATCH, - sort=" ".join(self.sort_order()), + + try: + resp = self._p_client.stats(stats_request).get() + self._total_count = sum([b["count"] for b in resp["buckets"]]) + if self._total_count: + response = self._p_client.quick_search( + self._request, + page_size=TOP_ITEMS_BATCH, + sort=" ".join(self.sort_order()), + ) + self._response_iterator = response.iter() + self.load_more() + self._set_widgets_visibility(True) + else: + self._set_widgets_visibility(False) + + except BadQuery as bq: + QgsMessageLog.logMessage( + f"Exception during search, " + f"the search parameters are invalid." + f"Error details {bq}.", + QGIS_LOG_SECTION_NAME, + Qgis.Critical, + ) + + iface.messageBar().pushMessage( + "Planet Explorer", + "Encountered a problem during search, " + "Make sure search parameters are valid. " + "See logs for details", + level=Qgis.Critical, + duration=10, ) - self._response_iterator = response.iter() - self.load_more() - self._set_widgets_visibility(True) - else: - self._set_widgets_visibility(False) @waitcursor def load_more(self): diff --git a/planet_explorer/pe_utils.py b/planet_explorer/pe_utils.py index bb47f5a9..edb5861e 100644 --- a/planet_explorer/pe_utils.py +++ b/planet_explorer/pe_utils.py @@ -286,36 +286,28 @@ def create_preview_vector_layer(image): QgsField("sort_order", QVariant.String), ] - prop_dates = [ - 'acquired', - 'published', - 'updated' - ] - prop_int = [ - 'anomalous_pixels' - ] + prop_dates = ["acquired", "published", "updated"] + prop_int = ["anomalous_pixels"] prop_double = [ - 'clear_confidence_percent', - 'clear_percent', - 'cloud_cover', - 'cloud_percent', - 'ground_control_ratio', # Only SkySat - 'gsd', - 'heavy_haze_percent', - 'light_haze_percent', - 'pixel_resolution', - 'satellite_azimuth', - 'shadow_percent', - 'snow_ice_percent', - 'sun_azimuth', - 'sun_elevation', - 'view_angle', - 'visible_confidence_percent', - 'visible_percent' - ] - prop_boolean = [ - 'ground_control' # Only PlanetScope + "clear_confidence_percent", + "clear_percent", + "cloud_cover", + "cloud_percent", + "ground_control_ratio", # Only SkySat + "gsd", + "heavy_haze_percent", + "light_haze_percent", + "pixel_resolution", + "satellite_azimuth", + "shadow_percent", + "snow_ice_percent", + "sun_azimuth", + "sun_elevation", + "view_angle", + "visible_confidence_percent", + "visible_percent", ] + prop_boolean = ["ground_control"] # Only PlanetScope for prop in image["properties"]: # Determines the field types diff --git a/planet_explorer/tests/test_daily_imagery.py b/planet_explorer/tests/test_daily_imagery.py index dbf8c636..14a3b2ba 100644 --- a/planet_explorer/tests/test_daily_imagery.py +++ b/planet_explorer/tests/test_daily_imagery.py @@ -730,7 +730,9 @@ def test_aoi_bb_from_layer(layer_dir, expected_coordinates): ), ], ) -def test_aoi_from_multiple_polygons(qtbot, pe_qgis_iface, layer_dir, expected_coordinates, perform_selection): +def test_aoi_from_multiple_polygons( + qtbot, pe_qgis_iface, layer_dir, expected_coordinates, perform_selection +): """Tests the filter for the AOI read from no selection and a selection on a layer loaded in QGIS. AOI calculated from each polygon. """ @@ -794,7 +796,9 @@ def test_aoi_from_multiple_polygons(qtbot, pe_qgis_iface, layer_dir, expected_co ), ], ) -def test_bb_aoi_from_multiple_polygons(qtbot, pe_qgis_iface, layer_dir, expected_coordinates): +def test_bb_aoi_from_multiple_polygons( + qtbot, pe_qgis_iface, layer_dir, expected_coordinates +): """Tests the filter for the AOI read from on the bounding box of a layer loaded in QGIS. AOI calculated using a bounding box covering all polygons. """ From f926785b3e34749962cc1d7ffcb939ef8d938791 Mon Sep 17 00:00:00 2001 From: Samweli Date: Fri, 10 Feb 2023 22:04:50 +0300 Subject: [PATCH 2/2] fix lint --- planet_explorer/gui/pe_dailyimages_search_results_widget.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planet_explorer/gui/pe_dailyimages_search_results_widget.py b/planet_explorer/gui/pe_dailyimages_search_results_widget.py index 8910dc28..4cd5b56a 100644 --- a/planet_explorer/gui/pe_dailyimages_search_results_widget.py +++ b/planet_explorer/gui/pe_dailyimages_search_results_widget.py @@ -50,7 +50,7 @@ QTreeWidgetItemIterator, ) -from planet.api.exceptions import APIException, BadQuery +from planet.api.exceptions import BadQuery from ..gui.pe_results_configuration_dialog import ( PlanetNodeMetadata,