Skip to content

Commit 0397f40

Browse files
Merge pull request #31 from EO-DataHub/bugfix/EODHP-1401-clipped-ordered-item-geometry-does-not-match-expected
EODHP-1401 clipped ordered item geometry does not match expected
2 parents 9029c63 + e5f134f commit 0397f40

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

resource_catalogue_fastapi/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ async def order_item(
722722
tag += "-" + projection
723723
if coordinates:
724724
logging.info(f"Coordinates found: {coordinates}")
725-
tag += "-" + str(hashlib.md5(str(order_request.coordinates).encode("utf-8")).hexdigest())
725+
tag += "-" + str(hashlib.md5(str(coordinates).encode("utf-8")).hexdigest())
726726
tag = (
727727
f"_{tag[1:].replace(' ', '-')}" # remove first character (hyphen), replace with underscore
728728
)

resource_catalogue_fastapi/utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from botocore.exceptions import ClientError
1818
from fastapi import Depends, HTTPException, Request
1919
from kubernetes import client, config
20+
from shapely.geometry import mapping, shape
2021

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

@@ -255,10 +256,26 @@ def upload_stac_hierarchy_for_order(
255256
item_title += " (Clipped)"
256257
item_data["properties"]["title"] = item_title
257258

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

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+
262279
# Fetch the STAC collection URL from the item links
263280
collection_url = None
264281
for link in item_data.get("links", []):
@@ -498,3 +515,16 @@ def decrypt_api_key(ciphertext_b64: str, otp_key_b64: str) -> str:
498515
except Exception as e:
499516
logging.error(f"Decryption failed: {e}")
500517
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

Comments
 (0)