|
2 | 2 |
|
3 | 3 | from pystac_monty.hazard_profiles import HazardProfiles |
4 | 4 |
|
| 5 | +from .geo_blocks import GeoBlocks |
| 6 | + |
5 | 7 |
|
6 | 8 | class Pairing: |
| 9 | + """Generation of correlation id for Events Pairing""" |
| 10 | + |
| 11 | + def _return_bbox_centroid_coordinates(self, bbox: list): |
| 12 | + """Returns the centroid of the bbox""" |
| 13 | + return [round((bbox[1] + bbox[3]) / 2.0, 1), round((bbox[0] + bbox[2]) / 2.0, 1)] # [lat, lon] |
| 14 | + |
7 | 15 | def generate_correlation_id(self, item: Item, hazard_profiles: HazardProfiles) -> str: |
| 16 | + """Generate the correlation ID for events pairing""" |
8 | 17 | # Get the necessary properties for creating the correlation id |
9 | 18 | hazards = item.properties.get("monty:hazard_codes", []) |
10 | 19 | country_codes = item.properties.get("monty:country_codes", []) |
11 | 20 | event_datetime = item.datetime |
12 | 21 | episode_number = item.properties.get("monty:episode_number", 0) |
| 22 | + geometry_lat_lon = self._return_bbox_centroid_coordinates(item.bbox) |
13 | 23 |
|
14 | | - if not hazards or not country_codes or not event_datetime or not episode_number: |
| 24 | + if not hazards or not country_codes or not event_datetime or not episode_number or not geometry_lat_lon: |
15 | 25 | raise ValueError("Missing required properties to generate correlation id") |
16 | 26 |
|
17 | 27 | hazard_cluster_code = hazard_profiles.get_canonical_hazard_codes(item)[0].upper() |
18 | 28 | # This should be dynamically determined based on existing events |
19 | 29 | eventdatestr = event_datetime.strftime("%Y%m%d") |
20 | 30 |
|
21 | | - event_id = f"{eventdatestr}-{country_codes[0]}-{hazard_cluster_code}-{episode_number}-GCDB" # noqa: E501 |
| 31 | + geoblocks_df = GeoBlocks.get_geoblocks_df() |
| 32 | + geoblocks_filtered_df = geoblocks_df[ |
| 33 | + (geoblocks_df["lat_min"] <= geometry_lat_lon[0]) |
| 34 | + & (geoblocks_df["lat_max"] > geometry_lat_lon[0]) |
| 35 | + & (geoblocks_df["lon_min"] <= geometry_lat_lon[1]) |
| 36 | + & (geoblocks_df["lon_max"] > geometry_lat_lon[1]) |
| 37 | + ] |
| 38 | + block_id = int(geoblocks_filtered_df["block_id"].iloc[0]) if len(geoblocks_filtered_df) else -1 |
| 39 | + |
| 40 | + event_id = f"{eventdatestr}-{country_codes[0]}-{block_id}-{hazard_cluster_code}-{episode_number}-GCDB" # noqa: E501 |
22 | 41 | return event_id |
0 commit comments