Skip to content

generate_terrain() drops caller's coords, res, and crs #3474

Description

@brendancol

Describe the bug

generate_terrain() throws away the caller's spatial reference. When the input
DataArray already has coordinates and geo-attributes (crs, res, units), the
output drops them and builds a synthetic grid from x_range/y_range (default
(0, 500)) instead.

It's easiest to hit through the .xrs accessor, where you pass your own
georeferenced raster:

my_attrs = {'res': (30, 30), 'crs': 'EPSG:5070', 'units': 'meters'}
xarr = xr.DataArray(arr, coords={'y': ys, 'x': xs}, dims=('y', 'x'),
                    name='study_area', attrs=my_attrs)

terrain = xarr.xrs.generate_terrain()

xarr has y/x coords on a 30 m grid, res=(30, 30), and crs='EPSG:5070'. The
result comes back as:

Coordinates:
  * y        (y) float64  0.625 1.875 3.125 ... 498.1 499.4
  * x        (x) float64  0.8333 2.5 4.167 ... 497.5 499.2
Attributes:
    res:      (1.6666666666666667, 1.25)

The coords are rescaled to the (0, 500) default, res is overwritten, and
crs/units are gone.

Expected behavior

When the caller's array carries its own coords and geo-attrs, the output should
keep them: matching coords, chunks (dask), attrs['res'], and attrs['crs'].
A bare template with no coords/attrs should keep today's behavior of synthesizing
coords from x_range/y_range.

Additional context

In xrspatial/terrain.py, generate_terrain() builds fresh coords from
x_range/y_range and sets attrs={'res': (dx, dy)}, ignoring the caller's
coords and attrs. The accessor in xrspatial/accessor.py just forwards
self._obj, so the fix belongs in generate_terrain. The dask data chunks
already survive through map_blocks — coords and attrs are the gap.

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