Skip to content

Commit

Permalink
Merge pull request #2 from IFRCGo/feature/add-transformation-for-glide
Browse files Browse the repository at this point in the history
Add transformer for Glide.
  • Loading branch information
emmanuelmathot authored Jan 12, 2025
2 parents 0c0f3da + 23ae3ec commit 0c75039
Show file tree
Hide file tree
Showing 7 changed files with 895 additions and 455 deletions.
6 changes: 3 additions & 3 deletions pystac_monty/sources/gdacs.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def make_items(self) -> list[Item]:
source_event_item = self.make_source_event_item()
items.append(source_event_item)

""" 2. Create the hazard item """
# """ 2. Create the hazard item """
hazard_event_item = self.make_hazard_event_item()
items.append(hazard_event_item)

Expand Down Expand Up @@ -367,7 +367,7 @@ def get_impact_category_from_sendai_b(
def get_impact_category_from_sendai_c(
sendainame: str,
) -> MontyImpactExposureCategory:
if sendainame == "houses damaged":
if sendainame == "houses damaged" or sendainame == "houses":
return MontyImpactExposureCategory.BUILDINGS
else:
raise ValueError(f"Unknown sendai name {sendainame} for indicators C")
Expand Down Expand Up @@ -422,7 +422,7 @@ def get_impact_type_from_sendai_b(sendainame: str) -> MontyImpactType:

@staticmethod
def get_impact_type_from_sendai_c(sendainame: str) -> MontyImpactType:
if sendainame == "houses damaged":
if sendainame == "houses damaged" or sendainame == "houses":
return MontyImpactType.DAMAGED
else:
raise ValueError(f"Unknown sendai name {sendainame} for indicators C")
Expand Down
120 changes: 120 additions & 0 deletions pystac_monty/sources/glide.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import json
from datetime import datetime
from typing import Any

import requests
from pystac import Collection, Item, Link
from shapely.geometry import Point, mapping

from pystac_monty.extension import MontyExtension
from pystac_monty.hazard_profiles import HazardProfiles
from pystac_monty.sources.common import MontyDataSource


class GlideDataSource(MontyDataSource):
def __init__(self, source_url: str, data: Any):
super().__init__(source_url, data)
self.data = json.loads(data)


class GlideTransformer:
"""
Transforms Glide event data into STAC Items
"""

hazard_profiles = HazardProfiles()

glide_events_collection_url = (
"https://github.com/IFRCGo/monty-stac-extension/raw/refs/heads/main/examples/glide-events/glide-events.json"
)

def __init__(self, data: GlideDataSource) -> None:
self.data = data

def make_items(self) -> list[Item]:
items = []

""" Create glide event items """
glide_events = self.make_event_items()
items.extend(glide_events)

return items

def make_event_items(self) -> list[Item]:
event_items = []
# validate data for glide transformation
glide_events = self.check_and_get_glide_events()

if not glide_events == []:
for data in glide_events:
glide_id = data.get("event") + "-" + data.get("number") + "-" + data.get("geocode")
latitude = float(data.get("latitude"))
longitude = float(data.get("longitude"))
event_date = {"year": data.get("year"), "month": data.get("month"), "day": data.get("day")}

point = Point(longitude, latitude)
geometry = mapping(point)
bbox = [longitude, latitude, longitude, latitude]

item = Item(
id=glide_id,
geometry=geometry,
bbox=bbox,
datetime=self.make_date(event_date),
properties={
"title": data.get("title", ""),
"description": data.get("comments", ""),
"magnitude": data.get("magnitude", ""),
"source": data.get("source", ""),
"docid": data.get("docid", ""),
"status": data.get("status", ""),
},
)

item.set_collection(self.get_event_collection())
item.properties["roles"] = ["source", "event"]

MontyExtension.add_to(item)
monty = MontyExtension.ext(item)
# Since there is no episode_number in glide data,
# we set it to 1 as it is required to create the correlation id
# in the method monty.compute_and_set_correlation_id(..)
monty.episode_number = 1
monty.hazard_codes = [data.get("event")]
monty.country_codes = [data.get("geocode")]

monty.compute_and_set_correlation_id(hazard_profiles=self.hazard_profiles)

item.add_link(Link("via", self.data.get_source_url(), "application/json", "Glide Event Data"))

event_items.append(item)

return event_items

def make_date(self, data: dict) -> datetime:
year = data["year"]
month = data["month"]
day = data["day"]

dt = datetime(year, month, day)
formatted_date = dt.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"

date = datetime.fromisoformat(formatted_date.replace("Z", "+00:00"))
return date

def get_event_collection(self) -> Collection:
response = requests.get(self.glide_events_collection_url)
collection_dict = json.loads(response.text)
return Collection.from_dict(collection_dict)

def check_and_get_glide_events(self) -> list[Any]:
glideset: list[Any] = self.data.get_data()["glideset"]
if glideset == []:
raise ValueError(f"No Glide data found in {self.data.get_source_url()}")
for obj in glideset:
required_fields = ["latitude", "longitude", "event", "number", "geocode"]
missing_fields = [field for field in required_fields if field not in obj]

if missing_fields:
raise ValueError(f"Missing required fields {missing_fields} in glide number {obj.get('number')}")
return glideset
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ interactions:
Content-Type:
- text/html; charset=utf-8
Date:
- Tue, 07 Jan 2025 15:07:01 GMT
- Wed, 08 Jan 2025 16:28:41 GMT
Location:
- https://raw.githubusercontent.com/IFRCGo/monty-stac-extension/refs/heads/main/examples/gdacs-events/gdacs-events.json
Referrer-Policy:
Expand All @@ -80,7 +80,7 @@ interactions:
X-Frame-Options:
- deny
X-GitHub-Request-Id:
- B21A:3D137C:3AC00B4:3C2871C:677D4315
- 7461:EBF00:2BBA15:2C7E28:677EA7B8
X-XSS-Protection:
- '0'
status:
Expand Down Expand Up @@ -132,11 +132,11 @@ interactions:
Cross-Origin-Resource-Policy:
- cross-origin
Date:
- Tue, 07 Jan 2025 15:07:01 GMT
- Wed, 08 Jan 2025 16:28:41 GMT
ETag:
- W/"7d50f8fb7c2a7c6341b790577d036dccb1a4fd14958bf3c9b7f5569dde9f2730"
Expires:
- Tue, 07 Jan 2025 15:12:01 GMT
- Wed, 08 Jan 2025 16:33:41 GMT
Source-Age:
- '0'
Strict-Transport-Security:
Expand All @@ -146,21 +146,21 @@ interactions:
Via:
- 1.1 varnish
X-Cache:
- HIT
- MISS
X-Cache-Hits:
- '0'
X-Content-Type-Options:
- nosniff
X-Fastly-Request-ID:
- ec22ffb8939fbff1d2e50785c26e3f27b7ecee4a
- edec2449f8ddeb91479725a2165bc7b7a1041a70
X-Frame-Options:
- deny
X-GitHub-Request-Id:
- 7080:1B667E:96155D:9C196F:677D3E6F
- ED9F:E901F:6B966:6EE60:677EA7B8
X-Served-By:
- cache-par-lfpg1960079-PAR
- cache-mrs10583-MRS
X-Timer:
- S1736262422.569080,VS0,VE165
- S1736353721.431842,VS0,VE352
X-XSS-Protection:
- 1; mode=block
status:
Expand Down Expand Up @@ -230,7 +230,7 @@ interactions:
Content-Type:
- text/html; charset=utf-8
Date:
- Tue, 07 Jan 2025 15:07:01 GMT
- Wed, 08 Jan 2025 16:28:41 GMT
Location:
- https://raw.githubusercontent.com/IFRCGo/monty-stac-extension/refs/heads/main/examples/gdacs-events/gdacs-events.json
Referrer-Policy:
Expand All @@ -247,7 +247,7 @@ interactions:
X-Frame-Options:
- deny
X-GitHub-Request-Id:
- B222:3E0D16:3A47076:3BAF605:677D4315
- 7501:F3C3F:2AEEFF:2BB389:677EA7B9
X-XSS-Protection:
- '0'
status:
Expand Down Expand Up @@ -299,11 +299,11 @@ interactions:
Cross-Origin-Resource-Policy:
- cross-origin
Date:
- Tue, 07 Jan 2025 15:07:01 GMT
- Wed, 08 Jan 2025 16:28:42 GMT
ETag:
- W/"7d50f8fb7c2a7c6341b790577d036dccb1a4fd14958bf3c9b7f5569dde9f2730"
Expires:
- Tue, 07 Jan 2025 15:12:01 GMT
- Wed, 08 Jan 2025 16:33:42 GMT
Source-Age:
- '0'
Strict-Transport-Security:
Expand All @@ -319,15 +319,15 @@ interactions:
X-Content-Type-Options:
- nosniff
X-Fastly-Request-ID:
- 61fa750d062e7803517fc78ec420ea1a05460554
- 70990105a06500dfde170eb13edfbef97cd6fb36
X-Frame-Options:
- deny
X-GitHub-Request-Id:
- 7080:1B667E:96155D:9C196F:677D3E6F
- ED9F:E901F:6B966:6EE60:677EA7B8
X-Served-By:
- cache-par-lfpg1960030-PAR
- cache-mrs10558-MRS
X-Timer:
- S1736262422.905105,VS0,VE6
- S1736353722.086844,VS0,VE1
X-XSS-Protection:
- 1; mode=block
status:
Expand Down Expand Up @@ -397,7 +397,7 @@ interactions:
Content-Type:
- text/html; charset=utf-8
Date:
- Tue, 07 Jan 2025 15:07:02 GMT
- Wed, 08 Jan 2025 16:28:42 GMT
Location:
- https://raw.githubusercontent.com/IFRCGo/monty-stac-extension/refs/heads/main/examples/gdacs-hazards/gdacs-hazards.json
Referrer-Policy:
Expand All @@ -414,7 +414,7 @@ interactions:
X-Frame-Options:
- deny
X-GitHub-Request-Id:
- B22E:3C9D:3A9676A:3BFEF25:677D4315
- 7561:116443:2B064D:2BCA35:677EA7BA
X-XSS-Protection:
- '0'
status:
Expand Down Expand Up @@ -465,11 +465,11 @@ interactions:
Cross-Origin-Resource-Policy:
- cross-origin
Date:
- Tue, 07 Jan 2025 15:07:02 GMT
- Wed, 08 Jan 2025 16:28:42 GMT
ETag:
- W/"0e1341982462438e4c8a456b2edbf7c112b9dd9d2b5ea3bca5598ad315ea569d"
Expires:
- Tue, 07 Jan 2025 15:12:02 GMT
- Wed, 08 Jan 2025 16:33:42 GMT
Source-Age:
- '0'
Strict-Transport-Security:
Expand All @@ -485,15 +485,15 @@ interactions:
X-Content-Type-Options:
- nosniff
X-Fastly-Request-ID:
- c9e40e77ca307f3d734c302ef1c0b51585c6b77f
- e5e3b534e87ba0d6a0ec658c14a99b2b8f44577d
X-Frame-Options:
- deny
X-GitHub-Request-Id:
- 4810:3DA8D1:96783D:9C9CE5:677D4312
- 6B32:EC7E7:6C0D0:6F5D5:677EA7BA
X-Served-By:
- cache-par-lfpg1960087-PAR
- cache-mrs1050091-MRS
X-Timer:
- S1736262423.588754,VS0,VE173
- S1736353723.571923,VS0,VE257
X-XSS-Protection:
- 1; mode=block
status:
Expand Down Expand Up @@ -554,35 +554,35 @@ interactions:
Cross-Origin-Resource-Policy:
- cross-origin
Date:
- Tue, 07 Jan 2025 15:07:02 GMT
- Wed, 08 Jan 2025 16:28:43 GMT
ETag:
- W/"5f6e1f3230195359862218fdea20141106751a1f1764da100ee01a376ddd4589"
Expires:
- Tue, 07 Jan 2025 15:12:02 GMT
- Wed, 08 Jan 2025 16:33:43 GMT
Source-Age:
- '202'
- '0'
Strict-Transport-Security:
- max-age=31536000
Vary:
- Authorization,Accept-Encoding,Origin
Via:
- 1.1 varnish
X-Cache:
- HIT
- MISS
X-Cache-Hits:
- '0'
X-Content-Type-Options:
- nosniff
X-Fastly-Request-ID:
- e8d1fc9beb8c5a37221532b0f3dc4ac524e5d64f
- 661152cc6c6e7413e8c07c72f5c603aa3b8e4610
X-Frame-Options:
- deny
X-GitHub-Request-Id:
- 621E:3D6372:91347C:971B5F:677D3A45
- 56F1:EC184:690A9:6C5B2:677EA7B8
X-Served-By:
- cache-par-lfpg1960042-PAR
- cache-mrs10532-MRS
X-Timer:
- S1736262423.887037,VS0,VE1
- S1736353723.031896,VS0,VE270
X-XSS-Protection:
- 1; mode=block
status:
Expand Down Expand Up @@ -643,13 +643,13 @@ interactions:
Cross-Origin-Resource-Policy:
- cross-origin
Date:
- Tue, 07 Jan 2025 15:07:03 GMT
- Wed, 08 Jan 2025 16:28:43 GMT
ETag:
- W/"5f6e1f3230195359862218fdea20141106751a1f1764da100ee01a376ddd4589"
Expires:
- Tue, 07 Jan 2025 15:12:03 GMT
- Wed, 08 Jan 2025 16:33:43 GMT
Source-Age:
- '202'
- '0'
Strict-Transport-Security:
- max-age=31536000
Vary:
Expand All @@ -663,15 +663,15 @@ interactions:
X-Content-Type-Options:
- nosniff
X-Fastly-Request-ID:
- 560e45e94b76da0b968efc9be43c40b90e880407
- 2179921ab52c7063d85b0f78163b8a318b8e8173
X-Frame-Options:
- deny
X-GitHub-Request-Id:
- 621E:3D6372:91347C:971B5F:677D3A45
- 56F1:EC184:690A9:6C5B2:677EA7B8
X-Served-By:
- cache-par-lfpg1960037-PAR
- cache-mrs1050109-MRS
X-Timer:
- S1736262423.032407,VS0,VE1
- S1736353724.538387,VS0,VE2
X-XSS-Protection:
- 1; mode=block
status:
Expand Down
Loading

0 comments on commit 0c75039

Please sign in to comment.