Skip to content

Commit

Permalink
Fix issue where band array of strs caused Raster(xdata) to fail
Browse files Browse the repository at this point in the history
np.allclose was used to compare the band array to [1, ..., N] but this
failed when the band array had dtype('O'). This comparison was not
really necessary anyway so now the band array is simply overwritten.
  • Loading branch information
fbunt committed Dec 2, 2024
1 parent 80e56c5 commit 88ef2eb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 2 additions & 3 deletions raster_tools/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,8 @@ def normalize_xarray_data(xdata):
xdata = xdata.rename(name_mapping)
xdata = xdata.transpose("band", "y", "x", transpose_coords=True)

band_coords = np.arange(1, len(xdata.band) + 1)
if not np.allclose(xdata.band.to_numpy(), band_coords):
xdata["band"] = band_coords
# Make sure that the band dim is the series [1, 2, ..., N].
xdata["band"] = np.arange(1, len(xdata.band) + 1)
if any(dim not in xdata.coords for dim in xdata.dims):
raise ValueError(
"Invalid coordinates on xarray.DataArray object:\n{xdata!r}"
Expand Down
8 changes: 8 additions & 0 deletions tests/test_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,14 @@ def test_raster_from_dataarray(xdata):
assert np.allclose(raster._ds.y, xdata[yc])


def test_raster_from_dataarray_str_band(xdem_small):
xdem_small["band"] = ["one"]
raster = Raster(xdem_small)
assert np.allclose(raster.band, np.array([1]))
assert np.allclose(raster.xdata.band.to_numpy(), np.array([1]))
assert raster.xdata.band.to_numpy().dtype == I64


@pytest.mark.parametrize(
"ds",
[
Expand Down

0 comments on commit 88ef2eb

Please sign in to comment.