Skip to content

Commit 62416ef

Browse files
committed
Fix STAC coordinate system and dimension detection
Corrected a metadata bug where projected coordinates (e.g., in meters for HRRR) were incorrectly labeled as EPSG:4326 (degrees) in the STAC Datacube extension. Added explicit CRS extraction via rioxarray and auto-detection of x/y dimension names to ensure the catalog accurately reflects the dataset's coordinate system.
1 parent 660d312 commit 62416ef

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

examples/build_dynamical_catalog_full.ipynb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,16 @@
222222
" # 1. BBox via rioxarray\n",
223223
" bbox = extract_spatial_extent_rio(ds)\n",
224224
" \n",
225-
" # 2. Temporal\n",
225+
" # 2. Dimensions & CRS\n",
226226
" time_dim = next((d for d in [\"time\", \"init_time\"] if d in ds.dims), None)\n",
227+
" x_dim = next((d for d in [\"lon\", \"longitude\", \"x\"] if d in ds.dims), None)\n",
228+
" y_dim = next((d for d in [\"lat\", \"latitude\", \"y\"] if d in ds.dims), None)\n",
229+
" \n",
230+
" try:\n",
231+
" ref_sys = ds.rio.crs.to_epsg() or ds.rio.crs.to_wkt()\n",
232+
" except Exception:\n",
233+
" ref_sys = 4326\n",
234+
"\n",
227235
" start_dt = end_dt = None\n",
228236
" if time_dim:\n",
229237
" start_dt = datetime.fromisoformat(str(ds[time_dim].min().values[()]).split(\".\")[0])\n",
@@ -277,7 +285,15 @@
277285
" # 6. Datacube Extension via xstac (if available)\n",
278286
" try:\n",
279287
" import xstac\n",
280-
" item = xstac.xarray_to_stac(ds, item, validate=False)\n",
288+
" item = xstac.xarray_to_stac(\n",
289+
" ds, \n",
290+
" item, \n",
291+
" temporal_dimension=time_dim, \n",
292+
" x_dimension=x_dim, \n",
293+
" y_dimension=y_dim, \n",
294+
" reference_system=ref_sys,\n",
295+
" validate=False\n",
296+
" )\n",
281297
" except ImportError:\n",
282298
" cube = build_datacube_extension(ds, start_dt.isoformat() + \"Z\" if start_dt else None, end_dt.isoformat() + \"Z\" if end_dt else None)\n",
283299
" item.properties.update(cube)\n",

scripts/build_dynamical_stac.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,22 @@ def build_item_for_store(
433433

434434
temporal_dim = detect_temporal_dimension(ds)
435435

436+
# Detect horizontal dimension names and CRS for xstac
437+
x_dim = y_dim = None
438+
for x in ["lon", "longitude", "x"]:
439+
for y in ["lat", "latitude", "y"]:
440+
if x in ds.dims and y in ds.dims:
441+
x_dim, y_dim = x, y
442+
break
443+
if x_dim: break
444+
445+
ref_sys = 4326
446+
try:
447+
if hasattr(ds, "rio") and ds.rio.crs:
448+
ref_sys = ds.rio.crs.to_epsg() or ds.rio.crs.to_wkt()
449+
except Exception:
450+
pass
451+
436452
description = entry.get("Description", "").strip()
437453
description += xarray_open_snippet(item_id, catalog_url)
438454

@@ -457,6 +473,9 @@ def build_item_for_store(
457473
providers=[DYNAMICAL_PROVIDER],
458474
virtual=False,
459475
temporal_dimension=temporal_dim,
476+
x_dimension=x_dim,
477+
y_dimension=y_dim,
478+
reference_system=ref_sys,
460479
)
461480
except Exception as exc:
462481
log.warning(" Failed to build STAC item for %s: %s", store_uri, exc)

0 commit comments

Comments
 (0)