Skip to content

Commit eea3aa9

Browse files
committed
post_dry_run Introduce _extract_spatial_extent_from_load_stac_item_collection
to allow caching
1 parent 2f10ca3 commit eea3aa9

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

openeogeotrellis/_backend/post_dry_run.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -275,19 +275,46 @@ def _extract_spatial_extent_from_constraint_load_stac(
275275
stac_url: str, *, constraint: dict, feature_flags: Optional[dict] = None
276276
) -> Union[None, AlignedExtentResult]:
277277
spatial_extent_from_pg = constraint.get("spatial_extent") or constraint.get("weak_spatial_extent")
278-
279-
extent_orig: Union[BoundingBox, None] = BoundingBox.from_dict_or_none(spatial_extent_from_pg, default_crs=4326)
280-
extent_variants = {"original": extent_orig}
281-
# TODO: improve logging: e.g. automatically include stac URL and what context we are in
282-
_log.info(f"_extract_spatial_extent_from_constraint_load_stac {stac_url=} {extent_orig=}")
283-
284278
spatiotemporal_extent = openeogeotrellis.load_stac._spatiotemporal_extent_from_load_params(
285279
spatial_extent=spatial_extent_from_pg,
286280
temporal_extent=constraint.get("temporal_extent") or (None, None),
287281
)
282+
288283
property_filter_pg_map = constraint.get("properties")
289284

290-
_log.info(f"Calling construct_item_collection for {stac_url=}")
285+
if (
286+
"resample" in constraint
287+
and (resample_crs := constraint["resample"].get("target_crs"))
288+
and (resample_resolution := constraint["resample"].get("resolution"))
289+
):
290+
resample_grid = _GridInfo(crs=resample_crs, resolution=resample_resolution)
291+
else:
292+
resample_grid = None
293+
294+
pixel_buffer_size = deep_get(constraint, "pixel_buffer", "buffer_size", default=None)
295+
296+
return _extract_spatial_extent_from_load_stac_item_collection(
297+
stac_url=stac_url,
298+
spatiotemporal_extent=spatiotemporal_extent,
299+
property_filter_pg_map=property_filter_pg_map,
300+
resample_grid=resample_grid,
301+
pixel_buffer_size=pixel_buffer_size,
302+
)
303+
304+
305+
def _extract_spatial_extent_from_load_stac_item_collection(
306+
*,
307+
stac_url: str,
308+
spatiotemporal_extent: Optional[openeogeotrellis.load_stac._SpatioTemporalExtent] = None,
309+
property_filter_pg_map: Optional[openeogeotrellis.load_stac.PropertyFilterPGMap] = None,
310+
resample_grid: Optional[_GridInfo] = None,
311+
pixel_buffer_size: Union[Tuple[float, float], int, float, None] = None,
312+
) -> Union[None, AlignedExtentResult]:
313+
extent_orig: Union[BoundingBox, None] = spatiotemporal_extent.spatial_extent.as_bbox()
314+
extent_variants = {"original": extent_orig}
315+
# TODO: improve logging: e.g. automatically include stac URL and what context we are in
316+
_log.info(f"_extract_spatial_extent_from_constraint_load_stac {stac_url=} {extent_orig=}")
317+
291318
item_collection, _, _, _ = openeogeotrellis.load_stac.construct_item_collection(
292319
url=stac_url,
293320
spatiotemporal_extent=spatiotemporal_extent,
@@ -337,27 +364,23 @@ def _extract_spatial_extent_from_constraint_load_stac(
337364
extent_variants["assets_covered_bbox"] = assets_covered_bbox
338365
extent_aligned = assets_covered_bbox or assets_full_bbox
339366

340-
if (
341-
"resample" in constraint
342-
and (resample_crs := constraint["resample"].get("target_crs"))
343-
and (resample_resolution := constraint["resample"].get("resolution"))
344-
):
367+
if resample_grid:
345368
to_resample = extent_orig or extent_aligned
346-
target_grid = _GridInfo(crs=resample_crs, resolution=resample_resolution)
369+
target_grid = resample_grid
347370
extent_resampled = to_resample.reproject(target_grid.crs_epsg).round_to_resolution(*target_grid.resolution)
348371
_log.debug(f"Resampled extent {to_resample=} into {extent_resampled=}")
349372
extent_variants["resampled"] = extent_resampled
350373
extent_aligned = extent_resampled
351374

352-
if target_grid and (pixel_buffer_size := deep_get(constraint, "pixel_buffer", "buffer_size", default=None)):
375+
if target_grid and pixel_buffer_size:
353376
extent_pixel_buffered = _buffer_extent(extent_aligned, buffer=pixel_buffer_size, sampling=target_grid)
354377
extent_variants["pixel_buffered"] = extent_pixel_buffered
355378
extent_aligned = extent_pixel_buffered
356379

357380
if not extent_aligned:
358381
# TODO: better way to handle this?
359382
_log.warning(
360-
f"No aligned extent could be determined for {stac_url=} with {constraint=} ({len(item_collection.items)=} {len(projection_metadatas)=}). Falling back on (non-aligned) {extent_orig=}."
383+
f"No aligned extent could be determined for {stac_url=} ({len(item_collection.items)=} {len(projection_metadatas)=}). Falling back on (non-aligned) {extent_orig=}."
361384
)
362385
extent_aligned = extent_orig
363386

0 commit comments

Comments
 (0)