From c5b6072344d327b50164a2f81c8202ec7bc95b20 Mon Sep 17 00:00:00 2001 From: Emmanuel Mathot Date: Mon, 13 Jan 2025 22:39:43 +0100 Subject: [PATCH] Refactor code for improved readability: clean up whitespace, update exception handling, and remove unused imports in geocoding and EMDAT modules. --- pystac_monty/extension.py | 2 +- pystac_monty/geocoding.py | 30 ++++++++++++------------------ pystac_monty/sources/emdat.py | 4 ++-- tests/extensions/test_emdat.py | 7 +++++-- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/pystac_monty/extension.py b/pystac_monty/extension.py index 532813e..5f9de3e 100644 --- a/pystac_monty/extension.py +++ b/pystac_monty/extension.py @@ -348,7 +348,7 @@ class ImpactDetail(ABC): Preferably used only in a Impact item. See the :stac-ext:`Monty Impact Detail Object ` docs for details. """ - + properties: dict[str, Any] def __init__( diff --git a/pystac_monty/geocoding.py b/pystac_monty/geocoding.py index 4b3e74c..820bb95 100644 --- a/pystac_monty/geocoding.py +++ b/pystac_monty/geocoding.py @@ -1,7 +1,7 @@ import json import zipfile from abc import ABC, abstractmethod -from typing import Any, Dict, List, Optional, Union, Set +from typing import Any, Dict, List, Optional, Set, Union import fiona # type: ignore from shapely.geometry import mapping, shape # type: ignore @@ -17,8 +17,10 @@ def get_geometry_from_admin_units(self, admin_units: str) -> Optional[Dict[str, def get_geometry_by_country_name(self, country_name: str) -> Optional[Dict[str, Any]]: pass + GAUL2014_2015_GPCK_ZIP = "gaul2014_2015.gpkg" + class GAULGeocoder(MontyGeoCoder): """ Implementation of MontyGeoCoder using GAUL geopackage for geocoding. @@ -54,7 +56,7 @@ def _initialize_path(self) -> None: def _is_zip_file(self, file_path: str) -> bool: """Check if a file is a ZIP file""" try: - with zipfile.ZipFile(file_path, "r") as zf: + with zipfile.ZipFile(file_path, "r"): return True except zipfile.BadZipFile: return False @@ -254,30 +256,22 @@ def __init__(self) -> None: [ [-9.0, 36.0], # Southwest [-9.0, 44.0], # Northwest - [3.0, 44.0], # Northeast - [3.0, 36.0], # Southeast - [-9.0, 36.0] # Close polygon + [3.0, 44.0], # Northeast + [3.0, 36.0], # Southeast + [-9.0, 36.0], # Close polygon ] - ] + ], }, - "bbox": [-9.0, 36.0, 3.0, 44.0] + "bbox": [-9.0, 36.0, 3.0, 44.0], }, # Test admin unit geometry "admin1": { "geometry": { "type": "Polygon", - "coordinates": [ - [ - [-2.0, 40.0], - [-2.0, 42.0], - [0.0, 42.0], - [0.0, 40.0], - [-2.0, 40.0] - ] - ] + "coordinates": [[[-2.0, 40.0], [-2.0, 42.0], [0.0, 42.0], [0.0, 40.0], [-2.0, 40.0]]], }, - "bbox": [-2.0, 40.0, 0.0, 42.0] - } + "bbox": [-2.0, 40.0, 0.0, 42.0], + }, } def get_geometry_from_admin_units(self, admin_units: str) -> Optional[Dict[str, Any]]: diff --git a/pystac_monty/sources/emdat.py b/pystac_monty/sources/emdat.py index ea6a47d..f6d621e 100644 --- a/pystac_monty/sources/emdat.py +++ b/pystac_monty/sources/emdat.py @@ -417,7 +417,7 @@ def _create_title_from_row(self, row: pd.Series) -> str | None: try: month_name = datetime(2000, int(row["Start Month"]), 1).strftime("%B") date_components.append(month_name) - except: + except ValueError: pass # Add year date_components.append(str(int(row["Start Year"]))) @@ -458,7 +458,7 @@ def _create_description_from_row(self, row: pd.Series) -> str: try: month_name = datetime(2000, int(row["Start Month"]), 1).strftime("%B") date_components.append(month_name) - except: + except ValueError: pass # Add year date_components.append(str(int(row["Start Year"]))) diff --git a/tests/extensions/test_emdat.py b/tests/extensions/test_emdat.py index 7f29d7a..aababca 100644 --- a/tests/extensions/test_emdat.py +++ b/tests/extensions/test_emdat.py @@ -9,7 +9,7 @@ from parameterized import parameterized from pystac_monty.extension import MontyExtension -from pystac_monty.geocoding import GAULGeocoder, MockGeocoder +from pystac_monty.geocoding import MockGeocoder from pystac_monty.sources.emdat import EMDATDataSource, EMDATTransformer from tests.conftest import get_data_file from tests.extensions.test_monty import CustomValidator @@ -41,7 +41,10 @@ def load_scenarios( spain_flood = ( "spain_flood", - "https://github.com/IFRCGo/monty-stac-extension/raw/refs/heads/EMDAT/model/sources/EM-DAT/public_emdat_custom_request_2025-01-13_4cf1ccf1-9f6e-41a3-9aec-0a19903febae.xlsx", + ( + "https://github.com/IFRCGo/monty-stac-extension/raw/refs/heads/EMDAT/model/sources/" + "EM-DAT/public_emdat_custom_request_2025-01-13_4cf1ccf1-9f6e-41a3-9aec-0a19903febae.xlsx" + ), )