Skip to content

Commit 3cb4f96

Browse files
Merge pull request #13 from IFRCGo/feature/add-transform-support-for-json
Add support for json in emdat transformation.
2 parents a1586d3 + 08cb78b commit 3cb4f96

18 files changed

+36376
-35267
lines changed

pystac_monty/sources/emdat.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from datetime import datetime
44
from typing import List, Optional, Union
55

6+
import numpy as np
67
import pandas as pd
78
import pytz
89
import requests
@@ -20,6 +21,7 @@
2021
from pystac_monty.geocoding import MontyGeoCoder
2122
from pystac_monty.hazard_profiles import MontyHazardProfiles
2223
from pystac_monty.sources.common import MontyDataSource
24+
from pystac_monty.utils import rename_columns
2325

2426
STAC_EVENT_ID_PREFIX = "emdat-event-"
2527
STAC_HAZARD_ID_PREFIX = "emdat-hazard-"
@@ -39,8 +41,13 @@ def __init__(self, source_url: str, data: Union[str, pd.DataFrame]):
3941
self.df = pd.read_excel(data)
4042
elif isinstance(data, pd.DataFrame):
4143
self.df = data
44+
elif isinstance(data, dict):
45+
# If data is a dict, assume it's Json content
46+
data = data["data"]["public_emdat"]["data"]
47+
df = pd.DataFrame(data)
48+
self.df = rename_columns(df)
4249
else:
43-
raise ValueError("Data must be either Excel content (str) or pandas DataFrame")
50+
raise ValueError("Data must be either Excel content (str) or pandas DataFrame or Json")
4451

4552
def get_data(self) -> pd.DataFrame:
4653
return self.df
@@ -125,7 +132,7 @@ def _create_event_item_from_row(self, row: pd.Series) -> Optional[Item]:
125132
bbox = None
126133

127134
# 1. Try admin units first if geocoder is available
128-
if self.geocoder and not pd.isna(row.get("Admin Units")):
135+
if self.geocoder and np.any(pd.notna(row.get("Admin Units"))):
129136
geom_data = self.geocoder.get_geometry_from_admin_units(row.get("Admin Units"))
130137
if geom_data:
131138
geometry = geom_data["geometry"]

pystac_monty/sources/idu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class IDUTransformer:
4646
"""Transform the source data into the STAC items"""
4747

4848
idu_events_collection_id = "idu-events"
49-
idu_events_collection_url = "https://raw.githubusercontent.com/IFRCGo/monty-stac-extension/refs/heads/feature/update-idu-documentation/examples/idu-events/idu-events.json" # noqa
49+
idu_events_collection_url = "https://raw.githubusercontent.com/IFRCGo/monty-stac-extension/refs/heads/main/examples/idmc-idu-events/idmc-idu-events.json" # noqa
5050
idu_impacts_collection_id = "idu-impacts"
5151
idu_impacts_collection_url = "https://raw.githubusercontent.com/IFRCGo/monty-stac-extension/refs/heads/feature/update-idu-documentation/examples/idu-impacts/idu-impacts.json" # noqa
5252

pystac_monty/utils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def rename_columns(df):
2+
"""Rename columns of a pandas DataFrame"""
3+
return df.rename(
4+
columns={
5+
"disno": "DisNo.",
6+
"admin_units": "Admin Units",
7+
"latitude": "Latitude",
8+
"longitude": "Longitude",
9+
"country": "Country",
10+
"classif_key": "Classification Key",
11+
"iso": "ISO",
12+
"total_deaths": "Total Deaths",
13+
"no_injured": "No Injured",
14+
"no_affected": "No Affected",
15+
"no_homeless": "No Homeless",
16+
"total_affected": "Total Affected",
17+
"total_dam": "Total Damages ('000 US$)",
18+
"start_year": "Start Year",
19+
"start_month": "Start Month",
20+
"start_day": "Start Day",
21+
"end_year": "End Year",
22+
"end_month": "End Month",
23+
"end_day": "End Day",
24+
"magnitude": "Magnitude",
25+
"magnitude_scale": "Magnitude Sacle",
26+
"name": "Event Name",
27+
"type": "Disaster Type",
28+
"subtype": "Disaster Subtype",
29+
"location": "Location",
30+
}
31+
)

0 commit comments

Comments
 (0)