|
| 1 | +"""Tests for CRS metadata embedding in Zarr attributes.""" |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | +import xarray as xr |
| 6 | + |
| 7 | +from eopf_geozarr.conversion.geozarr import prepare_dataset_with_crs_info |
| 8 | + |
| 9 | +EPSG_UTM32N = 32632 |
| 10 | +CRS_UTM32N = f"EPSG:{EPSG_UTM32N}" |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def utm_dataset(): |
| 15 | + """Minimal UTM Zone 32N dataset.""" |
| 16 | + return xr.Dataset( |
| 17 | + {"data": (["y", "x"], np.random.rand(10000, 11000))}, |
| 18 | + coords={"x": np.arange(0, 110000, 10), "y": np.arange(5300000, 5200000, -10)}, |
| 19 | + ) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def zarr_roundtrip(utm_dataset, tmp_path): |
| 24 | + """CRS-prepared dataset after Zarr write/read cycle.""" |
| 25 | + ds = prepare_dataset_with_crs_info(utm_dataset, reference_crs=CRS_UTM32N) |
| 26 | + path = tmp_path / "test.zarr" |
| 27 | + ds.drop_encoding().to_zarr(path, mode="w", zarr_format=3, consolidated=True) |
| 28 | + return xr.open_zarr(path, zarr_format=3, consolidated=True) |
| 29 | + |
| 30 | + |
| 31 | +def test_crs_attrs_embedded(utm_dataset): |
| 32 | + """EPSG metadata written to dataset attrs.""" |
| 33 | + ds = prepare_dataset_with_crs_info(utm_dataset, reference_crs=CRS_UTM32N) |
| 34 | + assert ds.attrs["proj:epsg"] == EPSG_UTM32N |
| 35 | + assert ds.attrs["proj:code"] == CRS_UTM32N |
| 36 | + |
| 37 | + |
| 38 | +def test_crs_persists_through_zarr(zarr_roundtrip): |
| 39 | + """CRS metadata survives Zarr write/read.""" |
| 40 | + assert zarr_roundtrip.attrs["proj:epsg"] == EPSG_UTM32N |
| 41 | + assert zarr_roundtrip.attrs["proj:code"] == CRS_UTM32N |
| 42 | + |
| 43 | + |
| 44 | +def test_rioxarray_reads_crs(zarr_roundtrip): |
| 45 | + """rioxarray reads CRS from Zarr metadata.""" |
| 46 | + assert zarr_roundtrip.rio.crs.to_epsg() == EPSG_UTM32N |
| 47 | + |
| 48 | + |
| 49 | +def test_utm_bounds_preserved(zarr_roundtrip): |
| 50 | + """Bounds remain UTM meters (not lat/lon degrees).""" |
| 51 | + left, bottom, right, top = zarr_roundtrip.rio.bounds() |
| 52 | + assert left < 200000 and right < 200000 # easting in valid UTM range |
| 53 | + assert bottom > 5000000 and top > 5000000 # northing in valid UTM range |
0 commit comments