-
Notifications
You must be signed in to change notification settings - Fork 85
Fix _write_vrt_tiled metadata drop vs to_geotiff (#1606) #1609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
brendancol
merged 2 commits into
main
from
deep-sweep-metadata-geotiff-2026-05-11-aa5f821c
May 11, 2026
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
xrspatial/geotiff/tests/test_vrt_tiled_metadata_1606.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| """Regression tests for issue #1606. | ||
|
|
||
| ``to_geotiff(da, 'out.vrt')`` (which dispatches to ``_write_vrt_tiled``) | ||
| used to drop a chunk of metadata that ``to_geotiff(da, 'out.tif')`` | ||
| preserved: | ||
|
|
||
| * ``attrs['nodatavals']`` / ``attrs['_FillValue']`` -- the VRT path | ||
| read ``attrs['nodata']`` directly instead of going through | ||
| ``_resolve_nodata_attr``. | ||
| * ``attrs['gdal_metadata']`` / ``attrs['gdal_metadata_xml']`` | ||
| * ``attrs['extra_tags']`` and the friendly tag attrs folded in by | ||
| ``_merge_friendly_extra_tags`` | ||
| * ``attrs['x_resolution']`` / ``attrs['y_resolution']`` / ``['resolution_unit']`` | ||
|
brendancol marked this conversation as resolved.
Outdated
|
||
| * ``attrs['raster_type']`` | ||
|
|
||
| Each tile under the VRT now carries the same rich tag set the | ||
| equivalent single-file ``.tif`` write would emit. | ||
| """ | ||
| import glob | ||
| import os | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
| import xarray as xr | ||
|
|
||
| from xrspatial.geotiff import open_geotiff, to_geotiff | ||
|
|
||
|
|
||
| def _make_rioxarray_style(arr=None): | ||
| """DataArray that looks like rioxarray output: nodata only via aliases.""" | ||
| if arr is None: | ||
| arr = np.arange(64, dtype=np.float32).reshape(8, 8) | ||
| arr[0, 0] = -9999.0 | ||
| return xr.DataArray( | ||
| arr, | ||
| dims=('y', 'x'), | ||
| coords={'y': np.arange(arr.shape[0], dtype=np.float64), | ||
| 'x': np.arange(arr.shape[1], dtype=np.float64)}, | ||
| attrs={ | ||
| # No bare 'nodata' key -- forces _resolve_nodata_attr to walk | ||
| # the alias chain. | ||
| 'nodatavals': (-9999.0,), | ||
| '_FillValue': -9999.0, | ||
| 'crs': 4326, | ||
| 'gdal_metadata': {'AREA_OR_POINT': 'Area', 'foo': 'bar'}, | ||
| 'x_resolution': 96, | ||
| 'y_resolution': 96, | ||
| 'resolution_unit': 'inch', | ||
| 'raster_type': 'point', | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| def _first_tile_path(vrt_path): | ||
| tiles_dir = vrt_path[:-len('.vrt')] + '_tiles' | ||
| tiles = sorted(glob.glob(os.path.join(tiles_dir, '*.tif'))) | ||
| assert tiles, f'no per-tile .tif files under {tiles_dir}' | ||
| return tiles[0] | ||
|
|
||
|
|
||
| class TestVrtTiledMetadataParity: | ||
| def test_nodatavals_alias_propagates_to_tiles(self, tmp_path): | ||
| da = _make_rioxarray_style() | ||
| vrt = str(tmp_path / 'nodatavals.vrt') | ||
| to_geotiff(da, vrt, tile_size=4) | ||
| tile_da = open_geotiff(_first_tile_path(vrt)) | ||
| # Before the fix this was None: _write_vrt_tiled read | ||
| # attrs['nodata'] directly and ignored the nodatavals alias. | ||
| assert tile_da.attrs.get('nodata') == -9999.0 | ||
|
|
||
| def test_fill_value_alias_propagates_to_tiles(self, tmp_path): | ||
| arr = np.arange(64, dtype=np.float32).reshape(8, 8) | ||
| arr[0, 0] = -9999.0 | ||
| da = xr.DataArray( | ||
| arr, dims=('y', 'x'), | ||
| coords={'y': np.arange(8.0), 'x': np.arange(8.0)}, | ||
| attrs={'_FillValue': -9999.0, 'crs': 4326}, | ||
| ) | ||
| vrt = str(tmp_path / 'fillvalue.vrt') | ||
| to_geotiff(da, vrt, tile_size=4) | ||
| tile_da = open_geotiff(_first_tile_path(vrt)) | ||
| assert tile_da.attrs.get('nodata') == -9999.0 | ||
|
|
||
| def test_gdal_metadata_propagates_to_tiles(self, tmp_path): | ||
| da = _make_rioxarray_style() | ||
| vrt = str(tmp_path / 'gdal_meta.vrt') | ||
| to_geotiff(da, vrt, tile_size=4) | ||
| tile_da = open_geotiff(_first_tile_path(vrt)) | ||
| gm = tile_da.attrs.get('gdal_metadata') | ||
| assert gm == {'AREA_OR_POINT': 'Area', 'foo': 'bar'} | ||
|
|
||
| def test_resolution_tags_propagate_to_tiles(self, tmp_path): | ||
| da = _make_rioxarray_style() | ||
| vrt = str(tmp_path / 'resolution.vrt') | ||
| to_geotiff(da, vrt, tile_size=4) | ||
| tile_da = open_geotiff(_first_tile_path(vrt)) | ||
| assert tile_da.attrs.get('x_resolution') == 96.0 | ||
| assert tile_da.attrs.get('y_resolution') == 96.0 | ||
| assert tile_da.attrs.get('resolution_unit') == 'inch' | ||
|
|
||
| def test_raster_type_point_propagates_to_tiles(self, tmp_path): | ||
| da = _make_rioxarray_style() | ||
| vrt = str(tmp_path / 'point.vrt') | ||
| to_geotiff(da, vrt, tile_size=4) | ||
| tile_da = open_geotiff(_first_tile_path(vrt)) | ||
| assert tile_da.attrs.get('raster_type') == 'point' | ||
|
|
||
| def test_tif_vs_vrt_tile_metadata_parity(self, tmp_path): | ||
| """Same DataArray, two destinations -- per-tile metadata matches.""" | ||
| da = _make_rioxarray_style() | ||
| tif_path = str(tmp_path / 'parity.tif') | ||
| vrt_path = str(tmp_path / 'parity.vrt') | ||
| to_geotiff(da, tif_path, tile_size=4) | ||
| to_geotiff(da, vrt_path, tile_size=4) | ||
|
|
||
| tif_da = open_geotiff(tif_path) | ||
| tile_da = open_geotiff(_first_tile_path(vrt_path)) | ||
|
|
||
| keys = ('nodata', 'gdal_metadata', 'raster_type', | ||
| 'x_resolution', 'y_resolution', 'resolution_unit') | ||
| for k in keys: | ||
| assert tif_da.attrs.get(k) == tile_da.attrs.get(k), ( | ||
| f'{k} mismatch: tif={tif_da.attrs.get(k)!r}, ' | ||
| f'vrt-tile={tile_da.attrs.get(k)!r}') | ||
|
|
||
|
|
||
| class TestVrtTiledMetadataDask: | ||
| def test_nodatavals_alias_dask(self, tmp_path): | ||
| pytest.importorskip('dask.array') | ||
| import dask.array as dska | ||
| arr = np.arange(64, dtype=np.float32).reshape(8, 8) | ||
| arr[0, 0] = -9999.0 | ||
| da_np = xr.DataArray( | ||
| arr, dims=('y', 'x'), | ||
| coords={'y': np.arange(8.0), 'x': np.arange(8.0)}, | ||
| attrs={'nodatavals': (-9999.0,), 'crs': 4326, | ||
| 'gdal_metadata': {'k': 'v'}}, | ||
| ) | ||
| # Dask-back the data so _write_vrt_tiled takes the dask branch. | ||
| da = xr.DataArray( | ||
| dska.from_array(arr, chunks=4), | ||
| dims=da_np.dims, coords=da_np.coords, attrs=da_np.attrs, | ||
| ) | ||
| vrt = str(tmp_path / 'dask.vrt') | ||
| to_geotiff(da, vrt, tile_size=4) | ||
| tile_da = open_geotiff(_first_tile_path(vrt)) | ||
| assert tile_da.attrs.get('nodata') == -9999.0 | ||
| assert tile_da.attrs.get('gdal_metadata') == {'k': 'v'} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.