Skip to content

Commit

Permalink
Refactor code for improved readability: clean up whitespace, update e…
Browse files Browse the repository at this point in the history
…xception handling, and remove unused imports in geocoding and EMDAT modules.
  • Loading branch information
emmanuelmathot committed Jan 13, 2025
1 parent 4ee6744 commit c5b6072
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pystac_monty/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ class ImpactDetail(ABC):
Preferably used only in a Impact item. See the :stac-ext:`Monty Impact Detail Object
<monty#montyimpact_detail>` docs for details.
"""

properties: dict[str, Any]

def __init__(
Expand Down
30 changes: 12 additions & 18 deletions pystac_monty/geocoding.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]]:
Expand Down
4 changes: 2 additions & 2 deletions pystac_monty/sources/emdat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])))
Expand Down Expand Up @@ -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"])))
Expand Down
7 changes: 5 additions & 2 deletions tests/extensions/test_emdat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
),
)


Expand Down

0 comments on commit c5b6072

Please sign in to comment.