Skip to content

Commit a0c834c

Browse files
authored
Handle partial success for desinventar (#48)
* Handle partial success for desinventar - Implement pyndantic validator for desinventar transformer - Use generators to process desinventar events and impacts - Add pydantic * Update usage of geocoder * Update base transformer class - Update use of geocoder
2 parents 7dd4511 + 1e7a536 commit a0c834c

24 files changed

+518
-461
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ dependencies = [
3333
"pytz>=2021.1",
3434
"pandas>=2.2.0",
3535
"lxml>=5.3.0",
36+
"pydantic",
3637
]
3738
dynamic = ["version"]
3839

pystac_monty/geocoding.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(self, fgb_path: str, simplify_tolerance: float = 0.01) -> None:
3737
self._layer = "Layer1"
3838
self._simplify_tolerance = simplify_tolerance
3939
self._cache: Dict[str, Union[Dict[str, Any], int, None]] = {}
40-
self._file_handle = None
40+
self._file_handle: fiona.Collection | None = None
4141
self._initialize_path()
4242
self._open_file()
4343

pystac_monty/sources/common.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,52 @@
1+
import abc
12
import json
3+
import typing
24
from dataclasses import dataclass
3-
from typing import Any
45

56
import requests
6-
from pystac import Collection
7+
from pystac import Collection, Item
8+
9+
from pystac_monty.geocoding import MontyGeoCoder
710

811

912
@dataclass
1013
class MontyDataSource:
1114
source_url: str
12-
data: Any
15+
data: typing.Any
1316

14-
def __init__(self, source_url: str, data: Any):
17+
def __init__(self, source_url: str, data: typing.Any):
1518
self.source_url = source_url
1619
self.data = data
1720

1821
def get_source_url(self) -> str:
1922
return self.source_url
2023

21-
def get_data(self) -> Any:
24+
def get_data(self) -> typing.Any:
2225
return self.data
2326

2427

28+
DataSource = typing.TypeVar("DataSource")
29+
30+
2531
@dataclass
26-
class MontyDataTransformer:
27-
events_collection_id: str
28-
events_collection_url: str
29-
hazards_collection_id: str
30-
hazards_collection_url: str
31-
impacts_collection_id: str
32-
impacts_collection_url: str
32+
class MontyDataTransformer(typing.Generic[DataSource]):
33+
# FIXME: Add a validation in subclass so that source_name is always defined
34+
source_name: str
35+
36+
_event_collection_cache: Collection | None = None
37+
_hazard_collection_cache: Collection | None = None
38+
_impact_collection_cache: Collection | None = None
39+
40+
# FIXME: Get this from submodule
41+
base_collection_url = "https://github.com/IFRCGo/monty-stac-extension/raw/refs/heads/main/examples"
3342

3443
# FIXME: we might have to get ids and urls manually
35-
def __init__(self, source_name: str):
36-
self.events_collection_id = f"{source_name}-events"
37-
self.hazards_collection_id = f"{source_name}-hazards"
38-
self.impacts_collection_id = f"{source_name}-impacts"
44+
def __init__(self, data_source: DataSource, geocoder: MontyGeoCoder):
45+
self.events_collection_id = f"{self.source_name}-events"
46+
self.hazards_collection_id = f"{self.source_name}-hazards"
47+
self.impacts_collection_id = f"{self.source_name}-impacts"
48+
49+
self.data_source = data_source
3950

4051
self.events_collection_url = (
4152
f"{MontyDataTransformer.base_collection_url}/{self.events_collection_id}/{self.events_collection_id}.json"
@@ -47,10 +58,7 @@ def __init__(self, source_name: str):
4758
f"{MontyDataTransformer.base_collection_url}/{self.impacts_collection_id}/{self.impacts_collection_id}.json"
4859
)
4960

50-
_event_collection_cache = None
51-
_hazard_collection_cache = None
52-
_impact_collection_cache = None
53-
base_collection_url = "https://github.com/IFRCGo/monty-stac-extension/raw/refs/heads/main/examples"
61+
self.geocoder = geocoder
5462

5563
def get_event_collection(self) -> Collection:
5664
"""Get event collection"""
@@ -99,3 +107,6 @@ def get_impact_collection(self) -> Collection:
99107
collection.set_self_href(self.impacts_collection_url)
100108
self._impact_collection_cache = collection
101109
return self._impact_collection_cache
110+
111+
@abc.abstractmethod
112+
def make_items(self) -> list[Item]: ...

0 commit comments

Comments
 (0)