From d30d5aac646b53ea4c3fb92200e7258ac444821e Mon Sep 17 00:00:00 2001 From: BRAUN REMI Date: Fri, 29 Nov 2024 10:51:41 +0100 Subject: [PATCH] FIX: Add a fallback in case `map-overlay.kml` is not readable for `Sentinel-1` data #180 --- CHANGES.md | 4 +++- CI/SCRIPTS/test_satellites.py | 6 ++++++ eoreader/products/sar/s1_product.py | 15 +++++---------- eoreader/products/sar/sar_product.py | 18 ++++++++++++++++-- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 8b402d1f..850099b1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,11 +1,13 @@ # Release History ## 0.21.8 (2024-mm-dd) -- FIX: Fix stack save_as_int to use updated int values + - ENH: Add a new type (`BandsType`) for list of BandType +- FIX: Fix stack `save_as_int` to use updated int values - by @TabeaW - FIX: Fixed PAZ Product Regex to properly indentify PAZ ST products as `PAZProduct` - by @guillemc23 - FIX: Fixed preprocessing graph paths in order to support relative paths in more complex environments or contexts - by @guillemc23 - FIX: Remove useless `_norm_diff` function `indices.py` +- FIX: Add a fallback in case `map-overlay.kml` is not readable for `Sentinel-1` data ([#180](https://github.com/sertit/eoreader/issues/180)) - DOC: Update `conf.py` (remove useless hunks and set Sphinx 7 as base) - DOC: Added the [PAZ product guide](https://earth.esa.int/eogateway/documents/20142/37627/PAZ-Image-Products-Guide.pdf) to the PAZ Product documentation instead of the TerraSAR-X one - by @guillemc23 diff --git a/CI/SCRIPTS/test_satellites.py b/CI/SCRIPTS/test_satellites.py index 5b979a94..b8b1fd5d 100644 --- a/CI/SCRIPTS/test_satellites.py +++ b/CI/SCRIPTS/test_satellites.py @@ -435,6 +435,12 @@ def _test_core( check_geometry(prod, "extent", tmp_dir) check_geometry(prod, "footprint", tmp_dir) + if hasattr(prod, "wgs84_extent"): + prod.wgs84_extent() + + if hasattr(prod, "_fallback_wgs84_extent"): + prod._fallback_wgs84_extent() + # Get the bands we want to stack / load stack_bands = [band for band in possible_bands if prod.has_band(band)] first_band = stack_bands[0] diff --git a/eoreader/products/sar/s1_product.py b/eoreader/products/sar/s1_product.py index 3a30ebc6..5118572e 100644 --- a/eoreader/products/sar/s1_product.py +++ b/eoreader/products/sar/s1_product.py @@ -255,17 +255,12 @@ def wgs84_extent(self) -> gpd.GeoDataFrame: # Open the KML file extent_wgs84 = vectors.read(preview_overlay) else: - raise InvalidProductError( - f"Impossible to find the map-overlay.kml in {self.path}" - ) + # raise to be caught by fallback + raise InvalidProductError - if extent_wgs84.empty: - raise InvalidProductError( - f"Cannot determine the WGS84 extent of {self.name}" - ) - - except Exception as ex: - raise InvalidProductError(ex) from ex + except Exception: + # Sometimes, map-overlay.kml of S1 products cannot be read properly + extent_wgs84 = self._fallback_wgs84_extent("preview/map-overlay.kml") return extent_wgs84 diff --git a/eoreader/products/sar/sar_product.py b/eoreader/products/sar/sar_product.py index 48511994..bb3f0b98 100644 --- a/eoreader/products/sar/sar_product.py +++ b/eoreader/products/sar/sar_product.py @@ -1024,7 +1024,16 @@ def _to_repr_constellation_specific(self) -> list: f"\torbit direction: {self.get_orbit_direction().value}", ] - def _fallback_wgs84_extent(self, extent_file_name: str): + def _fallback_wgs84_extent(self, extent_file_name: str = None) -> gpd.GeoDataFrame: + """ + Fallback for wgs84 extent (slower than to read a file, but should work in almsot any case) + + Args: + extent_file_name (str): Extent file name, if existing + + Returns: + gpd.GeoDataFrame: WGS84 extent + """ with rasterio.open(self.get_raw_band_paths()[self.get_default_band()]) as ds: if ds.crs is not None: extent_wgs84 = gpd.GeoDataFrame( @@ -1039,8 +1048,13 @@ def _fallback_wgs84_extent(self, extent_file_name: str): ) extent_wgs84 = gpd.GeoDataFrame(geometry=[extent_poly], crs=crs) else: + if extent_file_name: + name = f"({extent_file_name}) " + else: + name = "" + raise InvalidProductError( - f"Extent file ({extent_file_name}) not found in {self.path}. " + f"Extent file {name}not found in {self.path}. " "Default band isn't georeferenced and have no GCPs. " "It is therefore impossible to determine quickly the extent of this product. " "Please write an issue on GitHub!"