Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion resource_catalogue_fastapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ async def order_item(
tag += "-" + projection
if coordinates:
logging.info(f"Coordinates found: {coordinates}")
tag += "-" + str(hashlib.md5(str(order_request.coordinates).encode("utf-8")).hexdigest())
tag += "-" + str(hashlib.md5(str(coordinates).encode("utf-8")).hexdigest())
tag = (
f"_{tag[1:].replace(' ', '-')}" # remove first character (hyphen), replace with underscore
)
Expand Down
30 changes: 30 additions & 0 deletions resource_catalogue_fastapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from botocore.exceptions import ClientError
from fastapi import Depends, HTTPException, Request
from kubernetes import client, config
from shapely.geometry import mapping, shape

logger = logging.getLogger(__name__) # Add this line to define the logger

Expand Down Expand Up @@ -255,10 +256,26 @@ def upload_stac_hierarchy_for_order(
item_title += " (Clipped)"
item_data["properties"]["title"] = item_title

# Set the created and updated timestamps
current_time = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
item_data["properties"]["created"] = current_time
item_data["properties"]["updated"] = current_time

# Update item coordinates with the intersection of the image and AOI coordinates
aoi_coords = order_options.get("coordinates", [])
if aoi_coords:
aoi_geometry = {"type": "Polygon", "coordinates": aoi_coords}
image_geometry = item_data.get("geometry", {})
if image_geometry:
intersect_geometry = coordinates_intersection(image_geometry, aoi_geometry)
if intersect_geometry:
item_data["geometry"] = intersect_geometry
else:
raise ValueError("No intersection found between image geometry and AOI coordinates")
else:
# Original geometry not found. Unlikely case but assume the AOI is valid.
item_data["geometry"] = aoi_geometry

# Fetch the STAC collection URL from the item links
collection_url = None
for link in item_data.get("links", []):
Expand Down Expand Up @@ -498,3 +515,16 @@ def decrypt_api_key(ciphertext_b64: str, otp_key_b64: str) -> str:
except Exception as e:
logging.error(f"Decryption failed: {e}")
return None


def coordinates_intersection(image_geometry: dict, aoi_geometry: dict) -> dict:
"""
Obtain the intersection between an image's geometry and an area of interest.
Geometry is expected to be in GeoJSON format.
"""
image_shape = shape(image_geometry)
aoi_shape = shape(aoi_geometry)
intersection = image_shape.intersection(aoi_shape)
if intersection.is_empty:
return []
return mapping(intersection)