Skip to content

reproject and merge silently drop input DataArray attrs #1445

@brendancol

Description

@brendancol

Problem

Both reproject() and merge() in xrspatial/reproject/__init__.py build their output attrs from scratch instead of carrying the input attrs forward.

reproject() at lines ~672-697:

out_attrs = {
    'crs': tgt_wkt,
    'nodata': nd,
}

merge() at lines ~1473-1477:

attrs={
    'crs': tgt_wkt,
    'nodata': nd,
},

Anything attached to the input — units, long_name, description, scale_factor, add_offset, _FillValue, custom GeoTIFF tags, history, provenance — is dropped on the floor.

Impact

  • Downstream code that reads units (e.g. unit-aware plots, validators) sees nothing.
  • Packed datasets relying on scale_factor / add_offset lose the unpacking metadata. Any subsequent math is wrong.
  • _FillValue / nodata overrides set by users disappear.
  • Provenance text (history, description, long_name) is gone.

This is the same bug class as #1407 (sky_view_factor cellsize loss). Operations that transform a raster shouldn't strip every attr the user attached.

Fix

Merge input attrs first, then override the ones the operation legitimately changes:

out_attrs = {**raster.attrs}
out_attrs['crs'] = tgt_wkt
out_attrs['nodata'] = nd

A few input attrs become stale after reprojection and should be popped explicitly:

  • transform: the affine transform changed; the old one is wrong.
  • crs_wkt: we re-emit canonical crs, drop the duplicate.
  • res: refers to the old grid resolution.

For merge(), use rasters[0].attrs as the base (consistent with the default strategy='first'), and fall back name to rasters[0].name before 'merged'.

Tests

xrspatial/tests/test_reproject.py doesn't currently assert any attr survives the round-trip — only 'crs' in result.attrs. Add coverage so the next regression in this area gets caught.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions