Skip to content

preview() copies stale res attribute to the downsampled output #3569

Description

@brendancol

Description

.xrs.preview() downsamples a raster and rebuilds the x/y coordinates for the smaller grid, but copies the input's attrs unchanged. So attrs['res'] keeps describing the original full-resolution grid instead of the downsampled one. Any downstream code that reads res for pixel-size math or georeferencing gets the wrong value.

To Reproduce

study_area.xrs.generate_terrain().xrs.preview()
<xarray.DataArray 'preview' (y: 988, x: 1000)>
Coordinates:
  * y        (y) float64 8kB 3.37e+06 3.369e+06 ... 3.329e+06 3.329e+06
  * x        (x) float64 8kB 6.005e+05 6.005e+05 ... 6.414e+05 6.415e+05
Attributes:
    res:                (0.25, 0.25)
    crs:                32614
    ...

The x coordinates span about 41000 map units across 1000 pixels, so the real resolution is roughly 41 units/pixel. But res still reads (0.25, 0.25) from the source grid.

Expected behavior

res should match the grid that comes back. Recompute it from the output coordinate spacing so it lines up with the returned array's x/y coordinates.

Notes

The stale copy is in xrspatial/preview.py. Every output path rebuilds coords with _interpolate_coords but passes attrs=agg.attrs, and the main preview() re-assigns result.attrs = agg.attrs after the second-pass refinement. The fix recomputes res from the final coordinates. It affects every method (mean, median, max, min, nearest, bilinear) and all backends.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions