Skip to content

Commit

Permalink
fix transform for dask coordinate arrays (#801)
Browse files Browse the repository at this point in the history
Co-authored-by: Dirk Eilander <dirk.eilander@gmail.com>
Co-authored-by: DirkEilander <15379728+DirkEilander@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 29, 2024
1 parent 58daa40 commit e420c5c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Changed
-------
- Development environment is now set up via pixi instead of mamba / conda. See the documentation for more information on how to install.

Fixed
-----
- Bug in `raster.transform` with lazy coordinates. (#801)


v0.9.4 (2024-02-26)
Expand Down
12 changes: 8 additions & 4 deletions hydromt/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,8 @@ def res(self) -> tuple[float, float]:
)
):
dy = -1 * dy
if isinstance(dx, dask.array.Array):
dx, dy = dx.compute(), dy.compute()
self._res = dx, dy
return dx, dy

Expand Down Expand Up @@ -648,6 +650,8 @@ def origin(self) -> tuple[float, float]:
b = c * math.cos(beta - alpha)
x0 = xs[0, 0] - np.sign(dy) * a
y0 = ys[0, 0] - np.sign(dy) * b
if isinstance(x0, dask.array.Array): # compute if lazy
x0, y0 = x0.compute(), y0.compute()
self._origin = x0, y0
return x0, y0

Expand Down Expand Up @@ -1243,10 +1247,10 @@ def clip_bbox(self, bbox, align=None, buffer=0, crs=None):
xs, ys = zip(*gdf_bbox.dissolve().boundary[0].coords[:])
cs, rs = ~self.transform * (np.array(xs), np.array(ys))
# use round to get integer slices
c0 = max(int(round(cs.min() - buffer)), 0)
r0 = max(int(round(rs.min() - buffer)), 0)
c1 = int(round(cs.max() + buffer))
r1 = int(round(rs.max() + buffer))
c0 = max(int(np.round(cs.min() - buffer)), 0)
r0 = max(int(np.round(rs.min() - buffer)), 0)
c1 = int(np.round(cs.max() + buffer))
r1 = int(np.round(rs.max() + buffer))
return self.clip(slice(c0, c1), slice(r0, r1))

def clip_mask(self, da_mask: xr.DataArray, mask: bool = False):
Expand Down

0 comments on commit e420c5c

Please sign in to comment.