|
17 | 17 | from botocore.exceptions import ClientError |
18 | 18 | from fastapi import Depends, HTTPException, Request |
19 | 19 | from kubernetes import client, config |
| 20 | +from shapely.geometry import mapping, shape |
20 | 21 |
|
21 | 22 | logger = logging.getLogger(__name__) # Add this line to define the logger |
22 | 23 |
|
@@ -255,10 +256,26 @@ def upload_stac_hierarchy_for_order( |
255 | 256 | item_title += " (Clipped)" |
256 | 257 | item_data["properties"]["title"] = item_title |
257 | 258 |
|
| 259 | + # Set the created and updated timestamps |
258 | 260 | current_time = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ") |
259 | 261 | item_data["properties"]["created"] = current_time |
260 | 262 | item_data["properties"]["updated"] = current_time |
261 | 263 |
|
| 264 | + # Update item coordinates with the intersection of the image and AOI coordinates |
| 265 | + aoi_coords = order_options.get("coordinates", []) |
| 266 | + if aoi_coords: |
| 267 | + aoi_geometry = {"type": "Polygon", "coordinates": [aoi_coords]} |
| 268 | + image_geometry = item_data.get("geometry", {}) |
| 269 | + if image_geometry: |
| 270 | + intersect_geometry = coordinates_intersection(image_geometry, aoi_geometry) |
| 271 | + if intersect_geometry: |
| 272 | + item_data["geometry"] = intersect_geometry |
| 273 | + else: |
| 274 | + raise ValueError("No intersection found between image geometry and AOI coordinates") |
| 275 | + else: |
| 276 | + # Original geometry not found. Unlikely case but assume the AOI is valid. |
| 277 | + item_data["geometry"] = aoi_geometry |
| 278 | + |
262 | 279 | # Fetch the STAC collection URL from the item links |
263 | 280 | collection_url = None |
264 | 281 | for link in item_data.get("links", []): |
@@ -498,3 +515,16 @@ def decrypt_api_key(ciphertext_b64: str, otp_key_b64: str) -> str: |
498 | 515 | except Exception as e: |
499 | 516 | logging.error(f"Decryption failed: {e}") |
500 | 517 | return None |
| 518 | + |
| 519 | + |
| 520 | +def coordinates_intersection(image_geometry: dict, aoi_geometry: dict) -> dict: |
| 521 | + """ |
| 522 | + Obtain the intersection between an image's geometry and an area of interest. |
| 523 | + Geometry is expected to be in GeoJSON format. |
| 524 | + """ |
| 525 | + image_shape = shape(image_geometry) |
| 526 | + aoi_shape = shape(aoi_geometry) |
| 527 | + intersection = image_shape.intersection(aoi_shape) |
| 528 | + if intersection.is_empty: |
| 529 | + return [] |
| 530 | + return mapping(intersection) |
0 commit comments