Fix _write_vrt_tiled metadata drop vs to_geotiff (#1606)#1609
Merged
brendancol merged 2 commits intoMay 11, 2026
Conversation
`to_geotiff(da, "out.vrt")` (which dispatches to `_write_vrt_tiled`) silently dropped a chunk of metadata that the matching `to_geotiff(da, "out.tif")` call preserved: * `attrs['nodatavals']` / `attrs['_FillValue']` -- the VRT path read `attrs['nodata']` directly instead of going through `_resolve_nodata_attr`, so a rioxarray-style DataArray round-tripped without its sentinel. * `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']` / `attrs['resolution_unit']` * `attrs['raster_type']` Pull the same rich-tag set `to_geotiff` already extracts (resolving nodata via the alias-aware helper) and thread it through `_write_single_tile` so every per-tile file under the VRT carries the same metadata it would have received from a single-file `.tif` write. Verified across numpy / cupy / dask / dask+cupy backends. Found by /sweep-metadata.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes backend inconsistencies in xrspatial.geotiff.to_geotiff when writing VRT tiled outputs by ensuring _write_vrt_tiled preserves the same metadata set that the single-file GeoTIFF path already propagates (notably alias-resolved nodata and rich GDAL/TIFF tags), closing #1606.
Changes:
- Update
_write_vrt_tiledto resolve nodata via_resolve_nodata_attrand propagate rich metadata (GDAL metadata, extra tags, resolution tags, raster type) into each per-tile GeoTIFF. - Extend
_write_single_tileto accept/forward the additional metadata kwargs to the underlyingwrite()call. - Add a regression test module covering VRT tile metadata parity and a dask-backed VRT tiled path.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
xrspatial/geotiff/__init__.py |
Propagates alias-resolved nodata and rich metadata through the VRT tiled writer by threading new kwargs into per-tile writes. |
xrspatial/geotiff/tests/test_vrt_tiled_metadata_1606.py |
Adds regression coverage for metadata propagation/parity for VRT-tiled outputs, including a dask-backed variant. |
.claude/sweep-metadata-state.csv |
Records the #1606 sweep finding/state entry. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…ct_rich_tags Three follow-ups from the Copilot review on PR #1609: 1. **Docstring typo** in `test_vrt_tiled_metadata_1606.py`: the module header listed `` `['resolution_unit']` `` instead of the proper `` `attrs['resolution_unit']` `` form. Fixed. 2. **Test coverage** for the previously-uncovered rich-tag paths: - `attrs['gdal_metadata_xml']` (pre-built XML string, bypasses the dict->XML builder) round-trips through `to_geotiff(..., '*.vrt')` and the per-tile reader. - `attrs['extra_tags']` entry (Software tag 305 ASCII) round-trips. - `attrs['image_description']` (friendly-tag attr folded into `extra_tags` as tag 270 by `_merge_friendly_extra_tags`) round-trips. 3. **Refactor**: the rich-tag extraction logic (`raster_type` mapping, `gdal_metadata_xml` build, `extra_tags` merge, `resolution_unit` string->id mapping with the `_unit_ids` dict) was duplicated across `to_geotiff`, `_write_vrt_tiled`, and `write_geotiff_gpu`. Lifted into a private `_extract_rich_tags(attrs) -> dict` helper that returns kwargs ready to splat into `write(...)`. All three callsites now use it. The string-to-id map for `resolution_unit` is also promoted to a module-level `_RESOLUTION_UNIT_IDS` constant so the helper does not rebuild it on every call. Tests: full `xrspatial/geotiff/tests/` suite (minus three pre-existing matplotlib palette failures unrelated to this PR) is 1179 passed.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Closes #1606.
to_geotiff(da, "out.vrt")-- which dispatches to_write_vrt_tiled-- silently dropped a chunk of metadata that the matchingto_geotiff(da, "out.tif")call preserved:attrs['nodatavals']/attrs['_FillValue']-- the VRT path readattrs['nodata']directly instead of routing through_resolve_nodata_attr. A rioxarray-style DataArray therefore lost its sentinel through.vrtbut kept it through.tif.attrs['gdal_metadata']/attrs['gdal_metadata_xml']attrs['extra_tags']and the friendly tag attrs folded in by_merge_friendly_extra_tagsattrs['x_resolution']/attrs['y_resolution']/attrs['resolution_unit']attrs['raster_type']Fix
Pull the same rich-tag set
to_geotiffalready extracts (resolving nodata via the alias-aware_resolve_nodata_attrhelper added in #1582) and thread it through_write_single_tileso every per-tile file carries the same metadata that the single-file.tifwriter emits._write_single_tilenow accepts the extra kwargs and forwards them to the underlyingwrite()call.Test plan
test_vrt_tiled_metadata_1606.pycoversnodatavals,_FillValue,gdal_metadata,x_resolution/y_resolution/resolution_unit, andraster_typepropagation, plus a TIF-vs-VRT-tile parity test and a dask-backed varianttest_vrt_write.py+test_vrt_tiled_metadata_1606.pypassxrspatial/geotiff/tests/suite: 1180 passed (3 pre-existing matplotlib palette failures unrelated to this change)Found by /sweep-metadata.